postcss-loader
PostCSS Loader
Loader for webpack to process CSS with PostCSS
Install
Usage
Configuration
Configuration
postcss.config.js
You can read more about common PostCSS Config here.
Config Cascade
Config Cascade
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.
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 after css-loader
and style-loader
, but before other preprocessor loaders like e.g sass|less|stylus-loader
, if you use any.
webpack.config.js
⚠️ When
postcss-loader
is used standalone (withoutcss-loader
) don't use@import
in your CSS, since this can lead to quite bloated bundles
webpack.config.js
(recommended)
Options
Name | Type | Default | Description |
---|---|---|---|
|
| Enable PostCSS Parser support in | |
|
| Set PostCSS Parser | |
|
| Set PostCSS Syntax | |
|
| Set PostCSS Stringifier | |
|
| Set | |
|
| Set PostCSS Plugins | |
|
| Enable Source Maps |
Exec
Exec
If you use JS styles without the postcss-js
parser, add the exec
option.
webpack.config.js
Config
Config
Name | Type | Default | Description |
---|---|---|---|
|
| PostCSS Config Directory | |
|
| PostCSS Config Context |
Path
You can manually specify the path to search for your config (postcss.config.js
) with the config.path
option. This is needed if you store your config in a separate e.g ./config || ./.config
folder.
⚠️ Otherwise it is unnecessary to set this option and is not recommended
⚠️ Note that you can't use a filename other than the supported config formats (e.g
.postcssrc.js
,postcss.config.js
), this option only allows you to manually specify the directory where config lookup should start from
webpack.config.js
Context (ctx)
Name | Type | Default | Description |
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
| Options |
postcss-loader
exposes context ctx
to the config file, making your postcss.config.js
dynamic, so can use it to do some real magic ✨
postcss.config.js
webpack.config.js
Plugins
Plugins
webpack.config.js
⚠️ webpack requires an identifier (
ident
) inoptions
when{Function}/require
is used (Complex Options). Theident
can be freely named as long as it is unique. It's recommended to name it (ident: 'postcss'
)
Syntaxes
Syntaxes
Name | Type | Default | Description |
---|---|---|---|
|
| Custom PostCSS Parser | |
|
| Custom PostCSS Syntax | |
|
| Custom PostCSS Stringifier |
Parser
webpack.config.js
Syntax
webpack.config.js
Stringifier
webpack.config.js
SourceMap
SourceMap
Enables source map support, postcss-loader
will use the previous source map given by other loaders and update it accordingly, if no previous loader is applied before postcss-loader
, the loader will generate a source map for you.
webpack.config.js
'inline'
You can set the sourceMap: 'inline'
option to inline the source map within the CSS directly as an annotation comment.
webpack.config.js
Examples
Stylelint
Stylelint
webpack.config.js
Autoprefixing
Autoprefixing
webpack.config.js
⚠️
postcss-preset-env
includesautoprefixer
, so adding it separately is not necessary if you already use the preset.
CSS Modules
CSS Modules
This loader cannot be used with CSS Modules out of the box due to the way css-loader
processes file imports. To make them work properly, either add the css-loader’s importLoaders
option.
webpack.config.js
or use postcss-modules instead of css-loader
.
CSS-in-JS
CSS-in-JS
If you want to process styles written in JavaScript, use the postcss-js parser.
webpack.config.js
As result you will be able to write styles in the following way
⚠️ If you are using Babel you need to do the following in order for the setup to work
Add babel-plugin-add-module-exports to your configuration
You need to have only one default export per style module
webpack.config.js
Maintainers
Last updated