UNPKG

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