UNPKG

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