Skip to content

Commit e62c2f8

Browse files
feat: support array of minimizers for minify and terserOptions (#666)
1 parent d569842 commit e62c2f8

14 files changed

Lines changed: 756 additions & 220 deletions

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ module.exports = {
223223
Type:
224224

225225
```ts
226-
type minify = (
226+
type minifyFn = (
227227
input: Record<string, string>,
228228
sourceMap: import("@jridgewell/trace-mapping").SourceMapInput | undefined,
229229
minifyOptions: {
@@ -277,6 +277,8 @@ type minify = (
277277
warnings?: (string | Error)[] | undefined;
278278
extractedComments?: string[] | undefined;
279279
}>;
280+
281+
type minify = minifyFn | minifyFn[];
280282
```
281283

282284
Default: `TerserPlugin.terserMinify`
@@ -285,10 +287,14 @@ Allows you to override the default minify function.
285287
By default plugin uses [terser](https://github.com/terser/terser) package.
286288
Useful for using and testing unpublished versions or forks.
287289

290+
An array of functions can also be provided to chain multiple minimizers — the output of each minimizer is fed as input to the next. When an array is used, the [`terserOptions`](#terseroptions) option may also be an array (index-paired with `minify`) or a single object that is reused for every minimizer.
291+
288292
> **Warning**
289293
>
290294
> **Always use `require` inside `minify` function when `parallel` option enabled**.
291295
296+
#### `function`
297+
292298
**webpack.config.js**
293299

294300
```js
@@ -337,6 +343,36 @@ module.exports = {
337343
};
338344
```
339345

346+
#### `array`
347+
348+
If an array of functions is passed to the `minify` option, the output of each
349+
minimizer is fed as input to the next one. The `terserOptions` option can be
350+
either an array of option objects (index-paired with `minify`) or a single
351+
object that will be shared by all minimizers. Warnings, errors and extracted
352+
comments from all minimizers are merged together.
353+
354+
**webpack.config.js**
355+
356+
```js
357+
module.exports = {
358+
optimization: {
359+
minimize: true,
360+
minimizer: [
361+
new TerserPlugin({
362+
minify: [TerserPlugin.terserMinify, TerserPlugin.swcMinify],
363+
// `terserOptions` can be an array of options, one per `minify` entry
364+
terserOptions: [
365+
// Options for `TerserPlugin.terserMinify`
366+
{ mangle: false },
367+
// Options for `TerserPlugin.swcMinify`
368+
{},
369+
],
370+
}),
371+
],
372+
},
373+
};
374+
```
375+
340376
### `terserOptions`
341377

342378
Type:
@@ -360,12 +396,19 @@ interface terserOptions {
360396
sourceMap?: boolean | SourceMapOptions;
361397
toplevel?: boolean;
362398
}
399+
400+
type options = terserOptions | terserOptions[];
363401
```
364402

365403
Default: [default](https://github.com/terser/terser#minify-options)
366404

367405
Terser [options](https://github.com/terser/terser#minify-options).
368406

407+
When the [`minify`](#minify) option is an array of minimizers, `terserOptions`
408+
can also be an array. Each element is passed to the minimizer at the same
409+
index in the `minify` array. If a single object is provided instead, it is
410+
reused for every minimizer.
411+
369412
**webpack.config.js**
370413

371414
```js

0 commit comments

Comments
 (0)