CLI for PostCSS that provides command-line interface for processing CSS files with PostCSS plugins
npx @tessl/cli install tessl/npm-postcss-cli@11.0.0PostCSS CLI is a command-line interface tool for processing CSS files using PostCSS plugins. It provides comprehensive features for CSS processing workflows including file watching, sourcemaps, directory processing, and flexible plugin configuration.
npm install -D postcss postcss-cliPostCSS CLI requires PostCSS as a peer dependency:
npm install -D postcss postcss-cli# Basic file processing
postcss input.css -o output.css
# Directory processing
postcss src/**/*.css --base src --dir build
# Watch mode for development
postcss input.css -o output.css --watch
# Pipe processing
cat input.css | postcss -u autoprefixer > output.cssThe main postcss command provides all functionality through command-line options.
postcss [input.css] [OPTIONS] [-o|--output output.css] [--watch|-w]
postcss <input.css>... [OPTIONS] --dir <output-directory> [--watch|-w]
postcss <input-directory> [OPTIONS] --dir <output-directory> [--watch|-w]
postcss <input-glob-pattern> [OPTIONS] --dir <output-directory> [--watch|-w]
postcss <input.css>... [OPTIONS] --replaceBasic Options:
-o, --output - Output file (conflicts with --dir, --replace)-d, --dir - Output directory (conflicts with --output, --replace)-r, --replace - Replace (overwrite) the input file (conflicts with --output, --dir)-m, --map - Create an external sourcemap--no-map - Disable the default inline sourcemaps-w, --watch - Watch files for changes and recompile as needed--verbose - Be verbose with processing output--env - A shortcut for setting NODE_ENVPlugin Options (without config file):
-u, --use - List of postcss plugins to use--parser - Custom postcss parser--stringifier - Custom postcss stringifier--syntax - Custom postcss syntaxDirectory Options (for use with --dir):
--ext - Override the output file extension--base - Mirror the directory structure relative to this path in the output directoryAdvanced Options:
--include-dotfiles - Enable glob to match files/dirs that begin with "."--poll - Use polling for file watching (optional interval, default 100ms)--config - Set a custom directory to look for a config fileSingle File Processing:
postcss input.css -o output.cssMultiple File Processing:
postcss file1.css file2.css --dir build/Directory Processing:
postcss src/ --dir build/Glob Pattern Processing:
postcss src/**/*.css --base src --dir build/Replace Mode:
postcss src/**/*.css --replaceStdin/Stdout Processing:
cat input.css | postcss > output.css
# Or with plugins
cat input.css | postcss -u autoprefixer > output.cssWatch mode monitors input files and dependencies for changes:
# Basic watch mode
postcss input.css -o output.css --watch
# Watch with directory output
postcss src/**/*.css --base src --dir build --watch
# Watch with polling (useful for network drives)
postcss input.css -o output.css --watch --poll
postcss input.css -o output.css --watch --poll 500 # Custom intervalWatch Features:
PostCSS CLI provides flexible sourcemap options:
# Default: inline sourcemaps
postcss input.css -o output.css
# External sourcemaps
postcss input.css -o output.css --map
# Disable sourcemaps
postcss input.css -o output.css --no-mapSourcemap Behavior:
.map files alongside outputCLI Plugin Usage (without config file):
# Single plugin
postcss input.css -o output.css -u autoprefixer
# Multiple plugins
postcss input.css -o output.css -u autoprefixer -u cssnano
# Custom parser/syntax
postcss input.sss -o output.css --parser sugarss
postcss input.css -o output.css --syntax postcss-scssConfig File Support:
PostCSS CLI automatically discovers postcss.config.js files:
// postcss.config.js
module.exports = {
parser: 'sugarss',
plugins: [
require('postcss-import')({ ...options }),
require('postcss-url')({ url: 'copy', useHash: true }),
]
}Context-Aware Configuration:
// postcss.config.js with context
module.exports = (ctx) => ({
map: ctx.options.map,
parser: ctx.file.extname === '.sss' ? 'sugarss' : false,
plugins: {
'postcss-import': { root: ctx.file.dirname },
cssnano: ctx.env === 'production' ? {} : false
}
})Context Object Properties:
ctx.env - NODE_ENV value (default: 'development')ctx.file - File information object with dirname, basename, extnamectx.options - PostCSS options including map, parser, syntax, stringifierConfiguration Restrictions:
from or to options in config files (CLI-controlled)--config to specify custom config directoryBase Path Mirroring:
# Mirror directory structure
postcss src/components/**/*.css --base src --dir build/
# Input: src/components/button.css → Output: build/components/button.cssExtension Override:
# Change output extension
postcss src/**/*.scss --ext .css --dir build/Dotfile Processing:
# Include hidden files in glob matching
postcss src/**/*.css --include-dotfiles --dir build/Verbose Mode:
postcss input.css -o output.css --verboseError Types Handled:
Exit Codes:
0 - Success1 - Error (except in watch mode where processing continues)Environment Control:
# Set NODE_ENV for config functions
postcss input.css -o output.css --env productionFile Context in Config: Each processed file provides context to config functions:
Basic Plugin Chain:
// postcss.config.js
module.exports = {
plugins: [
require('postcss-import'),
require('autoprefixer'),
require('cssnano')
]
}Environment-Specific Configuration:
// postcss.config.js
module.exports = (ctx) => ({
map: ctx.options.map,
plugins: {
'postcss-import': { root: ctx.file.dirname },
'autoprefixer': {},
'cssnano': ctx.env === 'production' ? {} : false
}
})File Extension-Based Processing:
// postcss.config.js
module.exports = (ctx) => ({
parser: ctx.file.extname === '.sss' ? 'sugarss' : false,
plugins: [
require('postcss-import'),
require('autoprefixer')
]
})Build Tool Integration:
# npm scripts
"scripts": {
"css:build": "postcss src/styles.css -o dist/styles.css",
"css:watch": "postcss src/styles.css -o dist/styles.css --watch",
"css:prod": "postcss src/styles.css -o dist/styles.css --env production"
}CI/CD Usage:
# Production build with error checking
postcss src/**/*.css --base src --dir dist/ --env production
if [ $? -ne 0 ]; then
echo "CSS build failed"
exit 1
fiDevelopment Workflow:
# Development with watch and verbose output
postcss src/**/*.css --base src --dir build/ --watch --verbosefrom/to options in config files