Table of Contents for
Mastering PostCSS for Web Design

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering PostCSS for Web Design by Alex Libby Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. Mastering PostCSS for Web Design
  4. Mastering PostCSS for Web Design
  5. Credits
  6. About the Author
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Conventions
  13. Reader feedback
  14. Customer support
  15. 1. Introducing PostCSS
  16. Introducing PostCSS
  17. Setting up a development environment
  18. Creating a simple example using PostCSS
  19. Linting code using plugins
  20. Exploring how PostCSS works
  21. Summary
  22. 2. Creating Variables and Mixins
  23. Creating a hover effect example
  24. Transitioning to using PostCSS
  25. Adding variable support to PostCSS
  26. Updating our hover effect demo
  27. Setting the order of plugins
  28. Creating mixins with PostCSS
  29. Looping content with PostCSS
  30. Summary
  31. 3. Nesting Rules
  32. Navigating through pages
  33. Transitioning to using PostCSS plugins
  34. Exploring the pitfalls of nesting
  35. Making the switch to BEM
  36. Exploring our changes in more detail
  37. Summary
  38. 4. Building Media Queries
  39. Exploring custom media queries in PostCSS
  40. Making images responsive
  41. Adding responsive text support
  42. Optimizing media queries
  43. Retrofitting support for older browsers
  44. Moving away from responsive design
  45. Taking things further with CSS4
  46. Summary
  47. 5. Managing Colors, Images, and Fonts
  48. Managing fonts with PostCSS
  49. Creating image sprites
  50. Working with SVG in PostCSS
  51. Adding support for WebP images
  52. Manipulating colors and color palettes
  53. Creating color functions with PostCSS
  54. Summary
  55. 6. Creating Grids
  56. Creating an example with Bourbon Neat
  57. Exploring the grid plugins in PostCSS
  58. Transitioning to using PostCSS-Neat
  59. Creating a site using Neat and PostCSS
  60. Adding responsive capabilities
  61. Summary
  62. 7. Animating Elements
  63. Moving away from jQuery
  64. Making use of pre-built libraries
  65. Switching to using SASS
  66. Making the switch to PostCSS
  67. Exploring plugin options within PostCSS
  68. Updating code to use PostCSS
  69. Creating a demo in PostCSS
  70. Optimizing our animations
  71. Using our own animation plugin
  72. Summary
  73. 8. Creating PostCSS Plugins
  74. Dissecting the architecture of a standard plugin
  75. Creating an transition plugin
  76. Building a custom font plugin
  77. Simplifying the development process
  78. Guidelines for plugin building
  79. Making the plugin available for use
  80. Summary
  81. 9. Working with Shortcuts, Fallbacks, and Packs
  82. Exploring plugin packs for PostCSS
  83. Adding shortcuts with Rucksack
  84. Linting and optimizing your code
  85. Providing fallback support
  86. Summary
  87. 10. Building a Custom Processor
  88. Exploring our processor
  89. Dissecting issues with our processor
  90. Optimizing the output
  91. Adding reload capabilities
  92. Extending our processor further
  93. Testing the final pre-processor
  94. Getting started with some hints and tips
  95. Introducing the CSStyle library
  96. Summary
  97. 11. Manipulating Custom Syntaxes
  98. Preparing our environment
  99. Implementing custom syntax plugins
  100. Parsing CSS
  101. Formatting the output with the API
  102. Highlighting our syntax code
  103. Summary
  104. 12. Mixing Preprocessors
  105. Exploring the conversion process
  106. Introducing the Pleeease library
  107. Compiling with other preprocessors
  108. Using the PreCSS library
  109. Converting a WordPress installation
  110. Setting up our environment
  111. Considering the conversion process
  112. Making changes to our code
  113. Compiling and testing the changes
  114. Summary
  115. 13. Troubleshooting PostCSS Issues
  116. Exploring some common issues
  117. Getting help from others
  118. Summary
  119. 14. Preparing for the Future
  120. Converting CSS4 styles for use
  121. Supporting future syntax with cssnext
  122. Creating plugins to provide extra CSS4 support
  123. Summary
  124. Index

Parsing CSS

At the heart of writing any custom syntax is the ability to parse content—it doesn't matter whether this is CSS, JavaScript, or something else; we clearly need to understand what we're working with, before we can make changes! At a basic level, these are the steps we must take to transform our CSS when working with PostCSS:

Parsing CSS

We begin with our source CSS (which comes with or without a source map), which we parse only once, but then put through any number of specified plugins (the example shows two, but we can easily use more). We then convert the output to a string using a stringifier; at this point, we can view the contents on screen or save them to disk.

Let's for a moment take a look at parsing some example code. For this next example, we will use a single CSS rule and parse it using the postcss-value-parser plugin (from https://github.com/TrySound/postcss-value-parser); the reason for this will become clear shortly:

  1. From the code download that accompanies this book, extract and save copies of the gulpfile.js and package.json files from the T60 – parsing color values folder to the root of our project area; if you want to save any existing files from there, then please do so first.
  2. Fire up a NodeJS command prompt session then change the working folder to the root of our project area.
  3. We need to install the postcss-value-parser plugin, so at the prompt, enter this command and press Enter:
    npm install postcss-value-parser --save-dev 
    

    NPM will now install the plugin; keep the session open when it has finished:

    Parsing CSS
  4. At the prompt, type gulp then press Enter; gulp will now go away and display the contents, which will look something like this:
    Parsing CSS

Yikes! What does that all mean? Don't worry, it looks worse than it really is; this is an example of an AST, which we discussed earlier in this chapter. This gives us in-depth information on the contents of our CSS, such as the values, types of values, and where they appear in the tree.

The great thing, though, is that once we have all of this content, then we are free to query and manipulate the content at will. Once we have manipulated the content, we then need to convert it to string format, so it can be displayed on screen in a more intelligent format, or saved to disk.

For this demo, we used the postcss-value-parser plugin to create our AST; we can also try using the postcss-safe- parser plugin (from https://github.com/postcss/postcss-safe-parser), or the postcss-selector-parser plugin (from https://github.com/postcss/postcss-selector-parser), to achieve similar effects.

And the reason why we only used one line of CSS code in our demo? Well, parsing CSS code can get very complex. The example shown in our demo is relatively straightforward; just imagine what it will be like with 2,000+ lines of code!

Let's develop this theme further, and use it to replace some example RGBA values with equivalent HEX-based colors. We can easily do this through the use of the postc ss-unrgba plugin (from https://github.com/jonathantneal/postcss-unrgba), but it weighs in at almost 60 lines; our Gulp file is 43 lines, and a lot of that is comments!

Replacing RGBA colors

Our next example is a relatively straightforward search and replace; it is a perfect example of how it isn't always necessary to use plugins, and that we can parse our code directly to achieve the same effect. Let's make a start:

  1. We'll start by extracting a copy of the T61 – changing colors folder from the downloaded code that accompanies this book; save the folder to the root of our project area.
  2. Copy the gulpfile.js and package.json files from the T61 – changing colors folder to the root of our project area.
  3. Copy the src folder from the T61 – changing colors folder to the root of our project area.
  4. Next, fire up a NodeJS command prompt session then change the working folder to the root of our project area.
  5. We now need to install an additional plugin, color-convert (available from https://github.com/qix-/color-convert), which we will use to change the color once we've sucked out the details from within the AST. For this, go ahead and fire up a NodeJS command prompt, then change the working folder to the root of our project area.
  6. At the prompt, enter npm install color-convert --save-dev and press Enter.
  7. When the plugin has finished installing, go ahead and enter gulp, then press Enter. If all is well, we should see the, by now, familiar transformed style sheet appear in our destination folder:
    Replacing RGBA colors

At this point, our style sheet has been transformed. If we preview the results in a text editor, we can confirm that HEX-equivalent values have indeed replaced the original RGBA colors, as shown in the following screenshot:

Replacing RGBA colors

Not convinced? Take a look at the same rule within the source file; here it shows the original RGBA value:

Replacing RGBA colors

See how easy that was? There is one thing note; if we take a look at the Gulp file, it might at first glance look like we still have a few plugins in use. The key here is that three of these are part of Node (fs, path, and util), so we haven't had to install any new ones, over and above the value-parser and color-convert examples.

Exploring how it all works

It's worth taking the time to consider this code in more detail. This contains some useful techniques that will help get you started on the road to creating custom syntaxes, starting with retrieving the values we need.

We begin with reading the contents of our style sheet file, before parsing it through the postcss-value-parser plugin. We walk through each node within the AST, ignoring any that contain a node.type of function or a node.value of rgba. For any that remain, we collect any that have a node type of word, before mapping them into a single array value which we convert to a number.

This is then transformed from a function node to a word node, before we finally convert the value from an RGBA to HEX color. The contents are converted to a string, and saved to disk in the destination folder, with the same file name.

Note

Node types represent the type of selector we're working with—examples include root, string, tag, and attribute. In our example, we've used node.type to display a string representation of the selector type, which we can manipulate in code.

Okay, let's move on: the key basis for working with custom syntaxes is to understand the content we need to work with; crack this and you are part of the way to transforming your styles into valid CSS. To help with the process, though, we will need to convert our content to a format that can be saved to disk. It's time to take a look at how, using the PostCSS API.