What is postcss-cli?
The postcss-cli package is a command-line interface for PostCSS, a tool for transforming CSS with JavaScript plugins. It allows you to process CSS files using various PostCSS plugins directly from the command line.
What are postcss-cli's main functionalities?
Basic CSS Processing
This command processes the input CSS file (input.css) and outputs the transformed CSS to the specified output file (output.css).
postcss input.css -o output.css
Using Plugins
This command uses the 'autoprefixer' plugin to add vendor prefixes to the CSS rules in the input file and outputs the result to the specified output file.
postcss --use autoprefixer -o output.css input.css
Config File
This command uses a configuration file (postcss.config.js) to specify the plugins and options for processing the CSS file.
postcss -c postcss.config.js -o output.css input.css
Watch Mode
This command watches the input CSS file for changes and automatically processes it whenever it is modified, outputting the result to the specified output file.
postcss input.css -o output.css --watch
Other packages similar to postcss-cli
gulp-postcss
gulp-postcss is a Gulp plugin for PostCSS. It allows you to integrate PostCSS processing into your Gulp build pipeline. Compared to postcss-cli, gulp-postcss is more suitable for complex build processes where you are already using Gulp.
webpack
Webpack is a module bundler that can be configured to use PostCSS through loaders like 'postcss-loader'. It is more powerful and flexible than postcss-cli, making it suitable for larger projects with more complex build requirements.
grunt-postcss
grunt-postcss is a Grunt plugin for PostCSS. It allows you to use PostCSS in your Grunt build tasks. Similar to gulp-postcss, it is more suitable for projects that are already using Grunt for their build process.
postcss-cli
Traditional CLI for postcss
Instalation
npm install postcss-cli
Usage
postcss [options] -o output-file input-file
--output|-o
Output file name
--dir|-d
Output files location. Either --output
or --dir
option, but not both of them, need to be specified.
--dir
needs to be used if multiple input file is provided.
--use|-u
Plugin to be used. Multiple plugins can be specified. At least one is required.
--config|-c
JSON file with plugin configuration. Plugin names should be the keys.
{
"autoprefixer": {
"browsers": "> 5%"
},
"postcss-cachify": {
"baseUrl": "/res"
}
}
JS configuration can be used if functions are allowed as plugins parameters:
module.exports = {
"postcss-url": {
url: function(url) { return "http://example.com/" + url; }
},
autoprefixer: {
browsers: "> 5%"
}
};
Alternatively configuration options can be passed as --plugin.option
parameters.
--safe
Enable Safe Mode, in which PostCSS will try to fix CSS syntax errors.
-h, --help
Show help
Examples
Use autoprefixer as a postcss plugin pass parameters from a json file
postcss --use autoprefixer -c options.json -o screen.css screen.css
Use more than one plugin and pass config parameters
postcss --use autoprefixer --autoprefixer.browsers "> 5%" \
--use postcss-cachify --postcss-cachify.baseUrl /res \
-o screen.css screen.css
Use multiple plugins and multiple input files
postcss -u postcss-cachify -u autoprefixer -d build *.css
Licence
MIT