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