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

Extending our processor further

Over the last few pages, we've explored a number of ways to improve our existing processor, as well as a few ideas for extending functionality. Although we can always keep to PostCSS plugins, we run the risk of limiting the "art of the possible", or what is available for us to use.

Sometimes, we might want to go a little further afield—creating a processor isn't just about the nitty-gritty of compiling code, but also about our working environment and the processes required to support it (at least in part). To prove this, we're going to explore installing the postcss-stats plugin as an example of how we can extend both our plugin and working environment.

This plugin helps provide useful statistics about each project as it is compiled—it's based on the CSS Stats system, and is available online at http://www.cssstats.com.

Note

Throughout the demo, you may see a few issues with deprecated warnings—at the time of writing, the plugin needs a little polishing/updating. Don't worry though: the plugin will still work fine for the purposes of our demo.

The source for this plugin is available on GitHub at https://github.com/cssstats/postcss-cssstats, and can be installed using the usual route. Let's dive in and take a look:

  1. We'll start by firing up a Node.js command prompt session, then changing the working directory to the root of our project area.
  2. We need to install the plugin, so in the prompt, enter this command and press Enter:
    npm install postcss-cssstats --save-dev 
    

Keep this open—we will need it later in the exercise.

Next, we need to update our gulpfile.js and package.json files—go ahead and extract copies of both files from the T54 - using cssstats folder in the code download that accompanies this book. Save both files to the root of our project area:

  1. With our files in place, we can now test that it works—go ahead and save a copy of style.css from the same folder into the src folder of our project area.
  2. Revert to the Node.js command prompt we had open earlier—in the prompt, enter gulp, and press Enter.
  3. PostCSS will compile our code—if all is well, we should see files appear in the now familiar dest folder…and we should also see something akin to this screenshot:
    Extending our processor further

If I were a betting man (I'm not, but assume I am for this)—I would bet even odds that you're probably thinking "What on earth does all of that text mean?" Well, let me shed some light on what it all means.

In a nutshell, we've installed what is effectively a reporting system—this details a bunch of statistics about our code. It contains details about all kinds of information, including the number of selectors, colors, the level of CSS specificity, declarations, and so on. It's an easy way to get information about our code, as a means of documenting it for later use. The reason it is so easy to get the information lies in how it is configured—take a look at the gulpfile.js file; we will add a call to the plugin at the top:

var reporter = require('postcss-reporter');

We can then modify the styles single task, by adding this line near the end:

    .pipe(postcss([ cssstats( function(stats) {
      console.log(stats);
    })
  ]))
  .pipe(gulp.dest('dest/'));
})

The trouble is, whilst it might be easy to get the information, it's not so easy to store it! We can absolutely improve on it; instead of getting the information via our processor, we can go directly to the source. Let's explore how to make this happen:

  1. We'll start by firing up a Node.js command prompt, then changing the working folder to the root of our project area.
  2. At the prompt, go ahead and enter npm install gulp-stylestats --save-dev, then press Enter.
  3. We now need to edit the gulpfile.js and package.json files we used in the previous exercise, so open the gulpfile.js file in a text editor, and add these lines immediately below the closing bracket of the sourcemap task:
    gulp.task('stylestats', ['minifyCSS'], function () {
      gulp.src('dest/*.css')
        .pipe(stylestats({
          type: 'json',
          outfile: true
        }))
        .pipe(gulp.dest('dest/'));
    });
  4. Next, we need to update the default task—alter it as indicated:
    gulp.task('default', ['styles', 'lint', 'rename',
     'minifyCSS', 'sourcemap', 'stylestats']);
  5. Revert to the Node.js command prompt, then enter gulp and press Enter—assuming we still have the same style.css file in the src folder, we should see this appear in the dest folder at the root of our project area:
    Extending our processor further
  6. Whilst we clearly need to alter the parameters of our Gulp file to prevent it producing minified JSON files, we can at least see the result of the (uncompressed) JSON file. Go ahead and open it up—it will look something like this:
    Extending our processor further

Although we're still only seeing code, we can now parse the content at will; we could, for instance, use jQuery to explore the contents and render it on screen using an appropriate style and format. I am sure you will agree though that this is a much easier way to view (and store) the information! The plugin needs minimal configuration to get started. We can use it to view any standard CSS file, once it has been through the compilation process.

Tip

There are a number of options we can use with the gulp-stylestats plugin—for details, take a look at https://github.com/t32k/stylestats.

Right, we now have a completed processor; hopefully, this will also include a style guide that is running, using one of the plugins we've just discussed in the previous exercise. It's time we moved on—there is one task we should complete, though, before we embark on the next stage of our journey. It's time we put our processor to the test…