Lab 07: PostCSS Pipeline

Time: 30 minutes | Level: Advanced | Docker: docker run -it --rm node:20-alpine sh

Overview

Build a complete PostCSS pipeline: configuration, autoprefixer, postcss-preset-env for modern CSS polyfills, cssnano for minification, and writing a custom PostCSS plugin.


Step 1: PostCSS Fundamentals

PostCSS is a tool for transforming CSS with JavaScript plugins. It parses CSS into an AST, applies transforms, and outputs CSS.

// postcss.config.js (CJS)
module.exports = {
  plugins: [
    require('postcss-import'),           // inline @import statements
    require('postcss-preset-env')({      // modern CSS → compatible CSS
      stage: 3,
      features: {
        'nesting-rules': true,
        'custom-media-queries': true,
        'media-query-ranges': true,
      }
    }),
    require('autoprefixer'),             // add vendor prefixes
    require('cssnano')({ preset: 'default' }), // minify
  ]
};

// postcss.config.mjs (ESM)
import postcssImport from 'postcss-import';
import postcssPresetEnv from 'postcss-preset-env';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';

export default {
  plugins: [
    postcssImport(),
    postcssPresetEnv({ stage: 3 }),
    autoprefixer(),
    cssnano({ preset: 'default' }),
  ]
};

Step 2: postcss-preset-env

Transforms modern CSS to be compatible with older browsers, similar to Babel for JS:


Step 3: Autoprefixer


Step 4: cssnano Optimization


Step 5: Custom PostCSS Plugin


Step 6: PostCSS CLI Usage


Step 7: Integration Examples


Step 8: Capstone — PostCSS Pipeline Verification

📸 Verified Output:


Summary

Plugin
Purpose
Impact

postcss-import

Inline @import

Bundle CSS

postcss-preset-env

Modern CSS polyfills

Cross-browser

autoprefixer

Vendor prefixes

Cross-browser

cssnano

Minification

Bundle size

postcss-nested

Sass-style nesting

DX

Custom plugin

Any transform

Extensibility

.browserslistrc

Target browsers

Controls prefixes

--map flag

Source maps

Debugging

Last updated