| [9af201e] | 1 | <div align="center">
|
|---|
| 2 | <a href="https://github.com/webpack/webpack">
|
|---|
| 3 | <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
|
|---|
| 4 | </a>
|
|---|
| 5 | </div>
|
|---|
| 6 |
|
|---|
| 7 | [![npm][npm]][npm-url]
|
|---|
| 8 | [![node][node]][node-url]
|
|---|
| 9 | [![tests][tests]][tests-url]
|
|---|
| 10 | [![cover][cover]][cover-url]
|
|---|
| 11 | [![discussion][discussion]][discussion-url]
|
|---|
| 12 | [![size][size]][size-url]
|
|---|
| 13 |
|
|---|
| 14 | # minimizer-webpack-plugin
|
|---|
| 15 |
|
|---|
| 16 | This plugin minifies your assets in a webpack build. It ships with several
|
|---|
| 17 | built-in minimizers covering JavaScript, JSON, HTML, and CSS — pick one
|
|---|
| 18 | with the [`minify`](#minify) option and target the right files with
|
|---|
| 19 | [`test`](#test).
|
|---|
| 20 |
|
|---|
| 21 | JavaScript minimizers:
|
|---|
| 22 |
|
|---|
| 23 | - [`terser`](https://github.com/terser/terser) — `MinimizerPlugin.terserMinify` (default). The same JavaScript-based minifier that webpack uses out of the box; produces small, well-tested output and supports the full set of `extractComments` modes.
|
|---|
| 24 | - [`uglify-js`](https://github.com/mishoo/UglifyJS) — `MinimizerPlugin.uglifyJsMinify`. ES5-only minifier, useful when you specifically need UglifyJS-compatible output. Requires `npm install --save-dev uglify-js`.
|
|---|
| 25 | - [`@swc/core`](https://github.com/swc-project/swc) — `MinimizerPlugin.swcMinify`. A very fast Rust-based JavaScript/TypeScript minifier. Requires `npm install --save-dev @swc/core`.
|
|---|
| 26 | - [`esbuild`](https://github.com/evanw/esbuild) — `MinimizerPlugin.esbuildMinify`. An extremely fast JS bundler/minifier; legal comments are always preserved (no `extractComments` support). Requires `npm install --save-dev esbuild`.
|
|---|
| 27 |
|
|---|
| 28 | JSON minimizer:
|
|---|
| 29 |
|
|---|
| 30 | - `JSON.stringify` — `MinimizerPlugin.jsonMinify`. Built in (no extra dependency); supports `space` and `replacer` options.
|
|---|
| 31 |
|
|---|
| 32 | HTML minimizers:
|
|---|
| 33 |
|
|---|
| 34 | - [`html-minifier-terser`](https://github.com/terser/html-minifier-terser) — `MinimizerPlugin.htmlMinifierTerser`. The default HTML minimizer. JavaScript-based, no native dependency. Requires `npm install --save-dev html-minifier-terser`.
|
|---|
| 35 | - [`@swc/html`](https://github.com/swc-project/swc) — `MinimizerPlugin.swcMinifyHtml` (full HTML documents) and `MinimizerPlugin.swcMinifyHtmlFragment` (HTML fragments, e.g. `<template>` content). Very fast Rust-based platform for the Web. Requires `npm install --save-dev @swc/html`.
|
|---|
| 36 | - [`@minify-html/node`](https://github.com/wilsonzlin/minify-html) — `MinimizerPlugin.minifyHtmlNode`. A Rust HTML minifier optimised for speed and effectiveness. Requires `npm install --save-dev @minify-html/node`.
|
|---|
| 37 |
|
|---|
| 38 | CSS minimizers:
|
|---|
| 39 |
|
|---|
| 40 | - [`cssnano`](https://cssnano.github.io/cssnano/) — `MinimizerPlugin.cssnanoMinify`. The default CSS minimizer. Built on top of [PostCSS](https://postcss.org/). Requires `npm install --save-dev cssnano postcss`.
|
|---|
| 41 | - [`csso`](https://github.com/css/csso) — `MinimizerPlugin.cssoMinify`. A CSS minifier with structural optimisations. Requires `npm install --save-dev csso`.
|
|---|
| 42 | - [`clean-css`](https://github.com/clean-css/clean-css) — `MinimizerPlugin.cleanCssMinify`. A widely-used CSS optimiser. Requires `npm install --save-dev clean-css`.
|
|---|
| 43 | - [`esbuild`](https://github.com/evanw/esbuild) — `MinimizerPlugin.esbuildMinifyCss`. Very fast CSS minification using esbuild's CSS loader. Requires `npm install --save-dev esbuild`.
|
|---|
| 44 | - [`lightningcss`](https://github.com/parcel-bundler/lightningcss) — `MinimizerPlugin.lightningCssMinify`. A Rust-based CSS parser, transformer, and minifier. Requires `npm install --save-dev lightningcss`.
|
|---|
| 45 | - [`@swc/css`](https://github.com/swc-project/swc) — `MinimizerPlugin.swcMinifyCss`. A very fast Rust-based CSS minifier. Requires `npm install --save-dev @swc/css`.
|
|---|
| 46 |
|
|---|
| 47 | All of the non-default minimizers are declared as **optional** peer
|
|---|
| 48 | dependencies — install only the ones you actually use. You can also stack
|
|---|
| 49 | multiple `MinimizerPlugin` instances in the same build to handle different
|
|---|
| 50 | file types with different minimizers (see [Examples](#examples)).
|
|---|
| 51 |
|
|---|
| 52 | ## Getting Started
|
|---|
| 53 |
|
|---|
| 54 | Webpack v5 comes with the latest `minimizer-webpack-plugin` out of the box.
|
|---|
| 55 | If you are using Webpack v5 or above and wish to customize the options, you will still need to install `minimizer-webpack-plugin`.
|
|---|
| 56 | Using Webpack v4, you have to install `terser-webpack-plugin` v4 (`minimizer-webpack-plugin` is only published for Webpack v5+).
|
|---|
| 57 |
|
|---|
| 58 | To begin, you'll need to install `minimizer-webpack-plugin`:
|
|---|
| 59 |
|
|---|
| 60 | ```console
|
|---|
| 61 | npm install minimizer-webpack-plugin --save-dev
|
|---|
| 62 | ```
|
|---|
| 63 |
|
|---|
| 64 | or
|
|---|
| 65 |
|
|---|
| 66 | ```console
|
|---|
| 67 | yarn add -D minimizer-webpack-plugin
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | or
|
|---|
| 71 |
|
|---|
| 72 | ```console
|
|---|
| 73 | pnpm add -D minimizer-webpack-plugin
|
|---|
| 74 | ```
|
|---|
| 75 |
|
|---|
| 76 | Then add the plugin to your `webpack` configuration. For example:
|
|---|
| 77 |
|
|---|
| 78 | **webpack.config.js**
|
|---|
| 79 |
|
|---|
| 80 | ```js
|
|---|
| 81 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 82 |
|
|---|
| 83 | module.exports = {
|
|---|
| 84 | optimization: {
|
|---|
| 85 | minimize: true,
|
|---|
| 86 | minimizer: [new MinimizerPlugin()],
|
|---|
| 87 | },
|
|---|
| 88 | };
|
|---|
| 89 | ```
|
|---|
| 90 |
|
|---|
| 91 | Finally, run `webpack` using the method you normally use (e.g., via CLI or an npm script).
|
|---|
| 92 |
|
|---|
| 93 | ## Note about source maps
|
|---|
| 94 |
|
|---|
| 95 | **Works only with `source-map`, `inline-source-map`, `hidden-source-map` and `nosources-source-map` values for the [`devtool`](https://webpack.js.org/configuration/devtool/) option.**
|
|---|
| 96 |
|
|---|
| 97 | Why?
|
|---|
| 98 |
|
|---|
| 99 | - `eval` wraps modules in `eval("string")` and the minimizer does not handle strings.
|
|---|
| 100 | - `cheap` has no column information and the minimizer generates only a single line, which leaves only a single mapping.
|
|---|
| 101 |
|
|---|
| 102 | Using supported `devtool` values enable source map generation.
|
|---|
| 103 |
|
|---|
| 104 | ## Options
|
|---|
| 105 |
|
|---|
| 106 | - **[`test`](#test)**
|
|---|
| 107 | - **[`include`](#include)**
|
|---|
| 108 | - **[`exclude`](#exclude)**
|
|---|
| 109 | - **[`parallel`](#parallel)**
|
|---|
| 110 | - **[`minify`](#minify)**
|
|---|
| 111 | - **[`minimizerOptions`](#minimizeroptions)**
|
|---|
| 112 | - **[`extractComments`](#extractcomments)**
|
|---|
| 113 |
|
|---|
| 114 | ### `test`
|
|---|
| 115 |
|
|---|
| 116 | Type:
|
|---|
| 117 |
|
|---|
| 118 | ```ts
|
|---|
| 119 | type test = string | RegExp | (string | RegExp)[];
|
|---|
| 120 | ```
|
|---|
| 121 |
|
|---|
| 122 | Default: `/\.m?js(\?.*)?$/i`
|
|---|
| 123 |
|
|---|
| 124 | Test to match files against.
|
|---|
| 125 |
|
|---|
| 126 | **webpack.config.js**
|
|---|
| 127 |
|
|---|
| 128 | ```js
|
|---|
| 129 | module.exports = {
|
|---|
| 130 | optimization: {
|
|---|
| 131 | minimize: true,
|
|---|
| 132 | minimizer: [
|
|---|
| 133 | new MinimizerPlugin({
|
|---|
| 134 | test: /\.js(\?.*)?$/i,
|
|---|
| 135 | }),
|
|---|
| 136 | ],
|
|---|
| 137 | },
|
|---|
| 138 | };
|
|---|
| 139 | ```
|
|---|
| 140 |
|
|---|
| 141 | ### `include`
|
|---|
| 142 |
|
|---|
| 143 | Type:
|
|---|
| 144 |
|
|---|
| 145 | ```ts
|
|---|
| 146 | type include = string | RegExp | (string | RegExp)[];
|
|---|
| 147 | ```
|
|---|
| 148 |
|
|---|
| 149 | Default: `undefined`
|
|---|
| 150 |
|
|---|
| 151 | Files to include.
|
|---|
| 152 |
|
|---|
| 153 | **webpack.config.js**
|
|---|
| 154 |
|
|---|
| 155 | ```js
|
|---|
| 156 | module.exports = {
|
|---|
| 157 | optimization: {
|
|---|
| 158 | minimize: true,
|
|---|
| 159 | minimizer: [
|
|---|
| 160 | new MinimizerPlugin({
|
|---|
| 161 | include: /\/includes/,
|
|---|
| 162 | }),
|
|---|
| 163 | ],
|
|---|
| 164 | },
|
|---|
| 165 | };
|
|---|
| 166 | ```
|
|---|
| 167 |
|
|---|
| 168 | ### `exclude`
|
|---|
| 169 |
|
|---|
| 170 | Type:
|
|---|
| 171 |
|
|---|
| 172 | ```ts
|
|---|
| 173 | type exclude = string | RegExp | (string | RegExp)[];
|
|---|
| 174 | ```
|
|---|
| 175 |
|
|---|
| 176 | Default: `undefined`
|
|---|
| 177 |
|
|---|
| 178 | Files to exclude.
|
|---|
| 179 |
|
|---|
| 180 | **webpack.config.js**
|
|---|
| 181 |
|
|---|
| 182 | ```js
|
|---|
| 183 | module.exports = {
|
|---|
| 184 | optimization: {
|
|---|
| 185 | minimize: true,
|
|---|
| 186 | minimizer: [
|
|---|
| 187 | new MinimizerPlugin({
|
|---|
| 188 | exclude: /\/excludes/,
|
|---|
| 189 | }),
|
|---|
| 190 | ],
|
|---|
| 191 | },
|
|---|
| 192 | };
|
|---|
| 193 | ```
|
|---|
| 194 |
|
|---|
| 195 | ### `parallel`
|
|---|
| 196 |
|
|---|
| 197 | Type:
|
|---|
| 198 |
|
|---|
| 199 | ```ts
|
|---|
| 200 | type parallel = boolean | number;
|
|---|
| 201 | ```
|
|---|
| 202 |
|
|---|
| 203 | Default: `true`
|
|---|
| 204 |
|
|---|
| 205 | Use multi-process parallel running to improve the build speed.
|
|---|
| 206 |
|
|---|
| 207 | Default number of concurrent runs: `os.cpus().length - 1` or `os.availableParallelism() - 1` (if this function is supported).
|
|---|
| 208 |
|
|---|
| 209 | > **Note**
|
|---|
| 210 | >
|
|---|
| 211 | > Parallelization can speedup your build significantly and is therefore **highly recommended**.
|
|---|
| 212 |
|
|---|
| 213 | > **Warning**
|
|---|
| 214 | >
|
|---|
| 215 | > If you use **Circle CI** or any other environment that doesn't provide the real available count of CPUs then you need to explicitly set up the number of CPUs to avoid `Error: Call retries were exceeded` (see [#143](https://github.com/webpack/minimizer-webpack-plugin/issues/143), [#202](https://github.com/webpack/minimizer-webpack-plugin/issues/202)).
|
|---|
| 216 |
|
|---|
| 217 | #### `boolean`
|
|---|
| 218 |
|
|---|
| 219 | Enable/disable multi-process parallel running.
|
|---|
| 220 |
|
|---|
| 221 | **webpack.config.js**
|
|---|
| 222 |
|
|---|
| 223 | ```js
|
|---|
| 224 | module.exports = {
|
|---|
| 225 | optimization: {
|
|---|
| 226 | minimize: true,
|
|---|
| 227 | minimizer: [
|
|---|
| 228 | new MinimizerPlugin({
|
|---|
| 229 | parallel: true,
|
|---|
| 230 | }),
|
|---|
| 231 | ],
|
|---|
| 232 | },
|
|---|
| 233 | };
|
|---|
| 234 | ```
|
|---|
| 235 |
|
|---|
| 236 | #### `number`
|
|---|
| 237 |
|
|---|
| 238 | Enable multi-process parallel running and set number of concurrent runs.
|
|---|
| 239 |
|
|---|
| 240 | **webpack.config.js**
|
|---|
| 241 |
|
|---|
| 242 | ```js
|
|---|
| 243 | module.exports = {
|
|---|
| 244 | optimization: {
|
|---|
| 245 | minimize: true,
|
|---|
| 246 | minimizer: [
|
|---|
| 247 | new MinimizerPlugin({
|
|---|
| 248 | parallel: 4,
|
|---|
| 249 | }),
|
|---|
| 250 | ],
|
|---|
| 251 | },
|
|---|
| 252 | };
|
|---|
| 253 | ```
|
|---|
| 254 |
|
|---|
| 255 | ### `minify`
|
|---|
| 256 |
|
|---|
| 257 | Type:
|
|---|
| 258 |
|
|---|
| 259 | ```ts
|
|---|
| 260 | type minifyFn = (
|
|---|
| 261 | input: Record<string, string>,
|
|---|
| 262 | sourceMap: import("@jridgewell/trace-mapping").SourceMapInput | undefined,
|
|---|
| 263 | minifyOptions: {
|
|---|
| 264 | module?: boolean | undefined;
|
|---|
| 265 | ecma?: import("terser").ECMA | undefined;
|
|---|
| 266 | },
|
|---|
| 267 | extractComments:
|
|---|
| 268 | | boolean
|
|---|
| 269 | | "all"
|
|---|
| 270 | | "some"
|
|---|
| 271 | | RegExp
|
|---|
| 272 | | ((
|
|---|
| 273 | astNode: any,
|
|---|
| 274 | comment: {
|
|---|
| 275 | value: string;
|
|---|
| 276 | type: "comment1" | "comment2" | "comment3" | "comment4";
|
|---|
| 277 | pos: number;
|
|---|
| 278 | line: number;
|
|---|
| 279 | col: number;
|
|---|
| 280 | },
|
|---|
| 281 | ) => boolean)
|
|---|
| 282 | | {
|
|---|
| 283 | condition?:
|
|---|
| 284 | | boolean
|
|---|
| 285 | | "all"
|
|---|
| 286 | | "some"
|
|---|
| 287 | | RegExp
|
|---|
| 288 | | ((
|
|---|
| 289 | astNode: any,
|
|---|
| 290 | comment: {
|
|---|
| 291 | value: string;
|
|---|
| 292 | type: "comment1" | "comment2" | "comment3" | "comment4";
|
|---|
| 293 | pos: number;
|
|---|
| 294 | line: number;
|
|---|
| 295 | col: number;
|
|---|
| 296 | },
|
|---|
| 297 | ) => boolean)
|
|---|
| 298 | | undefined;
|
|---|
| 299 | filename?: string | ((fileData: any) => string) | undefined;
|
|---|
| 300 | banner?:
|
|---|
| 301 | | string
|
|---|
| 302 | | boolean
|
|---|
| 303 | | ((commentsFile: string) => string)
|
|---|
| 304 | | undefined;
|
|---|
| 305 | }
|
|---|
| 306 | | undefined,
|
|---|
| 307 | ) => Promise<{
|
|---|
| 308 | code: string;
|
|---|
| 309 | map?: import("@jridgewell/trace-mapping").SourceMapInput | undefined;
|
|---|
| 310 | errors?: (string | Error)[] | undefined;
|
|---|
| 311 | warnings?: (string | Error)[] | undefined;
|
|---|
| 312 | extractedComments?: string[] | undefined;
|
|---|
| 313 | }>;
|
|---|
| 314 |
|
|---|
| 315 | type minify = minifyFn | minifyFn[];
|
|---|
| 316 | ```
|
|---|
| 317 |
|
|---|
| 318 | Default: `MinimizerPlugin.terserMinify`
|
|---|
| 319 |
|
|---|
| 320 | Allows you to override the default minify function.
|
|---|
| 321 | By default plugin uses [terser](https://github.com/terser/terser) package.
|
|---|
| 322 | Useful for using and testing unpublished versions or forks.
|
|---|
| 323 |
|
|---|
| 324 | An array of functions can also be provided. Each minimizer can expose a
|
|---|
| 325 | `filter(name, info)` helper that decides whether it should run on a given
|
|---|
| 326 | asset; the plugin dispatches each asset only to the minimizers whose `filter`
|
|---|
| 327 | accepts it (or runs them all when no filter is set). All built-in minimizers
|
|---|
| 328 | ship with a `filter` that matches their natural extension, so a single plugin
|
|---|
| 329 | instance and a single worker pool can handle JS, CSS, HTML and JSON together
|
|---|
| 330 | without juggling multiple `MinimizerPlugin` instances — just widen `test` to
|
|---|
| 331 | let those asset types reach the dispatcher:
|
|---|
| 332 |
|
|---|
| 333 | ```js
|
|---|
| 334 | new MinimizerPlugin({
|
|---|
| 335 | test: /\.(?:[cm]?js|css|html?|json)(\?.*)?$/i,
|
|---|
| 336 | minify: [
|
|---|
| 337 | MinimizerPlugin.terserMinify,
|
|---|
| 338 | MinimizerPlugin.cssnanoMinify,
|
|---|
| 339 | MinimizerPlugin.htmlMinifierTerser,
|
|---|
| 340 | MinimizerPlugin.jsonMinify,
|
|---|
| 341 | ],
|
|---|
| 342 | });
|
|---|
| 343 | ```
|
|---|
| 344 |
|
|---|
| 345 | When more than one minimizer in the array claims the same asset, the chain
|
|---|
| 346 | semantic still applies: the output of each accepting minimizer is fed as
|
|---|
| 347 | input to the next. The [`minimizerOptions`](#minimizeroptions) option may
|
|---|
| 348 | be an array (index-paired with `minify`) or a single object reused by every
|
|---|
| 349 | minimizer.
|
|---|
| 350 |
|
|---|
| 351 | The `test` option always defaults to `/\.[cm]?js(\?.*)?$/i`. When you mix
|
|---|
| 352 | asset types in a single plugin instance, widen `test` so non-JS assets reach
|
|---|
| 353 | the dispatcher (for example `test: /\.(?:[cm]?js|css|html?|json)(\?.*)?$/i`).
|
|---|
| 354 |
|
|---|
| 355 | > **Warning**
|
|---|
| 356 | >
|
|---|
| 357 | > **Always use `require` inside `minify` function when `parallel` option enabled**.
|
|---|
| 358 |
|
|---|
| 359 | #### `function`
|
|---|
| 360 |
|
|---|
| 361 | **webpack.config.js**
|
|---|
| 362 |
|
|---|
| 363 | ```js
|
|---|
| 364 | // Can be async
|
|---|
| 365 | const minify = (input, sourceMap, minimizerOptions, extractsComments) => {
|
|---|
| 366 | // The `minimizerOptions` argument contains options from the `minimizerOptions` plugin option
|
|---|
| 367 | // You can use `minimizerOptions.myCustomOption`
|
|---|
| 368 |
|
|---|
| 369 | // Custom logic for extract comments
|
|---|
| 370 | const { map, code } = require("uglify-module") // Or require('./path/to/uglify-module')
|
|---|
| 371 | .minify(input, {
|
|---|
| 372 | /* Your options for minification */
|
|---|
| 373 | });
|
|---|
| 374 |
|
|---|
| 375 | return { map, code, warnings: [], errors: [], extractedComments: [] };
|
|---|
| 376 | };
|
|---|
| 377 |
|
|---|
| 378 | // Used to regenerate `fullhash`/`chunkhash` between different implementation
|
|---|
| 379 | // Example: you fix a bug in custom minimizer/custom function, but unfortunately webpack doesn't know about it, so you will get the same fullhash/chunkhash
|
|---|
| 380 | // to avoid this you can provide version of your custom minimizer
|
|---|
| 381 | // You don't need if you use only `contenthash`
|
|---|
| 382 | minify.getMinimizerVersion = () => {
|
|---|
| 383 | let packageJson;
|
|---|
| 384 |
|
|---|
| 385 | try {
|
|---|
| 386 | packageJson = require("uglify-module/package.json");
|
|---|
| 387 | } catch (error) {
|
|---|
| 388 | // Ignore
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | return packageJson && packageJson.version;
|
|---|
| 392 | };
|
|---|
| 393 |
|
|---|
| 394 | // Restrict the minimizer to the assets it can actually handle. The plugin
|
|---|
| 395 | // skips assets for which `filter` returns `false` and (when an array of
|
|---|
| 396 | // minimizers is used) dispatches each asset only to the minimizers that
|
|---|
| 397 | // accept it. Returning `undefined` is treated as accept.
|
|---|
| 398 | minify.filter = (name) => /\.[cm]?js(\?.*)?$/i.test(name);
|
|---|
| 399 |
|
|---|
| 400 | module.exports = {
|
|---|
| 401 | optimization: {
|
|---|
| 402 | minimize: true,
|
|---|
| 403 | minimizer: [
|
|---|
| 404 | new MinimizerPlugin({
|
|---|
| 405 | minimizerOptions: {
|
|---|
| 406 | myCustomOption: true,
|
|---|
| 407 | },
|
|---|
| 408 | minify,
|
|---|
| 409 | }),
|
|---|
| 410 | ],
|
|---|
| 411 | },
|
|---|
| 412 | };
|
|---|
| 413 | ```
|
|---|
| 414 |
|
|---|
| 415 | #### `array`
|
|---|
| 416 |
|
|---|
| 417 | If an array of functions is passed to the `minify` option, each asset is
|
|---|
| 418 | dispatched to the minimizers whose `filter` accepts it. When more than one
|
|---|
| 419 | minimizer accepts the same asset the output of each is fed as input to the
|
|---|
| 420 | next one (the chain semantic). The `minimizerOptions` option can be either an
|
|---|
| 421 | array of option objects (index-paired with `minify`) or a single object that
|
|---|
| 422 | will be shared by all minimizers. Warnings, errors and extracted comments
|
|---|
| 423 | from all running minimizers are merged together.
|
|---|
| 424 |
|
|---|
| 425 | **webpack.config.js**
|
|---|
| 426 |
|
|---|
| 427 | ```js
|
|---|
| 428 | module.exports = {
|
|---|
| 429 | optimization: {
|
|---|
| 430 | minimize: true,
|
|---|
| 431 | minimizer: [
|
|---|
| 432 | new MinimizerPlugin({
|
|---|
| 433 | minify: [MinimizerPlugin.terserMinify, MinimizerPlugin.swcMinify],
|
|---|
| 434 | // `minimizerOptions` can be an array of options, one per `minify` entry
|
|---|
| 435 | minimizerOptions: [
|
|---|
| 436 | // Options for `MinimizerPlugin.terserMinify`
|
|---|
| 437 | { mangle: false },
|
|---|
| 438 | // Options for `MinimizerPlugin.swcMinify`
|
|---|
| 439 | {},
|
|---|
| 440 | ],
|
|---|
| 441 | }),
|
|---|
| 442 | ],
|
|---|
| 443 | },
|
|---|
| 444 | };
|
|---|
| 445 | ```
|
|---|
| 446 |
|
|---|
| 447 | A single plugin instance can also handle multiple asset types — the built-in
|
|---|
| 448 | minimizers each ship with a `filter` matching their natural extension, so JS,
|
|---|
| 449 | CSS, HTML and JSON can all be minified by one shared worker pool:
|
|---|
| 450 |
|
|---|
| 451 | ```js
|
|---|
| 452 | module.exports = {
|
|---|
| 453 | optimization: {
|
|---|
| 454 | minimize: true,
|
|---|
| 455 | minimizer: [
|
|---|
| 456 | new MinimizerPlugin({
|
|---|
| 457 | // `test` still defaults to JS only, so widen it to catch every
|
|---|
| 458 | // asset type you want the dispatcher to consider.
|
|---|
| 459 | test: /\.(?:[cm]?js|css|html?|json)(\?.*)?$/i,
|
|---|
| 460 | minify: [
|
|---|
| 461 | MinimizerPlugin.terserMinify,
|
|---|
| 462 | MinimizerPlugin.cssnanoMinify,
|
|---|
| 463 | MinimizerPlugin.htmlMinifierTerser,
|
|---|
| 464 | MinimizerPlugin.jsonMinify,
|
|---|
| 465 | ],
|
|---|
| 466 | }),
|
|---|
| 467 | ],
|
|---|
| 468 | },
|
|---|
| 469 | };
|
|---|
| 470 | ```
|
|---|
| 471 |
|
|---|
| 472 | ### `minimizerOptions`
|
|---|
| 473 |
|
|---|
| 474 | Type:
|
|---|
| 475 |
|
|---|
| 476 | ```ts
|
|---|
| 477 | interface minimizerOptions {
|
|---|
| 478 | compress?: boolean | CompressOptions;
|
|---|
| 479 | ecma?: ECMA;
|
|---|
| 480 | enclose?: boolean | string;
|
|---|
| 481 | ie8?: boolean;
|
|---|
| 482 | keep_classnames?: boolean | RegExp;
|
|---|
| 483 | keep_fnames?: boolean | RegExp;
|
|---|
| 484 | mangle?: boolean | MangleOptions;
|
|---|
| 485 | module?: boolean;
|
|---|
| 486 | nameCache?: object;
|
|---|
| 487 | format?: FormatOptions;
|
|---|
| 488 | /** @deprecated */
|
|---|
| 489 | output?: FormatOptions;
|
|---|
| 490 | parse?: ParseOptions;
|
|---|
| 491 | safari10?: boolean;
|
|---|
| 492 | sourceMap?: boolean | SourceMapOptions;
|
|---|
| 493 | toplevel?: boolean;
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | type options = minimizerOptions | minimizerOptions[];
|
|---|
| 497 | ```
|
|---|
| 498 |
|
|---|
| 499 | Default: [default](https://github.com/terser/terser#minify-options)
|
|---|
| 500 |
|
|---|
| 501 | Options for the active minimizer. With the default Terser minify, see Terser's
|
|---|
| 502 | [minify options](https://github.com/terser/terser#minify-options).
|
|---|
| 503 |
|
|---|
| 504 | When the [`minify`](#minify) option is an array of minimizers, `minimizerOptions`
|
|---|
| 505 | can also be an array. Each element is passed to the minimizer at the same
|
|---|
| 506 | index in the `minify` array. If a single object is provided instead, it is
|
|---|
| 507 | reused for every minimizer.
|
|---|
| 508 |
|
|---|
| 509 | > **Note**
|
|---|
| 510 | >
|
|---|
| 511 | > `terserOptions` is kept as a deprecated alias of `minimizerOptions` for
|
|---|
| 512 | > backwards compatibility — passing either is equivalent. If both are set,
|
|---|
| 513 | > `minimizerOptions` wins. Prefer `minimizerOptions` in new code.
|
|---|
| 514 |
|
|---|
| 515 | **webpack.config.js**
|
|---|
| 516 |
|
|---|
| 517 | ```js
|
|---|
| 518 | module.exports = {
|
|---|
| 519 | optimization: {
|
|---|
| 520 | minimize: true,
|
|---|
| 521 | minimizer: [
|
|---|
| 522 | new MinimizerPlugin({
|
|---|
| 523 | minimizerOptions: {
|
|---|
| 524 | ecma: undefined,
|
|---|
| 525 | parse: {},
|
|---|
| 526 | compress: {},
|
|---|
| 527 | mangle: true, // Note `mangle.properties` is `false` by default.
|
|---|
| 528 | module: false,
|
|---|
| 529 | // Deprecated
|
|---|
| 530 | output: null,
|
|---|
| 531 | format: null,
|
|---|
| 532 | toplevel: false,
|
|---|
| 533 | nameCache: null,
|
|---|
| 534 | ie8: false,
|
|---|
| 535 | keep_classnames: undefined,
|
|---|
| 536 | keep_fnames: false,
|
|---|
| 537 | safari10: false,
|
|---|
| 538 | },
|
|---|
| 539 | }),
|
|---|
| 540 | ],
|
|---|
| 541 | },
|
|---|
| 542 | };
|
|---|
| 543 | ```
|
|---|
| 544 |
|
|---|
| 545 | ### `extractComments`
|
|---|
| 546 |
|
|---|
| 547 | Type:
|
|---|
| 548 |
|
|---|
| 549 | ```ts
|
|---|
| 550 | type extractComments =
|
|---|
| 551 | | boolean
|
|---|
| 552 | | string
|
|---|
| 553 | | RegExp
|
|---|
| 554 | | ((
|
|---|
| 555 | astNode: any,
|
|---|
| 556 | comment: {
|
|---|
| 557 | value: string;
|
|---|
| 558 | type: "comment1" | "comment2" | "comment3" | "comment4";
|
|---|
| 559 | pos: number;
|
|---|
| 560 | line: number;
|
|---|
| 561 | col: number;
|
|---|
| 562 | },
|
|---|
| 563 | ) => boolean)
|
|---|
| 564 | | {
|
|---|
| 565 | condition?:
|
|---|
| 566 | | boolean
|
|---|
| 567 | | "all"
|
|---|
| 568 | | "some"
|
|---|
| 569 | | RegExp
|
|---|
| 570 | | ((
|
|---|
| 571 | astNode: any,
|
|---|
| 572 | comment: {
|
|---|
| 573 | value: string;
|
|---|
| 574 | type: "comment1" | "comment2" | "comment3" | "comment4";
|
|---|
| 575 | pos: number;
|
|---|
| 576 | line: number;
|
|---|
| 577 | col: number;
|
|---|
| 578 | },
|
|---|
| 579 | ) => boolean)
|
|---|
| 580 | | undefined;
|
|---|
| 581 | filename?: string | ((fileData: any) => string) | undefined;
|
|---|
| 582 | banner?:
|
|---|
| 583 | | string
|
|---|
| 584 | | boolean
|
|---|
| 585 | | ((commentsFile: string) => string)
|
|---|
| 586 | | undefined;
|
|---|
| 587 | };
|
|---|
| 588 | ```
|
|---|
| 589 |
|
|---|
| 590 | Default: `true`
|
|---|
| 591 |
|
|---|
| 592 | Whether comments shall be extracted to a separate file, (see [details](https://github.com/webpack/webpack/commit/71933e979e51c533b432658d5e37917f9e71595a)).
|
|---|
| 593 |
|
|---|
| 594 | By default, extract only comments using `/^\**!|@preserve|@license|@cc_on/i` RegExp condition and remove remaining comments.
|
|---|
| 595 |
|
|---|
| 596 | If the original file is named `foo.js`, then the comments will be stored to `foo.js.LICENSE.txt`.
|
|---|
| 597 |
|
|---|
| 598 | The `minimizerOptions.format.comments` option specifies whether the comment will be preserved - i.e., it is possible to preserve some comments (e.g. annotations) while extracting others, or even preserve comments that have already been extracted.
|
|---|
| 599 |
|
|---|
| 600 | #### `boolean`
|
|---|
| 601 |
|
|---|
| 602 | Enable/disable extracting comments.
|
|---|
| 603 |
|
|---|
| 604 | **webpack.config.js**
|
|---|
| 605 |
|
|---|
| 606 | ```js
|
|---|
| 607 | module.exports = {
|
|---|
| 608 | optimization: {
|
|---|
| 609 | minimize: true,
|
|---|
| 610 | minimizer: [
|
|---|
| 611 | new MinimizerPlugin({
|
|---|
| 612 | extractComments: true,
|
|---|
| 613 | }),
|
|---|
| 614 | ],
|
|---|
| 615 | },
|
|---|
| 616 | };
|
|---|
| 617 | ```
|
|---|
| 618 |
|
|---|
| 619 | #### `string`
|
|---|
| 620 |
|
|---|
| 621 | Extract `all` or `some` (use the `/^\**!|@preserve|@license|@cc_on/i` RegExp) comments.
|
|---|
| 622 |
|
|---|
| 623 | **webpack.config.js**
|
|---|
| 624 |
|
|---|
| 625 | ```js
|
|---|
| 626 | module.exports = {
|
|---|
| 627 | optimization: {
|
|---|
| 628 | minimize: true,
|
|---|
| 629 | minimizer: [
|
|---|
| 630 | new MinimizerPlugin({
|
|---|
| 631 | extractComments: "all",
|
|---|
| 632 | }),
|
|---|
| 633 | ],
|
|---|
| 634 | },
|
|---|
| 635 | };
|
|---|
| 636 | ```
|
|---|
| 637 |
|
|---|
| 638 | #### `RegExp`
|
|---|
| 639 |
|
|---|
| 640 | All comments that match the given expression will be extracted to a separate file.
|
|---|
| 641 |
|
|---|
| 642 | **webpack.config.js**
|
|---|
| 643 |
|
|---|
| 644 | ```js
|
|---|
| 645 | module.exports = {
|
|---|
| 646 | optimization: {
|
|---|
| 647 | minimize: true,
|
|---|
| 648 | minimizer: [
|
|---|
| 649 | new MinimizerPlugin({
|
|---|
| 650 | extractComments: /@extract/i,
|
|---|
| 651 | }),
|
|---|
| 652 | ],
|
|---|
| 653 | },
|
|---|
| 654 | };
|
|---|
| 655 | ```
|
|---|
| 656 |
|
|---|
| 657 | #### `function`
|
|---|
| 658 |
|
|---|
| 659 | All comments that match the given expression will be extracted to a separate file.
|
|---|
| 660 |
|
|---|
| 661 | **webpack.config.js**
|
|---|
| 662 |
|
|---|
| 663 | ```js
|
|---|
| 664 | module.exports = {
|
|---|
| 665 | optimization: {
|
|---|
| 666 | minimize: true,
|
|---|
| 667 | minimizer: [
|
|---|
| 668 | new MinimizerPlugin({
|
|---|
| 669 | extractComments: (astNode, comment) => {
|
|---|
| 670 | if (/@extract/i.test(comment.value)) {
|
|---|
| 671 | return true;
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | return false;
|
|---|
| 675 | },
|
|---|
| 676 | }),
|
|---|
| 677 | ],
|
|---|
| 678 | },
|
|---|
| 679 | };
|
|---|
| 680 | ```
|
|---|
| 681 |
|
|---|
| 682 | #### `object`
|
|---|
| 683 |
|
|---|
| 684 | Allows you to customize condition for extracting comments, and specify the extracted file name and banner.
|
|---|
| 685 |
|
|---|
| 686 | **webpack.config.js**
|
|---|
| 687 |
|
|---|
| 688 | ```js
|
|---|
| 689 | module.exports = {
|
|---|
| 690 | optimization: {
|
|---|
| 691 | minimize: true,
|
|---|
| 692 | minimizer: [
|
|---|
| 693 | new MinimizerPlugin({
|
|---|
| 694 | extractComments: {
|
|---|
| 695 | condition: /^\**!|@preserve|@license|@cc_on/i,
|
|---|
| 696 | filename: (fileData) =>
|
|---|
| 697 | // The "fileData" argument contains object with "filename", "basename", "query" and "hash"
|
|---|
| 698 | `${fileData.filename}.LICENSE.txt${fileData.query}`,
|
|---|
| 699 | banner: (licenseFile) =>
|
|---|
| 700 | `License information can be found in ${licenseFile}`,
|
|---|
| 701 | },
|
|---|
| 702 | }),
|
|---|
| 703 | ],
|
|---|
| 704 | },
|
|---|
| 705 | };
|
|---|
| 706 | ```
|
|---|
| 707 |
|
|---|
| 708 | ##### `condition`
|
|---|
| 709 |
|
|---|
| 710 | Type:
|
|---|
| 711 |
|
|---|
| 712 | ```ts
|
|---|
| 713 | type condition =
|
|---|
| 714 | | boolean
|
|---|
| 715 | | "all"
|
|---|
| 716 | | "some"
|
|---|
| 717 | | RegExp
|
|---|
| 718 | | ((
|
|---|
| 719 | astNode: any,
|
|---|
| 720 | comment: {
|
|---|
| 721 | value: string;
|
|---|
| 722 | type: "comment1" | "comment2" | "comment3" | "comment4";
|
|---|
| 723 | pos: number;
|
|---|
| 724 | line: number;
|
|---|
| 725 | col: number;
|
|---|
| 726 | },
|
|---|
| 727 | ) => boolean)
|
|---|
| 728 | | undefined;
|
|---|
| 729 | ```
|
|---|
| 730 |
|
|---|
| 731 | The condition that determines which comments should be extracted.
|
|---|
| 732 |
|
|---|
| 733 | **webpack.config.js**
|
|---|
| 734 |
|
|---|
| 735 | ```js
|
|---|
| 736 | module.exports = {
|
|---|
| 737 | optimization: {
|
|---|
| 738 | minimize: true,
|
|---|
| 739 | minimizer: [
|
|---|
| 740 | new MinimizerPlugin({
|
|---|
| 741 | extractComments: {
|
|---|
| 742 | condition: "some",
|
|---|
| 743 | filename: (fileData) =>
|
|---|
| 744 | // The "fileData" argument contains object with "filename", "basename", "query" and "hash"
|
|---|
| 745 | `${fileData.filename}.LICENSE.txt${fileData.query}`,
|
|---|
| 746 | banner: (licenseFile) =>
|
|---|
| 747 | `License information can be found in ${licenseFile}`,
|
|---|
| 748 | },
|
|---|
| 749 | }),
|
|---|
| 750 | ],
|
|---|
| 751 | },
|
|---|
| 752 | };
|
|---|
| 753 | ```
|
|---|
| 754 |
|
|---|
| 755 | ##### `filename`
|
|---|
| 756 |
|
|---|
| 757 | Type:
|
|---|
| 758 |
|
|---|
| 759 | ```ts
|
|---|
| 760 | type filename = string | ((fileData: any) => string) | undefined;
|
|---|
| 761 | ```
|
|---|
| 762 |
|
|---|
| 763 | Default: `[file].LICENSE.txt[query]`
|
|---|
| 764 |
|
|---|
| 765 | Available placeholders: `[file]`, `[query]` and `[filebase]` (`[base]` for webpack 5).
|
|---|
| 766 |
|
|---|
| 767 | The file where the extracted comments will be stored.
|
|---|
| 768 |
|
|---|
| 769 | Default is to append the suffix `.LICENSE.txt` to the original filename.
|
|---|
| 770 |
|
|---|
| 771 | > **Warning**
|
|---|
| 772 | >
|
|---|
| 773 | > We highly recommend using the `.txt` extension. Using `.js`/`.cjs`/`.mjs` extensions may conflict with existing assets, which leads to broken code.
|
|---|
| 774 |
|
|---|
| 775 | **webpack.config.js**
|
|---|
| 776 |
|
|---|
| 777 | ```js
|
|---|
| 778 | module.exports = {
|
|---|
| 779 | optimization: {
|
|---|
| 780 | minimize: true,
|
|---|
| 781 | minimizer: [
|
|---|
| 782 | new MinimizerPlugin({
|
|---|
| 783 | extractComments: {
|
|---|
| 784 | condition: /^\**!|@preserve|@license|@cc_on/i,
|
|---|
| 785 | filename: "extracted-comments.js",
|
|---|
| 786 | banner: (licenseFile) =>
|
|---|
| 787 | `License information can be found in ${licenseFile}`,
|
|---|
| 788 | },
|
|---|
| 789 | }),
|
|---|
| 790 | ],
|
|---|
| 791 | },
|
|---|
| 792 | };
|
|---|
| 793 | ```
|
|---|
| 794 |
|
|---|
| 795 | ##### `banner`
|
|---|
| 796 |
|
|---|
| 797 | Type:
|
|---|
| 798 |
|
|---|
| 799 | ```ts
|
|---|
| 800 | type banner = string | boolean | ((commentsFile: string) => string) | undefined;
|
|---|
| 801 | ```
|
|---|
| 802 |
|
|---|
| 803 | Default: `/*! For license information please see ${commentsFile} */`
|
|---|
| 804 |
|
|---|
| 805 | The banner text that points to the extracted file and will be added at the top of the original file.
|
|---|
| 806 |
|
|---|
| 807 | It can be `false` (no banner), a `String`, or a `function<(string) -> String>` that will be called with the filename where the extracted comments have been stored.
|
|---|
| 808 |
|
|---|
| 809 | The banner will be wrapped in a comment.
|
|---|
| 810 |
|
|---|
| 811 | **webpack.config.js**
|
|---|
| 812 |
|
|---|
| 813 | ```js
|
|---|
| 814 | module.exports = {
|
|---|
| 815 | optimization: {
|
|---|
| 816 | minimize: true,
|
|---|
| 817 | minimizer: [
|
|---|
| 818 | new MinimizerPlugin({
|
|---|
| 819 | extractComments: {
|
|---|
| 820 | condition: true,
|
|---|
| 821 | filename: (fileData) =>
|
|---|
| 822 | // The "fileData" argument contains object with "filename", "basename", "query" and "hash"
|
|---|
| 823 | `${fileData.filename}.LICENSE.txt${fileData.query}`,
|
|---|
| 824 | banner: (commentsFile) =>
|
|---|
| 825 | `My custom banner about license information ${commentsFile}`,
|
|---|
| 826 | },
|
|---|
| 827 | }),
|
|---|
| 828 | ],
|
|---|
| 829 | },
|
|---|
| 830 | };
|
|---|
| 831 | ```
|
|---|
| 832 |
|
|---|
| 833 | ## Examples
|
|---|
| 834 |
|
|---|
| 835 | ### Preserve Comments
|
|---|
| 836 |
|
|---|
| 837 | Extract all legal comments (i.e. `/^\**!|@preserve|@license|@cc_on/i`) and preserve `/@license/i` comments.
|
|---|
| 838 |
|
|---|
| 839 | **webpack.config.js**
|
|---|
| 840 |
|
|---|
| 841 | ```js
|
|---|
| 842 | module.exports = {
|
|---|
| 843 | optimization: {
|
|---|
| 844 | minimize: true,
|
|---|
| 845 | minimizer: [
|
|---|
| 846 | new MinimizerPlugin({
|
|---|
| 847 | minimizerOptions: {
|
|---|
| 848 | format: {
|
|---|
| 849 | comments: /@license/i,
|
|---|
| 850 | },
|
|---|
| 851 | },
|
|---|
| 852 | extractComments: true,
|
|---|
| 853 | }),
|
|---|
| 854 | ],
|
|---|
| 855 | },
|
|---|
| 856 | };
|
|---|
| 857 | ```
|
|---|
| 858 |
|
|---|
| 859 | ### Remove Comments
|
|---|
| 860 |
|
|---|
| 861 | If you want to build without comments, use this config:
|
|---|
| 862 |
|
|---|
| 863 | **webpack.config.js**
|
|---|
| 864 |
|
|---|
| 865 | ```js
|
|---|
| 866 | module.exports = {
|
|---|
| 867 | optimization: {
|
|---|
| 868 | minimize: true,
|
|---|
| 869 | minimizer: [
|
|---|
| 870 | new MinimizerPlugin({
|
|---|
| 871 | minimizerOptions: {
|
|---|
| 872 | format: {
|
|---|
| 873 | comments: false,
|
|---|
| 874 | },
|
|---|
| 875 | },
|
|---|
| 876 | extractComments: false,
|
|---|
| 877 | }),
|
|---|
| 878 | ],
|
|---|
| 879 | },
|
|---|
| 880 | };
|
|---|
| 881 | ```
|
|---|
| 882 |
|
|---|
| 883 | ### [`uglify-js`](https://github.com/mishoo/UglifyJS)
|
|---|
| 884 |
|
|---|
| 885 | [`UglifyJS`](https://github.com/mishoo/UglifyJS) is a JavaScript parser, minifier, compressor and beautifier toolkit.
|
|---|
| 886 |
|
|---|
| 887 | **webpack.config.js**
|
|---|
| 888 |
|
|---|
| 889 | ```js
|
|---|
| 890 | module.exports = {
|
|---|
| 891 | optimization: {
|
|---|
| 892 | minimize: true,
|
|---|
| 893 | minimizer: [
|
|---|
| 894 | new MinimizerPlugin({
|
|---|
| 895 | minify: MinimizerPlugin.uglifyJsMinify,
|
|---|
| 896 | // `minimizerOptions` will be passed to `uglify-js`
|
|---|
| 897 | // Link to options - https://github.com/mishoo/UglifyJS#minify-options
|
|---|
| 898 | minimizerOptions: {},
|
|---|
| 899 | }),
|
|---|
| 900 | ],
|
|---|
| 901 | },
|
|---|
| 902 | };
|
|---|
| 903 | ```
|
|---|
| 904 |
|
|---|
| 905 | ### [`swc`](https://github.com/swc-project/swc)
|
|---|
| 906 |
|
|---|
| 907 | [`swc`](https://github.com/swc-project/swc) is a super-fast compiler written in `Rust`, producing widely supported JavaScript from modern standards and TypeScript.
|
|---|
| 908 |
|
|---|
| 909 | > **Warning**
|
|---|
| 910 | >
|
|---|
| 911 | > `extractComments` is supported with `@swc/core >= 1.15.30`.
|
|---|
| 912 | > Only serializable extract conditions are supported: booleans, `"some"`, `"all"`, string patterns, `RegExp` values without flags, or object conditions that resolve to those forms.
|
|---|
| 913 | > Function conditions and flagged regular expressions are not supported.
|
|---|
| 914 |
|
|---|
| 915 | **webpack.config.js**
|
|---|
| 916 |
|
|---|
| 917 | ```js
|
|---|
| 918 | module.exports = {
|
|---|
| 919 | optimization: {
|
|---|
| 920 | minimize: true,
|
|---|
| 921 | minimizer: [
|
|---|
| 922 | new MinimizerPlugin({
|
|---|
| 923 | minify: MinimizerPlugin.swcMinify,
|
|---|
| 924 | // `minimizerOptions` will be passed to `swc` (`@swc/core`)
|
|---|
| 925 | // Link to options - https://swc.rs/docs/config-js-minify
|
|---|
| 926 | minimizerOptions: {},
|
|---|
| 927 | }),
|
|---|
| 928 | ],
|
|---|
| 929 | },
|
|---|
| 930 | };
|
|---|
| 931 | ```
|
|---|
| 932 |
|
|---|
| 933 | ### [`esbuild`](https://github.com/evanw/esbuild)
|
|---|
| 934 |
|
|---|
| 935 | [`esbuild`](https://github.com/evanw/esbuild) is an extremely fast JavaScript bundler and minifier.
|
|---|
| 936 |
|
|---|
| 937 | > **Warning**
|
|---|
| 938 | >
|
|---|
| 939 | > The `extractComments` option is not supported, and all legal comments (i.e. copyright, licenses and etc) will be preserved.
|
|---|
| 940 |
|
|---|
| 941 | **webpack.config.js**
|
|---|
| 942 |
|
|---|
| 943 | ```js
|
|---|
| 944 | module.exports = {
|
|---|
| 945 | optimization: {
|
|---|
| 946 | minimize: true,
|
|---|
| 947 | minimizer: [
|
|---|
| 948 | new MinimizerPlugin({
|
|---|
| 949 | minify: MinimizerPlugin.esbuildMinify,
|
|---|
| 950 | // `minimizerOptions` will be passed to `esbuild`
|
|---|
| 951 | // Link to options - https://esbuild.github.io/api/#minify
|
|---|
| 952 | // Note: the `minify` options is true by default (and override other `minify*` options), so if you want to disable the `minifyIdentifiers` option (or other `minify*` options) please use:
|
|---|
| 953 | // minimizerOptions: {
|
|---|
| 954 | // minify: false,
|
|---|
| 955 | // minifyWhitespace: true,
|
|---|
| 956 | // minifyIdentifiers: false,
|
|---|
| 957 | // minifySyntax: true,
|
|---|
| 958 | // },
|
|---|
| 959 | minimizerOptions: {},
|
|---|
| 960 | }),
|
|---|
| 961 | ],
|
|---|
| 962 | },
|
|---|
| 963 | };
|
|---|
| 964 | ```
|
|---|
| 965 |
|
|---|
| 966 | ### JSON
|
|---|
| 967 |
|
|---|
| 968 | Uses `JSON.stringify()` to minify your JSON files during the build process.
|
|---|
| 969 |
|
|---|
| 970 | **webpack.config.js**
|
|---|
| 971 |
|
|---|
| 972 | ```js
|
|---|
| 973 | module.exports = {
|
|---|
| 974 | optimization: {
|
|---|
| 975 | minimize: true,
|
|---|
| 976 | minimizer: [
|
|---|
| 977 | // Keeps original terser plugin to minify JS files
|
|---|
| 978 | "...",
|
|---|
| 979 | // Will minify JSON files (they can come from copy-webpack-plugin or when you are using asset modules)
|
|---|
| 980 | new MinimizerPlugin({
|
|---|
| 981 | test: /\.json$/,
|
|---|
| 982 | minify: MinimizerPlugin.jsonMinify,
|
|---|
| 983 | // We are supporting `space` and `replacer` options, you can set them below
|
|---|
| 984 | minimizerOptions: {},
|
|---|
| 985 | }),
|
|---|
| 986 | ],
|
|---|
| 987 | },
|
|---|
| 988 | };
|
|---|
| 989 | ```
|
|---|
| 990 |
|
|---|
| 991 | ### HTML
|
|---|
| 992 |
|
|---|
| 993 | The plugin can minify HTML assets too. Pick one of the bundled HTML
|
|---|
| 994 | minimizers and set `test` to match your HTML files.
|
|---|
| 995 |
|
|---|
| 996 | Available HTML minimizers:
|
|---|
| 997 |
|
|---|
| 998 | - `MinimizerPlugin.htmlMinifierTerser` — uses [`html-minifier-terser`](https://github.com/terser/html-minifier-terser).
|
|---|
| 999 | - `MinimizerPlugin.swcMinifyHtml` — uses [`@swc/html`](https://github.com/swc-project/swc) for full HTML documents (with doctype and `<html>`/`<head>`/`<body>` tags).
|
|---|
| 1000 | - `MinimizerPlugin.swcMinifyHtmlFragment` — uses [`@swc/html`](https://github.com/swc-project/swc) for HTML fragments (e.g. content inside `<template></template>` or partial HTML strings).
|
|---|
| 1001 | - `MinimizerPlugin.minifyHtmlNode` — uses [`@minify-html/node`](https://github.com/wilsonzlin/minify-html).
|
|---|
| 1002 |
|
|---|
| 1003 | The HTML minimizers are optional peer dependencies — install only the one
|
|---|
| 1004 | you actually use:
|
|---|
| 1005 |
|
|---|
| 1006 | ```console
|
|---|
| 1007 | npm install --save-dev html-minifier-terser
|
|---|
| 1008 | # or
|
|---|
| 1009 | npm install --save-dev @swc/html
|
|---|
| 1010 | # or
|
|---|
| 1011 | npm install --save-dev @minify-html/node
|
|---|
| 1012 | ```
|
|---|
| 1013 |
|
|---|
| 1014 | > **Note**
|
|---|
| 1015 | >
|
|---|
| 1016 | > HTML assets typically come from plugins like
|
|---|
| 1017 | > [`copy-webpack-plugin`](https://github.com/webpack-contrib/copy-webpack-plugin),
|
|---|
| 1018 | > [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin),
|
|---|
| 1019 | > or webpack's [asset modules](https://webpack.js.org/guides/asset-modules/).
|
|---|
| 1020 |
|
|---|
| 1021 | > **Note**
|
|---|
| 1022 | >
|
|---|
| 1023 | > Whitespace handling differs between tools (defaults):
|
|---|
| 1024 | >
|
|---|
| 1025 | > - `@swc/html` — removes/collapses whitespace only in safe places (around `html`/`body`, inside `<head>`, between `<meta>`/`<script>`/`<link>` etc.).
|
|---|
| 1026 | > - `html-minifier-terser` — always collapses multiple whitespaces to a single space (never removes entirely); configurable via [its options](https://github.com/terser/html-minifier-terser#options-quick-reference).
|
|---|
| 1027 | > - `@minify-html/node` — see [its whitespace docs](https://github.com/wilsonzlin/minify-html#whitespace).
|
|---|
| 1028 |
|
|---|
| 1029 | #### `html-minifier-terser`
|
|---|
| 1030 |
|
|---|
| 1031 | [`html-minifier-terser`](https://github.com/terser/html-minifier-terser) is a JavaScript-based HTML minifier with no native dependency. It's the default HTML minimizer.
|
|---|
| 1032 |
|
|---|
| 1033 | **webpack.config.js**
|
|---|
| 1034 |
|
|---|
| 1035 | ```js
|
|---|
| 1036 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1037 |
|
|---|
| 1038 | module.exports = {
|
|---|
| 1039 | optimization: {
|
|---|
| 1040 | minimize: true,
|
|---|
| 1041 | minimizer: [
|
|---|
| 1042 | // Keeps the default Terser plugin for JS files
|
|---|
| 1043 | "...",
|
|---|
| 1044 | new MinimizerPlugin({
|
|---|
| 1045 | test: /\.html(\?.*)?$/i,
|
|---|
| 1046 | minify: MinimizerPlugin.htmlMinifierTerser,
|
|---|
| 1047 | // Options - https://github.com/terser/html-minifier-terser#options-quick-reference
|
|---|
| 1048 | minimizerOptions: {
|
|---|
| 1049 | collapseWhitespace: true,
|
|---|
| 1050 | removeComments: true,
|
|---|
| 1051 | },
|
|---|
| 1052 | }),
|
|---|
| 1053 | ],
|
|---|
| 1054 | },
|
|---|
| 1055 | };
|
|---|
| 1056 | ```
|
|---|
| 1057 |
|
|---|
| 1058 | #### `@swc/html` — HTML documents
|
|---|
| 1059 |
|
|---|
| 1060 | Use `swcMinifyHtml` for complete HTML documents (i.e. with a doctype and `<html>`/`<head>`/`<body>` tags).
|
|---|
| 1061 |
|
|---|
| 1062 | **webpack.config.js**
|
|---|
| 1063 |
|
|---|
| 1064 | ```js
|
|---|
| 1065 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1066 |
|
|---|
| 1067 | module.exports = {
|
|---|
| 1068 | optimization: {
|
|---|
| 1069 | minimize: true,
|
|---|
| 1070 | minimizer: [
|
|---|
| 1071 | "...",
|
|---|
| 1072 | new MinimizerPlugin({
|
|---|
| 1073 | test: /\.html(\?.*)?$/i,
|
|---|
| 1074 | minify: MinimizerPlugin.swcMinifyHtml,
|
|---|
| 1075 | // Options - https://github.com/swc-project/bindings/blob/main/packages/html/index.ts
|
|---|
| 1076 | minimizerOptions: {},
|
|---|
| 1077 | }),
|
|---|
| 1078 | ],
|
|---|
| 1079 | },
|
|---|
| 1080 | };
|
|---|
| 1081 | ```
|
|---|
| 1082 |
|
|---|
| 1083 | #### `@swc/html` — HTML fragments
|
|---|
| 1084 |
|
|---|
| 1085 | Use `swcMinifyHtmlFragment` for partial HTML — for example, content of `<template></template>` tags or HTML strings that get injected into another document.
|
|---|
| 1086 |
|
|---|
| 1087 | **webpack.config.js**
|
|---|
| 1088 |
|
|---|
| 1089 | ```js
|
|---|
| 1090 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1091 |
|
|---|
| 1092 | module.exports = {
|
|---|
| 1093 | optimization: {
|
|---|
| 1094 | minimize: true,
|
|---|
| 1095 | minimizer: [
|
|---|
| 1096 | "...",
|
|---|
| 1097 | new MinimizerPlugin({
|
|---|
| 1098 | test: /\.template\.html$/i,
|
|---|
| 1099 | minify: MinimizerPlugin.swcMinifyHtmlFragment,
|
|---|
| 1100 | // Options - https://github.com/swc-project/bindings/blob/main/packages/html/index.ts
|
|---|
| 1101 | minimizerOptions: {},
|
|---|
| 1102 | }),
|
|---|
| 1103 | ],
|
|---|
| 1104 | },
|
|---|
| 1105 | };
|
|---|
| 1106 | ```
|
|---|
| 1107 |
|
|---|
| 1108 | > **Note**
|
|---|
| 1109 | >
|
|---|
| 1110 | > The difference between `swcMinifyHtml` and `swcMinifyHtmlFragment` is the
|
|---|
| 1111 | > error reporting — invalid or broken syntax is reported at build time.
|
|---|
| 1112 |
|
|---|
| 1113 | #### `@minify-html/node`
|
|---|
| 1114 |
|
|---|
| 1115 | [`@minify-html/node`](https://github.com/wilsonzlin/minify-html) is a Rust HTML minifier.
|
|---|
| 1116 |
|
|---|
| 1117 | **webpack.config.js**
|
|---|
| 1118 |
|
|---|
| 1119 | ```js
|
|---|
| 1120 | const Minimizer = require("minimizer-webpack-plugin");
|
|---|
| 1121 |
|
|---|
| 1122 | module.exports = {
|
|---|
| 1123 | optimization: {
|
|---|
| 1124 | minimize: true,
|
|---|
| 1125 | minimizer: [
|
|---|
| 1126 | "...",
|
|---|
| 1127 | new Minimizer({
|
|---|
| 1128 | test: /\.html(\?.*)?$/i,
|
|---|
| 1129 | minify: Minimizer.minifyHtmlNode,
|
|---|
| 1130 | // Options - https://github.com/wilsonzlin/minify-html#minification
|
|---|
| 1131 | minimizerOptions: {},
|
|---|
| 1132 | }),
|
|---|
| 1133 | ],
|
|---|
| 1134 | },
|
|---|
| 1135 | };
|
|---|
| 1136 | ```
|
|---|
| 1137 |
|
|---|
| 1138 | You can also stack multiple `MinimizerPlugin` instances to compress different files with different `minify` functions in the same build (e.g. JS with `terserMinify`, HTML with `htmlMinifierTerser`, JSON with `jsonMinify`).
|
|---|
| 1139 |
|
|---|
| 1140 | ### CSS
|
|---|
| 1141 |
|
|---|
| 1142 | The plugin can minify CSS assets too. Pick one of the bundled CSS
|
|---|
| 1143 | minimizers and set `test` to match your CSS files.
|
|---|
| 1144 |
|
|---|
| 1145 | Available CSS minimizers:
|
|---|
| 1146 |
|
|---|
| 1147 | - `MinimizerPlugin.cssnanoMinify` — uses [`cssnano`](https://cssnano.github.io/cssnano/) (via [`postcss`](https://postcss.org/)).
|
|---|
| 1148 | - `MinimizerPlugin.cssoMinify` — uses [`csso`](https://github.com/css/csso).
|
|---|
| 1149 | - `MinimizerPlugin.cleanCssMinify` — uses [`clean-css`](https://github.com/clean-css/clean-css).
|
|---|
| 1150 | - `MinimizerPlugin.esbuildMinifyCss` — uses [`esbuild`](https://github.com/evanw/esbuild) with the CSS loader.
|
|---|
| 1151 | - `MinimizerPlugin.lightningCssMinify` — uses [`lightningcss`](https://github.com/parcel-bundler/lightningcss).
|
|---|
| 1152 | - `MinimizerPlugin.swcMinifyCss` — uses [`@swc/css`](https://github.com/swc-project/swc).
|
|---|
| 1153 |
|
|---|
| 1154 | The CSS minimizers are optional peer dependencies — install only the ones
|
|---|
| 1155 | you actually use:
|
|---|
| 1156 |
|
|---|
| 1157 | ```console
|
|---|
| 1158 | npm install --save-dev cssnano postcss
|
|---|
| 1159 | # or
|
|---|
| 1160 | npm install --save-dev csso
|
|---|
| 1161 | # or
|
|---|
| 1162 | npm install --save-dev clean-css
|
|---|
| 1163 | # or
|
|---|
| 1164 | npm install --save-dev esbuild
|
|---|
| 1165 | # or
|
|---|
| 1166 | npm install --save-dev lightningcss
|
|---|
| 1167 | # or
|
|---|
| 1168 | npm install --save-dev @swc/css
|
|---|
| 1169 | ```
|
|---|
| 1170 |
|
|---|
| 1171 | > **Note**
|
|---|
| 1172 | >
|
|---|
| 1173 | > CSS assets typically come from plugins like
|
|---|
| 1174 | > [`mini-css-extract-plugin`](https://github.com/webpack-contrib/mini-css-extract-plugin)
|
|---|
| 1175 | > or webpack's [asset modules](https://webpack.js.org/guides/asset-modules/).
|
|---|
| 1176 |
|
|---|
| 1177 | #### `cssnano`
|
|---|
| 1178 |
|
|---|
| 1179 | [`cssnano`](https://cssnano.github.io/cssnano/) is the default CSS minimizer. It runs as a [PostCSS](https://postcss.org/) plugin.
|
|---|
| 1180 |
|
|---|
| 1181 | **webpack.config.js**
|
|---|
| 1182 |
|
|---|
| 1183 | ```js
|
|---|
| 1184 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1185 |
|
|---|
| 1186 | module.exports = {
|
|---|
| 1187 | optimization: {
|
|---|
| 1188 | minimize: true,
|
|---|
| 1189 | minimizer: [
|
|---|
| 1190 | // Keeps the default Terser plugin for JS files
|
|---|
| 1191 | "...",
|
|---|
| 1192 | new MinimizerPlugin({
|
|---|
| 1193 | test: /\.css(\?.*)?$/i,
|
|---|
| 1194 | minify: MinimizerPlugin.cssnanoMinify,
|
|---|
| 1195 | // Options - https://cssnano.github.io/cssnano/docs/config-file/
|
|---|
| 1196 | minimizerOptions: {
|
|---|
| 1197 | preset: "default",
|
|---|
| 1198 | },
|
|---|
| 1199 | }),
|
|---|
| 1200 | ],
|
|---|
| 1201 | },
|
|---|
| 1202 | };
|
|---|
| 1203 | ```
|
|---|
| 1204 |
|
|---|
| 1205 | #### `csso`
|
|---|
| 1206 |
|
|---|
| 1207 | [`csso`](https://github.com/css/csso) is a CSS minifier with structural optimisations.
|
|---|
| 1208 |
|
|---|
| 1209 | **webpack.config.js**
|
|---|
| 1210 |
|
|---|
| 1211 | ```js
|
|---|
| 1212 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1213 |
|
|---|
| 1214 | module.exports = {
|
|---|
| 1215 | optimization: {
|
|---|
| 1216 | minimize: true,
|
|---|
| 1217 | minimizer: [
|
|---|
| 1218 | "...",
|
|---|
| 1219 | new MinimizerPlugin({
|
|---|
| 1220 | test: /\.css(\?.*)?$/i,
|
|---|
| 1221 | minify: MinimizerPlugin.cssoMinify,
|
|---|
| 1222 | // Options - https://github.com/css/csso#minifysource-options
|
|---|
| 1223 | minimizerOptions: {},
|
|---|
| 1224 | }),
|
|---|
| 1225 | ],
|
|---|
| 1226 | },
|
|---|
| 1227 | };
|
|---|
| 1228 | ```
|
|---|
| 1229 |
|
|---|
| 1230 | #### `clean-css`
|
|---|
| 1231 |
|
|---|
| 1232 | [`clean-css`](https://github.com/clean-css/clean-css) is a widely-used CSS optimiser.
|
|---|
| 1233 |
|
|---|
| 1234 | **webpack.config.js**
|
|---|
| 1235 |
|
|---|
| 1236 | ```js
|
|---|
| 1237 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1238 |
|
|---|
| 1239 | module.exports = {
|
|---|
| 1240 | optimization: {
|
|---|
| 1241 | minimize: true,
|
|---|
| 1242 | minimizer: [
|
|---|
| 1243 | "...",
|
|---|
| 1244 | new MinimizerPlugin({
|
|---|
| 1245 | test: /\.css(\?.*)?$/i,
|
|---|
| 1246 | minify: MinimizerPlugin.cleanCssMinify,
|
|---|
| 1247 | // Options - https://github.com/clean-css/clean-css#constructor-options
|
|---|
| 1248 | minimizerOptions: {},
|
|---|
| 1249 | }),
|
|---|
| 1250 | ],
|
|---|
| 1251 | },
|
|---|
| 1252 | };
|
|---|
| 1253 | ```
|
|---|
| 1254 |
|
|---|
| 1255 | #### `esbuild`
|
|---|
| 1256 |
|
|---|
| 1257 | [`esbuild`](https://github.com/evanw/esbuild) ships with a fast CSS minifier (used via its CSS loader).
|
|---|
| 1258 |
|
|---|
| 1259 | **webpack.config.js**
|
|---|
| 1260 |
|
|---|
| 1261 | ```js
|
|---|
| 1262 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1263 |
|
|---|
| 1264 | module.exports = {
|
|---|
| 1265 | optimization: {
|
|---|
| 1266 | minimize: true,
|
|---|
| 1267 | minimizer: [
|
|---|
| 1268 | "...",
|
|---|
| 1269 | new MinimizerPlugin({
|
|---|
| 1270 | test: /\.css(\?.*)?$/i,
|
|---|
| 1271 | minify: MinimizerPlugin.esbuildMinifyCss,
|
|---|
| 1272 | // Options - https://esbuild.github.io/api/#transform-api
|
|---|
| 1273 | minimizerOptions: {},
|
|---|
| 1274 | }),
|
|---|
| 1275 | ],
|
|---|
| 1276 | },
|
|---|
| 1277 | };
|
|---|
| 1278 | ```
|
|---|
| 1279 |
|
|---|
| 1280 | #### `lightningcss`
|
|---|
| 1281 |
|
|---|
| 1282 | [`lightningcss`](https://github.com/parcel-bundler/lightningcss) is a Rust-based CSS parser, transformer, and minifier.
|
|---|
| 1283 |
|
|---|
| 1284 | **webpack.config.js**
|
|---|
| 1285 |
|
|---|
| 1286 | ```js
|
|---|
| 1287 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1288 |
|
|---|
| 1289 | module.exports = {
|
|---|
| 1290 | optimization: {
|
|---|
| 1291 | minimize: true,
|
|---|
| 1292 | minimizer: [
|
|---|
| 1293 | "...",
|
|---|
| 1294 | new MinimizerPlugin({
|
|---|
| 1295 | test: /\.css(\?.*)?$/i,
|
|---|
| 1296 | minify: MinimizerPlugin.lightningCssMinify,
|
|---|
| 1297 | // Options - https://lightningcss.dev/transpilation.html
|
|---|
| 1298 | minimizerOptions: {},
|
|---|
| 1299 | }),
|
|---|
| 1300 | ],
|
|---|
| 1301 | },
|
|---|
| 1302 | };
|
|---|
| 1303 | ```
|
|---|
| 1304 |
|
|---|
| 1305 | #### `@swc/css`
|
|---|
| 1306 |
|
|---|
| 1307 | [`@swc/css`](https://github.com/swc-project/swc) is a Rust-based CSS minifier.
|
|---|
| 1308 |
|
|---|
| 1309 | **webpack.config.js**
|
|---|
| 1310 |
|
|---|
| 1311 | ```js
|
|---|
| 1312 | const MinimizerPlugin = require("minimizer-webpack-plugin");
|
|---|
| 1313 |
|
|---|
| 1314 | module.exports = {
|
|---|
| 1315 | optimization: {
|
|---|
| 1316 | minimize: true,
|
|---|
| 1317 | minimizer: [
|
|---|
| 1318 | "...",
|
|---|
| 1319 | new MinimizerPlugin({
|
|---|
| 1320 | test: /\.css(\?.*)?$/i,
|
|---|
| 1321 | minify: MinimizerPlugin.swcMinifyCss,
|
|---|
| 1322 | // Options - https://github.com/swc-project/bindings/blob/main/packages/css/index.ts
|
|---|
| 1323 | minimizerOptions: {},
|
|---|
| 1324 | }),
|
|---|
| 1325 | ],
|
|---|
| 1326 | },
|
|---|
| 1327 | };
|
|---|
| 1328 | ```
|
|---|
| 1329 |
|
|---|
| 1330 | ### Custom Minify Function
|
|---|
| 1331 |
|
|---|
| 1332 | Override the default minify function - use `uglify-js` for minification.
|
|---|
| 1333 |
|
|---|
| 1334 | **webpack.config.js**
|
|---|
| 1335 |
|
|---|
| 1336 | ```js
|
|---|
| 1337 | module.exports = {
|
|---|
| 1338 | optimization: {
|
|---|
| 1339 | minimize: true,
|
|---|
| 1340 | minimizer: [
|
|---|
| 1341 | new MinimizerPlugin({
|
|---|
| 1342 | minify: (file, sourceMap) => {
|
|---|
| 1343 | // https://github.com/mishoo/UglifyJS2#minify-options
|
|---|
| 1344 | const uglifyJsOptions = {
|
|---|
| 1345 | /* your `uglify-js` package options */
|
|---|
| 1346 | };
|
|---|
| 1347 |
|
|---|
| 1348 | if (sourceMap) {
|
|---|
| 1349 | uglifyJsOptions.sourceMap = {
|
|---|
| 1350 | content: sourceMap,
|
|---|
| 1351 | };
|
|---|
| 1352 | }
|
|---|
| 1353 |
|
|---|
| 1354 | return require("uglify-js").minify(file, uglifyJsOptions);
|
|---|
| 1355 | },
|
|---|
| 1356 | }),
|
|---|
| 1357 | ],
|
|---|
| 1358 | },
|
|---|
| 1359 | };
|
|---|
| 1360 | ```
|
|---|
| 1361 |
|
|---|
| 1362 | ### Typescript
|
|---|
| 1363 |
|
|---|
| 1364 | With default Terser minify function:
|
|---|
| 1365 |
|
|---|
| 1366 | ```ts
|
|---|
| 1367 | module.exports = {
|
|---|
| 1368 | optimization: {
|
|---|
| 1369 | minimize: true,
|
|---|
| 1370 | minimizer: [
|
|---|
| 1371 | new MinimizerPlugin({
|
|---|
| 1372 | minimizerOptions: {
|
|---|
| 1373 | compress: true,
|
|---|
| 1374 | },
|
|---|
| 1375 | }),
|
|---|
| 1376 | ],
|
|---|
| 1377 | },
|
|---|
| 1378 | };
|
|---|
| 1379 | ```
|
|---|
| 1380 |
|
|---|
| 1381 | With built-in minify functions:
|
|---|
| 1382 |
|
|---|
| 1383 | ```ts
|
|---|
| 1384 | import { type JsMinifyOptions as SwcOptions } from "@swc/core";
|
|---|
| 1385 | import { type MinifyOptions as SwcCssOptions } from "@swc/css";
|
|---|
| 1386 | import {
|
|---|
| 1387 | type FragmentOptions as SwcHtmlFragmentOptions,
|
|---|
| 1388 | type Options as SwcHtmlOptions,
|
|---|
| 1389 | } from "@swc/html";
|
|---|
| 1390 | import { type OptionsOutput as CleanCssOptions } from "clean-css";
|
|---|
| 1391 | import { type Options as CssnanoOptions } from "cssnano";
|
|---|
| 1392 | import { type CompressOptions as CssoOptions } from "csso";
|
|---|
| 1393 | import { type TransformOptions as EsbuildOptions } from "esbuild";
|
|---|
| 1394 | import { type Options as HtmlMinifierTerserOptions } from "html-minifier-terser";
|
|---|
| 1395 | import { type TransformOptions as LightningCssOptions } from "lightningcss";
|
|---|
| 1396 | import { type MinifyOptions as TerserOptions } from "terser";
|
|---|
| 1397 | import { type MinifyOptions as UglifyJSOptions } from "uglify-js";
|
|---|
| 1398 |
|
|---|
| 1399 | module.exports = {
|
|---|
| 1400 | optimization: {
|
|---|
| 1401 | minimize: true,
|
|---|
| 1402 | minimizer: [
|
|---|
| 1403 | new MinimizerPlugin<SwcOptions>({
|
|---|
| 1404 | minify: MinimizerPlugin.swcMinify,
|
|---|
| 1405 | minimizerOptions: {
|
|---|
| 1406 | // `swc` options
|
|---|
| 1407 | },
|
|---|
| 1408 | }),
|
|---|
| 1409 | new MinimizerPlugin<UglifyJSOptions>({
|
|---|
| 1410 | minify: MinimizerPlugin.uglifyJsMinify,
|
|---|
| 1411 | minimizerOptions: {
|
|---|
| 1412 | // `uglif-js` options
|
|---|
| 1413 | },
|
|---|
| 1414 | }),
|
|---|
| 1415 | new MinimizerPlugin<EsbuildOptions>({
|
|---|
| 1416 | minify: MinimizerPlugin.esbuildMinify,
|
|---|
| 1417 | minimizerOptions: {
|
|---|
| 1418 | // `esbuild` options
|
|---|
| 1419 | },
|
|---|
| 1420 | }),
|
|---|
| 1421 |
|
|---|
| 1422 | // Alternative usage:
|
|---|
| 1423 | new MinimizerPlugin<TerserOptions>({
|
|---|
| 1424 | minify: MinimizerPlugin.terserMinify,
|
|---|
| 1425 | minimizerOptions: {
|
|---|
| 1426 | // `terser` options
|
|---|
| 1427 | },
|
|---|
| 1428 | }),
|
|---|
| 1429 |
|
|---|
| 1430 | // HTML minimizers
|
|---|
| 1431 | new MinimizerPlugin<HtmlMinifierTerserOptions>({
|
|---|
| 1432 | test: /\.html(\?.*)?$/i,
|
|---|
| 1433 | minify: MinimizerPlugin.htmlMinifierTerser,
|
|---|
| 1434 | minimizerOptions: {
|
|---|
| 1435 | // `html-minifier-terser` options
|
|---|
| 1436 | },
|
|---|
| 1437 | }),
|
|---|
| 1438 | new MinimizerPlugin<SwcHtmlOptions>({
|
|---|
| 1439 | test: /\.html(\?.*)?$/i,
|
|---|
| 1440 | minify: MinimizerPlugin.swcMinifyHtml,
|
|---|
| 1441 | minimizerOptions: {
|
|---|
| 1442 | // `@swc/html` options
|
|---|
| 1443 | },
|
|---|
| 1444 | }),
|
|---|
| 1445 | new MinimizerPlugin<SwcHtmlFragmentOptions>({
|
|---|
| 1446 | test: /\.template\.html$/i,
|
|---|
| 1447 | minify: MinimizerPlugin.swcMinifyHtmlFragment,
|
|---|
| 1448 | minimizerOptions: {
|
|---|
| 1449 | // `@swc/html` fragment options
|
|---|
| 1450 | },
|
|---|
| 1451 | }),
|
|---|
| 1452 |
|
|---|
| 1453 | // CSS minimizers
|
|---|
| 1454 | new MinimizerPlugin<CssnanoOptions>({
|
|---|
| 1455 | test: /\.css(\?.*)?$/i,
|
|---|
| 1456 | minify: MinimizerPlugin.cssnanoMinify,
|
|---|
| 1457 | minimizerOptions: {
|
|---|
| 1458 | // `cssnano` options
|
|---|
| 1459 | },
|
|---|
| 1460 | }),
|
|---|
| 1461 | new MinimizerPlugin<CssoOptions>({
|
|---|
| 1462 | test: /\.css(\?.*)?$/i,
|
|---|
| 1463 | minify: MinimizerPlugin.cssoMinify,
|
|---|
| 1464 | minimizerOptions: {
|
|---|
| 1465 | // `csso` options
|
|---|
| 1466 | },
|
|---|
| 1467 | }),
|
|---|
| 1468 | new MinimizerPlugin<CleanCssOptions>({
|
|---|
| 1469 | test: /\.css(\?.*)?$/i,
|
|---|
| 1470 | minify: MinimizerPlugin.cleanCssMinify,
|
|---|
| 1471 | minimizerOptions: {
|
|---|
| 1472 | // `clean-css` options
|
|---|
| 1473 | },
|
|---|
| 1474 | }),
|
|---|
| 1475 | new MinimizerPlugin<EsbuildOptions>({
|
|---|
| 1476 | test: /\.css(\?.*)?$/i,
|
|---|
| 1477 | minify: MinimizerPlugin.esbuildMinifyCss,
|
|---|
| 1478 | minimizerOptions: {
|
|---|
| 1479 | // `esbuild` options (CSS loader)
|
|---|
| 1480 | },
|
|---|
| 1481 | }),
|
|---|
| 1482 | new MinimizerPlugin<LightningCssOptions>({
|
|---|
| 1483 | test: /\.css(\?.*)?$/i,
|
|---|
| 1484 | minify: MinimizerPlugin.lightningCssMinify,
|
|---|
| 1485 | minimizerOptions: {
|
|---|
| 1486 | // `lightningcss` options
|
|---|
| 1487 | },
|
|---|
| 1488 | }),
|
|---|
| 1489 | new MinimizerPlugin<SwcCssOptions>({
|
|---|
| 1490 | test: /\.css(\?.*)?$/i,
|
|---|
| 1491 | minify: MinimizerPlugin.swcMinifyCss,
|
|---|
| 1492 | minimizerOptions: {
|
|---|
| 1493 | // `@swc/css` options
|
|---|
| 1494 | },
|
|---|
| 1495 | }),
|
|---|
| 1496 | ],
|
|---|
| 1497 | },
|
|---|
| 1498 | };
|
|---|
| 1499 | ```
|
|---|
| 1500 |
|
|---|
| 1501 | ## Contributing
|
|---|
| 1502 |
|
|---|
| 1503 | We welcome all contributions!
|
|---|
| 1504 | If you're new here, please take a moment to review our contributing guidelines before submitting issues or pull requests.
|
|---|
| 1505 |
|
|---|
| 1506 | [CONTRIBUTING](https://github.com/webpack/minimizer-webpack-plugin?tab=contributing-ov-file#contributing)
|
|---|
| 1507 |
|
|---|
| 1508 | ## License
|
|---|
| 1509 |
|
|---|
| 1510 | [MIT](./LICENSE)
|
|---|
| 1511 |
|
|---|
| 1512 | [npm]: https://img.shields.io/npm/v/minimizer-webpack-plugin.svg
|
|---|
| 1513 | [npm-url]: https://npmjs.com/package/minimizer-webpack-plugin
|
|---|
| 1514 | [node]: https://img.shields.io/node/v/minimizer-webpack-plugin.svg
|
|---|
| 1515 | [node-url]: https://nodejs.org
|
|---|
| 1516 | [tests]: https://github.com/webpack/minimizer-webpack-plugin/workflows/minimizer-webpack-plugin/badge.svg
|
|---|
| 1517 | [tests-url]: https://github.com/webpack/minimizer-webpack-plugin/actions
|
|---|
| 1518 | [cover]: https://codecov.io/gh/webpack/minimizer-webpack-plugin/branch/main/graph/badge.svg
|
|---|
| 1519 | [cover-url]: https://codecov.io/gh/webpack/minimizer-webpack-plugin
|
|---|
| 1520 | [discussion]: https://img.shields.io/github/discussions/webpack/webpack
|
|---|
| 1521 | [discussion-url]: https://github.com/webpack/webpack/discussions
|
|---|
| 1522 | [size]: https://packagephobia.now.sh/badge?p=minimizer-webpack-plugin
|
|---|
| 1523 | [size-url]: https://packagephobia.now.sh/result?p=minimizer-webpack-plugin
|
|---|