A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://webpack.js.org/plugins/css-minimizer-webpack-plugin/ below:

CssMinimizerWebpackPlugin | webpack

Disclaimer: CssMinimizerWebpackPlugin is a third-party package maintained by community members, it potentially does not have the same support, security policy or license as webpack, and it is not maintained by webpack.

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

Test to match files against.

module.exports = {
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        test: /\.foo\.css$/i,
      }),
    ],
  },
};
include

Files to include.

webpack.config.js

module.exports = {
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        include: /\/includes/,
      }),
    ],
  },
};
exclude

Files to exclude.

webpack.config.js

module.exports = {
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        exclude: /\/excludes/,
      }),
    ],
  },
};
parallel

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

Overrides the default minify function.

By default, plugin uses cssnano package.

This is useful when using or testing unpublished versions or forks.

Possible options:

[!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: [
          {}, 
          {}, 
          {}, 
        ],
        minify: [
          CssMinimizerPlugin.cssnanoMinify,
          CssMinimizerPlugin.cleanCssMinify,
          async (data, inputMap, minimizerOptions) =>
            
            ({
              code: "a{color: red}",
              map: '{"version": "3", ...}',
              warnings: [],
              errors: [],
            }),
        ],
      }),
    ],
  },
};
minimizerOptions

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)

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

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 = {
  
  
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minify: CssMinimizerPlugin.cssoMinify,
        
        
      }),
    ],
  },
};
Using custom minifier clean-css

webpack.config.js

module.exports = {
  
  
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minify: CssMinimizerPlugin.cleanCssMinify,
        
        
      }),
    ],
  },
};
Using custom minifier esbuild

webpack.config.js

module.exports = {
  
  
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minify: CssMinimizerPlugin.esbuildMinify,
      }),
    ],
  },
};
Using custom minifier lightningcss, previously @parcel/css

webpack.config.js

module.exports = {
  
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minify: CssMinimizerPlugin.lightningCssMinify,
        
        
      }),
    ],
  },
};
Using custom minifier swc

webpack.config.js

module.exports = {
  
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        minify: CssMinimizerPlugin.swcMinify,
        
        
      }),
    ],
  },
};
Contributing

We welcome all contributions!

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

CONTRIBUTING

License

MIT


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4