Lab 02: CSS Build Pipeline

Time: 60 minutes | Level: Architect | Docker: node:20-alpine

Overview

Engineer a production-grade PostCSS pipeline: postcss-preset-env (Stage 2), autoprefixer, cssnano, purgecss, postcss-import, and a custom plugin. Master the entire CSS compilation lifecycle from source to optimized output.


Step 1: Pipeline Architecture

src/
├── tokens.css           # Design token definitions
├── base.css             # @import './tokens.css'; reset + base
├── components/
│   ├── button.css
│   └── card.css
└── utilities.css        # Utility classes

postcss build →

dist/
├── bundle.css           # Full build
└── bundle.min.css       # Minified + purged

The pipeline stages in order:

  1. postcss-import — inline all @import statements

  2. postcss-preset-env (Stage 2) — transpile modern CSS

  3. autoprefixer — add vendor prefixes

  4. purgecss — remove unused CSS rules

  5. cssnano — minify + optimize


Step 2: Install and Configure

💡 filter(Boolean) removes false entries from the plugins array — a clean pattern for conditional plugins.


Step 3: Modern CSS Features (Stage 2)


Step 4: Custom PostCSS Plugin


Step 5: NPM Scripts


Step 6: PurgeCSS Configuration

💡 Always test PurgeCSS in staging first — aggressive purging can remove dynamically-added classes. Use safelist.greedy for attribute selectors.


Step 7: Bundle Analysis


Step 8: Capstone — Full Pipeline Verification

📸 Verified Output:


Summary

Stage
Plugin
Purpose

Import resolution

postcss-import

Inline all @import

Modern syntax

postcss-preset-env Stage 2

Transpile nesting/custom-media

Vendor prefixes

autoprefixer

-webkit-, -moz- etc.

Dead code removal

purgecss

Remove unused selectors

Minification

cssnano

Compress output

Custom transform

DIY plugin

Token replacement

Analysis

Custom plugin

Bundle statistics

Last updated