You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add filter helper to dispatch assets per-minimizer
Each built-in minimizer now exposes a `filter(name, info)` helper based on
file extension. When `minify` is an array, each asset is dispatched only to
the minimizers whose `filter` accepts it; chain semantics still apply when
multiple minimizers claim the same asset. The `test` option defaults to
`undefined` for the array form so that filters can decide. This lets a
single `TerserPlugin` instance handle JS/CSS/HTML/JSON with one shared
worker pool instead of forcing a separate plugin instance per type.
Copy file name to clipboardExpand all lines: README.md
+65-6Lines changed: 65 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -321,7 +321,34 @@ Allows you to override the default minify function.
321
321
By default plugin uses [terser](https://github.com/terser/terser) package.
322
322
Useful for using and testing unpublished versions or forks.
323
323
324
-
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 [`minimizerOptions`](#minimizeroptions) option may also be an array (index-paired with `minify`) or a single object that is reused for every minimizer.
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 `TerserPlugin` instances:
331
+
332
+
```js
333
+
newTerserPlugin({
334
+
minify: [
335
+
TerserPlugin.terserMinify,
336
+
TerserPlugin.cssnanoMinify,
337
+
TerserPlugin.htmlMinifierTerser,
338
+
TerserPlugin.jsonMinify,
339
+
],
340
+
});
341
+
```
342
+
343
+
When more than one minimizer in the array claims the same asset, the chain
344
+
semantic still applies: the output of each accepting minimizer is fed as
345
+
input to the next. The [`minimizerOptions`](#minimizeroptions) option may
346
+
be an array (index-paired with `minify`) or a single object reused by every
347
+
minimizer.
348
+
349
+
When `minify` is an array, the `test` option defaults to `undefined` so each
350
+
minimizer's `filter` decides which assets it processes. With a single `minify`
351
+
function, `test` keeps its JS-only default of `/\.[cm]?js(\?.*)?$/i`.
Copy file name to clipboardExpand all lines: src/index.js
+81-7Lines changed: 81 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -124,6 +124,7 @@ const {
124
124
* @property {() => string | undefined=} getMinimizerVersion function that returns version of minimizer
125
125
* @property {() => boolean | undefined=} supportsWorkerThreads true when minimizer support worker threads, otherwise false
126
126
* @property {() => boolean | undefined=} supportsWorker true when minimizer support worker, otherwise false
127
+
* @property {(name: string, info?: AssetInfo) => boolean | undefined=} filter return true when the minimizer supports the asset, otherwise false. When an array of minimizers is configured, each asset is dispatched only to the minimizers whose `filter` accepts it. Assets rejected by every minimizer in the array are skipped entirely.
127
128
*/
128
129
129
130
/**
@@ -189,13 +190,17 @@ class TerserPlugin {
189
190
190
191
// TODO handle json and etc in the next major release
191
192
// TODO make `minimizer` option instead `minify` and `terserOptions` in the next major release, also rename `terserMinify` to `terserMinimize`
193
+
// When an array of minimizers is supplied, leave `test` undefined so that
194
+
// each minimizer's own `filter` decides which assets it processes;
0 commit comments