1 | <div align="center">
|
2 | <a href="https://github.com/webpack/webpack">
|
3 | <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
|
4 | </a>
|
5 | </div>
|
6 |
|
7 | [![npm][npm]][npm-url]
|
8 | [![node][node]][node-url]
|
9 | [![deps][deps]][deps-url]
|
10 | [![tests][tests]][tests-url]
|
11 | [![cover][cover]][cover-url]
|
12 | [![chat][chat]][chat-url]
|
13 | [![size][size]][size-url]
|
14 |
|
15 | # html-minimizer-webpack-plugin
|
16 |
|
17 | This plugin uses [html-minifier-terser](https://github.com/terser/html-minifier-terser) to optimize and minify your HTML.
|
18 |
|
19 | ## Getting Started
|
20 |
|
21 | To begin, you'll need to install `html-minimizer-webpack-plugin`:
|
22 |
|
23 | ```console
|
24 | $ npm install html-minimizer-webpack-plugin --save-dev
|
25 | ```
|
26 |
|
27 | Then add the plugin to your `webpack` configuration. For example:
|
28 |
|
29 | **webpack.config.js**
|
30 |
|
31 | ```js
|
32 | const HtmlMinimizerPlugin = require('html-minimizer-webpack-plugin');
|
33 | const CopyPlugin = require('copy-webpack-plugin');
|
34 |
|
35 | module.exports = {
|
36 | module: {
|
37 | loaders: [
|
38 | {
|
39 | test: /\.html$/i,
|
40 | use: [
|
41 | {
|
42 | loader: 'file-loader',
|
43 | options: {
|
44 | name: '[name].[ext]',
|
45 | },
|
46 | },
|
47 | ],
|
48 | },
|
49 | ],
|
50 | },
|
51 | plugins: [
|
52 | new CopyPlugin({
|
53 | patterns: [
|
54 | {
|
55 | context: path.resolve(__dirname, 'dist'),
|
56 | from: './src/*.html',
|
57 | },
|
58 | ],
|
59 | }),
|
60 | ],
|
61 | optimization: {
|
62 | minimize: true,
|
63 | minimizer: [
|
64 | // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
|
65 | // `...`
|
66 | new HtmlMinimizerPlugin(),
|
67 | ],
|
68 | },
|
69 | };
|
70 | ```
|
71 |
|
72 | This will enable HTML optimization only in production mode.
|
73 | If you want to run it also in development set the `optimization.minimize` option to `true`.
|
74 |
|
75 | And run `webpack` via your preferred method.
|
76 |
|
77 | ## Options
|
78 |
|
79 | ### `test`
|
80 |
|
81 | Type: `String|RegExp|Array<String|RegExp>` - default: `/\.html(\?.*)?$/i`
|
82 |
|
83 | Test to match files against.
|
84 |
|
85 | ```js
|
86 | module.exports = {
|
87 | optimization: {
|
88 | minimize: true,
|
89 | minimizer: [
|
90 | new HtmlMinimizerPlugin({
|
91 | test: /\.foo\.html/i,
|
92 | }),
|
93 | ],
|
94 | },
|
95 | };
|
96 | ```
|
97 |
|
98 | ### `include`
|
99 |
|
100 | Type: `String|RegExp|Array<String|RegExp>`
|
101 | Default: `undefined`
|
102 |
|
103 | Files to include.
|
104 |
|
105 | **webpack.config.js**
|
106 |
|
107 | ```js
|
108 | module.exports = {
|
109 | optimization: {
|
110 | minimize: true,
|
111 | minimizer: [
|
112 | new HtmlMinimizerPlugin({
|
113 | include: /\/includes/,
|
114 | }),
|
115 | ],
|
116 | },
|
117 | };
|
118 | ```
|
119 |
|
120 | ### `exclude`
|
121 |
|
122 | Type: `String|RegExp|Array<String|RegExp>`
|
123 | Default: `undefined`
|
124 |
|
125 | Files to exclude.
|
126 |
|
127 | **webpack.config.js**
|
128 |
|
129 | ```js
|
130 | module.exports = {
|
131 | optimization: {
|
132 | minimize: true,
|
133 | minimizer: [
|
134 | new HtmlMinimizerPlugin({
|
135 | exclude: /\/excludes/,
|
136 | }),
|
137 | ],
|
138 | },
|
139 | };
|
140 | ```
|
141 |
|
142 | ### `cache`
|
143 |
|
144 | > ⚠ Ignored in webpack 5! Please use https://webpack.js.org/configuration/other-options/#cache.
|
145 |
|
146 | Type: `Boolean|String`
|
147 | Default: `true`
|
148 |
|
149 | Enable file caching.
|
150 | Default path to cache directory: `node_modules/.cache/html-minimizer-webpack-plugin`.
|
151 |
|
152 | > ℹ️ If you use your own `minify` function please read the `minify` section for cache invalidation correctly.
|
153 |
|
154 | #### `Boolean`
|
155 |
|
156 | Enable/disable file caching.
|
157 |
|
158 | **webpack.config.js**
|
159 |
|
160 | ```js
|
161 | module.exports = {
|
162 | optimization: {
|
163 | minimize: true,
|
164 | minimizer: [
|
165 | new HtmlMinimizerPlugin({
|
166 | cache: true,
|
167 | }),
|
168 | ],
|
169 | },
|
170 | };
|
171 | ```
|
172 |
|
173 | #### `String`
|
174 |
|
175 | Enable file caching and set path to cache directory.
|
176 |
|
177 | **webpack.config.js**
|
178 |
|
179 | ```js
|
180 | module.exports = {
|
181 | optimization: {
|
182 | minimize: true,
|
183 | minimizer: [
|
184 | new HtmlMinimizerPlugin({
|
185 | cache: 'path/to/cache',
|
186 | }),
|
187 | ],
|
188 | },
|
189 | };
|
190 | ```
|
191 |
|
192 | ### `cacheKeys`
|
193 |
|
194 | > ⚠ Ignored in webpack 5! Please use https://webpack.js.org/configuration/other-options/#cache.
|
195 |
|
196 | Type: `Function<(defaultCacheKeys, file) -> Object>`
|
197 | Default: `defaultCacheKeys => defaultCacheKeys`
|
198 |
|
199 | Allows you to override default cache keys.
|
200 |
|
201 | Default cache keys:
|
202 |
|
203 | ```js
|
204 | ({
|
205 | htmlMinimizer: require('html-minifier-terser/package.json').version, // html-minifier-terser version
|
206 | 'html-minimizer-webpack-plugin': require('../package.json').version, // plugin version
|
207 | 'html-minimizer-webpack-plugin-options': this.options, // plugin options
|
208 | nodeVersion: process.version, // Node.js version
|
209 | assetName: file, // asset path
|
210 | contentHash: crypto.createHash('md4').update(input).digest('hex'), // source file hash
|
211 | });
|
212 | ```
|
213 |
|
214 | **webpack.config.js**
|
215 |
|
216 | ```js
|
217 | module.exports = {
|
218 | optimization: {
|
219 | minimize: true,
|
220 | minimizer: [
|
221 | new HtmlMinimizerPlugin({
|
222 | cache: true,
|
223 | cacheKeys: (defaultCacheKeys, file) => {
|
224 | defaultCacheKeys.myCacheKey = 'myCacheKeyValue';
|
225 |
|
226 | return defaultCacheKeys;
|
227 | },
|
228 | }),
|
229 | ],
|
230 | },
|
231 | };
|
232 | ```
|
233 |
|
234 | ### `parallel`
|
235 |
|
236 | Type: `Boolean|Number`
|
237 | Default: `true`
|
238 |
|
239 | Use multi-process parallel running to improve the build speed.
|
240 | Default number of concurrent runs: `os.cpus().length - 1`.
|
241 |
|
242 | > ℹ️ Parallelization can speedup your build significantly and is therefore **highly recommended**.
|
243 |
|
244 | #### `Boolean`
|
245 |
|
246 | Enable/disable multi-process parallel running.
|
247 |
|
248 | **webpack.config.js**
|
249 |
|
250 | ```js
|
251 | module.exports = {
|
252 | optimization: {
|
253 | minimize: true,
|
254 | minimizer: [
|
255 | new HtmlMinimizerPlugin({
|
256 | parallel: true,
|
257 | }),
|
258 | ],
|
259 | },
|
260 | };
|
261 | ```
|
262 |
|
263 | #### `Number`
|
264 |
|
265 | Enable multi-process parallel running and set number of concurrent runs.
|
266 |
|
267 | **webpack.config.js**
|
268 |
|
269 | ```js
|
270 | module.exports = {
|
271 | optimization: {
|
272 | minimize: true,
|
273 | minimizer: [
|
274 | new HtmlMinimizerPlugin({
|
275 | parallel: 4,
|
276 | }),
|
277 | ],
|
278 | },
|
279 | };
|
280 | ```
|
281 |
|
282 | ### `minify`
|
283 |
|
284 | Type: `Function`
|
285 | Default: `undefined`
|
286 |
|
287 | Allows you to override default minify function.
|
288 | By default plugin uses [html-minifier-terser](https://github.com/terser/html-minifier-terser) package.
|
289 | Useful for using and testing unpublished versions or forks.
|
290 |
|
291 | > ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
|
292 |
|
293 | **webpack.config.js**
|
294 |
|
295 | ```js
|
296 | module.exports = {
|
297 | optimization: {
|
298 | minimize: true,
|
299 | minimizer: [
|
300 | new HtmlMinimizerPlugin({
|
301 | minimizerOptions: {
|
302 | collapseWhitespace: true,
|
303 | },
|
304 | minify: (data, minimizerOptions) => {
|
305 | const htmlMinifier = require('html-minifier-terser');
|
306 | const [[filename, input]] = Object.entries(data);
|
307 |
|
308 | return htmlMinifier.minify(input, minimizerOptions);
|
309 | },
|
310 | }),
|
311 | ],
|
312 | },
|
313 | };
|
314 | ```
|
315 |
|
316 | ### `minimizerOptions`
|
317 |
|
318 | Type: `Object`
|
319 | Default: `{ caseSensitive: true, collapseWhitespace: true, conservativeCollapse: true, keepClosingSlash: true, minifyCSS: true, minifyJS: true, removeComments: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, }`
|
320 |
|
321 | `Html-minifier-terser` optimisations [options](https://github.com/terser/html-minifier-terser#options-quick-reference).
|
322 |
|
323 | ```js
|
324 | module.exports = {
|
325 | optimization: {
|
326 | minimize: true,
|
327 | minimizer: [
|
328 | new HtmlMinimizerPlugin({
|
329 | minimizerOptions: {
|
330 | collapseWhitespace: false,
|
331 | },
|
332 | }),
|
333 | ],
|
334 | },
|
335 | };
|
336 | ```
|
337 |
|
338 | ## Contributing
|
339 |
|
340 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
341 |
|
342 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
343 |
|
344 | ## License
|
345 |
|
346 | [MIT](./LICENSE)
|
347 |
|
348 | [npm]: https://img.shields.io/npm/v/html-minimizer-webpack-plugin.svg
|
349 | [npm-url]: https://npmjs.com/package/html-minimizer-webpack-plugin
|
350 | [node]: https://img.shields.io/node/v/html-minimizer-webpack-plugin.svg
|
351 | [node-url]: https://nodejs.org
|
352 | [deps]: https://david-dm.org/webpack-contrib/html-minimizer-webpack-plugin.svg
|
353 | [deps-url]: https://david-dm.org/webpack-contrib/html-minimizer-webpack-plugin
|
354 | [tests]: https://github.com/webpack-contrib/html-minimizer-webpack-plugin/workflows/html-minimizer-webpack-plugin/badge.svg
|
355 | [tests-url]: https://github.com/webpack-contrib/html-minimizer-webpack-plugin/actions
|
356 | [cover]: https://codecov.io/gh/webpack-contrib/html-minimizer-webpack-plugin/branch/master/graph/badge.svg
|
357 | [cover-url]: https://codecov.io/gh/webpack-contrib/html-minimizer-webpack-plugin
|
358 | [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
|
359 | [chat-url]: https://gitter.im/webpack/webpack
|
360 | [size]: https://packagephobia.now.sh/badge?p=html-minimizer-webpack-plugin
|
361 | [size-url]: https://packagephobia.now.sh/result?p=html-minimizer-webpack-plugin
|