UNPKG

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