UNPKG

8.16 kBMarkdownView Raw
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
17This plugin uses [html-minifier-terser](https://github.com/terser/html-minifier-terser) to optimize and minify your HTML.
18
19## Getting Started
20
21To begin, you'll need to install `html-minimizer-webpack-plugin`:
22
23```console
24$ npm install html-minimizer-webpack-plugin --save-dev
25```
26
27Then add the plugin to your `webpack` configuration. For example:
28
29**webpack.config.js**
30
31```js
32const HtmlMinimizerPlugin = require('html-minimizer-webpack-plugin');
33const CopyPlugin = require('copy-webpack-plugin');
34
35module.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
72This will enable HTML optimization only in production mode.
73If you want to run it also in development set the `optimization.minimize` option to `true`.
74
75And run `webpack` via your preferred method.
76
77## Options
78
79### `test`
80
81Type: `String|RegExp|Array<String|RegExp>` - default: `/\.html(\?.*)?$/i`
82
83Test to match files against.
84
85```js
86module.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
100Type: `String|RegExp|Array<String|RegExp>`
101Default: `undefined`
102
103Files to include.
104
105**webpack.config.js**
106
107```js
108module.exports = {
109 optimization: {
110 minimize: true,
111 minimizer: [
112 new HtmlMinimizerPlugin({
113 include: /\/includes/,
114 }),
115 ],
116 },
117};
118```
119
120### `exclude`
121
122Type: `String|RegExp|Array<String|RegExp>`
123Default: `undefined`
124
125Files to exclude.
126
127**webpack.config.js**
128
129```js
130module.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
146Type: `Boolean|String`
147Default: `true`
148
149Enable file caching.
150Default 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
156Enable/disable file caching.
157
158**webpack.config.js**
159
160```js
161module.exports = {
162 optimization: {
163 minimize: true,
164 minimizer: [
165 new HtmlMinimizerPlugin({
166 cache: true,
167 }),
168 ],
169 },
170};
171```
172
173#### `String`
174
175Enable file caching and set path to cache directory.
176
177**webpack.config.js**
178
179```js
180module.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
196Type: `Function<(defaultCacheKeys, file) -> Object>`
197Default: `defaultCacheKeys => defaultCacheKeys`
198
199Allows you to override default cache keys.
200
201Default 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
217module.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
236Type: `Boolean|Number`
237Default: `true`
238
239Use multi-process parallel running to improve the build speed.
240Default 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
246Enable/disable multi-process parallel running.
247
248**webpack.config.js**
249
250```js
251module.exports = {
252 optimization: {
253 minimize: true,
254 minimizer: [
255 new HtmlMinimizerPlugin({
256 parallel: true,
257 }),
258 ],
259 },
260};
261```
262
263#### `Number`
264
265Enable multi-process parallel running and set number of concurrent runs.
266
267**webpack.config.js**
268
269```js
270module.exports = {
271 optimization: {
272 minimize: true,
273 minimizer: [
274 new HtmlMinimizerPlugin({
275 parallel: 4,
276 }),
277 ],
278 },
279};
280```
281
282### `minify`
283
284Type: `Function`
285Default: `undefined`
286
287Allows you to override default minify function.
288By default plugin uses [html-minifier-terser](https://github.com/terser/html-minifier-terser) package.
289Useful 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
296module.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
318Type: `Object`
319Default: `{ 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
324module.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
340Please 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