{Boolean}
Loader to process CSS with postcss.
To begin, you'll need to install postcss-loader and postcss:
npm install --save-dev postcss-loader postcss
Then add the plugin to your webpack config. For example:
file.js
import css from 'file.css';
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
'postcss-present-env',
{
// Options
},
],
},
},
},
],
},
],
},
};
Alternative use with config files:
postcss.config.js
module.exports = {
plugins: [
[
'postcss-preset-env',
{
// Options
},
],
],
};
The loader automatically searches for configuration files.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader', 'postcss-loader'],
},
],
},
};
And run webpack via your preferred method.
| Name | Type | Default | Description |
|---|---|---|---|
Name Type Default Description
|
{Boolean} |
undefined |
Enable PostCSS Parser support in
CSS-in-JS |
Name Type Default Description
|
{Object\/Function} |
defaults values for Postcss.process |
Set
postcss
options and plugins |
Name Type Default Description
|
{Boolean} |
compiler.devtool |
Enables/Disables generation of source maps |
executeType: Boolean
Default: undefined
If you use JS styles the postcss-js parser, add the execute option.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.style.js$/,
use: [
'style-loader',
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
parser: 'postcss-js',
},
exec: true,
},
},
],
},
],
},
};
postcssOptions| Name | Type | Default | Description |
|---|---|---|---|
Name Type Default Description
|
{Function\|Object\|Array<Function\|Object>} |
[] |
Set PostCSS Plugins |
Name Type Default Description
|
{Function\|Object\|Array<Function\|Object>} |
[] |
Set PostCSS Plugins |
Name Type Default Description
|
{String\|Object\|Function} |
undefined |
Set custom PostCSS Parser |
Name Type Default Description
|
{String\|Object} |
undefined |
Set custom PostCSS Syntax |
Name Type Default Description
|
{String\|Object\|Function} |
undefined |
Set custom PostCSS Stringifier |
Type: Object|Function
Default: undefined
Allows to set postcss options(http://api.postcss.org/global.html#processOptions) and plugins.
Objectmodule.exports = {
module: {
rules: [
{
test: /\.sss$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
parser: require('sugarss'),
},
},
},
],
},
};
Functionmodule.exports = {
module: {
rules: [
{
test: /\.sss$/i,
loader: 'postcss-loader',
options: {
postcssOptions: (loaderContext) => ({
parser: require('sugarss'),
}),
},
},
],
},
};
configType: Boolean|String
Default: undefined
Allows to set options using config files. Options specified in the config file are combined with options passed to the loader, the loader options overwrite options from config.
The loader will search up the directory tree for configuration in the following places:
postcss property in package.json.postcssrc file in JSON or YAML format.postcss.json, .postcss.yaml, .postcss.yml, .postcss.js, or .postcss.cjs filepostcss.config.js or postcss.config.cjs CommonJS module exporting an object (recommended)You can use different postcss.config.js files in different directories.
Config lookup starts from path.dirname(file) and walks the file tree upwards until a config file is found.
|– components
| |– component
| | |– index.js
| | |– index.png
| | |– style.css (1)
| | |– postcss.config.js (1)
| |– component
| | |– index.js
| | |– image.png
| | |– style.css (2)
|
|– postcss.config.js (1 && 2 (recommended))
|– webpack.config.js
|
|– package.json
After setting up your postcss.config.js, add postcss-loader to your webpack.config.js.
You can use it standalone or in conjunction with css-loader (recommended).
Use it before css-loader and style-loader, but after other preprocessor loaders like e.g sass|less|stylus-loader, if you use any (since webpack loaders evaluate right to left/bottom to top).
webpack.config.js (recommended)
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
'postcss-loader',
],
},
],
},
};
Enables/Disables autoloading config.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
config: false,
},
},
],
},
};
Allows to specify the absolute path to the config file.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
config: path.resolve(__dirname, 'custom.config.js'),
},
},
],
},
};
PluginsType: Function|Object|Array<String|Function\|Object|Array>
Default: []
It is recommended to specify plugins in the format Array<String\|Array> or Function that returns the same array as shown below.
Object format ({pluginName: pluginOptions}) is deprecated and will be removed in the next major release.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
plugins: [
'postcss-import',
'postcss-nested',
['postcss-short', { prefix: 'x' }],
],
},
},
],
},
};
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
plugins: (loader) => [
['postcss-import', { root: loader.resourcePath }],
'postcss-nested',
'cssnano',
],
},
},
],
},
};
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: (loader) => [
require('postcss-import')({ root: loader.resourcePath }),
require('postcss-preset-env')(),
require('cssnano')(),
],
},
},
},
],
},
};
⚠️ The method below for specifying plugins is deprecated.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
plugins: {
'postcss-import': {},
'postcss-nested': {},
'postcss-short': { prefix: 'x' },
},
},
},
],
},
};
It is possible to disable the plugin specified in the config.
⚠️ The method below for specifying plugins is deprecated.
postcss.config.js
module.exports = {
plugins: {
'postcss-short': { prefix: 'x' },
'postcss-import': {},
'postcss-nested': {},
},
};
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: {
'postcss-import': {},
'postcss-nested': {},
// Turn off the plugin
'postcss-short': false,
},
},
},
},
],
},
};
ParserType: String|Object|Function
Default: undefined
StringThe passed string is converted to the form require('string').
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.sss$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
// Will be converted to `require('sugarss')`
parser: 'sugarss',
},
},
},
],
},
};
Objectwebpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.sss$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
parser: require('sugarss'),
},
},
},
],
},
};
Functionwebpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.sss$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
parser: require('sugarss').parse,
},
},
},
],
},
};
SyntaxType: String|Object
Default: undefined
StringThe passed string is converted to the form require('string').
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
// Will be converted to `require('sugarss')`
syntax: 'sugarss',
},
},
},
],
},
};
Objectwebpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
stringifier: require('sugarss'),
},
},
},
],
},
};
StringifierType: String|Object|Function
Default: undefined
StringThe passed string is converted to the form require('string').
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
// Will be converted to `require('sugarss')`
stringifier: 'sugarss',
},
},
},
],
},
};
Objectwebpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
stringifier: require('sugarss'),
},
},
},
],
},
};
Functionwebpack.config.js
const Midas = require('midas');
const midas = new Midas();
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'postcss-loader',
options: {
postcssOptions: {
stringifier: midas.stringifier,
},
},
},
],
},
};
sourceMapType: Boolean
Default: depends on the compiler.devtool value
By default generation of source maps depends on the devtool option.
All values enable source map generation except eval and false value.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'postcss-loader', options: { sourceMap: true } },
{ loader: 'sass-loader', options: { sourceMap: true } },
],
},
],
},
};
Alternative setup:
webpack.config.js
module.exports = {
devtool: 'source-map',
module: {
rules: [
{
test: /\.css$/i,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
{ loader: 'postcss-loader' },
{ loader: 'sass-loader' },
],
},
],
},
};
Add vendor prefixes to CSS rules using autoprefixer.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: { importLoaders: 1 },
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
[
'autoprefixer',
{
// Options
},
],
],
},
},
},
],
},
],
},
};
:warning:
postcss-preset-envincludesautoprefixer, so adding it separately is not necessary if you already use the preset. More information
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: { importLoaders: 1 },
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
[
'postcss-preset-env',
{
// Options
},
],
],
},
},
},
],
},
],
},
};
What is CSS Modules? Please read.
No additional options required on the postcss-loader side.
To make them work properly, either add the css-loader’s importLoaders option.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
},
},
'postcss-loader',
],
},
],
},
};
postcss-jsIf you want to process styles written in JavaScript, use the postcss-js parser.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.style.js$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 2,
},
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
parser: 'postcss-js',
},
execute: true,
},
},
'babel-loader',
],
},
],
},
};
As result you will be able to write styles in the following way
import colors from './styles/colors';
export default {
'.menu': {
color: colors.main,
height: 25,
'&_link': {
color: 'white',
},
},
};
:warning: If you are using Babel you need to do the following in order for the setup to work
- Add
babel-plugin-add-module-exportsto your configuration.- You need to have only one default export per style module.
Using mini-css-extract-plugin.
webpack.config.js
const isProductionMode = process.env.NODE_ENV === 'production';
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
mode: isProductionMode ? 'production' : 'development',
module: {
rules: [
{
test: /\.css$/,
use: [
isProductionMode ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader',
'postcss-loader',
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: isProductionMode ? '[name].[contenthash].css' : '[name].css',
}),
],
};
To write a asset from the postcss plugin to the webpack's output, need to add a message in result.messages.
The message should contain the following fields:
type = asset - Message type (require, should be equal asset)file - file name (require)content - file content (require)sourceMap - sourceMapinfo - asset infowebpack.config.js
const customPlugin = () => (css, result) => {
result.messages.push({
type: 'asset',
file: 'sprite.svg',
content: '<svg>...</svg>',
});
};
const postcssPlugin = postcss.plugin('postcss-assets', customPlugin);
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [postcssPlugin()],
},
},
},
],
},
],
},
};
There are two way to add dependencies:
result.messages.The message should contain the following fields:
type = dependency - Message type (require, should be equal dependency)file - absolute file path (require)webpack.config.js
const path = require('path');
const customPlugin = () => (css, result) => {
result.messages.push({
type: 'dependency',
file: path.resolve(__dirname, 'path', 'to', 'file'),
});
};
const postcssPlugin = postcss.plugin('postcss-assets', customPlugin);
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [postcssPlugin()],
},
},
},
],
},
],
},
};
loaderContext in plugin.webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
config: 'path/to/postcss.config.js',
},
},
],
},
],
},
};
postcss.config.js
module.exports = (loaderContext) => ({
postcssOptions: {
plugins: [require('path/to/customPlugin')(loaderContext)],
},
});
customPlugin.js
const path = require('path');
const customPlugin = (loaderContext) => (css, result) => {
loaderContext.webpack.addDependency(
path.resolve(__dirname, 'path', 'to', 'file')
);
};
module.exports = postcss.plugin('postcss-assets', customPlugin);
Please take a moment to read our contributing guidelines if you haven't yet done so.