Gathering detailed insights and metrics for css-minimizer-webpack-plugin
Gathering detailed insights and metrics for css-minimizer-webpack-plugin
Gathering detailed insights and metrics for css-minimizer-webpack-plugin
Gathering detailed insights and metrics for css-minimizer-webpack-plugin
optimize-css-assets-webpack-plugin
A Webpack plugin to optimize \ minimize CSS assets.
image-minimizer-webpack-plugin
Webpack loader and plugin to optimize (compress) images using imagemin
terser-webpack-plugin
Terser plugin for webpack
@rsbuild/plugin-css-minimizer
An Rsbuild to customize the CSS minimizer, switch to [cssnano](https://github.com/cssnano/cssnano) or other tools for CSS compression.
npm install css-minimizer-webpack-plugin
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
319 Stars
240 Commits
30 Forks
12 Watching
2 Branches
38 Contributors
Updated on 25 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-7.7%
1,129,525
Compared to previous day
Last week
1.1%
5,979,928
Compared to previous week
Last month
8.2%
25,504,153
Compared to previous month
Last year
10.4%
288,248,556
Compared to previous year
1
40
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 caching and works in parallel mode.
To begin, you'll need to install css-minimizer-webpack-plugin
:
1npm install css-minimizer-webpack-plugin --save-dev
or
1yarn add -D css-minimizer-webpack-plugin
or
1pnpm add -D css-minimizer-webpack-plugin
Then add the plugin to your webpack
configuration. For example:
webpack.config.js
1const MiniCssExtractPlugin = require("mini-css-extract-plugin"); 2const CssMinimizerPlugin = require("css-minimizer-webpack-plugin"); 3 4module.exports = { 5 module: { 6 rules: [ 7 { 8 test: /.s?css$/, 9 use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"], 10 }, 11 ], 12 }, 13 optimization: { 14 minimizer: [ 15 // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line 16 // `...`, 17 new CssMinimizerPlugin(), 18 ], 19 }, 20 plugins: [new MiniCssExtractPlugin()], 21};
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
:
webpack.config.js
1// [...] 2module.exports = { 3 optimization: { 4 // [...] 5 minimize: true, 6 }, 7};
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? Because CSS support only these source map types.
The plugin respect the devtool
and using the SourceMapDevToolPlugin
plugin.
Using supported devtool
values enable source map generation.
Using SourceMapDevToolPlugin
with enabled the columns
option enables source map generation.
Use source maps to map error message locations to modules (this slows down the compilation).
If you use your own minify
function please read the minify
section for handling source maps correctly.
Name | Type | Default | Description |
---|---|---|---|
test | String|RegExp|Array<String|RegExp> | /\.css(\?.*)?$/i | Test to match files against. |
include | String|RegExp|Array<String|RegExp> | undefined | Files to include. |
exclude | String|RegExp|Array<String|RegExp> | undefined | Files to exclude. |
parallel | Boolean|Number | true | Enable/disable multi-process parallel running. |
minify | Function|Array<Function> | CssMinimizerPlugin.cssnanoMinify | Allows to override default minify function. |
minimizerOptions | Object|Array<Object> | { preset: 'default' } | Cssnano optimisations options. |
warningsFilter | Function<(warning, file, source) -> Boolean> | () => true | Allow to filter css-minimizer warnings. |
test
Type: String|RegExp|Array<String|RegExp>
- default: /\.css(\?.*)?$/i
Test to match files against.
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 test: /\.foo\.css$/i, 7 }), 8 ], 9 }, 10};
include
Type: String|RegExp|Array<String|RegExp>
Default: undefined
Files to include.
webpack.config.js
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 include: /\/includes/, 7 }), 8 ], 9 }, 10};
exclude
Type: String|RegExp|Array<String|RegExp>
Default: undefined
Files to exclude.
webpack.config.js
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 exclude: /\/excludes/, 7 }), 8 ], 9 }, 10};
parallel
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 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
orrequire.resolve(packageName)
). Read more inminimizerOptions
Boolean
Enable/disable multi-process parallel running.
webpack.config.js
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 parallel: true, 7 }), 8 ], 9 }, 10};
Number
Enable multi-process parallel running and set number of concurrent runs.
webpack.config.js
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 parallel: 4, 7 }), 8 ], 9 }, 10};
minify
Type: Function|Array<Function>
Default: CssMinimizerPlugin.cssnanoMinify
Allows overriding default minify function. By default, plugin uses cssnano package. Useful for using and testing unpublished versions or forks.
Possible options:
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
insideminify
function whenparallel
option enabled.
Function
webpack.config.js
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 minimizerOptions: { 7 level: { 8 1: { 9 roundingPrecision: "all=3,px=5", 10 }, 11 }, 12 }, 13 minify: CssMinimizerPlugin.cleanCssMinify, 14 }), 15 ], 16 }, 17};
Array
If 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.
webpack.config.js
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 minimizerOptions: [ 7 {}, // Options for the first function (CssMinimizerPlugin.cssnanoMinify) 8 {}, // Options for the second function (CssMinimizerPlugin.cleanCssMinify) 9 {}, // Options for the third function 10 ], 11 minify: [ 12 CssMinimizerPlugin.cssnanoMinify, 13 CssMinimizerPlugin.cleanCssMinify, 14 async (data, inputMap, minimizerOptions) => { 15 // To do something 16 return { 17 code: `a{color: red}`, 18 map: `{"version": "3", ...}`, 19 warnings: [], 20 errors: [], 21 }; 22 }, 23 ], 24 }), 25 ], 26 }, 27};
minimizerOptions
Type: Object|Array<Object>
Default: { preset: 'default' }
Cssnano optimisations options.
Object
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 minimizerOptions: { 7 preset: [ 8 "default", 9 { 10 discardComments: { removeAll: true }, 11 }, 12 ], 13 }, 14 }), 15 ], 16 }, 17};
Array
The 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 a parallelization is enabled, the packages in
minimizerOptions
must be required via strings (packageName
orrequire.resolve(packageName)
). In this case, we shouldn't userequire
/import
.
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 minimizerOptions: { 7 preset: require.resolve("cssnano-preset-simple"), 8 }, 9 }), 10 ], 11 }, 12};
processorOptions
(⚠ only cssnano)Type: Object
Default: { from: assetName }
Allows filtering options processoptions
for the cssnano.
The parser
, stringifier
and syntax
can be either a function or a string indicating the module that will be imported.
[!WARNING]
If a function is passed, the
parallel
option must be disabled..
1import sugarss from "sugarss"; 2 3module.exports = { 4 optimization: { 5 minimize: true, 6 minimizer: [ 7 new CssMinimizerPlugin({ 8 parallel: false, 9 minimizerOptions: { 10 processorOptions: { 11 parser: sugarss, 12 }, 13 }, 14 }), 15 ], 16 }, 17};
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 minimizerOptions: { 7 processorOptions: { 8 parser: "sugarss", 9 }, 10 }, 11 }), 12 ], 13 }, 14};
warningsFilter
Type: Function<(warning, file, source) -> Boolean>
Default: () => true
Allow filtering css-minimizer warnings (By default cssnano).
Return true
to keep the warning, a falsy value (false
/null
/undefined
) otherwise.
[!WARNING]
The
source
argument will containundefined
if you don't use source maps.
webpack.config.js
1module.exports = { 2 optimization: { 3 minimize: true, 4 minimizer: [ 5 new CssMinimizerPlugin({ 6 warningsFilter: (warning, file, source) => { 7 if (/Dropping unreachable code/i.test(warning)) { 8 return true; 9 } 10 11 if (/file\.css/i.test(file)) { 12 return true; 13 } 14 15 if (/source\.css/i.test(source)) { 16 return true; 17 } 18 19 return false; 20 }, 21 }), 22 ], 23 }, 24};
Don't forget to enable sourceMap
options for all loaders.
1const CssMinimizerPlugin = require("css-minimizer-webpack-plugin"); 2 3module.exports = { 4 devtool: "source-map", 5 module: { 6 rules: [ 7 { 8 test: /.s?css$/, 9 use: [ 10 MiniCssExtractPlugin.loader, 11 { loader: "css-loader", options: { sourceMap: true } }, 12 { loader: "sass-loader", options: { sourceMap: true } }, 13 ], 14 }, 15 ], 16 }, 17 optimization: { 18 minimizer: [new CssMinimizerPlugin()], 19 }, 20 plugins: [new MiniCssExtractPlugin()], 21};
Remove all comments (including comments starting with /*!
).
1module.exports = { 2 optimization: { 3 minimizer: [ 4 new CssMinimizerPlugin({ 5 minimizerOptions: { 6 preset: [ 7 "default", 8 { 9 discardComments: { removeAll: true }, 10 }, 11 ], 12 }, 13 }), 14 ], 15 }, 16};
webpack.config.js
1module.exports = { 2 // Uncomment if you need source maps 3 // devtool: "source-map", 4 optimization: { 5 minimize: true, 6 minimizer: [ 7 new CssMinimizerPlugin({ 8 minify: CssMinimizerPlugin.cssoMinify, 9 // Uncomment this line for options 10 // minimizerOptions: { restructure: false }, 11 }), 12 ], 13 }, 14};
webpack.config.js
1module.exports = { 2 // Uncomment if you need source maps 3 // devtool: "source-map", 4 optimization: { 5 minimize: true, 6 minimizer: [ 7 new CssMinimizerPlugin({ 8 minify: CssMinimizerPlugin.cleanCssMinify, 9 // Uncomment this line for options 10 // minimizerOptions: { compatibility: 'ie11,-properties.merging' }, 11 }), 12 ], 13 }, 14};
webpack.config.js
1module.exports = { 2 // Uncomment if you need source maps 3 // devtool: "source-map", 4 optimization: { 5 minimize: true, 6 minimizer: [ 7 new CssMinimizerPlugin({ 8 minify: CssMinimizerPlugin.esbuildMinify, 9 }), 10 ], 11 }, 12};
@parcel/css
webpack.config.js
1module.exports = { 2 // Uncomment if you need source maps 3 // devtool: "source-map", 4 optimization: { 5 minimize: true, 6 minimizer: [ 7 new CssMinimizerPlugin({ 8 minify: CssMinimizerPlugin.lightningCssMinify, 9 // Uncomment this line for options 10 // minimizerOptions: { targets: { ie: 11 }, drafts: { nesting: true } }, 11 }), 12 ], 13 }, 14};
webpack.config.js
1module.exports = { 2 // Uncomment if you need source maps 3 // devtool: "source-map", 4 optimization: { 5 minimize: true, 6 minimizer: [ 7 new CssMinimizerPlugin({ 8 minify: CssMinimizerPlugin.swcMinify, 9 // Uncomment this line for options 10 // minimizerOptions: {}, 11 }), 12 ], 13 }, 14};
Please take a moment to read our contributing guidelines if you haven't yet done so.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
Found 23/28 approved changesets -- score normalized to 8
Reason
3 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More