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

Compiling and testing the changes

Over the last few pages, we've covered a number of key tasks that would be perfect for compiling WordPress themes.

In an ideal world, we would automate as much as possible, or at least tasks where it would make sense—to remove tasks that give little value when done manually, for example. The key, though, is to give plenty of thought as to which order these tasks should be carried out; getting the order right can be the difference between receiving a valid style sheet file ready for use, and getting…well, what can only be classed as rubbish! Yes, that might seem a little extreme, but if the task order isn't right, then you can get compiled files that break your theme.

Leaving this aside, let's take a look at compiling our code—for the purposes of this exercise, we will use a copy of the pre-edited files that are available in the code download that accompanies this book.

Note

Before continuing, you may like to save copies of the source files you've created in the src folder that sits at the root of our project area, for safe keeping.

To get a feel for what we will be discussing, this is a screenshot excerpt of the Twenty Sixteen theme in action:

Compiling and testing the changes

Let's make a start:

  1. We'll start by downloading and extracting a copy of the src folder that is under T68 - converting a WordPress theme in the code download; save this to the root of our project area. Make sure there are no other files present in this folder first!
  2. Next, make sure that the gulpfile.js and package.json files that we downloaded earlier are still present—we will need both during compilation.
  3. Go ahead and fire up a Node.js command prompt session, then change the working folder to the root of our project area.
  4. At the prompt, enter gulp then press Enter; Gulp will run through each task and spit out a compiled theme file in the dest folder at the root of our project area.
  5. Copy this to the root of the Twenty Sixteen folder, which can be found at C:\wamp\www\wordpress\wp-content\themes\twentysixteen—if you use Linux or OSX, then please alter the path accordingly.

We now have a fully compiled style sheet! There are some points of note though—for example, the order of tasks that we covered earlier in this section doesn't match the order shown in the gulpfile.js file. Let's cover these points now:

  • The type of tasks you include can be split into two groups—those that I would describe as core, such as minifying files or providing source maps; these can be done for any sites. The second ones are custom tasks—these will be specific for each site, and might include requests to compile variables or nested code. It's up to you to plan how these are created, so that you can reuse them for future projects.
  • There is no right or wrong answer as to what should be included in a Gulp task file; the tasks you want to include, and the order they are called, will ultimately be determined by your own requirements. In our example, we used this order, from top to bottom:

    Name of task

    Purpose

    styles

    This compiled the raw code—merging the rules into one file, running PreCSS through the code, and updating vendor prefixes where needed.

    pxrem

    With the base code in place, we can now run through it and add pixel fallback for rem units, where appropriate.

    lint

    In this task, we're checking the compiled code for consistency.

    rename

    We then rename our base compiled file to have a .min.css extension; this is in preparation for minifying our code.

    sourcemap

    At this stage, we want to produce a source map, so this task kicks in to create a suitable map file for us.

    minifyCSS

    The final task is to minify the CSS style sheet file—it already has the right extension in place.

We've done the hard work—it's time to see the fruits of our labor in action. The style sheet is now in the theme folder within our WordPress installation—all that remains is to test it! We could get into using automated testing facilities such as Quixote (available from http://www.quixote-css.com/), but this would fall outside of the scope of this book. Instead, we can simply launch our local WordPress installation and check to see what it looks like—this is a useful way to gauge if there is anything amiss with our theme.

For our demo, we used the Twenty Sixteen theme. Out of the box, this is a very bare theme. Although this may not seem much, we've done most of the hard work needed to implement a PostCSS workflow when working with WordPress. A key measure of success is that the base theme should be identical to the original version that came with WordPress. If this is the case, it then means we can now concentrate on manipulating our style sheet over a period of time to further develop the styles within this (or any other) theme.