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

Exploring some common issues

PostCSS as a processing system is easy to set up and use, but occasionally we may come across issues during development. The issues will vary, of course, but to help you along, we can explore some of the more common issues we may face during development.

For the purposes of this chapter, we will assume that the Gulp task runner has been used—you will likely see similar issues with other task runners such as Grunt or Broccoli. It's time to explore some of these issues in more detail.

Not compatible with your operating system…

The beauty of PostCSS is that we can install any one of dozens of plugins available—if we're using a task runner such as Gulp, then we can easily extend this to cover the vast array available for use.

In most instances, plugins will install without a hitch; you may find that you see this appear occasionally:

Not compatible with your operating system…

The warning message that is of most importance to us is not the ENOENT messages, but this one:

notsup: Not compatible with your operating system or architecture: fsevents@1.0.6

It should be noted that this is only a warning, and not an error (as such)—it is caused by the use of fsevents, which is MacOSX only and will not work on Windows or Linux environments. In most cases this can be ignored, although it would be wise to test your processor to ensure it has not had any detrimental impact on your code.

Note

This error is not limited to fsevents—it can apply to any package, which is not supported in the environment you use.

Task '<name of task>' is not in your gulpfile

We've created a Gulp task file with a number of tasks within, and run it to compile our style sheet. Instead of getting our processed files, we end up with this message:

Task '<name of task>' is not in your gulpfile

This is caused by the Gulp task not being present in the gulp file—in this declaration example, we're calling the rename task:

Task '<name of task>' is not in your gulpfile

But a look through the gulp file shows that there is no rename task in sight:

Task '<name of task>' is not in your gulpfile

This is a simple fix—just rename the task so that the names match, and retry the compilation. Note, though—if more than one task is incorrectly named, then the process will fail but will only show the name of the first one that is at fault. Make sure that each task name is entered correctly in the file to ensure successful compilation.

Cannot find module '<name of plugin>'

If any error is likely to catch us out, then it is this one—let me explain:

As you get to grips with installing PostCSS plugins, you will see many that use the naming convention postcss-<name of plugin>. It seems a sensible proposition, but beware – not every PostCSS plugin uses this naming convention!

A great example is Rucksack – one would expect to use postcss-rucksack (and yes, that includes me!), but we will get this error if we do:

Cannot find module '<name of plugin>'

It turns out that Rucksack is one of those examples that doesn't use the same naming convention that many people would expect it to use. Instead, it uses rucksack-css, as the name we would expect to use had already been taken.

This is one of those instances where it pays to read the documentation, if only to save a lot of embarrassment later:

Cannot find module '<name of plugin>'

It clearly states what name to use when installing the plugin! Yes, it happens to the best of us…

ReferenceError: <name of task> is not defined

Let us assume that we have a series of tasks in our gulp file, but for some unknown reason, we're getting this error when we compile our code:

ReferenceError: <name of task> is not defined

What could be causing it? Well, there are at least two possible causes:

  • We include the plugin within the task itself, but forget to include a reference to it within the declarations at the top of our file
  • We do manage to include both the plugin within the task and its associated declaration, but somehow manage not to give the same declaration name

The fix for this is to make sure that when we include the declaration at the top of the Gulp task file, that the same name is used when calling the task later in the file.

Please provide array of postcss processors!

This next error is one that can catch anyone out, but is easy to fix—as time goes by you will develop your own processor; you will likely reach a point where you start to move some of the PostCSS processor tasks out from the main PostCSS call into their own task.

Imagine that you have a PostCSS task similar to this:

Please provide array of postcss processors!

There is a natural temptation to split this task into separate ones; after all, I extolled the virtues of keeping a 1:1 relationship between the task name (that is, styles), and what it does in the task! If, however, you take things a little too far, and think that you don't need to have a PostCSS processor in the task, then you may come unstuck, and land up with an error similar to this screenshot:

Please provide array of postcss processors!

A quick check in your Gulp task file will likely show something akin to this:

Please provide array of postcss processors!

We have a postcss task, but without any processors within it! Although it is tempting to rework processors to ensure we maintain that 1:1 relationship, we must always leave one processor in the postcss() task, to ensure it operates correctly.

Note

You will hear talk of the term processor used in several different ways—they can equally apply as a generic term for what is our Gulp task file. It can also apply to the processor tasks that should be added to any postcss() task.

Entries failing to appear in the package.json file

This next error is a little trickier to catch, but the fix for it is easy—over time, you will likely make changes to your Gulp task file; this of course means that new plugins will need to be added from another package.json file (if already installed), or added afresh, if they haven't already been installed.

As the Gulp task file is just a plain text file, we can edit it in any text editor—my personal favorite is Sublime Text 3 (http://www.sublimetext.com/3), but any will suffice. Notepad isn't a good one, as it will fail to handle the line-endings correctly!

This aside, if we edit our package.json file to remove an entry, then add a new one, we might come across this warning:

Entries failing to appear in the package.json file

The cause of this little gem is a really irritating one—it's amazing how one single character can cause all these warnings! The culprit is the extraneous comma at the end of a line where there isn't another plugin listed immediately after, as shown on line 27 in this screenshot:

Entries failing to appear in the package.json file

If we remove the comma and retry the installation, we will soon see that the error has disappeared.

Output of compiling results is not as expected

This final error can be deceptive—it's not strictly speaking even an error! Imagine we've created a killer processor application using Node and Gulp; it contains a number of tasks similar to the ones we created earlier, and have since used throughout this book.

We enter the relevant command and hit Enter. PostCSS starts the compilation: so far so good. A look in the dest folder shows this—where's our minified file and source map?

Output of compiling results is not as expected

But hold on – where's the error? A check of the output from the compilation process shows no error, so what gives?

Output of compiling results is not as expected

This little oddity is caused by miscalculating the order in which tasks should run—although the screenshot clearly says an error, it's not an error in the true sense. Let me explain.

The key to a successful compilation process (and by default, a working processor), lies in the need to make sure that each task we run is fired in the right order. To generate this error, I removed the constraint on our rename task in the Gulp file from the T45 – converting to use Rucksack demo, thus:

gulp.task('rename', function () {
  return gulp.src('dest/*.css')
    .pipe(postcss([ cssnano() ]))
    .pipe(rename('style.min.css'))
    .pipe(gulp.dest("dest/"));
});

The task looks perfectly acceptable, but introduces a problem—instead of just one starting task, we now have two!

The net result of this is that the styles task is run first (as it is called first in the default task from line 36). It's quickly followed by the rename task (no constraint on it), then sourcemap and lint-styles (following the constraints set against each task).

We end up with just one compiled file in the dest folder—the rename and styles tasks are both run at the same time; as the latter isn't completed, the former can't produce any content!

Let's move on. If all else fails, and you find problems you can't fix, then it is time to seek help….here's a quick rundown of the options available to you as a beginner to using PostCSS.