Skip to content package
css-minimizer-webpack-plugin v7.0.3 CSS minimizer (minifier) plugin for Webpack
node >= 18.12.0 6 deps cjs types open source
overview readme
license MIT
deps 6
unpacked 73.7 kB
files 10
node >= 18.12.0 $ npm install css-minimizer-webpack-plugin@7.0.3n npmREADME GitHub-flavored package documentation.
downloads Across all versions.
Download trend unavailable.
compatibility Runtime metadata.
keywords Tagged topics.
cssnano css csso clean-css swc esbuild webpack webpack-plugin minimize minimizer minify minifier
install size Estimated unpacked size across 41 packages.
9.5 MB
Includes dependencies.
dependencies 6 direct · 1 outdated
versions Latest published Mar 2, 2026
maintainers Registry package owners.
sokra
tobias.koppers@googlemail.com
css-minimizer-webpack-plugin This plugin uses cssnano to optimize and minify your CSS.
It serves as a more accurate alternative to optimize-css-assets-webpack-plugin , with better support for source maps, assets with query strings, caching, and parallel processing.
Getting Started To begin, you'll need to install css-minimizer-webpack-plugin:
npm install css-minimizer-webpack-plugin --save-dev copy yarn add -D css-minimizer-webpack-plugin copy pnpm add -D css-minimizer-webpack-plugin copy Then add the plugin to your webpack configuration. For example:
const CssMinimizerPlugin = require ( "css-minimizer-webpack-plugin" );
const MiniCssExtractPlugin = require ( "mini-css-extract-plugin" );
module . exports = {
module: {
rules: [
{
test: /\.s?css$/ ,
use: [MiniCssExtractPlugin.loader, "css-loader" , "sass-loader" ],
},
],
},
optimization: {
minimizer: [
// For webpack v5, you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line // `...`,
new CssMinimizerPlugin (),
],
},
plugins: [ new MiniCssExtractPlugin ()],
}; copy
[!NOTE]
This enables CSS optimization only in production mode by default.
To enable it in development mode as well, set the optimization.minimize option to true:
// [...]
module . exports = {
optimization: {
// [...]
minimize: true ,
},
}; copy Finally, run Webpack using your preferred method.
Note about source maps This plugin works only with source-map, inline-source-map, hidden-source-map and nosources-source-map values for the devtool option.
Why? Because CSS support only these source map types.
The plugin respects the devtool setting and uses the SourceMapDevToolPlugin internally.
Using a supported devtool value enables source map generation.
Enabling the columns option in SourceMapDevToolPlugin also allows source map generation.
Use source maps to map error message locations to their original modules (note that this may slow down compilation).
If you use your own minify function please refer to the minify section for correct handling of source maps.
Options Name Type Default Description testString|RegExp|Array<String|RegExp>/\.css(\?.*)?$/iTest to match files against. includeString|RegExp|Array<String|RegExp>undefinedFiles to include. excludeString|RegExp|Array<String|RegExp>undefinedFiles to exclude. parallelBoolean|NumbertrueEnable or disable multi-process parallel running. minifyFunction|Array<Function>CssMinimizerPlugin.cssnanoMinifyAllows to override default minify function. minimizerOptionsObject|Array<Object>{ preset: 'default' }Cssnano optimisations options . warningsFilterFunction<(warning, file, source) -> Boolean>() => trueAllows filtering of css-minimizer warnings.
test
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 ,
}),
],
},
}; copy
include
Type: String|RegExp|Array<String|RegExp>
Default: undefined
module . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
include: /\/includes/ ,
}),
],
},
}; copy
exclude
Type: String|RegExp|Array<String|RegExp>
Default: undefined
module . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
exclude: /\/excludes/ ,
}),
],
},
}; copy
parallel
Type: Boolean|Number
Default: true
Use multi-process parallel running to improve the build speed.
The default number of concurrent runs: os.cpus().length - 1 or os.availableParallelism() - 1 (if this function is supported).
ℹ️ Parallelization can speed up your build significantly and is therefore highly recommended .
If a parallelization is enabled, the packages in minimizerOptions must be required via strings (packageName or require.resolve(packageName)). Read more in minimizerOptions
BooleanEnable or disable multi-process parallel running.
module . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
parallel: true ,
}),
],
},
}; copy
NumberEnable multi-process parallel running and specify the number of concurrent runs.
module . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
parallel: 4 ,
}),
],
},
}; copy
minify
Type: Function|Array<Function>
Default: CssMinimizerPlugin.cssnanoMinify
Overrides the default minify function.
By default, plugin uses cssnano package.
This is useful when using or testing unpublished versions or forks.
CssMinimizerPlugin.cssnanoMinify
CssMinimizerPlugin.cssoMinify
CssMinimizerPlugin.cleanCssMinify
CssMinimizerPlugin.esbuildMinify
CssMinimizerPlugin.lightningCssMinify (previouslyCssMinimizerPlugin.parcelCssMinify, the package was renamed, but we keep it for backward compatibility)
async (data, inputMap, minimizerOptions) => {return {code: "a{color: red}", map: "...", warnings: [], errors: []}}
[!WARNING]
Always use require inside minify function when parallel option is enabled .
Functionmodule . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minimizerOptions: {
level: {
1 : {
roundingPrecision: "all=3,px=5" ,
},
},
},
minify: CssMinimizerPlugin.cleanCssMinify,
}),
],
},
}; copy
ArrayIf an array of functions is passed to the minify option, the minimizerOptions must also be an array.
The function index in the minify array corresponds to the options object with the same index in the minimizerOptions array.
module . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minimizerOptions: [
{}, // Options for the first function (CssMinimizerPlugin.cssnanoMinify),
{}, // Options for the second function (CssMinimizerPlugin.cleanCssMinify),
{}, // Options for the third function
],
minify: [
CssMinimizerPlugin.cssnanoMinify,
CssMinimizerPlugin.cleanCssMinify,
async (data, inputMap, minimizerOptions) =>
// Custom minifier function
({
code: "a{color: red}" ,
map: '{"version": "3", ...}' ,
warnings: [],
errors: [],
}),
],
}),
],
},
}; copy
minimizerOptions
Type: Object|Array<Object>
Default: { preset: 'default' }
Objectmodule . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minimizerOptions: {
preset: [
"default" ,
{
discardComments: { removeAll: true },
},
],
},
}),
],
},
}; copy
ArrayThe function index in the minify array corresponds to the options object with the same index in the minimizerOptions array.
If you use minimizerOptions like object, all minify function accept it.
If parallelization is enabled, the packages in minimizerOptions must be referenced via strings (packageName or require.resolve(packageName)). In this case, we shouldn't use require/import.
module . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minimizerOptions: {
preset: require. resolve ( "cssnano-preset-simple" ),
},
}),
],
},
}; copy
processorOptions (⚠ only cssnano)
Type: Object
Default: { from: assetName }
The parser, stringifier and syntax can be either a function or a string indicating the module that will be imported.
[!WARNING]
If any of these options are passed as a function, the parallel option must be disabled. .
import sugarss from "sugarss" ;
module . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
parallel: false ,
minimizerOptions: {
processorOptions: {
parser: sugarss,
},
},
}),
],
},
}; copy module . exports = {
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minimizerOptions: {
processorOptions: {
parser: "sugarss" ,
},
},
}),
],
},
}; copy
warningsFilter
Type: Function<(warning, file, source) -> Boolean>
Default: () => true
Filter css-minimizer warnings (By default cssnano ).
Return true to keep the warning, or a falsy value (false/null/undefined) to suppress it.
[!WARNING]
The source parameter will be undefined unless source maps are enabled.
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 ;
},
}),
],
},
}; copy
Examples
Use sourcemaps Don't forget to enable sourceMap options for all loaders.
const CssMinimizerPlugin = require ( "css-minimizer-webpack-plugin" );
module . exports = {
devtool: "source-map" ,
module: {
rules: [
{
test: /\.s?css$/ ,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader" , options: { sourceMap: true } },
{ loader: "sass-loader" , options: { sourceMap: true } },
],
},
],
},
optimization: {
minimizer: [ new CssMinimizerPlugin ()],
},
plugins: [ new MiniCssExtractPlugin ()],
}; copy
Remove all comments, including those starting with /*!.
module . exports = {
optimization: {
minimizer: [
new CssMinimizerPlugin ({
minimizerOptions: {
preset: [
"default" ,
{
discardComments: { removeAll: true },
},
],
},
}),
],
},
}; copy Using custom minifier csso module . exports = {
// Uncomment if you need source maps
// devtool: "source-map",
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minify: CssMinimizerPlugin.cssoMinify,
// Uncomment this line for options
// minimizerOptions: { restructure: false },
}),
],
},
}; copy module . exports = {
// Uncomment if you need source maps
// devtool: "source-map",
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minify: CssMinimizerPlugin.cleanCssMinify,
// Uncomment this line for options
// minimizerOptions: { compatibility: 'ie11,-properties.merging' },
}),
],
},
}; copy module . exports = {
// Uncomment if you need source maps
// devtool: "source-map",
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minify: CssMinimizerPlugin.esbuildMinify,
}),
],
},
}; copy module . exports = {
// devtool: "source-map", // Uncomment for source maps
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minify: CssMinimizerPlugin.lightningCssMinify,
// Uncomment this line for options
// minimizerOptions: { targets: { ie: 11 }, drafts: { nesting: true } },
}),
],
},
}; copy Using custom minifier swc module . exports = {
// devtool: "source-map", // Uncomment for source maps
optimization: {
minimize: true ,
minimizer: [
new CssMinimizerPlugin ({
minify: CssMinimizerPlugin.swcMinify,
// Uncomment this line for options
// minimizerOptions: {},
}),
],
},
}; copy
Contributing We welcome all contributions!
If you're new here, please take a moment to review our contributing guidelines.
License