1 | ![Dependencies status](https://david-dm.org/tcoopman/image-webpack-loader/status.svg)
|
2 | ![devDependencies status](https://david-dm.org/tcoopman/image-webpack-loader/dev-status.svg)
|
3 | ![Build status](https://github.com/tcoopman/image-webpack-loader/workflows/Testing/badge.svg)
|
4 |
|
5 | # image-webpack-loader
|
6 |
|
7 | Image loader module for webpack
|
8 |
|
9 | > Minify PNG, JPEG, GIF, SVG and WEBP images with [imagemin](https://github.com/kevva/imagemin)
|
10 |
|
11 | *Issues with the output should be reported on the imagemin [issue tracker](https://github.com/kevva/imagemin/issues).*
|
12 |
|
13 | ## Install
|
14 |
|
15 | ```sh
|
16 | $ npm install image-webpack-loader --save-dev
|
17 | ```
|
18 |
|
19 | ### Install in container
|
20 |
|
21 | #### `node:12-buster`
|
22 |
|
23 | No additional preparations required.
|
24 | All dependencies will be compiled automatically.
|
25 | **Not** recommended because of large image size (~1 GB).
|
26 |
|
27 | #### `node:12-buster-slim`
|
28 |
|
29 | Prepare script:
|
30 |
|
31 | ```shell script
|
32 | apt-get update
|
33 | apt-get install -y --no-install-recommends autoconf automake g++ libpng-dev make
|
34 | ```
|
35 |
|
36 | **Recommended** container image.
|
37 |
|
38 | #### `node:12-alpine`
|
39 |
|
40 | Prepare script:
|
41 |
|
42 | ```shell script
|
43 | apk add --no-cache autoconf automake file g++ libtool make nasm libpng-dev
|
44 | ```
|
45 |
|
46 | **Not** recommended because of long build time.
|
47 |
|
48 | #### Benchmark
|
49 |
|
50 | | Container distro | Pull time | Build time | Total time |
|
51 | | --------------------- | ---------- | ----------- | ----------- |
|
52 | | `node:12-buster` | 42 seconds | 77 seconds | 119 seconds |
|
53 | | `node:12-buster-slim` | 11 seconds | 103 seconds | 114 seconds |
|
54 | | `node:12-alpine` | 8 seconds | 122 seconds | 130 seconds |
|
55 |
|
56 | ### libpng issues
|
57 |
|
58 | Installing on some versions of OSX may raise errors with a [missing libpng dependency](https://github.com/tcoopman/image-webpack-loader/issues/51#issuecomment-273597313):
|
59 | ```
|
60 | Module build failed: Error: dyld: Library not loaded: /usr/local/opt/libpng/lib/libpng16.16.dylib
|
61 | ```
|
62 | This can be remedied by installing the newest version of libpng with [homebrew](http://brew.sh/):
|
63 |
|
64 | ```sh
|
65 | brew install libpng
|
66 | ```
|
67 |
|
68 | ## Usage
|
69 |
|
70 | [Documentation: Using loaders](https://webpack.js.org/concepts/loaders/)
|
71 |
|
72 | In your `webpack.config.js`, add the image-loader, chained after the [file-loader](https://github.com/webpack/file-loader):
|
73 |
|
74 | ```js
|
75 | rules: [{
|
76 | test: /\.(gif|png|jpe?g|svg)$/i,
|
77 | use: [
|
78 | 'file-loader',
|
79 | {
|
80 | loader: 'image-webpack-loader',
|
81 | options: {
|
82 | bypassOnDebug: true, // webpack@1.x
|
83 | disable: true, // webpack@2.x and newer
|
84 | },
|
85 | },
|
86 | ],
|
87 | }]
|
88 | ```
|
89 |
|
90 | For each optimizer you wish to configure, specify the corresponding key in options:
|
91 |
|
92 | ```js
|
93 | rules: [{
|
94 | test: /\.(gif|png|jpe?g|svg)$/i,
|
95 | use: [
|
96 | 'file-loader',
|
97 | {
|
98 | loader: 'image-webpack-loader',
|
99 | options: {
|
100 | mozjpeg: {
|
101 | progressive: true,
|
102 | },
|
103 | // optipng.enabled: false will disable optipng
|
104 | optipng: {
|
105 | enabled: false,
|
106 | },
|
107 | pngquant: {
|
108 | quality: [0.65, 0.90],
|
109 | speed: 4
|
110 | },
|
111 | gifsicle: {
|
112 | interlaced: false,
|
113 | },
|
114 | // the webp option will enable WEBP
|
115 | webp: {
|
116 | quality: 75
|
117 | }
|
118 | }
|
119 | },
|
120 | ],
|
121 | }]
|
122 | ```
|
123 |
|
124 | Comes bundled with the following optimizers, which are automatically enabled by default:
|
125 |
|
126 | - [mozjpeg](https://github.com/imagemin/imagemin-mozjpeg) — *Compress JPEG images*
|
127 | - [optipng](https://github.com/kevva/imagemin-optipng) — *Compress PNG images*
|
128 | - [pngquant](https://github.com/imagemin/imagemin-pngquant) — *Compress PNG images*
|
129 | - [svgo](https://github.com/kevva/imagemin-svgo) — *Compress SVG images*
|
130 | - [gifsicle](https://github.com/kevva/imagemin-gifsicle) — *Compress GIF images*
|
131 |
|
132 | And optional optimizers:
|
133 |
|
134 | - [webp](https://github.com/imagemin/imagemin-webp) — *Compress JPG & PNG images into WEBP*
|
135 |
|
136 | _Each optimizers can be disabled by specifying `optimizer.enabled: false`, and optional ones can be enabled by simply putting them in the options_
|
137 |
|
138 | If you are using Webpack 1, take a look at the [old docs](http://webpack.github.io/docs/using-loaders.html) (or consider upgrading).
|
139 |
|
140 | ## Options
|
141 |
|
142 | Loader options:
|
143 |
|
144 | #### bypassOnDebug *(all)*
|
145 |
|
146 | Type: `boolean`
|
147 | Default: `false`
|
148 |
|
149 | Using this, no processing is done when webpack 'debug' mode is used and the loader acts as a regular file-loader. Use this to speed up initial and, to a lesser extent, subsequent compilations while developing or using webpack-dev-server. Normal builds are processed normally, outputting optimized files.
|
150 |
|
151 | #### disable
|
152 |
|
153 | Type: `boolean`
|
154 | Default `false`
|
155 |
|
156 | Same functionality as **bypassOnDebug** option, but doesn't depend on webpack debug mode, which was deprecated in 2.x. Basically you want to use this option if you're running webpack@2.x or newer.
|
157 |
|
158 | For optimizer options, an up-to-date and exhaustive list is available on each optimizer repository:
|
159 |
|
160 | - [mozjpeg](https://github.com/imagemin/imagemin-mozjpeg#options)
|
161 | - [optipng](https://github.com/kevva/imagemin-optipng#options)
|
162 | - [pngquant](https://github.com/imagemin/imagemin-pngquant#options)
|
163 | - [svgo](https://github.com/imagemin/imagemin-svgo#options)
|
164 | - [gifsicle](https://github.com/imagemin/imagemin-gifsicle#options)
|
165 | - [webp](https://github.com/imagemin/imagemin-webp#options)
|
166 |
|
167 | ## Inspiration
|
168 |
|
169 | * [gulp-imagemin](https://github.com/sindresorhus/gulp-imagemin)
|
170 | * [file-loader](https://github.com/webpack/file-loader)
|
171 | * [imagemin-pngquant](https://github.com/imagemin/imagemin-pngquant)
|
172 |
|
173 | ## License
|
174 |
|
175 | MIT (http://www.opensource.org/licenses/mit-license.php)
|