This plugin uses cssnano to optimize and minify your CSS.
Just like optimize-css-assets-webpack-plugin but more accurate with source maps and assets using query string, allows to cache and works in parallel mode.
To begin, you'll need to install css-minimizer-webpack-plugin
:
$ npm install css-minimizer-webpack-plugin --save-dev
Then add the plugin to your webpack
configuration. For example:
webpack.config.js
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
module.exports = {
module: {
loaders: [
{
test: /.s?css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
},
],
},
optimization: {
minimize: true,
minimizer: [
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
// `...`
new CssMinimizerPlugin(),
],
},
};
This will enable CSS optimization only in production mode.
If you want to run it also in development set the optimization.minimize
option to true
.
And run webpack
via your preferred method.
Works only with source-map
, inline-source-map
, hidden-source-map
and nosources-source-map
values for the devtool
option.
Why?
eval
wraps modules ineval("string")
and the minimizer does not handle strings.cheap
has not column information and minimizer generate only a single line, which leave only a single mapping.
Using supported devtool
values enable source map generation.
If you use your own minify
function please read the minify
section for handling source maps correctly.
Type: String|RegExp|Array<String|RegExp>
- default: /\.css(\?.*)?$/i
Test to match files against.
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
test: /\.foo\.css$/i,
}),
],
},
};
Type: String|RegExp|Array<String|RegExp>
Default: undefined
Files to include.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
include: /\/includes/,
}),
],
},
};
Type: String|RegExp|Array<String|RegExp>
Default: undefined
Files to exclude.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
exclude: /\/excludes/,
}),
],
},
};
Type: Boolean|Number
Default: true
Use multi-process parallel running to improve the build speed.
Default number of concurrent runs: os.cpus().length - 1
.
ℹ️ Parallelization can speedup your build significantly and is therefore highly recommended.
Enable/disable multi-process parallel running.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
parallel: true,
}),
],
},
};
Enable multi-process parallel running and set number of concurrent runs.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
parallel: 4,
}),
],
},
};
Type: Function
Default: undefined
Allows you to override default minify function. By default plugin uses cssnano package. Useful for using and testing unpublished versions or forks.
⚠️ Always userequire
insideminify
function whenparallel
option enabled.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
sourceMap: true,
minify: (data, inputMap, minimizerOptions) => {
const postcss = require('postcss');
const plugin = postcss.plugin(
'custom-plugin',
() => (css, result) => {
// custom code
}
);
const [[filename, input]] = Object.entries(data);
const postcssOptions = {
from: filename,
to: filename,
map: {
prev: inputMap,
},
};
return postcss([plugin])
.process(input, postcssOptions)
.then((result) => {
return {
css: result.css,
map: result.map,
warnings: result.warnings(),
};
});
},
}),
],
},
};
Type: Object
Default: { preset: 'default' }
Cssnano optimisations options.
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
},
],
},
}),
],
},
};
Type: Function<(warning, file, source) -> Boolean>
Default: () => true
Allow to filter css-minimizer warnings (By default cssnano).
Return true
to keep the warning, a falsy value (false
/null
/undefined
) otherwise.
⚠️ Thesource
argument will containundefined
if you don't use source maps.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
warningsFilter: (warning, file, source) => {
if (/Dropping unreachable code/i.test(warning)) {
return true;
}
if (/file\.css/i.test(file)) {
return true;
}
if (/source\.css/i.test(source)) {
return true;
}
return false;
},
}),
],
},
};
Don't forget to enable sourceMap
options for all loaders.
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
module.exports = {
module: {
loaders: [
{
test: /.s?css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'sass-loader', options: { sourceMap: true } },
],
},
],
},
optimization: {
minimizer: [
new CssMinimizerPlugin({
sourceMap: true,
}),
],
},
};
Remove all comments (including comments starting with /*!
).
module.exports = {
optimization: {
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
},
],
},
}),
],
},
};
Using custom minifier csso
By default plugin uses cssnano package. It is possible to use another minify function.
⚠️ Always userequire
insideminify
function whenparallel
option enabled.
webpack.config.js
module.exports = {
devtool: 'source-map',
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
sourceMap: true,
minify: async (data, inputMap) => {
const csso = require('csso');
const sourcemap = require('source-map');
const [[filename, input]] = Object.entries(data);
const minifiedCss = csso.minify(input, {
filename: filename,
sourceMap: true,
});
if (inputMap) {
minifiedCss.map.applySourceMap(
new sourcemap.SourceMapConsumer(inputMap),
filename
);
}
return {
css: minifiedCss.css,
map: minifiedCss.map.toJSON(),
};
},
}),
],
},
};
Using custom minifier clean-css
By default plugin uses cssnano package. It is possible to use another minify function.
⚠️ Always userequire
insideminify
function whenparallel
option enabled.
webpack.config.js
module.exports = {
devtool: 'source-map',
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
sourceMap: true,
minify: async (data, inputMap) => {
// eslint-disable-next-line global-require
const CleanCSS = require('clean-css');
const [[filename, input]] = Object.entries(data);
const minifiedCss = await new CleanCSS({ sourceMap: true }).minify({
[filename]: {
styles: input,
sourceMap: inputMap,
},
});
return {
css: minifiedCss.styles,
map: minifiedCss.sourceMap.toJSON(),
warnings: minifiedCss.warnings,
};
},
}),
],
},
};
Please take a moment to read our contributing guidelines if you haven't yet done so.