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