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

Introducing the Pleeease library

The Pleeease library, available from http://pleeease.io/, is designed to simplify the use of preprocessors, and combine the benefits of using multiple tools in one library. It means we can configure it to any one of three processors, such as SASS, Less, or Stylus, in addition to PostCSS, when compiling our code. The compilation can also include all of the typical tasks we might otherwise have to do, such as generating source maps, adding vendor prefixes, and minifying the results.

The library is easy to install—in its simplest format, we can use a configuration file to compile at the command line. Alternatively, we can use any one of several plugins to hook into a task runner, such as Gulp or Grunt. Let's take a moment to explore this in more detail, beginning with installing and configuring the plugin for use.

Installing and configuring the Pleeease library

The Pleeease library is based on Node.js; it is a cinch to install, either for use manually at the command line, or via a task runner such as Gulp. Let's make a start on getting everything set up for use:

  1. We'll begin by installing the Pleeease library—for this, go ahead and fire up a Node.js command prompt session, then change the working folder to our project area.
  2. At the prompt, enter npm install -g pleeease-cli, then press Enter—wait for Node to complete the installation.

At this point, the Pleeease library is installed and configured for use from the command line—if Node complains of elements that need to be updated, then it may be necessary to run npm update –g n to bring your version up to date. If you are a Windows user, then there is a handy PowerShell script available at https://github.com/felixrieseberg/npm-windows-upgrade to help with this process.

Note

You may get a couple of warnings about deprecated modules for graceful-fs and lodash: these can be ignored for the purposes of the demo.

Assuming we did not encounter any issues with installing Pleeease (over and above some deprecation warnings, as already mentioned), then we are now ready to use Pleeease in anger. Over the next few pages, we will take a look at compiling manually as well as using Gulp as our favored task runner. Let's begin by exploring how easy it is to perform a basic compilation at the command line using a .pleeeaserc file.

Note

For the purposes of this chapter, we will concentrate on using SASS; if your preference is to use Less, you can use the gulp-less plugin as an alternative. Likewise, if your desire is to use Stylus, then the gulp-stylus plugin will work equally well with Pleeease.

Compiling code manually

The simplest way to compile code using the Pleeease library is with a .pleeeaserc configuration file.

This resembles a (simplified) JSON file, and will look something like this:

{
  "in": ["foo.css", "bar.css"],
  "out": "baz.css",
  "browsers": ["last 3 versions", "Android 2.3"]
}

Looks pretty straightforward, doesn't it? We simply need to specify our source files (in), and what we should get (out). In this example, we've gone one step further, to specify the level of browser support needed—this is mainly to ensure that the right vendor prefixes have been applied.

This setting uses the same configuration as Autoprefixer: we can equally pass it a valid query from the Browserslist query list at https://github.com/ai/browserslist#queries.

This is a useful method for compiling if our requirements do not stretch to using a task runner, or we want to keep our processes simple. The only downside is that we can't tie in any other tasks that could be automated, such as renaming the compiled style sheet with a .min.css extension—for this, we will need to use a task runner such as Gulp.

If we do use a task runner, this opens up all kinds of possibilities, such as automating processes to resize images, renaming compiled style sheets, and checking our code for consistency. Before we do so, let's just cover a useful tip—the Pleeease site includes an online playground (available at http://pleeease.io/play/). We can use this to help familiarize ourselves with using the library before committing ourselves to compiling code for a production site:

Compiling code manually

Okay, enough chitchat: it's time to get practical, so to speak! For some, compiling at the command line might be enough, but in this modern age of automation, why spend time performing manual processes that can easily be automated?

Compiling using a task runner

If you haven't already guessed by now, I'm a big fan of using Node.js—my task runner of choice is Gulp. It used to be Grunt, but there is something about Gulp that I find is easier to use—I'm not sure why! Anyway, either can be used with Pleeease, so if your preference isn't Gulp, then please feel free to alter the code accordingly.

Let's work through the steps needed to use Gulp to run our compilation process:

  1. Fire up a Node.js command prompt session, then change the working folder to our project area.
  2. At the prompt, enter these commands, pressing Enter after each:
    npm install gulp-pleeease
    

    Keep the Node.js command prompt session open—we will need it shortly.

  3. Extract a copy of the T65 – using gulp-pleeease folder from the code download that accompanies this book to the root of our project area.
  4. Copy the package.json and gulpfile.js files to the root of our project area, then copy example.css from the src folder under T65 – using gulp-pleeease to the src folder at the root of our project area.
  5. Revert to the Node.js command prompt session, then at the prompt enter gulp and press Enter:
    Compiling using a task runner

Assuming compilation is successful, Gulp will produce the now familiar files within the dest folder at the root of our project area. If we take a look at the results, we should see that it has minified the file, added vendor prefixes, and converted the blue and red color attributes to their equivalent HEX values.

Let's put this technique to good use and create a simple web page as an example of how we can use Pleeease. When checking our Gulp file, we will see that we don't need to use half of the plugins we've used in previous exercises, as Pleeease adds that support from within its plugin.

Building an example using Pleeease

Throughout many of the demos in this book, we've had to import a series of plugins to manage different tasks such as minifying code, or checking it for consistency.

There is nothing technically wrong with this approach, but it is inefficient—after all, why use six tools when one will suffice, so to speak? We've tried to maintain a one plugin—one job rule throughout the book, so why are we breaking with convention?

The great thing about using Pleeease is that it already contains support for some of these tasks that would otherwise require separate plugins; this means we can remove some of the plugins referenced in the Gulp task file. Pleeease is simply a layer that abstracts support for six other plugins through one common interface.

Let's put that to use in the form of compiling styles for a simple web page:

Building an example using Pleeease

Let's make a start:

  1. We'll start, as always, by extracting a copy of the TXX – creating a page using pleeease folder from the code download that accompanies this book; save it to the root of our project area.
  2. From the css – completed version folder, copy the styles – pre compile.css file to the src folder at the root of our project area; rename it styles.css.
  3. Copy the gulpfile.js and package.json files from the root of the tutorial folder to the root of the project area—these should replace any already present at the root of our project area.
  4. Fire up a Node.js command prompt session, then change the working folder to the project area.
  5. At the prompt, type gulp then press Enter—Pleeease will now go away and compile our code, and spit out valid style sheet files in the dest folder at the root of our project area.
  6. Once completed, copy the contents of the dest folder to the css folder within the tutorial folder.

If we try previewing the results of our work by double-clicking on webpage.html, we should see a web page appear, similar to the screenshot at the start of this demo. The real proof, though, is in the Gulp task file—in comparison to other examples we've created in earlier demos, we've managed to remove one task completely, and reduce the number of plugins referenced by over half!