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