Lab 13: Monorepo CSS

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

Overview

CSS at monorepo scale: shared design token packages via npm workspaces, CSS layer strategy across packages, PostCSS config inheritance, shared stylelint config package, token versioning with changelog, and consuming tokens across multiple apps.


Step 1: Monorepo Structure

workspace/
├── package.json                # Root workspace config
├── packages/
│   ├── tokens/                 # @company/tokens
│   │   ├── package.json
│   │   ├── src/
│   │   │   ├── primitive.json
│   │   │   ├── semantic.json
│   │   │   └── component.json
│   │   └── dist/
│   │       ├── tokens.css      # CSS custom properties
│   │       ├── tokens.js       # CommonJS
│   │       └── tokens.d.ts     # TypeScript types
│   ├── stylelint-config/       # @company/stylelint-config
│   │   └── index.js
│   └── postcss-config/         # @company/postcss-config
│       └── index.js
├── apps/
│   ├── web/                    # Next.js app
│   │   ├── package.json        # depends on @company/tokens
│   │   └── postcss.config.js   # extends @company/postcss-config
│   └── admin/                  # Vite app
│       └── package.json
└── turbo.json                  # Build pipeline (optional)

Step 2: Root Workspace Configuration


Step 3: Shared Token Package


Step 4: Shared PostCSS Config Package


Step 5: CSS Layer Strategy Across Packages

💡 Declare all expected layers at the top of the root CSS file. Any library that uses the same layer names will fall into the correct cascade position.


Step 6: Shared Stylelint Config


Step 7: Token Versioning and Changelog


Step 8: Capstone — npm Workspaces Token Resolution

📸 Verified Output:


Summary

Concern
Solution
Package

Token distribution

npm workspaces

@company/tokens

PostCSS pipeline

Shared config

@company/postcss-config

CSS linting

Shared rules

@company/stylelint-config

Layer strategy

Declared at root

All packages use same layers

Token versioning

Changesets

Automated semver

Breaking changes

CHANGELOG.md

Clear migration path

Build caching

Turborepo

Only rebuild changed packages

Last updated