diff --git a/.changeset/060-minify-filter.md b/.changeset/060-minify-filter.md new file mode 100644 index 0000000..96786f0 --- /dev/null +++ b/.changeset/060-minify-filter.md @@ -0,0 +1,5 @@ +--- +"minimizer-webpack-plugin": minor +--- + +Allow a minimizer in `minify` to state its own `filter`. diff --git a/README.md b/README.md index 7c30a9b..98042d8 100644 --- a/README.md +++ b/README.md @@ -370,6 +370,22 @@ default one, and the tables in [webpack's own minimizers](#webpacks-own-minimizers) for `cssMinify` and `htmlMinify`. +`filter(name, info)` states which assets this minimizer is offered — return +`false` to decline one, and anything else (`undefined` included) to accept. It +answers for a `filter` property on the minimizer function itself, which is what +the built-ins carry, so setting it here is how you narrow one of them without +wrapping it. + +```js +new MinimizerPlugin({ + minify: { + implementation: MinimizerPlugin.sharpMinify, + options: { encodeOptions: { jpeg: { quality: 80 } } }, + filter: (name) => !name.includes("do-not-touch"), + }, +}); +``` + Two keys are filled in before a minimizer sees them, and only when `options` does not set them itself: `ecma`, from [`output.environment`](https://webpack.js.org/configuration/output/#outputenvironment), @@ -441,7 +457,28 @@ module.exports = { }; ``` -This is what lets **one plugin instance and one worker pool** handle every +Each entry carries its own `filter` as well as its own `options`, which is how +the same minimizer runs twice over different assets: + +```js +new MinimizerPlugin({ + test: /\.(jpe?g|png)$/i, + minify: [ + { + implementation: MinimizerPlugin.sharpMinify, + options: { encodeOptions: { jpeg: { quality: 60 } } }, + filter: (name) => name.includes("thumb"), + }, + { + implementation: MinimizerPlugin.sharpMinify, + options: { encodeOptions: { jpeg: { quality: 90 } } }, + filter: (name) => !name.includes("thumb"), + }, + ], +}); +``` + +This is also what lets **one plugin instance and one worker pool** handle every asset type: each built-in ships with a `filter` matching its natural extension, so JS, CSS, HTML and JSON need no second instance. `test` still defaults to JS only, so widen it to let the other assets reach the dispatcher: @@ -1177,7 +1214,9 @@ What this reaches: `exportType` but `link`). - The text an `asset/source` module embeds, and the payload an `asset/inline` module encodes — the payload before it is encoded, so the encoding covers - what came back. + what came back. **A language written as text only**: an inline `svg` is + offered, an inline `png` or `jpeg` is not, so a raster image that becomes a + `data:` URI is minified by [`generate`](#generate) rather than here. - What a document or a stylesheet nests inside itself: an inline `