Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
css-minimizer-webpack-plugin
Advanced tools
The css-minimizer-webpack-plugin is a plugin for webpack that optimizes and minimizes CSS assets. It uses cssnano to optimize CSS by removing comments, whitespace, and other unnecessary characters without affecting the functionality of the CSS code. It can be used in production builds to reduce the size of CSS files, which can lead to faster load times for web applications.
Minification of CSS
This feature allows you to integrate CSS minification into your webpack build process. By adding the CssMinimizerPlugin to the optimization.minimizer array in your webpack configuration, you can ensure that your CSS files are automatically minimized during the build.
{"optimization": {"minimizer": [new CssMinimizerPlugin()]}}
Source Map Generation
This feature enables the generation of source maps for the minimized CSS files. Source maps allow you to trace back the minified CSS to the original source files, which is useful for debugging purposes.
{"optimization": {"minimizer": [new CssMinimizerPlugin({sourceMap: true})]}}
Custom Minification Options
This feature allows you to provide custom options to the cssProcessor used by the plugin. In this example, the cssProcessorOptions are set to use the 'default' preset and configure it to remove all comments from the CSS.
{"optimization": {"minimizer": [new CssMinimizerPlugin({cssProcessorOptions: {preset: ['default', {discardComments: {removeAll: true}}]}})]}}
The uglifyjs-webpack-plugin is a plugin for webpack that minimizes JavaScript files. While it does not handle CSS, it serves a similar purpose for JavaScript assets. It uses UglifyJS to compress and optimize JavaScript code.
The terser-webpack-plugin is another webpack plugin for minimizing JavaScript files. It is similar to uglifyjs-webpack-plugin but uses Terser for minification, which supports ES6+ syntax. It is often used as a more modern alternative to uglifyjs-webpack-plugin.
The optimize-css-assets-webpack-plugin is a plugin that can be used to optimize and minimize CSS assets. It is similar to css-minimizer-webpack-plugin but uses cssnano and can also work with other CSS processors. It is an alternative for CSS optimization in webpack builds.
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
:
npm install css-minimizer-webpack-plugin --save-dev
or
yarn add -D css-minimizer-webpack-plugin
or
pnpm add -D css-minimizer-webpack-plugin
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: {
rules: [
{
test: /.s?css$/,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
},
],
},
optimization: {
minimizer: [
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
// `...`,
new CssMinimizerPlugin(),
],
},
plugins: [new MiniCssExtractPlugin()],
};
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
// [...]
module.exports = {
optimization: {
// [...]
minimize: 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? 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.
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
test: /\.foo\.css$/i,
}),
],
},
};
include
Type: String|RegExp|Array<String|RegExp>
Default: undefined
Files to include.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
include: /\/includes/,
}),
],
},
};
exclude
Type: String|RegExp|Array<String|RegExp>
Default: undefined
Files to exclude.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
exclude: /\/excludes/,
}),
],
},
};
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
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
parallel: true,
}),
],
},
};
Number
Enable multi-process parallel running and set number of concurrent runs.
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
parallel: 4,
}),
],
},
};
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
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: {
level: {
1: {
roundingPrecision: "all=3,px=5",
},
},
},
minify: CssMinimizerPlugin.cleanCssMinify,
}),
],
},
};
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
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) => {
// To do something
return {
code: `a{color: red}`,
map: `{"version": "3", ...}`,
warnings: [],
errors: [],
};
},
],
}),
],
},
};
minimizerOptions
Type: Object|Array<Object>
Default: { preset: 'default' }
Cssnano optimisations options.
Object
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
"default",
{
discardComments: { removeAll: true },
},
],
},
}),
],
},
};
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
.
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: {
preset: require.resolve("cssnano-preset-simple"),
},
}),
],
},
};
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..
import sugarss from "sugarss";
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
parallel: false,
minimizerOptions: {
processorOptions: {
parser: sugarss,
},
},
}),
],
},
};
module.exports = {
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: {
processorOptions: {
parser: "sugarss",
},
},
}),
],
},
};
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
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 = {
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()],
};
Remove all comments (including comments starting with /*!
).
module.exports = {
optimization: {
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
"default",
{
discardComments: { removeAll: true },
},
],
},
}),
],
},
};
webpack.config.js
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 },
}),
],
},
};
webpack.config.js
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' },
}),
],
},
};
webpack.config.js
module.exports = {
// Uncomment if you need source maps
// devtool: "source-map",
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minify: CssMinimizerPlugin.esbuildMinify,
}),
],
},
};
@parcel/css
webpack.config.js
module.exports = {
// Uncomment if you need source maps
// devtool: "source-map",
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minify: CssMinimizerPlugin.lightningCssMinify,
// Uncomment this line for options
// minimizerOptions: { targets: { ie: 11 }, drafts: { nesting: true } },
}),
],
},
};
webpack.config.js
module.exports = {
// Uncomment if you need source maps
// devtool: "source-map",
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minify: CssMinimizerPlugin.swcMinify,
// Uncomment this line for options
// minimizerOptions: {},
}),
],
},
};
Please take a moment to read our contributing guidelines if you haven't yet done so.
FAQs
CSS minimizer (minifier) plugin for Webpack
The npm package css-minimizer-webpack-plugin receives a total of 3,558,766 weekly downloads. As such, css-minimizer-webpack-plugin popularity was classified as popular.
We found that css-minimizer-webpack-plugin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.