1 |
|
2 | <div align="center">
|
3 | <a href="https://github.com/webpack/webpack">
|
4 | <img width="200" height="200" hspace="10"
|
5 | src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
|
6 | </a>
|
7 | <h1>Imagemin Webpack</h1>
|
8 | <p>
|
9 | Plugin and Loader for <a href="http://webpack.js.org/">webpack</a> to optimize (compress) all images using <a href="https://github.com/imagemin/imagemin">imagemin</a>.
|
10 | Do not worry about size of images, now they are always optimized/compressed.
|
11 | </p>
|
12 | </div>
|
13 |
|
14 |
|
15 |
|
16 | [![npm][npm]][npm-url]
|
17 | [![node][node]][node-url]
|
18 | [![tests][tests]][tests-url]
|
19 | [![cover][cover]][cover-url]
|
20 | [![discussion][discussion]][discussion-url]
|
21 | [![size][size]][size-url]
|
22 |
|
23 | # image-minimizer-webpack-plugin
|
24 |
|
25 |
|
26 | This TOC was generated by VS Code extension [Markdown All in One](https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one)
|
27 | with option `"markdown.extension.toc.levels": "2..6"`
|
28 | -->
|
29 |
|
30 | - [Getting Started](#getting-started)
|
31 | - [Install optimize/generate tool](#install-optimizegenerate-tool)
|
32 | - [Optimize with `imagemin`](#optimize-with-imagemin)
|
33 | - [[**DEPRECATED**] Optimize with `squoosh`](#deprecated-optimize-with-squoosh)
|
34 | - [Optimize with `sharp`](#optimize-with-sharp)
|
35 | - [Optimize with `svgo`](#optimize-with-svgo)
|
36 | - [Advanced setup](#advanced-setup)
|
37 | - [Query Parameters (only `squoosh` and `sharp` currently)](#query-parameters-only-squoosh-and-sharp-currently)
|
38 | - [Standalone Loader](#standalone-loader)
|
39 | - [Standalone Plugin](#standalone-plugin)
|
40 | - [Plugin Options](#plugin-options)
|
41 | - [`test`](#test)
|
42 | - [`include`](#include)
|
43 | - [`exclude`](#exclude)
|
44 | - [`minimizer`](#minimizer)
|
45 | - [Available minimizers](#available-minimizers)
|
46 | - [Single minimizer example for `imagemin`](#single-minimizer-example-for-imagemin)
|
47 | - [[**DEPRECATED**] Single minimizer example for `squoosh`](#deprecated-single-minimizer-example-for-squoosh)
|
48 | - [Single minimizer example for `sharp`](#single-minimizer-example-for-sharp)
|
49 | - [Single minimizer example for user defined implementation](#single-minimizer-example-for-user-defined-implementation)
|
50 | - [Multiple minimizers example](#multiple-minimizers-example)
|
51 | - [Minimizer options](#minimizer-options)
|
52 | - [`implementation`](#implementation)
|
53 | - [`options`](#options)
|
54 | - [`filter`](#filter)
|
55 | - [`filename`](#filename)
|
56 | - [`generator`](#generator)
|
57 | - [Available generators](#available-generators)
|
58 | - [Generator example for `imagemin`](#generator-example-for-imagemin)
|
59 | - [[**DEPRECATED**] Generator example for `squoosh`](#deprecated-generator-example-for-squoosh)
|
60 | - [Generator example for `sharp`](#generator-example-for-sharp)
|
61 | - [Generator example for user defined implementation](#generator-example-for-user-defined-implementation)
|
62 | - [Generator options](#generator-options)
|
63 | - [`type`](#type)
|
64 | - [`preset`](#preset)
|
65 | - [`implementation`](#implementation-1)
|
66 | - [`options`](#options-1)
|
67 | - [`filter`](#filter-1)
|
68 | - [`filename`](#filename-1)
|
69 | - [`severityError`](#severityerror)
|
70 | - [`loader`](#loader)
|
71 | - [`concurrency`](#concurrency)
|
72 | - [`deleteOriginalAssets`](#deleteoriginalassets)
|
73 | - [Loader Options](#loader-options)
|
74 | - [`minimizer`](#minimizer-1)
|
75 | - [Loader minimizer example for `imagemin`](#loader-minimizer-example-for-imagemin)
|
76 | - [`generator`](#generator-1)
|
77 | - [Loader generator example for `imagemin`](#loader-generator-example-for-imagemin)
|
78 | - [`severityError`](#severityerror-1)
|
79 | - [Additional API](#additional-api)
|
80 | - [`imageminNormalizeConfig(config)`](#imageminnormalizeconfigconfig)
|
81 | - [Examples](#examples)
|
82 | - [Optimize images based on size](#optimize-images-based-on-size)
|
83 | - [Optimize and generate `webp` images](#optimize-and-generate-webp-images)
|
84 | - [Generate `webp` images from copied assets](#generate-webp-images-from-copied-assets)
|
85 | - [Contributing](#contributing)
|
86 | - [License](#license)
|
87 |
|
88 | ## Getting Started
|
89 |
|
90 | This plugin can use 4 tools to optimize/generate images:
|
91 |
|
92 | - [`imagemin`](https://github.com/imagemin/imagemin) - optimize your images by default, since it is stable and works with all types of images
|
93 | - **DEPRECATED** [`squoosh`](https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh) - while working in experimental mode with `.jpg`, `.jpeg`, `.png`, `.webp`, `.avif` file types.
|
94 | - [`sharp`](https://github.com/lovell/sharp) - High performance Node.js image processing, the fastest module to resize and compress JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
|
95 | - [`svgo`](https://github.com/svg/svgo) - tool for optimizing SVG vector graphics files. Supports only SVG files minification.
|
96 |
|
97 | > [!WARNING]
|
98 | >
|
99 | > By default we don't install anything
|
100 |
|
101 | ### Install optimize/generate tool
|
102 |
|
103 | To begin, you'll need to install `image-minimizer-webpack-plugin` and image minimizer/generator:
|
104 |
|
105 | - [`imagemin`](https://github.com/imagemin/imagemin):
|
106 |
|
107 | ```console
|
108 | npm install image-minimizer-webpack-plugin imagemin --save-dev
|
109 | ```
|
110 |
|
111 | > [!WARNING]
|
112 | >
|
113 | > imagemin uses plugin to optimize/generate images, so you need to install them too
|
114 |
|
115 | - **DEPRECATED** [`squoosh`](https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh):
|
116 |
|
117 | ```console
|
118 | npm install image-minimizer-webpack-plugin @squoosh/lib --save-dev
|
119 | ```
|
120 |
|
121 | - [`sharp`](https://github.com/lovell/sharp):
|
122 |
|
123 | ```console
|
124 | npm install image-minimizer-webpack-plugin sharp --save-dev
|
125 | ```
|
126 |
|
127 | - [`svgo`](https://github.com/svg/svgo):
|
128 |
|
129 | ```console
|
130 | npm install image-minimizer-webpack-plugin svgo --save-dev
|
131 | ```
|
132 |
|
133 | Images can be optimized in two modes:
|
134 |
|
135 | 1. [Lossless](https://en.wikipedia.org/wiki/Lossless_compression) (without loss of quality).
|
136 | 2. [Lossy](https://en.wikipedia.org/wiki/Lossy_compression) (with loss of quality).
|
137 |
|
138 | ### Optimize with [`imagemin`](https://github.com/imagemin/imagemin)
|
139 |
|
140 | > [!NOTE]
|
141 | >
|
142 | > - [imagemin-mozjpeg](https://github.com/imagemin/imagemin-mozjpeg) can be configured in lossless and lossy mode.
|
143 | > - [imagemin-svgo](https://github.com/imagemin/imagemin-svgo) can be configured in lossless and lossy mode.
|
144 |
|
145 | Explore the options to get the best result for you.
|
146 |
|
147 | **Recommended imagemin plugins for lossless optimization**
|
148 |
|
149 | ```shell
|
150 | npm install imagemin-gifsicle imagemin-jpegtran imagemin-optipng imagemin-svgo --save-dev
|
151 | ```
|
152 |
|
153 | **Recommended imagemin plugins for lossy optimization**
|
154 |
|
155 | ```shell
|
156 | npm install imagemin-gifsicle imagemin-mozjpeg imagemin-pngquant imagemin-svgo --save-dev
|
157 | ```
|
158 |
|
159 | For `imagemin-svgo` v9.0.0+ need use svgo [configuration](https://github.com/svg/svgo#configuration)
|
160 |
|
161 | **webpack.config.js**
|
162 |
|
163 | ```js
|
164 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
165 |
|
166 | module.exports = {
|
167 | module: {
|
168 | rules: [
|
169 | {
|
170 | test: /\.(jpe?g|png|gif|svg)$/i,
|
171 | type: "asset",
|
172 | },
|
173 | ],
|
174 | },
|
175 | optimization: {
|
176 | minimizer: [
|
177 | "...",
|
178 | new ImageMinimizerPlugin({
|
179 | minimizer: {
|
180 | implementation: ImageMinimizerPlugin.imageminMinify,
|
181 | options: {
|
182 | // Lossless optimization with custom option
|
183 | // Feel free to experiment with options for better result for you
|
184 | plugins: [
|
185 | ["gifsicle", { interlaced: true }],
|
186 | ["jpegtran", { progressive: true }],
|
187 | ["optipng", { optimizationLevel: 5 }],
|
188 | // Svgo configuration here https://github.com/svg/svgo#configuration
|
189 | [
|
190 | "svgo",
|
191 | {
|
192 | plugins: [
|
193 | {
|
194 | name: "preset-default",
|
195 | params: {
|
196 | overrides: {
|
197 | removeViewBox: false,
|
198 | addAttributesToSVGElement: {
|
199 | params: {
|
200 | attributes: [
|
201 | { xmlns: "http://www.w3.org/2000/svg" },
|
202 | ],
|
203 | },
|
204 | },
|
205 | },
|
206 | },
|
207 | },
|
208 | ],
|
209 | },
|
210 | ],
|
211 | ],
|
212 | },
|
213 | },
|
214 | }),
|
215 | ],
|
216 | },
|
217 | };
|
218 | ```
|
219 |
|
220 | ### **DEPRECATED** Optimize with [`squoosh`](https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh)
|
221 |
|
222 | ```console
|
223 | npm install @squoosh/lib --save-dev
|
224 | ```
|
225 |
|
226 | **Recommended `@squoosh/lib` options for lossy optimization**
|
227 |
|
228 | For lossy optimization we recommend using the default settings of `@squoosh/lib` package.
|
229 | The default values and supported file types for each option can be found in the [codecs.ts](https://github.com/GoogleChromeLabs/squoosh/blob/dev/libsquoosh/src/codecs.ts) file under codecs.
|
230 |
|
231 | **webpack.config.js**
|
232 |
|
233 | ```js
|
234 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
235 |
|
236 | module.exports = {
|
237 | module: {
|
238 | rules: [
|
239 | // You need this, if you are using `import file from "file.ext"`, for `new URL(...)` syntax you don't need it
|
240 | {
|
241 | test: /\.(jpe?g|png)$/i,
|
242 | type: "asset",
|
243 | },
|
244 | ],
|
245 | },
|
246 | optimization: {
|
247 | minimizer: [
|
248 | "...",
|
249 | new ImageMinimizerPlugin({
|
250 | minimizer: {
|
251 | implementation: ImageMinimizerPlugin.squooshMinify,
|
252 | options: {
|
253 | // Your options for `squoosh`
|
254 | },
|
255 | },
|
256 | }),
|
257 | ],
|
258 | },
|
259 | };
|
260 | ```
|
261 |
|
262 | **Recommended `squoosh` options for lossless optimization**
|
263 |
|
264 | For lossless optimization we recommend using the options listed below in `minimizer.options.encodeOptions`.
|
265 |
|
266 | **webpack.config.js**
|
267 |
|
268 | ```js
|
269 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
270 |
|
271 | module.exports = {
|
272 | module: {
|
273 | rules: [
|
274 | // You need this, if you are using `import file from "file.ext"`, for `new URL(...)` syntax you don't need it
|
275 | {
|
276 | test: /\.(jpe?g|png)$/i,
|
277 | type: "asset",
|
278 | },
|
279 | ],
|
280 | },
|
281 | optimization: {
|
282 | minimizer: [
|
283 | new ImageMinimizerPlugin({
|
284 | minimizer: {
|
285 | implementation: ImageMinimizerPlugin.squooshMinify,
|
286 | options: {
|
287 | encodeOptions: {
|
288 | mozjpeg: {
|
289 | // That setting might be close to lossless, but it’s not guaranteed
|
290 | // https://github.com/GoogleChromeLabs/squoosh/issues/85
|
291 | quality: 100,
|
292 | },
|
293 | webp: {
|
294 | lossless: 1,
|
295 | },
|
296 | avif: {
|
297 | // https://github.com/GoogleChromeLabs/squoosh/blob/dev/codecs/avif/enc/README.md
|
298 | cqLevel: 0,
|
299 | },
|
300 | },
|
301 | },
|
302 | },
|
303 | }),
|
304 | ],
|
305 | },
|
306 | };
|
307 | ```
|
308 |
|
309 | ### Optimize with [`sharp`](https://github.com/lovell/sharp)
|
310 |
|
311 | ```console
|
312 | npm install sharp --save-dev
|
313 | ```
|
314 |
|
315 | **Recommended `sharp` options for lossy optimization**
|
316 |
|
317 | For lossy optimization we recommend using the default settings of `sharp` package.
|
318 | The default values and supported file types for each option can be found in the [sharp documentation](https://sharp.pixelplumbing.com/api-output).
|
319 |
|
320 | **webpack.config.js**
|
321 |
|
322 | ```js
|
323 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
324 |
|
325 | module.exports = {
|
326 | module: {
|
327 | rules: [
|
328 | // You need this, if you are using `import file from "file.ext"`, for `new URL(...)` syntax you don't need it
|
329 | {
|
330 | test: /\.(jpe?g|png)$/i,
|
331 | type: "asset",
|
332 | },
|
333 | ],
|
334 | },
|
335 | optimization: {
|
336 | minimizer: [
|
337 | "...",
|
338 | new ImageMinimizerPlugin({
|
339 | minimizer: {
|
340 | implementation: ImageMinimizerPlugin.sharpMinify,
|
341 | options: {
|
342 | encodeOptions: {
|
343 | // Your options for `sharp`
|
344 | // https://sharp.pixelplumbing.com/api-output
|
345 | },
|
346 | },
|
347 | },
|
348 | }),
|
349 | ],
|
350 | },
|
351 | };
|
352 | ```
|
353 |
|
354 | **Recommended `sharp` options for lossless optimization**
|
355 |
|
356 | For lossless optimization we recommend using the options listed below in `minimizer.options.encodeOptions`.
|
357 |
|
358 | **webpack.config.js**
|
359 |
|
360 | ```js
|
361 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
362 |
|
363 | module.exports = {
|
364 | module: {
|
365 | rules: [
|
366 | // You need this, if you are using `import file from "file.ext"`, for `new URL(...)` syntax you don't need it
|
367 | {
|
368 | test: /\.(jpe?g|png)$/i,
|
369 | type: "asset",
|
370 | },
|
371 | ],
|
372 | },
|
373 | optimization: {
|
374 | minimizer: [
|
375 | new ImageMinimizerPlugin({
|
376 | minimizer: {
|
377 | implementation: ImageMinimizerPlugin.sharpMinify,
|
378 | options: {
|
379 | encodeOptions: {
|
380 | jpeg: {
|
381 | // https://sharp.pixelplumbing.com/api-output#jpeg
|
382 | quality: 100,
|
383 | },
|
384 | webp: {
|
385 | // https://sharp.pixelplumbing.com/api-output#webp
|
386 | lossless: true,
|
387 | },
|
388 | avif: {
|
389 | // https://sharp.pixelplumbing.com/api-output#avif
|
390 | lossless: true,
|
391 | },
|
392 |
|
393 | // png by default sets the quality to 100%, which is same as lossless
|
394 | // https://sharp.pixelplumbing.com/api-output#png
|
395 | png: {},
|
396 |
|
397 | // gif does not support lossless compression at all
|
398 | // https://sharp.pixelplumbing.com/api-output#gif
|
399 | gif: {},
|
400 | },
|
401 | },
|
402 | },
|
403 | }),
|
404 | ],
|
405 | },
|
406 | };
|
407 | ```
|
408 |
|
409 | ### Optimize with [`svgo`](https://github.com/svg/svgo)
|
410 |
|
411 | ```console
|
412 | npm install svgo --save-dev
|
413 | ```
|
414 |
|
415 | **Recommended `svgo` options for optimization**
|
416 |
|
417 | For optimization we recommend using the options listed below in `minimizer.options.encodeOptions`.
|
418 | The default values for plugins can be found in the [svgo plugins source code](https://github.com/svg/svgo/tree/main/plugins).
|
419 |
|
420 | **webpack.config.js**
|
421 |
|
422 | ```js
|
423 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
424 |
|
425 | module.exports = {
|
426 | module: {
|
427 | rules: [
|
428 | // You need this, if you are using `import file from "file.ext"`, for `new URL(...)` syntax you don't need it
|
429 | {
|
430 | test: /\.(svg)$/i,
|
431 | type: "asset",
|
432 | },
|
433 | ],
|
434 | },
|
435 | optimization: {
|
436 | minimizer: [
|
437 | "...",
|
438 | new ImageMinimizerPlugin({
|
439 | minimizer: {
|
440 | implementation: ImageMinimizerPlugin.svgoMinify,
|
441 | options: {
|
442 | encodeOptions: {
|
443 | // Pass over SVGs multiple times to ensure all optimizations are applied. False by default
|
444 | multipass: true,
|
445 | plugins: [
|
446 | // set of built-in plugins enabled by default
|
447 | // see: https://github.com/svg/svgo#default-preset
|
448 | "preset-default",
|
449 | ],
|
450 | },
|
451 | },
|
452 | },
|
453 | }),
|
454 | ],
|
455 | },
|
456 | };
|
457 | ```
|
458 |
|
459 | ### Advanced setup
|
460 |
|
461 | If you want to use `loader` or `plugin` standalone see sections below, but this is **not recommended**.
|
462 |
|
463 | By default, plugin configures `loader` (please use the `loader` option if you want to disable this behaviour), therefore you should not setup standalone loader when you use a plugin setup.
|
464 |
|
465 | Loader optimizes or generates images using options, so inlined images via `data` URI (i.e. `data:`) will be optimized or generated too, not inlined images will be optimized too.
|
466 |
|
467 | #### Query Parameters (only `squoosh` and `sharp` currently)
|
468 |
|
469 | The plugin supports the following query parameters:
|
470 |
|
471 | - `width`/`w` - allows you to set the image width
|
472 | - `height`/`h` - allows you to set the image height
|
473 | - `as` - to specify the [preset](#preset) option
|
474 |
|
475 | **Only supported for `sharp` currently:**
|
476 |
|
477 | - `unit`/`u` - can be `px` or `percent` and allows you to resize by a percentage of the image's size.
|
478 |
|
479 | Examples:
|
480 |
|
481 | ```js
|
482 | const myImage1 = new URL("image.png?width=150&height=120", import.meta.url);
|
483 | const myImage2 = new URL("image.png?w=150&h=120", import.meta.url);
|
484 | // You can omit one of the parameters to auto-scale
|
485 | const myImage3 = new URL("image.png?w=150", import.meta.url);
|
486 | // It works with the `preset` query parameter
|
487 | const myImage4 = new URL("image.png?as=webp&w=150&h=120", import.meta.url);
|
488 | // You can use `auto` to reset `width` or `height` from the `preset` option
|
489 | const myImage5 = new URL("image.png?as=webp&w=150&h=auto", import.meta.url);
|
490 | // You can use `unit` to get the non-retina resize of images that are retina sized
|
491 | const myImage6 = new URL("image.png?width=50&unit=percent", import.meta.url);
|
492 | ```
|
493 |
|
494 | ```css
|
495 | .class {
|
496 | background: url("./image.png?width=150&height=120");
|
497 | }
|
498 | ```
|
499 |
|
500 | ```html
|
501 | <picture>
|
502 | <source srcset="photo.jpg?as=avif&width=150&height=120" type="image/avif" />
|
503 | <source srcset="photo.jpg?as=webp&width=150&height=120" type="image/webp" />
|
504 | <img src="photo.jpg?width=150&height=120" alt="photo" />
|
505 | </picture>
|
506 | ```
|
507 |
|
508 | > [!NOTE]
|
509 | >
|
510 | > You need to setup `avif` and `webp` presets, [example for webp](#optimize-and-generate-webp-images).
|
511 |
|
512 | #### Standalone Loader
|
513 |
|
514 | [Documentation: Using loaders](https://webpack.js.org/concepts/loaders/).
|
515 |
|
516 | In your `webpack.config.js`, add the `ImageMinimizerPlugin.loader` and specify the [asset modules options](https://webpack.js.org/guides/asset-modules/) (if you use images in `import`):
|
517 |
|
518 | **webpack.config.js**
|
519 |
|
520 | ```js
|
521 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
522 |
|
523 | module.exports = {
|
524 | module: {
|
525 | rules: [
|
526 | // You need this, if you are using `import file from "file.ext"`, for `new URL(...)` syntax you don't need it
|
527 | {
|
528 | test: /\.(jpe?g|png|gif|svg)$/i,
|
529 | type: "asset",
|
530 | },
|
531 | // We recommend using only for the "production" mode
|
532 | {
|
533 | test: /\.(jpe?g|png|gif|svg)$/i,
|
534 | use: [
|
535 | {
|
536 | loader: ImageMinimizerPlugin.loader,
|
537 | enforce: "pre",
|
538 | options: {
|
539 | minimizer: {
|
540 | implementation: ImageMinimizerPlugin.imageminMinify,
|
541 | options: {
|
542 | plugins: [
|
543 | "imagemin-gifsicle",
|
544 | "imagemin-mozjpeg",
|
545 | "imagemin-pngquant",
|
546 | "imagemin-svgo",
|
547 | ],
|
548 | },
|
549 | },
|
550 | },
|
551 | },
|
552 | ],
|
553 | },
|
554 | ],
|
555 | },
|
556 | };
|
557 | ```
|
558 |
|
559 | #### Standalone Plugin
|
560 |
|
561 | [Documentation: Using plugins](https://webpack.js.org/concepts/plugins/).
|
562 |
|
563 | **webpack.config.js**
|
564 |
|
565 | ```js
|
566 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
567 |
|
568 | module.exports = {
|
569 | module: {
|
570 | rules: [
|
571 | // You need this, if you are using `import file from "file.ext"`, for `new URL(...)` syntax you don't need it
|
572 | {
|
573 | test: /\.(jpe?g|png|gif|svg)$/i,
|
574 | type: "asset",
|
575 | },
|
576 | ],
|
577 | },
|
578 | optimization: {
|
579 | minimizer: [
|
580 | // Extend default minimizer, i.e. `terser-webpack-plugin` for JS
|
581 | "...",
|
582 | // We recommend using only for the "production" mode
|
583 | new ImageMinimizerPlugin({
|
584 | minimizer: {
|
585 | implementation: ImageMinimizerPlugin.imageminMinify,
|
586 | options: {
|
587 | plugins: [
|
588 | "imagemin-gifsicle",
|
589 | "imagemin-mozjpeg",
|
590 | "imagemin-pngquant",
|
591 | "imagemin-svgo",
|
592 | ],
|
593 | },
|
594 | },
|
595 | // Disable `loader`
|
596 | loader: false,
|
597 | }),
|
598 | ],
|
599 | },
|
600 | };
|
601 | ```
|
602 |
|
603 | ## Plugin Options
|
604 |
|
605 | - **[`test`](#test)**
|
606 | - **[`include`](#include)**
|
607 | - **[`exclude`](#exclude)**
|
608 | - **[`minimizer`](#minimizer)**
|
609 | - **[`generator`](#generator)**
|
610 | - **[`severityError`](#severityerror)**
|
611 | - **[`loader`](#loader)**
|
612 | - **[`concurrency`](#concurrency)**
|
613 | - **[`deleteOriginalAssets`](#deleteoriginalassets)**
|
614 |
|
615 | ### `test`
|
616 |
|
617 | Type:
|
618 |
|
619 | ```ts
|
620 | type test = string | RegExp | Array<string | RegExp>;
|
621 | ```
|
622 |
|
623 | Default: `/\.(jpe?g\|png\|gif\|tif\|webp\|svg\|avif)\$/i`
|
624 |
|
625 | Test to match files against.
|
626 |
|
627 | **webpack.config.js**
|
628 |
|
629 | ```js
|
630 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
631 |
|
632 | module.exports = {
|
633 | optimization: {
|
634 | minimizer: [
|
635 | "...",
|
636 | new ImageMinimizerPlugin({
|
637 | test: /\.(jpe?g|png|gif|svg)$/i,
|
638 | }),
|
639 | ],
|
640 | },
|
641 | };
|
642 | ```
|
643 |
|
644 | ### `include`
|
645 |
|
646 | Type:
|
647 |
|
648 | ```ts
|
649 | type include = string | RegExp | Array<string | RegExp>;
|
650 | ```
|
651 |
|
652 | Default: `undefined`
|
653 |
|
654 | Files to include.
|
655 |
|
656 | **webpack.config.js**
|
657 |
|
658 | ```js
|
659 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
660 |
|
661 | module.exports = {
|
662 | optimization: {
|
663 | minimizer: [
|
664 | "...",
|
665 | new ImageMinimizerPlugin({
|
666 | include: /\/includes/,
|
667 | }),
|
668 | ],
|
669 | },
|
670 | };
|
671 | ```
|
672 |
|
673 | ### `exclude`
|
674 |
|
675 | Type:
|
676 |
|
677 | ```ts
|
678 | type exclude = string | RegExp | Array<string | RegExp>;
|
679 | ```
|
680 |
|
681 | Default: `undefined`
|
682 |
|
683 | Files to exclude.
|
684 |
|
685 | **webpack.config.js**
|
686 |
|
687 | ```js
|
688 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
689 |
|
690 | module.exports = {
|
691 | optimization: {
|
692 | minimizer: [
|
693 | "...",
|
694 | new ImageMinimizerPlugin({
|
695 | exclude: /\/excludes/,
|
696 | }),
|
697 | ],
|
698 | },
|
699 | };
|
700 | ```
|
701 |
|
702 | ### `minimizer`
|
703 |
|
704 | Type:
|
705 |
|
706 | ```ts
|
707 | type minimizer =
|
708 | | {
|
709 | implementation: (
|
710 | original: {
|
711 | filename: string;
|
712 | data: Buffer;
|
713 | warnings: Array<Error>;
|
714 | errors: Array<Error>;
|
715 | info: import("webpack").AssetInfo;
|
716 | },
|
717 | options?:
|
718 | | {
|
719 | [key: string]: any;
|
720 | }
|
721 | | undefined,
|
722 | ) => Promise<{
|
723 | filename: string;
|
724 | data: Buffer;
|
725 | warnings: Array<Error>;
|
726 | errors: Array<Error>;
|
727 | info: import("webpack").AssetInfo;
|
728 | }> & {
|
729 | setup?: (() => void) | undefined;
|
730 | teardown?: (() => void) | undefined;
|
731 | };
|
732 | options?:
|
733 | | {
|
734 | [key: string]: any;
|
735 | }
|
736 | | undefined;
|
737 | filter?: (source: Buffer, sourcePath: string) => boolean | undefined;
|
738 | filename?:
|
739 | | string
|
740 | | ((
|
741 | pathData: {
|
742 | filename?: string | undefined;
|
743 | },
|
744 | assetInfo?: import("webpack").AssetInfo | undefined,
|
745 | ) => string)
|
746 | | undefined;
|
747 | }
|
748 | | Array<{
|
749 | implementation: (
|
750 | original: {
|
751 | filename: string;
|
752 | data: Buffer;
|
753 | warnings: Array<Error>;
|
754 | errors: Array<Error>;
|
755 | info: import("webpack").AssetInfo;
|
756 | },
|
757 | options?:
|
758 | | {
|
759 | [key: string]: any;
|
760 | }
|
761 | | undefined,
|
762 | ) => Promise<{
|
763 | filename: string;
|
764 | data: Buffer;
|
765 | warnings: Array<Error>;
|
766 | errors: Array<Error>;
|
767 | info: import("webpack").AssetInfo;
|
768 | }> & {
|
769 | setup?: (() => void) | undefined;
|
770 | teardown?: (() => void) | undefined;
|
771 | };
|
772 | options?:
|
773 | | {
|
774 | [key: string]: any;
|
775 | }
|
776 | | undefined;
|
777 | filter?: (source: Buffer, sourcePath: string) => boolean | undefined;
|
778 | filename?:
|
779 | | string
|
780 | | ((
|
781 | pathData: {
|
782 | filename?: string | undefined;
|
783 | },
|
784 | assetInfo?: import("webpack").AssetInfo | undefined,
|
785 | ) => string)
|
786 | | undefined;
|
787 | }>;
|
788 | ```
|
789 |
|
790 | Default: `undefined`
|
791 |
|
792 | Allows to setup default minify function.
|
793 |
|
794 | #### Available minimizers
|
795 |
|
796 | - `ImageMinimizerPlugin.imageminMinify`
|
797 | - **DEPRECATED** `ImageMinimizerPlugin.squooshMinify`
|
798 | - `ImageMinimizerPlugin.sharpMinify`
|
799 | - `ImageMinimizerPlugin.svgoMinify`
|
800 |
|
801 | #### Single minimizer example for `imagemin`
|
802 |
|
803 | **webpack.config.js**
|
804 |
|
805 | ```js
|
806 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
807 |
|
808 | module.exports = {
|
809 | optimization: {
|
810 | minimizer: [
|
811 | "...",
|
812 | new ImageMinimizerPlugin({
|
813 | minimizer: {
|
814 | // Implementation
|
815 | implementation: ImageMinimizerPlugin.imageminMinify,
|
816 | // Options
|
817 | options: {
|
818 | plugins: [
|
819 | "imagemin-gifsicle",
|
820 | "imagemin-mozjpeg",
|
821 | "imagemin-pngquant",
|
822 | "imagemin-svgo",
|
823 | ],
|
824 | },
|
825 | },
|
826 | }),
|
827 | ],
|
828 | },
|
829 | };
|
830 | ```
|
831 |
|
832 | More information and examples [here](https://github.com/imagemin/imagemin).
|
833 |
|
834 | #### **DEPRECATED** Single minimizer example for `squoosh`
|
835 |
|
836 | **webpack.config.js**
|
837 |
|
838 | ```js
|
839 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
840 |
|
841 | module.exports = {
|
842 | optimization: {
|
843 | minimizer: [
|
844 | "...",
|
845 | new ImageMinimizerPlugin({
|
846 | minimizer: {
|
847 | // Implementation
|
848 | implementation: ImageMinimizerPlugin.squooshMinify,
|
849 | // Options
|
850 | options: {
|
851 | encodeOptions: {
|
852 | mozjpeg: {
|
853 | quality: 90,
|
854 | },
|
855 | },
|
856 | },
|
857 | },
|
858 | }),
|
859 | ],
|
860 | },
|
861 | };
|
862 | ```
|
863 |
|
864 | More information and examples [here](https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh).
|
865 |
|
866 | #### Single minimizer example for `sharp`
|
867 |
|
868 | **webpack.config.js**
|
869 |
|
870 | ```js
|
871 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
872 |
|
873 | module.exports = {
|
874 | optimization: {
|
875 | minimizer: [
|
876 | "...",
|
877 | new ImageMinimizerPlugin({
|
878 | minimizer: {
|
879 | // Implementation
|
880 | implementation: ImageMinimizerPlugin.sharpMinify,
|
881 | // Options
|
882 | options: {
|
883 | encodeOptions: {
|
884 | jpeg: {
|
885 | quality: 90,
|
886 | },
|
887 | },
|
888 | },
|
889 | },
|
890 | }),
|
891 | ],
|
892 | },
|
893 | };
|
894 | ```
|
895 |
|
896 | More information and examples [here](https://sharp.pixelplumbing.com/api-output#toformat).
|
897 |
|
898 | #### Single minimizer example for user defined implementation
|
899 |
|
900 | **webpack.config.js**
|
901 |
|
902 | ```js
|
903 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
904 |
|
905 | module.exports = {
|
906 | optimization: {
|
907 | minimizer: [
|
908 | "...",
|
909 | new ImageMinimizerPlugin({
|
910 | minimizer: {
|
911 | implementation: async (original, options) => {
|
912 | const inputExt = path.extname(original.filename).toLowerCase();
|
913 |
|
914 | if (inputExt !== ".xxx") {
|
915 | // Return `null` if the implementation does not support this file type
|
916 | return null;
|
917 | }
|
918 |
|
919 | let result;
|
920 |
|
921 | try {
|
922 | result = await minifyAndReturnBuffer(original.data);
|
923 | } catch (error) {
|
924 | // Store error and return `null` if there was an error
|
925 | original.errors.push(error);
|
926 | return null;
|
927 | }
|
928 |
|
929 | return {
|
930 | filename: original.filename,
|
931 | data: result,
|
932 | warnings: [...original.warnings],
|
933 | errors: [...original.errors],
|
934 | info: {
|
935 | ...original.info,
|
936 | // Please always set it to prevent double minification
|
937 | minimized: true,
|
938 | // Optional
|
939 | minimizedBy: ["custom-name-of-minimication"],
|
940 | },
|
941 | };
|
942 | },
|
943 | options: {
|
944 | // Custom options
|
945 | },
|
946 | },
|
947 | }),
|
948 | ],
|
949 | },
|
950 | };
|
951 | ```
|
952 |
|
953 | #### Multiple minimizers example
|
954 |
|
955 | Allows to setup multiple minimizers.
|
956 |
|
957 | **webpack.config.js**
|
958 |
|
959 | ```js
|
960 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
961 |
|
962 | module.exports = {
|
963 | optimization: {
|
964 | minimizer: [
|
965 | "...",
|
966 | new ImageMinimizerPlugin({
|
967 | minimizer: [
|
968 | {
|
969 | // `sharp` will handle all bitmap formats (JPG, PNG, GIF, ...)
|
970 | implementation: ImageMinimizerPlugin.sharpMinify,
|
971 |
|
972 | // exclude SVG if implementation support it. Not required for `sharp`.
|
973 | // filter: (source, sourcePath) => !(/\.(svg)$/i.test(sourcePath)),
|
974 |
|
975 | options: {
|
976 | encodeOptions: {
|
977 | // Your options for `sharp`
|
978 | // https://sharp.pixelplumbing.com/api-output
|
979 | },
|
980 | },
|
981 | },
|
982 | {
|
983 | // `svgo` will handle vector images (SVG)
|
984 | implementation: ImageMinimizerPlugin.svgoMinify,
|
985 | options: {
|
986 | encodeOptions: {
|
987 | // Pass over SVGs multiple times to ensure all optimizations are applied. False by default
|
988 | multipass: true,
|
989 | plugins: [
|
990 | // set of built-in plugins enabled by default
|
991 | // see: https://github.com/svg/svgo#default-preset
|
992 | "preset-default",
|
993 | ],
|
994 | },
|
995 | },
|
996 | },
|
997 | ],
|
998 | }),
|
999 | ],
|
1000 | },
|
1001 | };
|
1002 | ```
|
1003 |
|
1004 | #### Minimizer options
|
1005 |
|
1006 | ##### `implementation`
|
1007 |
|
1008 | Type:
|
1009 |
|
1010 | ```ts
|
1011 | type implementation = (
|
1012 | original: {
|
1013 | filename: string;
|
1014 | data: Buffer;
|
1015 | warnings: Array<Error>;
|
1016 | errors: Array<Error>;
|
1017 | info: import("webpack").AssetInfo;
|
1018 | },
|
1019 | options?: BasicTransformerOptions<T>,
|
1020 | ) => Promise<{
|
1021 | filename: string;
|
1022 | data: Buffer;
|
1023 | warnings: Array<Error>;
|
1024 | errors: Array<Error>;
|
1025 | info: import("webpack").AssetInfo;
|
1026 | }> & {
|
1027 | setup?: (() => void) | undefined;
|
1028 | teardown?: (() => void) | undefined;
|
1029 | };
|
1030 | ```
|
1031 |
|
1032 | Default: `undefined`
|
1033 |
|
1034 | Configure the default `implementation`.
|
1035 |
|
1036 | **webpack.config.js**
|
1037 |
|
1038 | ```js
|
1039 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1040 |
|
1041 | module.exports = {
|
1042 | optimization: {
|
1043 | minimizer: [
|
1044 | "...",
|
1045 | new ImageMinimizerPlugin({
|
1046 | minimizer: {
|
1047 | // Implementation
|
1048 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1049 | // Options
|
1050 | options: {
|
1051 | encodeOptions: {
|
1052 | jpeg: {
|
1053 | quality: 90,
|
1054 | },
|
1055 | },
|
1056 | },
|
1057 | },
|
1058 | }),
|
1059 | ],
|
1060 | },
|
1061 | };
|
1062 | ```
|
1063 |
|
1064 | ##### `options`
|
1065 |
|
1066 | Type:
|
1067 |
|
1068 | ```ts
|
1069 | type options = {
|
1070 | [key: string]: any;
|
1071 | };
|
1072 | ```
|
1073 |
|
1074 | Default: `undefined`
|
1075 |
|
1076 | Options for the `implementation` option (i.e. options for `imagemin`/`squoosh`/`sharp`/custom implementation).
|
1077 |
|
1078 | **webpack.config.js**
|
1079 |
|
1080 | ```js
|
1081 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1082 |
|
1083 | module.exports = {
|
1084 | optimization: {
|
1085 | minimizer: [
|
1086 | "...",
|
1087 | new ImageMinimizerPlugin({
|
1088 | minimizer: {
|
1089 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1090 | // Options
|
1091 | options: {
|
1092 | encodeOptions: {
|
1093 | jpeg: {
|
1094 | quality: 90,
|
1095 | },
|
1096 | },
|
1097 | },
|
1098 | },
|
1099 | }),
|
1100 | ],
|
1101 | },
|
1102 | };
|
1103 | ```
|
1104 |
|
1105 | ##### `filter`
|
1106 |
|
1107 | Type:
|
1108 |
|
1109 | ```ts
|
1110 | type filter = (source: Buffer, sourcePath: string) => boolean | undefined;
|
1111 | ```
|
1112 |
|
1113 | Default: `() => true`
|
1114 |
|
1115 | Allows filtering of images for optimization/generation.
|
1116 |
|
1117 | Return `true` to optimize the image, `false` otherwise.
|
1118 |
|
1119 | **webpack.config.js**
|
1120 |
|
1121 | ```js
|
1122 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1123 |
|
1124 | module.exports = {
|
1125 | optimization: {
|
1126 | minimizer: [
|
1127 | "...",
|
1128 | new ImageMinimizerPlugin({
|
1129 | minimizer: {
|
1130 | filter: (source, sourcePath) => {
|
1131 | // The `source` argument is a `Buffer` of source file
|
1132 | // The `sourcePath` argument is an absolute path to source
|
1133 | if (source.byteLength < 8192) {
|
1134 | return false;
|
1135 | }
|
1136 |
|
1137 | return true;
|
1138 | },
|
1139 | implementation: ImageMinimizerPlugin.imageminMinify,
|
1140 | options: {
|
1141 | plugins: [
|
1142 | "imagemin-gifsicle",
|
1143 | "imagemin-mozjpeg",
|
1144 | "imagemin-pngquant",
|
1145 | "imagemin-svgo",
|
1146 | ],
|
1147 | },
|
1148 | },
|
1149 | }),
|
1150 | ],
|
1151 | },
|
1152 | };
|
1153 | ```
|
1154 |
|
1155 | ##### `filename`
|
1156 |
|
1157 | Type:
|
1158 |
|
1159 | ```ts
|
1160 | type filename =
|
1161 | | string
|
1162 | | ((
|
1163 | pathData: {
|
1164 | filename?: string | undefined;
|
1165 | },
|
1166 | assetInfo?: import("webpack").AssetInfo | undefined,
|
1167 | ) => string)
|
1168 | | undefined;
|
1169 | ```
|
1170 |
|
1171 | Default: `undefined`
|
1172 |
|
1173 | Allows to set the filename.
|
1174 | Supported values see in [`webpack template strings`](https://webpack.js.org/configuration/output/#template-strings), `File-level` section.
|
1175 |
|
1176 | We also support `[width]` and `[height]` placeholders (only `sharp` and `squoosh`).
|
1177 |
|
1178 | **webpack.config.js**
|
1179 |
|
1180 | ```js
|
1181 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1182 |
|
1183 | module.exports = {
|
1184 | optimization: {
|
1185 | minimizer: [
|
1186 | "...",
|
1187 | new ImageMinimizerPlugin({
|
1188 | minimizer: {
|
1189 | filename: "optimized-[name][ext]",
|
1190 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1191 | // Options
|
1192 | options: {
|
1193 | encodeOptions: {
|
1194 | jpeg: {
|
1195 | quality: 90,
|
1196 | },
|
1197 | },
|
1198 | },
|
1199 | },
|
1200 | }),
|
1201 | ],
|
1202 | },
|
1203 | };
|
1204 | ```
|
1205 |
|
1206 | Example `function` usage:
|
1207 |
|
1208 | **webpack.config.js**
|
1209 |
|
1210 | ```js
|
1211 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1212 |
|
1213 | module.exports = {
|
1214 | optimization: {
|
1215 | minimizer: [
|
1216 | "...",
|
1217 | new ImageMinimizerPlugin({
|
1218 | minimizer: {
|
1219 | filename: () => "optimized-[name][ext]",
|
1220 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1221 | // Options
|
1222 | options: {
|
1223 | encodeOptions: {
|
1224 | jpeg: {
|
1225 | quality: 90,
|
1226 | },
|
1227 | },
|
1228 | },
|
1229 | },
|
1230 | }),
|
1231 | ],
|
1232 | },
|
1233 | };
|
1234 | ```
|
1235 |
|
1236 | ### `generator`
|
1237 |
|
1238 | Type:
|
1239 |
|
1240 | ```ts
|
1241 | type generator = Array<{
|
1242 | implementation: (
|
1243 | original: {
|
1244 | filename: string;
|
1245 | data: Buffer;
|
1246 | warnings: Array<Error>;
|
1247 | errors: Array<Error>;
|
1248 | info: import("webpack").AssetInfo;
|
1249 | },
|
1250 | options?:
|
1251 | | {
|
1252 | [key: string]: any;
|
1253 | }
|
1254 | | undefined,
|
1255 | ) => Promise<{
|
1256 | filename: string;
|
1257 | data: Buffer;
|
1258 | warnings: Array<Error>;
|
1259 | errors: Array<Error>;
|
1260 | info: import("webpack").AssetInfo;
|
1261 | }> & {
|
1262 | setup?: (() => void) | undefined;
|
1263 | teardown?: (() => void) | undefined;
|
1264 | };
|
1265 | options?:
|
1266 | | {
|
1267 | [key: string]: any;
|
1268 | }
|
1269 | | undefined;
|
1270 | filter?: (source: Buffer, sourcePath: string) => boolean | undefined;
|
1271 | filename?:
|
1272 | | string
|
1273 | | ((
|
1274 | pathData: {
|
1275 | filename?: string | undefined;
|
1276 | },
|
1277 | assetInfo?: import("webpack").AssetInfo | undefined,
|
1278 | ) => string)
|
1279 | | undefined;
|
1280 | preset?: string | undefined;
|
1281 | type?: "import" | "asset" | undefined;
|
1282 | }>;
|
1283 | ```
|
1284 |
|
1285 | Default: `undefined`
|
1286 |
|
1287 | Allow to setup default generators.
|
1288 | Useful if you need generate `webp`/`avif`/etc from other formats.
|
1289 |
|
1290 | > [!WARNING]
|
1291 | >
|
1292 | > If no generator was found for the image (i.e. no `?as=webp` was found in query params), the `minimizer` option will be used. Therefore, it is recommended to configure generator outputs optimized image.
|
1293 |
|
1294 | > [!WARNING]
|
1295 | >
|
1296 | > The option will not work if you disable `loader` (i.e. set the `loader` option to `false`).
|
1297 |
|
1298 | #### Available generators
|
1299 |
|
1300 | - `ImageMinimizerPlugin.imageminGenerate`
|
1301 | - **DEPRECATED** `ImageMinimizerPlugin.squooshGenerate`
|
1302 | - `ImageMinimizerPlugin.sharpGenerate`
|
1303 |
|
1304 | #### Generator example for `imagemin`
|
1305 |
|
1306 | Example `webp` generator:
|
1307 |
|
1308 | **webpack.config.js**
|
1309 |
|
1310 | ```js
|
1311 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1312 |
|
1313 | module.exports = {
|
1314 | optimization: {
|
1315 | minimizer: [
|
1316 | "...",
|
1317 | new ImageMinimizerPlugin({
|
1318 | generator: [
|
1319 | {
|
1320 | // You can apply generator using `?as=webp`, you can use any name and provide more options
|
1321 | preset: "webp",
|
1322 | implementation: ImageMinimizerPlugin.imageminGenerate,
|
1323 | options: {
|
1324 | // Please specify only one plugin here, multiple plugins will not work
|
1325 | plugins: ["imagemin-webp"],
|
1326 | },
|
1327 | },
|
1328 | ],
|
1329 | }),
|
1330 | ],
|
1331 | },
|
1332 | };
|
1333 | ```
|
1334 |
|
1335 | #### **DEPRECATED** Generator example for `squoosh`
|
1336 |
|
1337 | **webpack.config.js**
|
1338 |
|
1339 | ```js
|
1340 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1341 |
|
1342 | module.exports = {
|
1343 | optimization: {
|
1344 | minimizer: [
|
1345 | "...",
|
1346 | new ImageMinimizerPlugin({
|
1347 | generator: [
|
1348 | {
|
1349 | // You can apply generator using `?as=webp`, you can use any name and provide more options
|
1350 | preset: "webp",
|
1351 | implementation: ImageMinimizerPlugin.squooshGenerate,
|
1352 | options: {
|
1353 | encodeOptions: {
|
1354 | // Please specify only one codec here, multiple codecs will not work
|
1355 | webp: {
|
1356 | quality: 90,
|
1357 | },
|
1358 | },
|
1359 | },
|
1360 | },
|
1361 | ],
|
1362 | }),
|
1363 | ],
|
1364 | },
|
1365 | };
|
1366 | ```
|
1367 |
|
1368 | #### Generator example for `sharp`
|
1369 |
|
1370 | **webpack.config.js**
|
1371 |
|
1372 | ```js
|
1373 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1374 |
|
1375 | module.exports = {
|
1376 | optimization: {
|
1377 | minimizer: [
|
1378 | "...",
|
1379 | new ImageMinimizerPlugin({
|
1380 | generator: [
|
1381 | {
|
1382 | // You can apply generator using `?as=webp`, you can use any name and provide more options
|
1383 | preset: "webp",
|
1384 | implementation: ImageMinimizerPlugin.sharpGenerate,
|
1385 | options: {
|
1386 | encodeOptions: {
|
1387 | // Please specify only one codec here, multiple codecs will not work
|
1388 | webp: {
|
1389 | quality: 90,
|
1390 | },
|
1391 | },
|
1392 | },
|
1393 | },
|
1394 | ],
|
1395 | }),
|
1396 | ],
|
1397 | },
|
1398 | };
|
1399 | ```
|
1400 |
|
1401 | Now you can generate the new image using:
|
1402 |
|
1403 | ```js
|
1404 | // Old approach for getting URL
|
1405 | import webp from "./file.jpg?as=webp";
|
1406 |
|
1407 | // Assets modules
|
1408 | console.log(new URL("./file.jpg?as=webp"));
|
1409 | ```
|
1410 |
|
1411 | ```css
|
1412 | div {
|
1413 | background: url("./file.jpg?as=webp");
|
1414 | }
|
1415 | ```
|
1416 |
|
1417 | You can use `?as=webp` in any type of files.
|
1418 |
|
1419 | Example multiple generators:
|
1420 |
|
1421 | **webpack.config.js**
|
1422 |
|
1423 | ```js
|
1424 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1425 |
|
1426 | module.exports = {
|
1427 | optimization: {
|
1428 | minimizer: [
|
1429 | "...",
|
1430 | new ImageMinimizerPlugin({
|
1431 | generator: [
|
1432 | {
|
1433 | // You can apply generator using `?as=webp`, you can use any name and provide more options
|
1434 | preset: "webp",
|
1435 | implementation: ImageMinimizerPlugin.sharpGenerate,
|
1436 | options: {
|
1437 | encodeOptions: {
|
1438 | webp: {
|
1439 | lossless: false,
|
1440 | },
|
1441 | },
|
1442 | },
|
1443 | },
|
1444 | {
|
1445 | // You can apply generator using `?as=avif`, you can use any name and provide more options
|
1446 | preset: "avif",
|
1447 | implementation: ImageMinimizerPlugin.sharpGenerate,
|
1448 | options: {
|
1449 | encodeOptions: {
|
1450 | avif: {
|
1451 | lossless: false,
|
1452 | },
|
1453 | },
|
1454 | },
|
1455 | },
|
1456 | ],
|
1457 | }),
|
1458 | ],
|
1459 | },
|
1460 | };
|
1461 | ```
|
1462 |
|
1463 | `squoosh` and `sharp` generator supports more options, for example you can resize an image:
|
1464 |
|
1465 | **webpack.config.js**
|
1466 |
|
1467 | ```js
|
1468 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1469 |
|
1470 | module.exports = {
|
1471 | optimization: {
|
1472 | minimizer: [
|
1473 | "...",
|
1474 | new ImageMinimizerPlugin({
|
1475 | generator: [
|
1476 | {
|
1477 | // You can apply generator using `?as=webp-100-50`, you can use any name and provide more options
|
1478 | preset: "webp-100-50",
|
1479 | // implementation: ImageMinimizerPlugin.squooshGenerate,
|
1480 | implementation: ImageMinimizerPlugin.sharpGenerate,
|
1481 | options: {
|
1482 | resize: {
|
1483 | enabled: true,
|
1484 | width: 100,
|
1485 | height: 50,
|
1486 | },
|
1487 | encodeOptions: {
|
1488 | webp: {
|
1489 | quality: 90,
|
1490 | },
|
1491 | },
|
1492 | },
|
1493 | },
|
1494 | ],
|
1495 | }),
|
1496 | ],
|
1497 | },
|
1498 | };
|
1499 | ```
|
1500 |
|
1501 | You can find more information [here](https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh).
|
1502 |
|
1503 | For only `sharp` currently, you can even generate the non-retina resizes of images:
|
1504 |
|
1505 | **webpack.config.js**
|
1506 |
|
1507 | ```js
|
1508 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1509 |
|
1510 | module.exports = {
|
1511 | optimization: {
|
1512 | minimizer: [
|
1513 | "...",
|
1514 | new ImageMinimizerPlugin({
|
1515 | generator: [
|
1516 | {
|
1517 | // You can apply generator using `?as=webp-1x`, you can use any name and provide more options
|
1518 | preset: "webp-1x",
|
1519 | implementation: ImageMinimizerPlugin.sharpGenerate,
|
1520 | options: {
|
1521 | resize: {
|
1522 | enabled: true,
|
1523 | width: 50,
|
1524 | unit: "percent",
|
1525 | },
|
1526 | encodeOptions: {
|
1527 | webp: {
|
1528 | quality: 90,
|
1529 | },
|
1530 | },
|
1531 | },
|
1532 | },
|
1533 | ],
|
1534 | }),
|
1535 | ],
|
1536 | },
|
1537 | };
|
1538 | ```
|
1539 |
|
1540 | #### Generator example for user defined implementation
|
1541 |
|
1542 | You can use your own generator implementation.
|
1543 |
|
1544 | **webpack.config.js**
|
1545 |
|
1546 | ```js
|
1547 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1548 |
|
1549 | module.exports = {
|
1550 | optimization: {
|
1551 | minimizer: [
|
1552 | "...",
|
1553 | new ImageMinimizerPlugin({
|
1554 | generator: [
|
1555 | {
|
1556 | // You can apply generator using `?as=webp`, you can use any name and provide more options
|
1557 | preset: "webp",
|
1558 | implementation: async (original, options) => {
|
1559 | const inputExt = path.extname(original.filename).toLowerCase();
|
1560 |
|
1561 | if (inputExt !== ".xxx") {
|
1562 | // Store error and return `null` if the implementation does not support this file type
|
1563 | original.errors.push(error);
|
1564 | return null;
|
1565 | }
|
1566 |
|
1567 | let result;
|
1568 |
|
1569 | try {
|
1570 | result = await minifyAndReturnBuffer(original.data);
|
1571 | } catch (error) {
|
1572 | // Store error and return `null` if there was an error
|
1573 | original.errors.push(error);
|
1574 | return null;
|
1575 | }
|
1576 |
|
1577 | return {
|
1578 | filename: original.filename,
|
1579 | data: result,
|
1580 | warnings: [...original.warnings],
|
1581 | errors: [...original.errors],
|
1582 | info: {
|
1583 | ...original.info,
|
1584 | // Please always set it to prevent double minification
|
1585 | generated: true,
|
1586 | // Optional
|
1587 | generatedBy: ["custom-name-of-minification"],
|
1588 | },
|
1589 | };
|
1590 | },
|
1591 | options: {
|
1592 | // Your options
|
1593 | },
|
1594 | },
|
1595 | ],
|
1596 | }),
|
1597 | ],
|
1598 | },
|
1599 | };
|
1600 | ```
|
1601 |
|
1602 | #### Generator options
|
1603 |
|
1604 | ##### `type`
|
1605 |
|
1606 | Type:
|
1607 |
|
1608 | ```ts
|
1609 | type type = "import" | "asset" | undefined;
|
1610 | ```
|
1611 |
|
1612 | Default: `"import"`
|
1613 |
|
1614 | Allows you to apply the generator for `import` or assets from compilation (useful for copied assets).
|
1615 | By default, generators are applying on `import`/`require`, but sometimes you need to generate new images from other plugins (for example - `copy-webpack-plugin`), if you need this, please set `asset` value for the `type` option.
|
1616 |
|
1617 | **webpack.config.js**
|
1618 |
|
1619 | ```js
|
1620 | const CopyPlugin = require("copy-webpack-plugin");
|
1621 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1622 |
|
1623 | module.exports = {
|
1624 | optimization: {
|
1625 | minimizer: [
|
1626 | "...",
|
1627 | new ImageMinimizerPlugin({
|
1628 | minimizer: {
|
1629 | implementation: ImageMinimizerPlugin.imageminMinify,
|
1630 | options: {
|
1631 | plugins: [
|
1632 | "imagemin-gifsicle",
|
1633 | "imagemin-mozjpeg",
|
1634 | "imagemin-pngquant",
|
1635 | "imagemin-svgo",
|
1636 | ],
|
1637 | },
|
1638 | },
|
1639 | generator: [
|
1640 | {
|
1641 | // Apply generator for copied assets
|
1642 | type: "asset",
|
1643 | // You can use `ImageMinimizerPlugin.squooshGenerate`
|
1644 | // You can use `ImageMinimizerPlugin.sharpGenerate`
|
1645 | implementation: ImageMinimizerPlugin.imageminGenerate,
|
1646 | options: {
|
1647 | plugins: ["imagemin-webp"],
|
1648 | },
|
1649 | },
|
1650 | ],
|
1651 | }),
|
1652 | ],
|
1653 | },
|
1654 | plugins: [new CopyPlugin({ patterns: ["images/**/*.png"] })],
|
1655 | };
|
1656 | ```
|
1657 |
|
1658 | ##### `preset`
|
1659 |
|
1660 | Type:
|
1661 |
|
1662 | ```ts
|
1663 | type preset = string | undefined;
|
1664 | ```
|
1665 |
|
1666 | Default: `undefined`
|
1667 |
|
1668 | Configure the name of preset, i.e. you can use it in `?as=name`.
|
1669 |
|
1670 | **webpack.config.js**
|
1671 |
|
1672 | ```js
|
1673 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1674 |
|
1675 | module.exports = {
|
1676 | optimization: {
|
1677 | minimizer: [
|
1678 | "...",
|
1679 | new ImageMinimizerPlugin({
|
1680 | generator: [
|
1681 | {
|
1682 | preset: "name",
|
1683 | // Implementation
|
1684 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1685 | options: {
|
1686 | encodeOptions: {
|
1687 | jpeg: {
|
1688 | quality: 85,
|
1689 | },
|
1690 | },
|
1691 | },
|
1692 | },
|
1693 | ],
|
1694 | }),
|
1695 | ],
|
1696 | },
|
1697 | };
|
1698 | ```
|
1699 |
|
1700 | ##### `implementation`
|
1701 |
|
1702 | Type:
|
1703 |
|
1704 | ```ts
|
1705 | type implementation = (
|
1706 | original: {
|
1707 | filename: string;
|
1708 | data: Buffer;
|
1709 | warnings: Array<Error>;
|
1710 | errors: Array<Error>;
|
1711 | info: import("webpack").AssetInfo;
|
1712 | },
|
1713 | options?:
|
1714 | | {
|
1715 | [key: string]: any;
|
1716 | }
|
1717 | | undefined,
|
1718 | ) => Promise<{
|
1719 | filename: string;
|
1720 | data: Buffer;
|
1721 | warnings: Array<Error>;
|
1722 | errors: Array<Error>;
|
1723 | info: import("webpack").AssetInfo;
|
1724 | }> & {
|
1725 | setup?: (() => void) | undefined;
|
1726 | teardown?: (() => void) | undefined;
|
1727 | };
|
1728 | ```
|
1729 |
|
1730 | Default: `undefined`
|
1731 |
|
1732 | Configure the default `implementation`.
|
1733 |
|
1734 | **webpack.config.js**
|
1735 |
|
1736 | ```js
|
1737 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1738 |
|
1739 | module.exports = {
|
1740 | optimization: {
|
1741 | minimizer: [
|
1742 | "...",
|
1743 | new ImageMinimizerPlugin({
|
1744 | generator: [
|
1745 | {
|
1746 | preset: "name",
|
1747 | // Implementation
|
1748 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1749 | options: {
|
1750 | encodeOptions: {
|
1751 | jpeg: {
|
1752 | quality: 85,
|
1753 | },
|
1754 | },
|
1755 | },
|
1756 | },
|
1757 | ],
|
1758 | }),
|
1759 | ],
|
1760 | },
|
1761 | };
|
1762 | ```
|
1763 |
|
1764 | ##### `options`
|
1765 |
|
1766 | Type:
|
1767 |
|
1768 | ```ts
|
1769 | type options = {
|
1770 | [key: string]: any;
|
1771 | };
|
1772 | ```
|
1773 |
|
1774 | Default: `undefined`
|
1775 |
|
1776 | Options for the `implementation` option (i.e. options for `imagemin`/`squoosh`/`sharp`/custom implementation).
|
1777 |
|
1778 | **webpack.config.js**
|
1779 |
|
1780 | ```js
|
1781 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1782 |
|
1783 | module.exports = {
|
1784 | optimization: {
|
1785 | minimizer: [
|
1786 | "...",
|
1787 | new ImageMinimizerPlugin({
|
1788 | generator: [
|
1789 | {
|
1790 | preset: "name",
|
1791 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1792 | // Options
|
1793 | options: {
|
1794 | encodeOptions: {
|
1795 | jpeg: {
|
1796 | quality: 90,
|
1797 | },
|
1798 | },
|
1799 | },
|
1800 | },
|
1801 | ],
|
1802 | }),
|
1803 | ],
|
1804 | },
|
1805 | };
|
1806 | ```
|
1807 |
|
1808 | ##### `filter`
|
1809 |
|
1810 | Type:
|
1811 |
|
1812 | ```ts
|
1813 | type filter = (source: Buffer, sourcePath: string) => boolean;
|
1814 | ```
|
1815 |
|
1816 | Default: `() => true`
|
1817 |
|
1818 | Allows filtering of images for optimization/generation.
|
1819 |
|
1820 | Return `true` to optimize the image, `false` otherwise.
|
1821 |
|
1822 | **webpack.config.js**
|
1823 |
|
1824 | ```js
|
1825 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1826 |
|
1827 | module.exports = {
|
1828 | optimization: {
|
1829 | minimizer: [
|
1830 | "...",
|
1831 | new ImageMinimizerPlugin({
|
1832 | generator: [
|
1833 | {
|
1834 | preset: "name",
|
1835 | filter: (source, sourcePath) => {
|
1836 | // The `source` argument is a `Buffer` of source file
|
1837 | // The `sourcePath` argument is an absolute path to source
|
1838 | if (source.byteLength < 8192) {
|
1839 | return false;
|
1840 | }
|
1841 |
|
1842 | return true;
|
1843 | },
|
1844 | implementation: ImageMinimizerPlugin.imageminMinify,
|
1845 | options: {
|
1846 | plugins: [
|
1847 | "imagemin-gifsicle",
|
1848 | "imagemin-mozjpeg",
|
1849 | "imagemin-pngquant",
|
1850 | "imagemin-svgo",
|
1851 | ],
|
1852 | },
|
1853 | },
|
1854 | ],
|
1855 | }),
|
1856 | ],
|
1857 | },
|
1858 | };
|
1859 | ```
|
1860 |
|
1861 | ##### `filename`
|
1862 |
|
1863 | Type:
|
1864 |
|
1865 | ```ts
|
1866 | type filename =
|
1867 | | string
|
1868 | | ((
|
1869 | pathData: PathData,
|
1870 | assetInfo?: import("webpack").AssetInfo | undefined,
|
1871 | ) => string);
|
1872 | ```
|
1873 |
|
1874 | Default: `undefined`
|
1875 |
|
1876 | Allows to set the filename.
|
1877 | Supported values see in [`webpack template strings`](https://webpack.js.org/configuration/output/#template-strings), `File-level` section.
|
1878 |
|
1879 | **webpack.config.js**
|
1880 |
|
1881 | ```js
|
1882 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1883 |
|
1884 | module.exports = {
|
1885 | optimization: {
|
1886 | minimizer: [
|
1887 | "...",
|
1888 | new ImageMinimizerPlugin({
|
1889 | generator: [
|
1890 | {
|
1891 | preset: "name",
|
1892 | filename: "generated-[name][ext]",
|
1893 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1894 | // Options
|
1895 | options: {
|
1896 | encodeOptions: {
|
1897 | jpeg: {
|
1898 | quality: 90,
|
1899 | },
|
1900 | },
|
1901 | },
|
1902 | },
|
1903 | ],
|
1904 | }),
|
1905 | ],
|
1906 | },
|
1907 | };
|
1908 | ```
|
1909 |
|
1910 | Example of `function` usage:
|
1911 |
|
1912 | **webpack.config.js**
|
1913 |
|
1914 | ```js
|
1915 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1916 |
|
1917 | module.exports = {
|
1918 | optimization: {
|
1919 | minimizer: [
|
1920 | "...",
|
1921 | new ImageMinimizerPlugin({
|
1922 | generator: [
|
1923 | {
|
1924 | preset: "name",
|
1925 | filename: () => "generated-[name][ext]",
|
1926 | implementation: ImageMinimizerPlugin.sharpMinify,
|
1927 | // Options
|
1928 | options: {
|
1929 | encodeOptions: {
|
1930 | jpeg: {
|
1931 | quality: 90,
|
1932 | },
|
1933 | },
|
1934 | },
|
1935 | },
|
1936 | ],
|
1937 | }),
|
1938 | ],
|
1939 | },
|
1940 | };
|
1941 | ```
|
1942 |
|
1943 | ### `severityError`
|
1944 |
|
1945 | Type:
|
1946 |
|
1947 | ```ts
|
1948 | type severityError = string;
|
1949 | ```
|
1950 |
|
1951 | Default: `'error'`
|
1952 |
|
1953 | Allows to choose how errors are displayed.
|
1954 |
|
1955 | Сan have the following values:
|
1956 |
|
1957 | - `'off'` - suppresses errors and warnings
|
1958 | - `'warning'` - emit warnings instead errors
|
1959 | - `'error'` - emit errors
|
1960 |
|
1961 | **webpack.config.js**
|
1962 |
|
1963 | ```js
|
1964 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
1965 |
|
1966 | module.exports = {
|
1967 | optimization: {
|
1968 | minimizer: [
|
1969 | "...",
|
1970 | new ImageMinimizerPlugin({
|
1971 | severityError: "warning",
|
1972 | minimizer: {
|
1973 | implementation: ImageMinimizerPlugin.imageminMinify,
|
1974 | options: {
|
1975 | plugins: [
|
1976 | "imagemin-gifsicle",
|
1977 | "imagemin-mozjpeg",
|
1978 | "imagemin-pngquant",
|
1979 | "imagemin-svgo",
|
1980 | ],
|
1981 | },
|
1982 | },
|
1983 | }),
|
1984 | ],
|
1985 | },
|
1986 | };
|
1987 | ```
|
1988 |
|
1989 | ### `loader`
|
1990 |
|
1991 | Type:
|
1992 |
|
1993 | ```ts
|
1994 | type loader = boolean;
|
1995 | ```
|
1996 |
|
1997 | Default: `true`
|
1998 |
|
1999 | Automatically adding built-in `loader`, used to optimize/generate images.
|
2000 |
|
2001 | **webpack.config.js**
|
2002 |
|
2003 | ```js
|
2004 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2005 |
|
2006 | module.exports = {
|
2007 | optimization: {
|
2008 | minimizer: [
|
2009 | "...",
|
2010 | new ImageMinimizerPlugin({
|
2011 | loader: false,
|
2012 | // `generator` will not work in this case
|
2013 | minimizer: {
|
2014 | implementation: ImageMinimizerPlugin.imageminMinify,
|
2015 | options: {
|
2016 | plugins: [
|
2017 | "imagemin-gifsicle",
|
2018 | "imagemin-mozjpeg",
|
2019 | "imagemin-pngquant",
|
2020 | "imagemin-svgo",
|
2021 | ],
|
2022 | },
|
2023 | },
|
2024 | }),
|
2025 | ],
|
2026 | },
|
2027 | };
|
2028 | ```
|
2029 |
|
2030 | ### `concurrency`
|
2031 |
|
2032 | Type:
|
2033 |
|
2034 | ```ts
|
2035 | type concurrency = number;
|
2036 | ```
|
2037 |
|
2038 | Default: `Math.max(1, os.cpus().length - 1)`
|
2039 |
|
2040 | Maximum number of concurrency optimization processes in one time.
|
2041 |
|
2042 | **webpack.config.js**
|
2043 |
|
2044 | ```js
|
2045 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2046 |
|
2047 | module.exports = {
|
2048 | optimization: {
|
2049 | minimizer: [
|
2050 | "...",
|
2051 | new ImageMinimizerPlugin({
|
2052 | concurrency: 3,
|
2053 | minimizer: {
|
2054 | implementation: ImageMinimizerPlugin.imageminMinify,
|
2055 | options: {
|
2056 | plugins: [
|
2057 | "imagemin-gifsicle",
|
2058 | "imagemin-mozjpeg",
|
2059 | "imagemin-pngquant",
|
2060 | "imagemin-svgo",
|
2061 | ],
|
2062 | },
|
2063 | },
|
2064 | }),
|
2065 | ],
|
2066 | },
|
2067 | };
|
2068 | ```
|
2069 |
|
2070 | ### `deleteOriginalAssets`
|
2071 |
|
2072 | Type:
|
2073 |
|
2074 | ```ts
|
2075 | type deleteOriginalAssets = boolean;
|
2076 | ```
|
2077 |
|
2078 | Default: `true`
|
2079 |
|
2080 | Allows removing original assets after optimization.
|
2081 |
|
2082 | **Please use this option if you are set the `filename` option for the `minimizer` option, disable `loader: false` and want to keep optimized and not optimized assets.**
|
2083 |
|
2084 | **webpack.config.js**
|
2085 |
|
2086 | ```js
|
2087 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2088 |
|
2089 | module.exports = {
|
2090 | optimization: {
|
2091 | minimizer: [
|
2092 | "...",
|
2093 | new ImageMinimizerPlugin({
|
2094 | // Disable loader
|
2095 | loader: false,
|
2096 | // Allows to keep original asset and minimized assets with different filenames
|
2097 | deleteOriginalAssets: false,
|
2098 | minimizer: {
|
2099 | filename: "[path][name].webp",
|
2100 | implementation: ImageMinimizerPlugin.imageminMinify,
|
2101 | options: {
|
2102 | plugins: [
|
2103 | "imagemin-gifsicle",
|
2104 | "imagemin-mozjpeg",
|
2105 | "imagemin-pngquant",
|
2106 | "imagemin-svgo",
|
2107 | ],
|
2108 | },
|
2109 | },
|
2110 | }),
|
2111 | ],
|
2112 | },
|
2113 | };
|
2114 | ```
|
2115 |
|
2116 | ## Loader Options
|
2117 |
|
2118 | - **[`minimizer`](#minimizer-1)**
|
2119 | - **[`generator`](#generator-1)**
|
2120 | - **[`severityError`](severityerror-1)**
|
2121 |
|
2122 | ### `minimizer`
|
2123 |
|
2124 | Type:
|
2125 |
|
2126 | ```ts
|
2127 | type minimizer =
|
2128 | | {
|
2129 | implementation: (
|
2130 | original: {
|
2131 | filename: string;
|
2132 | data: Buffer;
|
2133 | warnings: Array<Error>;
|
2134 | errors: Array<Error>;
|
2135 | info: import("webpack").AssetInfo;
|
2136 | },
|
2137 | options?:
|
2138 | | {
|
2139 | [key: string]: any;
|
2140 | }
|
2141 | | undefined,
|
2142 | ) => Promise<{
|
2143 | filename: string;
|
2144 | data: Buffer;
|
2145 | warnings: Array<Error>;
|
2146 | errors: Array<Error>;
|
2147 | info: import("webpack").AssetInfo;
|
2148 | }> & {
|
2149 | setup?: (() => void) | undefined;
|
2150 | teardown?: (() => void) | undefined;
|
2151 | };
|
2152 | options?:
|
2153 | | {
|
2154 | [key: string]: any;
|
2155 | }
|
2156 | | undefined;
|
2157 | filter?: (source: Buffer, sourcePath: string) => boolean | undefined;
|
2158 | filename?:
|
2159 | | string
|
2160 | | ((
|
2161 | pathData: {
|
2162 | filename?: string | undefined;
|
2163 | },
|
2164 | assetInfo?: import("webpack").AssetInfo | undefined,
|
2165 | ) => string)
|
2166 | | undefined;
|
2167 | }
|
2168 | | Array<{
|
2169 | implementation: (
|
2170 | original: {
|
2171 | filename: string;
|
2172 | data: Buffer;
|
2173 | warnings: Array<Error>;
|
2174 | errors: Array<Error>;
|
2175 | info: import("webpack").AssetInfo;
|
2176 | },
|
2177 | options?:
|
2178 | | {
|
2179 | [key: string]: any;
|
2180 | }
|
2181 | | undefined,
|
2182 | ) => Promise<{
|
2183 | filename: string;
|
2184 | data: Buffer;
|
2185 | warnings: Array<Error>;
|
2186 | errors: Array<Error>;
|
2187 | info: import("webpack").AssetInfo;
|
2188 | }> & {
|
2189 | setup?: (() => void) | undefined;
|
2190 | teardown?: (() => void) | undefined;
|
2191 | };
|
2192 | options?:
|
2193 | | {
|
2194 | [key: string]: any;
|
2195 | }
|
2196 | | undefined;
|
2197 | filter?: (source: Buffer, sourcePath: string) => boolean | undefined;
|
2198 | filename?:
|
2199 | | string
|
2200 | | ((
|
2201 | pathData: {
|
2202 | filename?: string | undefined;
|
2203 | },
|
2204 | assetInfo?: import("webpack").AssetInfo | undefined,
|
2205 | ) => string)
|
2206 | | undefined;
|
2207 | }>;
|
2208 | ```
|
2209 |
|
2210 | Default: `undefined`
|
2211 |
|
2212 | Allows to setup default minimizer.
|
2213 |
|
2214 | #### Loader minimizer example for `imagemin`
|
2215 |
|
2216 | **webpack.config.js**
|
2217 |
|
2218 | ```js
|
2219 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2220 |
|
2221 | module.exports = {
|
2222 | module: {
|
2223 | rules: [
|
2224 | {
|
2225 | test: /\.(jpe?g|png|gif|svg)$/i,
|
2226 | type: "asset",
|
2227 | },
|
2228 | {
|
2229 | test: /\.(jpe?g|png|gif|svg)$/i,
|
2230 | loader: ImageMinimizerPlugin.loader,
|
2231 | enforce: "pre",
|
2232 | options: {
|
2233 | minimizer: {
|
2234 | implementation: ImageMinimizerPlugin.imageminMinify,
|
2235 | options: {
|
2236 | plugins: [
|
2237 | "imagemin-gifsicle",
|
2238 | "imagemin-mozjpeg",
|
2239 | "imagemin-pngquant",
|
2240 | "imagemin-svgo",
|
2241 | ],
|
2242 | },
|
2243 | },
|
2244 | },
|
2245 | },
|
2246 | ],
|
2247 | },
|
2248 | };
|
2249 | ```
|
2250 |
|
2251 | For more information and supported options please read [here](#minimizer).
|
2252 |
|
2253 | ### `generator`
|
2254 |
|
2255 | Type:
|
2256 |
|
2257 | ```ts
|
2258 | type generator = Array<{
|
2259 | implementation: TransformerFunction<T>;
|
2260 | options?: BasicTransformerOptions<T>;
|
2261 | filter?: FilterFn | undefined;
|
2262 | filename?: string | FilenameFn | undefined;
|
2263 | preset?: string | undefined;
|
2264 | type?: "import" | "asset" | undefined;
|
2265 | }>;
|
2266 | ```
|
2267 |
|
2268 | Default: `undefined`
|
2269 |
|
2270 | Allow to setup default generators.
|
2271 | Useful if you need generate `webp`/`avif`/etc from other formats.
|
2272 |
|
2273 | #### Loader generator example for `imagemin`
|
2274 |
|
2275 | **webpack.config.js**
|
2276 |
|
2277 | ```js
|
2278 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2279 |
|
2280 | module.exports = {
|
2281 | module: {
|
2282 | rules: [
|
2283 | {
|
2284 | test: /\.(jpe?g|png|gif|svg)$/i,
|
2285 | type: "asset",
|
2286 | },
|
2287 | {
|
2288 | test: /\.(jpe?g|png|gif|svg)$/i,
|
2289 | loader: ImageMinimizerPlugin.loader,
|
2290 | enforce: "pre",
|
2291 | options: {
|
2292 | generator: [
|
2293 | {
|
2294 | preset: "webp",
|
2295 | implementation: ImageMinimizerPlugin.imageminGenerate,
|
2296 | options: {
|
2297 | plugins: ["imagemin-webp"],
|
2298 | },
|
2299 | },
|
2300 | ],
|
2301 | },
|
2302 | },
|
2303 | ],
|
2304 | },
|
2305 | };
|
2306 | ```
|
2307 |
|
2308 | For more information and supported options please read [here](#generator).
|
2309 |
|
2310 | ### `severityError`
|
2311 |
|
2312 | Type:
|
2313 |
|
2314 | ```ts
|
2315 | type severityError = string;
|
2316 | ```
|
2317 |
|
2318 | Default: `'error'`
|
2319 |
|
2320 | Allows to choose how errors are displayed.
|
2321 |
|
2322 | Сan have the following values:
|
2323 |
|
2324 | - `'off'` - suppresses errors and warnings
|
2325 | - `'warning'` - emit warnings instead errors
|
2326 | - `'error'` - emit errors
|
2327 |
|
2328 | **webpack.config.js**
|
2329 |
|
2330 | ```js
|
2331 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2332 |
|
2333 | module.exports = {
|
2334 | module: {
|
2335 | rules: [
|
2336 | {
|
2337 | test: /\.(jpe?g|png|gif|svg)$/i,
|
2338 | type: "asset",
|
2339 | },
|
2340 | {
|
2341 | test: /\.(jpe?g|png|gif|svg)$/i,
|
2342 | use: [
|
2343 | {
|
2344 | loader: ImageMinimizerPlugin.loader,
|
2345 | options: {
|
2346 | severityError: "warning",
|
2347 | minimizerOptions: {
|
2348 | plugins: ["gifsicle"],
|
2349 | },
|
2350 | },
|
2351 | },
|
2352 | ],
|
2353 | },
|
2354 | ],
|
2355 | },
|
2356 | };
|
2357 | ```
|
2358 |
|
2359 | ## Additional API
|
2360 |
|
2361 | ### `imageminNormalizeConfig(config)`
|
2362 |
|
2363 | The function normalizes configuration (converts plugins names and options to `Function`s) for using in `imagemin` package directly.
|
2364 |
|
2365 | ```js
|
2366 | const imagemin = require("imagemin");
|
2367 | const { imageminNormalizeConfig } = require("image-minimizer-webpack-plugin");
|
2368 |
|
2369 | /*
|
2370 | console.log(imageminConfig);
|
2371 | =>
|
2372 | {
|
2373 | plugins: [Function, Function],
|
2374 | pluginsMeta: [
|
2375 | { name: "imagemin-jpegtran", version: "x.x.x", options: {} },
|
2376 | { name: "imagemin-pngquant", version: "x.x.x", options: { quality: [0.6, 0.8] }
|
2377 | ]
|
2378 | }
|
2379 | */
|
2380 |
|
2381 | (async () => {
|
2382 | const imageminConfig = await imageminNormalizeConfig({
|
2383 | plugins: ["jpegtran", ["pngquant", { quality: [0.6, 0.8] }]],
|
2384 | });
|
2385 | const files = await imagemin(["images/*.{jpg,png}"], {
|
2386 | destination: "build/images",
|
2387 | plugins: imageminConfig.plugins,
|
2388 | });
|
2389 |
|
2390 | console.log(files);
|
2391 | // => [{data: <Buffer 89 50 4e …>, path: 'build/images/foo.jpg'}, …]
|
2392 | })();
|
2393 | ```
|
2394 |
|
2395 | ## Examples
|
2396 |
|
2397 | ### Optimize images based on size
|
2398 |
|
2399 | You can use difference options (like `progressive`/`interlaced`/etc.) based on image size (example - don't do progressive transformation for small images).
|
2400 |
|
2401 | What is `progressive` image? [`Answer here`](https://jmperezperez.com/medium-image-progressive-loading-placeholder/).
|
2402 |
|
2403 | **webpack.config.js**
|
2404 |
|
2405 | ```js
|
2406 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2407 |
|
2408 | module.exports = {
|
2409 | optimization: {
|
2410 | minimizer: [
|
2411 | new ImageMinimizerPlugin({
|
2412 | minimizer: {
|
2413 | implementation: ImageMinimizerPlugin.imageminMinify,
|
2414 | options: {
|
2415 | plugins: [["jpegtran", { progressive: true }]],
|
2416 | },
|
2417 | // Only apply this one to files equal to or over 8192 bytes
|
2418 | filter: (source) => {
|
2419 | if (source.byteLength >= 8192) {
|
2420 | return true;
|
2421 | }
|
2422 |
|
2423 | return false;
|
2424 | },
|
2425 | },
|
2426 | }),
|
2427 | new ImageMinimizerPlugin({
|
2428 | minimizer: {
|
2429 | implementation: ImageMinimizerPlugin.imageminMinify,
|
2430 | options: {
|
2431 | plugins: [["jpegtran", { progressive: false }]],
|
2432 | },
|
2433 | // Only apply this one to files under 8192
|
2434 | filter: (source) => {
|
2435 | if (source.byteLength < 8192) {
|
2436 | return true;
|
2437 | }
|
2438 |
|
2439 | return false;
|
2440 | },
|
2441 | },
|
2442 | }),
|
2443 | ],
|
2444 | },
|
2445 | };
|
2446 | ```
|
2447 |
|
2448 | ### Optimize and generate `webp` images
|
2449 |
|
2450 | - imagemin
|
2451 |
|
2452 | **webpack.config.js**
|
2453 |
|
2454 | ```js
|
2455 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2456 |
|
2457 | module.exports = {
|
2458 | optimization: {
|
2459 | minimizer: [
|
2460 | "...",
|
2461 | new ImageMinimizerPlugin({
|
2462 | minimizer: {
|
2463 | implementation: ImageMinimizerPlugin.imageminMinify,
|
2464 | options: {
|
2465 | plugins: [
|
2466 | "imagemin-gifsicle",
|
2467 | "imagemin-mozjpeg",
|
2468 | "imagemin-pngquant",
|
2469 | "imagemin-svgo",
|
2470 | ],
|
2471 | },
|
2472 | },
|
2473 | generator: [
|
2474 | {
|
2475 | // You can apply generator using `?as=webp`, you can use any name and provide more options
|
2476 | preset: "webp",
|
2477 | implementation: ImageMinimizerPlugin.imageminGenerate,
|
2478 | options: {
|
2479 | plugins: ["imagemin-webp"],
|
2480 | },
|
2481 | },
|
2482 | ],
|
2483 | }),
|
2484 | ],
|
2485 | },
|
2486 | };
|
2487 | ```
|
2488 |
|
2489 | - **DEPRECATED** squoosh
|
2490 |
|
2491 | **webpack.config.js**
|
2492 |
|
2493 | ```js
|
2494 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2495 |
|
2496 | module.exports = {
|
2497 | optimization: {
|
2498 | minimizer: [
|
2499 | "...",
|
2500 | new ImageMinimizerPlugin({
|
2501 | minimizer: {
|
2502 | implementation: ImageMinimizerPlugin.squooshMinify,
|
2503 | },
|
2504 | generator: [
|
2505 | {
|
2506 | // You can apply generator using `?as=webp`, you can use any name and provide more options
|
2507 | preset: "webp",
|
2508 | implementation: ImageMinimizerPlugin.squooshGenerate,
|
2509 | options: {
|
2510 | encodeOptions: {
|
2511 | webp: {
|
2512 | quality: 90,
|
2513 | },
|
2514 | },
|
2515 | },
|
2516 | },
|
2517 | ],
|
2518 | }),
|
2519 | ],
|
2520 | },
|
2521 | };
|
2522 | ```
|
2523 |
|
2524 | - sharp
|
2525 |
|
2526 | **webpack.config.js**
|
2527 |
|
2528 | ```js
|
2529 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2530 |
|
2531 | module.exports = {
|
2532 | optimization: {
|
2533 | minimizer: [
|
2534 | "...",
|
2535 | new ImageMinimizerPlugin({
|
2536 | minimizer: {
|
2537 | implementation: ImageMinimizerPlugin.sharpMinify,
|
2538 | },
|
2539 | generator: [
|
2540 | {
|
2541 | // You can apply generator using `?as=webp`, you can use any name and provide more options
|
2542 | preset: "webp",
|
2543 | implementation: ImageMinimizerPlugin.sharpGenerate,
|
2544 | options: {
|
2545 | encodeOptions: {
|
2546 | webp: {
|
2547 | quality: 90,
|
2548 | },
|
2549 | },
|
2550 | },
|
2551 | },
|
2552 | ],
|
2553 | }),
|
2554 | ],
|
2555 | },
|
2556 | };
|
2557 | ```
|
2558 |
|
2559 | ### Generate `webp` images from copied assets
|
2560 |
|
2561 | - imagemin
|
2562 |
|
2563 | **webpack.config.js**
|
2564 |
|
2565 | ```js
|
2566 | const CopyPlugin = require("copy-webpack-plugin");
|
2567 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2568 |
|
2569 | module.exports = {
|
2570 | optimization: {
|
2571 | minimizer: [
|
2572 | "...",
|
2573 | new ImageMinimizerPlugin({
|
2574 | minimizer: {
|
2575 | implementation: ImageMinimizerPlugin.imageminMinify,
|
2576 | options: {
|
2577 | plugins: [
|
2578 | "imagemin-gifsicle",
|
2579 | "imagemin-mozjpeg",
|
2580 | "imagemin-pngquant",
|
2581 | "imagemin-svgo",
|
2582 | ],
|
2583 | },
|
2584 | },
|
2585 | generator: [
|
2586 | {
|
2587 | type: "asset",
|
2588 | implementation: ImageMinimizerPlugin.imageminGenerate,
|
2589 | options: {
|
2590 | plugins: ["imagemin-webp"],
|
2591 | },
|
2592 | },
|
2593 | ],
|
2594 | }),
|
2595 | ],
|
2596 | },
|
2597 | plugins: [new CopyPlugin({ patterns: ["images/**/*.png"] })],
|
2598 | };
|
2599 | ```
|
2600 |
|
2601 | - **DEPRECATED** squoosh
|
2602 |
|
2603 | **webpack.config.js**
|
2604 |
|
2605 | ```js
|
2606 | const CopyPlugin = require("copy-webpack-plugin");
|
2607 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2608 |
|
2609 | module.exports = {
|
2610 | optimization: {
|
2611 | minimizer: [
|
2612 | "...",
|
2613 | new ImageMinimizerPlugin({
|
2614 | minimizer: {
|
2615 | implementation: ImageMinimizerPlugin.squooshMinify,
|
2616 | },
|
2617 | generator: [
|
2618 | {
|
2619 | type: "asset",
|
2620 | implementation: ImageMinimizerPlugin.squooshGenerate,
|
2621 | options: {
|
2622 | encodeOptions: {
|
2623 | webp: {
|
2624 | quality: 90,
|
2625 | },
|
2626 | },
|
2627 | },
|
2628 | },
|
2629 | ],
|
2630 | }),
|
2631 | ],
|
2632 | },
|
2633 | plugins: [new CopyPlugin({ patterns: ["images/**/*.png"] })],
|
2634 | };
|
2635 | ```
|
2636 |
|
2637 | - sharp
|
2638 |
|
2639 | **webpack.config.js**
|
2640 |
|
2641 | ```js
|
2642 | const CopyPlugin = require("copy-webpack-plugin");
|
2643 | const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
|
2644 |
|
2645 | module.exports = {
|
2646 | optimization: {
|
2647 | minimizer: [
|
2648 | "...",
|
2649 | new ImageMinimizerPlugin({
|
2650 | minimizer: {
|
2651 | implementation: ImageMinimizerPlugin.sharpMinify,
|
2652 | },
|
2653 | generator: [
|
2654 | {
|
2655 | type: "asset",
|
2656 | implementation: ImageMinimizerPlugin.sharpGenerate,
|
2657 | options: {
|
2658 | encodeOptions: {
|
2659 | webp: {
|
2660 | quality: 90,
|
2661 | },
|
2662 | },
|
2663 | },
|
2664 | },
|
2665 | ],
|
2666 | }),
|
2667 | ],
|
2668 | },
|
2669 | plugins: [new CopyPlugin({ patterns: ["images/**/*.png"] })],
|
2670 | };
|
2671 | ```
|
2672 |
|
2673 | ## Contributing
|
2674 |
|
2675 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
2676 |
|
2677 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
2678 |
|
2679 | ## License
|
2680 |
|
2681 | [MIT](./LICENSE)
|
2682 |
|
2683 | [npm]: https://img.shields.io/npm/v/image-minimizer-webpack-plugin.svg
|
2684 | [npm-url]: https://npmjs.com/package/image-minimizer-webpack-plugin
|
2685 | [node]: https://img.shields.io/node/v/image-minimizer-webpack-plugin.svg
|
2686 | [node-url]: https://nodejs.org
|
2687 | [tests]: https://github.com/webpack-contrib/image-minimizer-webpack-plugin/workflows/image-minimizer-webpack-plugin/badge.svg
|
2688 | [tests-url]: https://github.com/webpack-contrib/image-minimizer-webpack-plugin/actions
|
2689 | [cover]: https://codecov.io/gh/webpack-contrib/image-minimizer-webpack-plugin/branch/master/graph/badge.svg
|
2690 | [cover-url]: https://codecov.io/gh/webpack-contrib/image-minimizer-webpack-plugin
|
2691 | [discussion]: https://img.shields.io/github/discussions/webpack/webpack
|
2692 | [discussion-url]: https://github.com/webpack/webpack/discussions
|
2693 | [size]: https://packagephobia.now.sh/badge?p=image-minimizer-webpack-plugin
|
2694 | [size-url]: https://packagephobia.now.sh/result?p=image-minimizer-webpack-plugin
|