Detalhes do pacote

css-minimizer-webpack-plugin

webpack28mMIT7.0.4

CSS minimizer (minifier) plugin for Webpack

cssnano, css, csso, clean-css

readme (leia-me)

npm node tests cover discussion size

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

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 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()],
};

[!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:

webpack.config.js

// [...]
module.exports = {
  optimization: {
    // [...]
    minimize: true,
  },
};

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
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 or 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 Allows 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,
      }),
    ],
  },
};

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.

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

Boolean

Enable or 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 specify the 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

Overrides the default minify function.

By default, plugin uses cssnano package.

This is useful when using or 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 inside minify function when parallel option is 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) =>
            //  Custom minifier function
            ({
              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 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"),
        },
      }),
    ],
  },
};
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 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,
          },
        },
      }),
    ],
  },
};
module.exports = {
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minimizerOptions: {
          processorOptions: {
            parser: "sugarss",
          },
        },
      }),
    ],
  },
};

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.

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;
        },
      }),
    ],
  },
};

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()],
};

Remove all comments

Remove all comments, including those starting with /*!.

module.exports = {
  optimization: {
    minimizer: [
      new CssMinimizerPlugin({
        minimizerOptions: {
          preset: [
            "default",
            {
              discardComments: { removeAll: true },
            },
          ],
        },
      }),
    ],
  },
};

Using custom minifier csso

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 },
      }),
    ],
  },
};

Using custom minifier clean-css

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' },
      }),
    ],
  },
};

Using custom minifier esbuild

webpack.config.js

module.exports = {
  // Uncomment if you need source maps
  // devtool: "source-map",
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minify: CssMinimizerPlugin.esbuildMinify,
      }),
    ],
  },
};

Using custom minifier lightningcss, previously @parcel/css

webpack.config.js

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 } },
      }),
    ],
  },
};

Using custom minifier swc

webpack.config.js

module.exports = {
  // devtool: "source-map", // Uncomment for source maps
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minify: CssMinimizerPlugin.swcMinify,
        // Uncomment this line for options
        // minimizerOptions: {},
      }),
    ],
  },
};

Contributing

We welcome all contributions!

If you're new here, please take a moment to review our contributing guidelines.

CONTRIBUTING

License

MIT

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

7.0.4 (2025-12-11)

Bug Fixes

7.0.3 (2025-12-05)

Bug Fixes

  • respect errors and warnings from minimizer without code (933fb49)

7.0.2 (2025-03-06)

Bug Fixes

7.0.1 (2025-03-06)

Bug Fixes

  • better support worker threads (eeaa5e1)
  • use os.availableParallelism() for parallelism when it is available (b07feeb)

7.0.0 (2024-05-07)

⚠ BREAKING CHANGES

6.0.0 (2024-01-17)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 18.12.0 (#252) (f7f74c0)

5.0.1 (2023-06-13)

Bug Fixes

5.0.0 (2023-03-27)

⚠ BREAKING CHANGES

Features

4.2.2 (2022-10-13)

Bug Fixes

4.2.1 (2022-10-06)

Bug Fixes

4.2.0 (2022-09-29)

Features

4.1.0 (2022-09-09)

Features

4.0.0 (2022-05-18)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 14.15.0

3.4.1 (2022-01-18)

Bug Fixes

3.4.0 (2022-01-18)

Features

3.3.1 (2021-12-21)

Bug Fixes

3.3.0 (2021-12-16)

Features

  • removed cjs wrapper and generated types in commonjs format (export = and namespaces used in types), now you can directly use exported types (3262a9a)

3.2.0 (2021-11-23)

Features

3.1.4 (2021-11-17)

Chore

  • update schema-utils package to 4.0.0 version

3.1.3 (2021-11-10)

Bug Fixes

  • source map generation for cssnano and clean-css (#135) (a9dd43e)

3.1.2 (2021-11-08)

Bug Fixes

  • handle esbuild warnings (f427f41)

3.1.1 (2021-10-05)

Bug Fixes

3.1.0 (2021-10-04)

Features

  • added esbuild minimizer (#122) (987d454)
  • allow returning errors from custom minimize function (#121) (c9a11b2)
  • output documentation links on errors (4e8afba)

Bug Fixes

  • source map generation for multiple minify functions (b736099)

3.0.2 (2021-06-25)

Chore

  • update serialize-javascript

3.0.1 (2021-05-31)

Chore

  • update jest-worker

3.0.0 (2021-05-12)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 12.13.0

2.0.0 (2021-04-10)

⚠ BREAKING CHANGES

  • update cssnano to 5.0.0 version
  • drop webpack v4 support,
  • removed the cache option (respect the cache option from webpack)
  • removed the cacheKeys option respect the cache option from webpack)
  • removed the sourceMap option (respect the devtool option from webpack)

Features

  • added defaults functions for clean-css and csso, please look at here (5211eed)
  • added the ability to pass an array of functions to the minify (91f9977)
  • update cssnano to 5.0.0 version (4d2a8fd)

1.3.0 (2021-03-15)

Features

  • added support processorOptions for cssnano (8865423)

1.2.0 (2021-01-08)

Features

  • optimize CSS assets added later by plugins (webpack@5 only) (#47) (bdb3f52)

Bug Fixes

  • crash with source maps when the parallel option is false (#53) (4fe95f9)

1.1.5 (2020-10-07)

Chore

  • update schema-utils

1.1.4 (2020-09-18)

Bug Fixes

  • weak cache
  • source map generation
  • cache warnings between builds

1.1.3 (2020-09-03)

Bug Fixes

  • do not crash on the minify option (fd9abac)

1.1.2 (2020-08-24)

Bug Fixes

  • compatibility with webpack 5 (6232829)

1.1.1 (2020-08-10)

Bug Fixes

  • compatibility with 10.13 version of Node.js (d38ea79)

1.1.0 (2020-08-04)

Features

  • show minimized assets in stats for webpack@5 (#19) (cb038b9)

Bug Fixes

  • compatibility cache feature with webpack@5 (#16) (997e00f)
  • skip double compression for child compilation (#18) (ffc71c2)

1.0.0 - 2020-08-01

Initial release