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 our processor

As part of creating the demos we've worked through in this book, we concentrated on ensuring plugins are installed, and that we have the right files in the right place. There is something missing though, and that is—what actually happens in the files? Why do we have tasks in a particular order? What is the reasoning behind choosing some of the plugins that we've used…and so on—you get the idea!

Over the next few pages, we're going to try to answer some of these questions (and more), by exploring the processor that we've used in some of the recent examples; you will see that there isn't a one-answer-fits-all approach, but more a case of working through your requirements, and picking plugins to suit your needs.

Before we go into depth, though, let's just quickly recap the make-up of our processor, starting with the package.json file.

Dissecting the package.json file

The package.json file tells PostCSS which plugins to use, and may contain some of the key configuration settings to be used during compilation:

{
  "name": "postcss",
  "version": "1.0.0",
  "description": "Configuration file for PostCSS",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Alex Libby",
  "license": "ISC",
  "dependencies": { "postcss": "^5.0.8" },

The top half of our processor contains a number of key properties that tell us details such as the version, description, who created it, any dependencies, and the license being used for the project:

  "devDependencies": {
    "autoprefixer": "^6.0.3",
    "cssnano": "^3.2.0",
    "gulp": "^3.9.0",
    "gulp-postcss": "^6.0.0",
    "gulp-rename": "^1.2.2",
    "gulp-sourcemaps": "^1.5.2",
    "postcss-reporter": "^1.3.0",
    "stylelint": "^2.3.7"
  }
}

In comparison, the key part for us is in the bottom half; this lists all of the plugins that will be used within our project. In many of our projects, we've installed the plugin—at point of installation, the plugin will add an entry into this file that contains the name and the minimum version required (represented by the ^ symbol).

It is worth noting that we can manually add entries to, or remove entries from this file, or even copy package.json files from one project to another if needed. This is particularly useful if we know that a new project has identical (or very similar) requirements to an existing one; plugins will only add an entry into this file at installation, if one does not already exist.

Exploring the Gulp task file

The gulpfile.js file is where the real magic happens—this contains all of the tasks that need to be performed on each style sheet within our project. Outside of the style sheet, this is the second of two files that we've simply copied across from the code download to our project area. Now that we've been using it in anger, it's worth taking a moment to explore what happens in more detail.

The gulpfile.js file is made up of several sections—in our example, we begin with a list of variables that define references to each of our plugins:

'use strict';

var gulp = require('gulp');
var postcss = require('gulp-postcss');
//var autoprefixer = require('autoprefixer');
var cssnano = require('gulp-cssnano');
var sourcemaps = require('gulp-sourcemaps');
var rename = require('gulp-rename');
var stylelint = require('stylelint');
var reporter = require('postcss-reporter');
var rucksack = require('rucksack-css');

The first task in our list is the most important one—this picks up and compiles the source code into a valid CSS file and deposits it in the dest folder. As part of this, we provide links to any PostCSS plugin that is needed to transform our code—in this example, we're using Rucksack, set to include fallback support but not add vendor prefixes:

gulp.task('styles', function () {
  return gulp.src('src/*.css')
    .pipe(postcss([ rucksack({ fallbacks: true, autoprefixer: true }) ]))
    .pipe(gulp.dest('dest/'));
});

This chunky task is less complicated than it looks—it checks our code for consistency, based on the rules set; it outputs any warnings or errors on screen using the reporter plugin. The key here is the ['styles'] attribute—this tells PostCSS not to perform this task until the styles task has been completed:

gulp.task("lint-styles", ['styles'], function() {
    return gulp.src("dest/*.css")
    .pipe(postcss([ stylelint({
        "rules": {
          "color-no-invalid-hex": 2,
          "declaration-colon-space-before": [2, "never"],
          "indentation": [2, 2],
          "number-leading-zero": [2, "always"]
        }
      }),
      reporter({ clearMessages: true, })
    ]))
});

In comparison, the next two tasks are relatively straightforward—this one takes care of compressing our compiled code, and renaming it with a .min.css extension:

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

This task is equally straightforward—it creates a source map of our style sheet, and sets it in a format that PostCSS can release into a file within the dest folder of our project area:

gulp.task('sourcemap', ['rename'], function () {
  return gulp.src('dest/*.css')
    .pipe(sourcemaps.init())
    .pipe(sourcemaps.write('maps/'))
    .pipe(gulp.dest("dest/"));
});

The last two steps play the most important role in any Gulp task file—the first will fire off calls to each of our tasks if we enter gulp in a command line prompt:

gulp.task('default', ['styles', 'lint-styles',  'rename', 'sourcemap']);

This task, although not obligatory, watches out for any changes to our code and sets off the tasks in our Gulp file automatically. It will respect any constraints set, although for consistency, it is preferable to list the tasks being performed in the same order as they are shown in the file:

var watcher = gulp.watch('src/*.css', ['styles',
'lint-styles', 'rename', 'sourcemap']);
watcher.on('change', function(event) {
  console.log('File ' + event.path + ' was ' +
event.type + ', running tasks...');
});

There is more to the compilation process than these two files—thought should also be given to how we structure our working environment. A quick look at the Gulp task file should reveal that we've used a simple in-tray/out-tray approach; code is picked up from the src folder, and the results placed into the dest folder at the end of compilation.

This is an important part of the process—after all, there is no benefit in using PostCSS if we don't give any thought to the structure of our project area! Part of this is to maintain separation between source and compiled files, but also that we may decide to expand our compilation process to include tasks such as shrinking images. There is no right or wrong in how this area should be structured—this will be dictated by our project requirements.