UNPKG

28.9 kBMarkdownView Raw
1[![npm][npm]][npm-url]
2[![node][node]][node-url]
3![npm](https://img.shields.io/npm/dw/html-webpack-plugin.svg)
4[![deps][deps]][deps-url]
5[![tests][tests]][tests-url]
6[![Backers on Open Collective](https://opencollective.com/html-webpack-plugin/backers/badge.svg)](#backers)
7 [![Sponsors on Open Collective](https://opencollective.com/html-webpack-plugin/sponsors/badge.svg)](#sponsors)
8
9<div align="center">
10 <img width="200" height="200" src="https://worldvectorlogo.com/logos/html5.svg">
11 <a href="https://github.com/webpack/webpack">
12 <img width="200" height="200"
13 src="https://webpack.js.org/assets/icon-square-big.svg">
14 </a>
15 <div>
16 <img width="100" height="100" title="Webpack Plugin" src="http://michael-ciniawsky.github.io/postcss-load-plugins/logo.svg">
17 </div>
18 <h1>HTML Webpack Plugin</h1>
19 <p>Plugin that simplifies creation of HTML files to serve your bundles</p>
20</div>
21
22<h2 align="center">Install</h2>
23
24```bash
25 npm i --save-dev html-webpack-plugin
26```
27
28```bash
29 yarn add --dev html-webpack-plugin
30```
31
32
33This is a [webpack](http://webpack.js.org/) plugin that simplifies creation of HTML files to serve your `webpack` bundles. This is especially useful for `webpack` bundles that include a hash in the filename which changes every compilation. You can either let the plugin generate an HTML file for you, supply
34your own template using `lodash` templates or use your own loader.
35
36<h2 align="center">Sponsors</h2>
37
38<a href="https://opencollective.com/html-webpack-plugin/sponsor/0/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/0/avatar.svg"></a>
39<a href="https://opencollective.com/html-webpack-plugin/sponsor/1/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/1/avatar.svg"></a>
40<a href="https://opencollective.com/html-webpack-plugin/sponsor/2/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/2/avatar.svg"></a>
41<a href="https://opencollective.com/html-webpack-plugin/sponsor/3/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/3/avatar.svg"></a>
42<a href="https://opencollective.com/html-webpack-plugin/sponsor/4/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/4/avatar.svg"></a>
43<a href="https://opencollective.com/html-webpack-plugin/sponsor/5/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/5/avatar.svg"></a>
44<a href="https://opencollective.com/html-webpack-plugin/sponsor/6/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/6/avatar.svg"></a>
45<a href="https://opencollective.com/html-webpack-plugin/sponsor/7/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/7/avatar.svg"></a>
46<a href="https://opencollective.com/html-webpack-plugin/sponsor/8/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/8/avatar.svg"></a>
47<a href="https://opencollective.com/html-webpack-plugin/sponsor/9/website" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/sponsor/9/avatar.svg"></a>
48
49Thanks for supporting the ongoing improvements to the html-webpack-plugin!
50
51<h2 align="center">Zero Config</h2>
52
53The `html-webpack-plugin` works without configuration.
54It's a great addition to the [⚙️ webpack-config-plugins](https://github.com/namics/webpack-config-plugins/blob/master/README.md#zero-config-webpack-dev-server-example).
55
56<h2 align="center">Plugins</h2>
57
58The `html-webpack-plugin` provides [hooks](https://github.com/jantimon/html-webpack-plugin#events) to extend it to your needs. There are already some really powerful plugins which can be integrated with zero configuration
59
60 * [webpack-subresource-integrity](https://www.npmjs.com/package/webpack-subresource-integrity) for enhanced asset security
61 * [appcache-webpack-plugin](https://github.com/lettertwo/appcache-webpack-plugin) for iOS and Android offline usage
62 * [favicons-webpack-plugin](https://github.com/jantimon/favicons-webpack-plugin) which generates favicons and icons for iOS, Android and desktop browsers
63 * [html-webpack-harddisk-plugin](https://github.com/jantimon/html-webpack-harddisk-plugin) can be used to always write to disk the html file, useful when webpack-dev-server / HMR are being used
64 * [html-webpack-inline-source-plugin](https://github.com/DustinJackson/html-webpack-inline-source-plugin) to inline your assets in the resulting HTML file
65 * [html-webpack-inline-svg-plugin](https://github.com/thegc/html-webpack-inline-svg-plugin) to inline SVGs in the resulting HTML file.
66 * [html-webpack-exclude-assets-plugin](https://github.com/jamesjieye/html-webpack-exclude-assets-plugin) for excluding assets using regular expressions
67 * [html-webpack-include-assets-plugin](https://github.com/jharris4/html-webpack-include-assets-plugin) for including lists of js or css file paths (such as those copied by the copy-webpack-plugin).
68 * [script-ext-html-webpack-plugin](https://github.com/numical/script-ext-html-webpack-plugin) to add `async`, `defer` or `module` attributes to your `<script>` elements, or even inline them
69 * [style-ext-html-webpack-plugin](https://github.com/numical/style-ext-html-webpack-plugin) to convert your `<link>`s to external stylesheets into `<style>` elements containing internal CSS
70 * [html-webpack-injector](https://github.com/thearchitgarg/html-webpack-injector) to inject chunks in `head` or `body` (different locations ) of same html document.
71 * [resource-hints-webpack-plugin](https://github.com/jantimon/resource-hints-webpack-plugin) to add resource hints for faster initial page loads using `<link rel='preload'>` and `<link rel='prefetch'>`
72 * [preload-webpack-plugin](https://github.com/GoogleChrome/preload-webpack-plugin) for automatically wiring up asynchronous (and other types) of JavaScript chunks using `<link rel='preload'>` helping with lazy-loading
73 * [link-media-html-webpack-plugin](https://github.com/yaycmyk/link-media-html-webpack-plugin) allows for injected stylesheet `<link />` tags to have their media attribute set automatically; useful for providing specific desktop/mobile/print etc. stylesheets that the browser will conditionally download
74 * [inline-chunk-manifest-html-webpack-plugin](https://github.com/jouni-kantola/inline-chunk-manifest-html-webpack-plugin) for inlining webpack's chunk manifest. Default extracts manifest and inlines in `<head>`
75 * [html-webpack-inline-style-plugin](https://github.com/djaax/html-webpack-inline-style-plugin) for inlining styles to HTML elements using [juice](https://github.com/Automattic/juice). Useful for email generation automatisation.
76 * [html-webpack-exclude-empty-assets-plugin](https://github.com/KnisterPeter/html-webpack-exclude-empty-assets-plugin) removes empty assets from being added to the html. This fixes some problems with extract-text-plugin with webpack 4.
77 * [webpack-concat-plugin](https://github.com/hxlniada/webpack-concat-plugin) for concat and uglify files that needn't to be webpack bundles(for legacy files) and inject to html-webpack-plugin.
78 * [html-webpack-link-type-plugin](https://github.com/steadyapp/html-webpack-link-type-plugin) adds a configurable mimetype to resources injected as links (such as adding type="text/css" to external stylesheets) for compatibility with "strict mode".
79 * [csp-html-webpack-plugin](https://github.com/slackhq/csp-html-webpack-plugin) to add [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) meta tags to the HTML output
80 * [webpack-nomodule-plugin](https://github.com/swimmadude66/webpack-nomodule-plugin) allows you to add a `nomodule` attribute to specific injected scripts, which prevents the scripts from being loaded by newer browsers. Good for limiting loads of polyfills.
81
82
83<h2 align="center">Usage</h2>
84
85The plugin will generate an HTML5 file for you that includes all your `webpack`
86bundles in the body using `script` tags. Just add the plugin to your `webpack`
87config as follows:
88
89**webpack.config.js**
90```js
91const HtmlWebpackPlugin = require('html-webpack-plugin')
92
93module.exports = {
94 entry: 'index.js',
95 output: {
96 path: __dirname + '/dist',
97 filename: 'index_bundle.js'
98 },
99 plugins: [
100 new HtmlWebpackPlugin()
101 ]
102}
103```
104
105This will generate a file `dist/index.html` containing the following
106
107```html
108<!DOCTYPE html>
109<html>
110 <head>
111 <meta charset="utf-8">
112 <title>Webpack App</title>
113 </head>
114 <body>
115 <script src="index_bundle.js"></script>
116 </body>
117</html>
118```
119
120If you have multiple `webpack` entry points, they will all be included with `script` tags in the generated HTML.
121
122If you have any CSS assets in webpack's output (for example, CSS extracted with the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin))
123then these will be included with `<link>` tags in the HTML head.
124
125If you have plugins that make use of it, `html-webpack-plugin` should be ordered first before any of the integrated plugins.
126
127<h2 align="center">Options</h2>
128
129You can pass a hash of configuration options to `html-webpack-plugin`.
130Allowed values are as follows
131
132|Name|Type|Default|Description|
133|:--:|:--:|:-----:|:----------|
134|**`title`**|`{String}`|`Webpack App`|The title to use for the generated HTML document|
135|**`filename`**|`{String}`|`'index.html'`|The file to write the HTML to. Defaults to `index.html`. You can specify a subdirectory here too (eg: `assets/admin.html`)|
136|**`template`**|`{String}`|``|`webpack` relative or absolute path to the template. By default it will use `src/index.ejs` if it exists. Please see the [docs](https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md) for details|
137|**`templateContent`**|`{string\|Function\|false}`|false| Can be used instead of `template` to provide an inline template - please read the [Writing Your Own Templates](https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates) section |
138|**`templateParameters`**|`{Boolean\|Object\|Function}`| `false`| Allows to overwrite the parameters used in the template - see [example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/template-parameters) |
139|**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `true` or `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element - see the [inject:false example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/custom-insertion-position)|
140|**`scriptLoading`**|`{'blocking'\|'defer'}`|`'blocking'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. |
141|**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
142|**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|
143|**`base`**|`{Object\|String\|false}`|`false`|Inject a [`base`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) tag. E.g. `base: "https://example.com/path/page.html`|
144|**`minify`**|`{Boolean\|Object}`|`true` if `mode` is `'production'`, otherwise `false`|Controls if and in what ways the output should be minified. See [minification](#minification) below for more details.|
145|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|
146|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|
147|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|
148|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|
149|**[`chunksSortMode`](#plugins)**|`{String\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \| 'auto' \| 'manual' \| {Function}`|
150|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|
151|**`xhtml`**|`{Boolean}`|`false`|If `true` render the `link` tags as self-closing (XHTML compliant)|
152
153Here's an example webpack config illustrating how to use these options
154
155**webpack.config.js**
156```js
157{
158 entry: 'index.js',
159 output: {
160 path: __dirname + '/dist',
161 filename: 'index_bundle.js'
162 },
163 plugins: [
164 new HtmlWebpackPlugin({
165 title: 'My App',
166 filename: 'assets/admin.html'
167 })
168 ]
169}
170```
171
172### Generating Multiple HTML Files
173
174To generate more than one HTML file, declare the plugin more than
175once in your plugins array
176
177**webpack.config.js**
178```js
179{
180 entry: 'index.js',
181 output: {
182 path: __dirname + '/dist',
183 filename: 'index_bundle.js'
184 },
185 plugins: [
186 new HtmlWebpackPlugin(), // Generates default index.html
187 new HtmlWebpackPlugin({ // Also generate a test.html
188 filename: 'test.html',
189 template: 'src/assets/test.html'
190 })
191 ]
192}
193```
194
195### Writing Your Own Templates
196
197If the default generated HTML doesn't meet your needs you can supply
198your own template. The easiest way is to use the `template` option and pass a custom HTML file.
199The html-webpack-plugin will automatically inject all necessary CSS, JS, manifest
200and favicon files into the markup.
201
202Details of other template loaders are [documented here](https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md).
203
204```js
205plugins: [
206 new HtmlWebpackPlugin({
207 title: 'Custom template',
208 // Load a custom template (lodash by default)
209 template: 'index.html'
210 })
211]
212```
213
214**index.html**
215```html
216<!DOCTYPE html>
217<html>
218 <head>
219 <meta charset="utf-8"/>
220 <title><%= htmlWebpackPlugin.options.title %></title>
221 </head>
222 <body>
223 </body>
224</html>
225```
226
227If you already have a template loader, you can use it to parse the template.
228Please note that this will also happen if you specify the html-loader and use `.html` file as template.
229
230**webpack.config.js**
231```js
232module: {
233 loaders: [
234 { test: /\.hbs$/, loader: "handlebars-loader" }
235 ]
236},
237plugins: [
238 new HtmlWebpackPlugin({
239 title: 'Custom template using Handlebars',
240 template: 'index.hbs'
241 })
242]
243```
244
245You can use the `lodash` syntax out of the box. If the `inject` feature doesn't fit your needs and you want full control over the asset placement use the [default template](https://github.com/jaketrent/html-webpack-template/blob/86f285d5c790a6c15263f5cc50fd666d51f974fd/index.html) of the [html-webpack-template project](https://github.com/jaketrent/html-webpack-template) as a starting point for writing your own.
246
247The following variables are available in the template by default (you can extend them using the `templateParameters` option):
248
249- `htmlWebpackPlugin`: data specific to this plugin
250
251 - `htmlWebpackPlugin.options`: the options hash that was passed to
252 the plugin. In addition to the options actually used by this plugin,
253 you can use this hash to pass arbitrary data through to your template.
254
255 - `htmlWebpackPlugin.tags`: the prepared `headTags` and `bodyTags` Array to render the `<base>`, `<meta>`, `<script>` and `<link>` tags.
256 Can be used directly in templates and literals. For example:
257 ```html
258 <html>
259 <head>
260 <%= htmlWebpackPlugin.tags.headTags %>
261 </head>
262 <body>
263 <%= htmlWebpackPlugin.tags.bodyTags %>
264 </body>
265 </html>
266 ```
267
268 - `htmlWebpackPlugin.files`: direct access to the files used during the compilation.
269
270 ```typescript
271 publicPath: string;
272 js: string[];
273 css: string[];
274 manifest?: string;
275 favicon?: string;
276 ```
277
278
279- `webpackConfig`: the webpack configuration that was used for this compilation. This
280 can be used, for example, to get the `publicPath` (`webpackConfig.output.publicPath`).
281
282- `compilation`: the webpack [compilation object](https://webpack.js.org/api/compilation-object/).
283 This can be used, for example, to get the contents of processed assets and inline them
284 directly in the page, through `compilation.assets[...].source()`
285 (see [the inline template example](examples/inline/template.pug)).
286
287
288The template can also be directly inlined directly into the options object.
289⚠️ **`templateContent` does not allow to use webpack loaders for your template and will not watch for template file changes**
290
291**webpack.config.js**
292```js
293new HtmlWebpackPlugin({
294 templateContent: `
295 <html>
296 <body>
297 <h1>Hello World</h1>
298 </body>
299 </html>
300 `
301})
302```
303
304The `templateContent` can also access all `templateParameters` values.
305⚠️ **`templateContent` does not allow to use webpack loaders for your template and will not watch for template file changes**
306
307**webpack.config.js**
308```js
309new HtmlWebpackPlugin({
310 inject: false,
311 templateContent: ({htmlWebpackPlugin}) => `
312 <html>
313 <head>
314 ${htmlWebpackPlugin.tags.headTags}
315 </head>
316 <body>
317 <h1>Hello World</h1>
318 ${htmlWebpackPlugin.tags.bodyTags}
319 </body>
320 </html>
321 `
322})
323```
324
325### Filtering Chunks
326
327To include only certain chunks you can limit the chunks being used
328
329**webpack.config.js**
330```js
331plugins: [
332 new HtmlWebpackPlugin({
333 chunks: ['app']
334 })
335]
336```
337
338It is also possible to exclude certain chunks by setting the `excludeChunks` option
339
340**webpack.config.js**
341```js
342plugins: [
343 new HtmlWebpackPlugin({
344 excludeChunks: [ 'dev-helper' ]
345 })
346]
347```
348
349### Minification
350
351If the `minify` option is set to `true` (the default when webpack's `mode` is `'production'`),
352the generated HTML will be minified using [html-minifier-terser](https://github.com/DanielRuf/html-minifier-terser)
353and the following options:
354
355```js
356{
357 collapseWhitespace: true,
358 removeComments: true,
359 removeRedundantAttributes: true,
360 removeScriptTypeAttributes: true,
361 removeStyleLinkTypeAttributes: true,
362 useShortDoctype: true
363}
364```
365
366To use custom [html-minifier options](https://github.com/DanielRuf/html-minifier-terser#options-quick-reference)
367pass an object to `minify` instead. This object will not be merged with the defaults above.
368
369To disable minification during production mode set the `minify` option to `false`.
370
371### Meta Tags
372
373If the `meta` option is set the html-webpack-plugin will inject meta tags.
374For the default template the html-webpack-plugin will already provide a default for the `viewport` meta tag.
375
376Please take a look at this well maintained list of almost all [possible meta tags](https://github.com/joshbuchea/HEAD#meta).
377
378#### name/content meta tags
379
380Most meta tags are configured by setting a `name` and a `content` attribute.
381To add those use a key/value pair:
382
383**webpack.config.js**
384```js
385plugins: [
386 new HtmlWebpackPlugin({
387 'meta': {
388 'viewport': 'width=device-width, initial-scale=1, shrink-to-fit=no',
389 // Will generate: <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
390 'theme-color': '#4285f4'
391 // Will generate: <meta name="theme-color" content="#4285f4">
392 }
393 })
394]
395```
396
397#### Simulate http response headers
398
399The **http-equiv** attribute is essentially used to simulate a HTTP response header.
400This format is supported using an object notation which allows you to add any attribute:
401
402**webpack.config.js**
403```js
404plugins: [
405 new HtmlWebpackPlugin({
406 'meta': {
407 'Content-Security-Policy': { 'http-equiv': 'Content-Security-Policy', 'content': 'default-src https:' },
408 // Will generate: <meta http-equiv="Content-Security-Policy" content="default-src https:">
409 // Which equals to the following http header: `Content-Security-Policy: default-src https:`
410 'set-cookie': { 'http-equiv': 'set-cookie', content: 'name=value; expires=date; path=url' },
411 // Will generate: <meta http-equiv="set-cookie" content="value; expires=date; path=url">
412 // Which equals to the following http header: `set-cookie: value; expires=date; path=url`
413 }
414 })
415]
416```
417
418### Base Tag
419
420When the `base` option is used,
421html-webpack-plugin will inject a [base tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base).
422By default, a base tag will not be injected.
423
424The following two are identical and will both insert `<base href="http://example.com/some/page.html">`:
425
426```js
427new HtmlWebpackPlugin({
428 'base': 'http://example.com/some/page.html'
429})
430```
431
432```js
433new HtmlWebpackPlugin({
434 'base': { 'href': 'http://example.com/some/page.html' }
435})
436```
437
438The `target` can be specified with the corresponding key:
439
440```js
441new HtmlWebpackPlugin({
442 'base': {
443 'href': 'http://example.com/some/page.html',
444 'target': '_blank'
445 }
446})
447```
448
449which will inject the element `<base href="http://example.com/some/page.html" target="_blank">`.
450
451### Long Term Caching
452
453For long term caching add `contenthash/templatehash` to the filename.
454
455**Example:**
456
457```js
458plugins: [
459 new HtmlWebpackPlugin({
460 filename: 'index.[contenthash].html'
461 })
462]
463```
464
465`contenthash/templatehash` is the hash of the content of the output file.
466
467Optionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`
468
469* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512` or any other node.js supported hash type
470* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
471* `maxlength` - maximum length of the generated hash in chars
472
473**Defaults:** `[md5:contenthash:hex:9999]`
474
475### Events
476
477To allow other [plugins](https://github.com/webpack/docs/wiki/plugins) to alter the HTML this plugin executes
478[tapable](https://github.com/webpack/tapable/tree/master) hooks.
479
480The [lib/hooks.js](https://github.com/jantimon/html-webpack-plugin/blob/master/lib/hooks.js) contains all information
481about which values are passed.
482
483[![Concept flow uml](https://raw.githubusercontent.com/jantimon/html-webpack-plugin/master/flow.png)](https://github.com/jantimon/html-webpack-plugin/blob/master/flow.puml)
484
485#### `beforeAssetTagGeneration` hook
486
487```
488 AsyncSeriesWaterfallHook<{
489 assets: {
490 publicPath: string,
491 js: Array<{string}>,
492 css: Array<{string}>,
493 favicon?: string | undefined,
494 manifest?: string | undefined
495 },
496 outputName: string,
497 plugin: HtmlWebpackPlugin
498 }>
499```
500
501#### `alterAssetTags` hook
502
503```
504 AsyncSeriesWaterfallHook<{
505 assetTags: {
506 scripts: Array<HtmlTagObject>,
507 styles: Array<HtmlTagObject>,
508 meta: Array<HtmlTagObject>,
509 },
510 outputName: string,
511 plugin: HtmlWebpackPlugin
512 }>
513```
514
515#### `alterAssetTagGroups` hook
516
517```
518 AsyncSeriesWaterfallHook<{
519 headTags: Array<HtmlTagObject | HtmlTagObject>,
520 bodyTags: Array<HtmlTagObject | HtmlTagObject>,
521 outputName: string,
522 plugin: HtmlWebpackPlugin
523 }>
524```
525
526#### `afterTemplateExecution` hook
527
528```
529 AsyncSeriesWaterfallHook<{
530 html: string,
531 headTags: Array<HtmlTagObject | HtmlTagObject>,
532 bodyTags: Array<HtmlTagObject | HtmlTagObject>,
533 outputName: string,
534 plugin: HtmlWebpackPlugin,
535 }>
536```
537
538#### `beforeEmit` hook
539
540```
541 AsyncSeriesWaterfallHook<{
542 html: string,
543 outputName: string,
544 plugin: HtmlWebpackPlugin,
545 }>
546```
547
548#### `afterEmit` hook
549
550```
551 AsyncSeriesWaterfallHook<{
552 outputName: string,
553 plugin: HtmlWebpackPlugin
554 }>
555```
556
557Example implementation: [webpack-subresource-integrity](https://www.npmjs.com/package/webpack-subresource-integrity)
558
559**plugin.js**
560```js
561// If your plugin is direct dependent to the html webpack plugin:
562const HtmlWebpackPlugin = require('html-webpack-plugin');
563// If your plugin is using html-webpack-plugin as an optional dependency
564// you can use https://github.com/tallesl/node-safe-require instead:
565const HtmlWebpackPlugin = require('safe-require')('html-webpack-plugin');
566
567class MyPlugin {
568 apply (compiler) {
569 compiler.hooks.compilation.tap('MyPlugin', (compilation) => {
570 console.log('The compiler is starting a new compilation...')
571
572 // Static Plugin interface |compilation |HOOK NAME | register listener
573 HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
574 'MyPlugin', // <-- Set a meaningful name here for stacktraces
575 (data, cb) => {
576 // Manipulate the content
577 data.html += 'The Magic Footer'
578 // Tell webpack to move on
579 cb(null, data)
580 }
581 )
582 })
583 }
584}
585
586module.exports = MyPlugin
587```
588
589**webpack.config.js**
590```js
591plugins: [
592 new MyPlugin({ options: '' })
593]
594```
595
596Note that the callback must be passed the HtmlWebpackPluginData in order to pass this onto any other plugins listening on the same `beforeEmit` event
597
598<h2 align="center">Maintainers</h2>
599
600<table>
601 <tbody>
602 <tr>
603 <td align="center">
604 <img width="150" height="150"
605 src="https://avatars3.githubusercontent.com/u/4113649?v=3&s=150">
606 </br>
607 <a href="https://github.com/jantimon">Jan Nicklas</a>
608 </td>
609 <td align="center">
610 <img width="150" height="150"
611 src="https://avatars2.githubusercontent.com/u/4112409?v=3&s=150">
612 </br>
613 <a href="https://github.com/mastilver">Thomas Sileghem</a>
614 </td>
615 </tr>
616 <tbody>
617</table>
618
619
620## Backers
621
622Thank you to all our backers!
623If you want to support the project as well [become a sponsor](https://opencollective.com/html-webpack-plugin#sponsor) or a [a backer](https://opencollective.com/html-webpack-plugin#backer).
624
625<a href="https://opencollective.com/html-webpack-plugin/backer/0/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/0/avatar.svg?requireActive=false"></a>
626<a href="https://opencollective.com/html-webpack-plugin/backer/1/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/1/avatar.svg?requireActive=false"></a>
627<a href="https://opencollective.com/html-webpack-plugin/backer/2/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/2/avatar.svg?requireActive=false"></a>
628<a href="https://opencollective.com/html-webpack-plugin/backer/3/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/3/avatar.svg?requireActive=false"></a>
629<a href="https://opencollective.com/html-webpack-plugin/backer/4/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/4/avatar.svg?requireActive=false"></a>
630<a href="https://opencollective.com/html-webpack-plugin/backer/5/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/5/avatar.svg?requireActive=false"></a>
631<a href="https://opencollective.com/html-webpack-plugin/backer/6/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/6/avatar.svg?requireActive=false"></a>
632<a href="https://opencollective.com/html-webpack-plugin/backer/7/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/7/avatar.svg?requireActive=false"></a>
633<a href="https://opencollective.com/html-webpack-plugin/backer/8/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/8/avatar.svg?requireActive=false"></a>
634<a href="https://opencollective.com/html-webpack-plugin/backer/9/website?requireActive=false" target="_blank"><img src="https://opencollective.com/html-webpack-plugin/backer/9/avatar.svg?requireActive=false"></a>
635
636
637## Contributors
638
639This project exists thanks to all the people who contribute.
640
641You're free to contribute to this project by submitting [issues](https://github.com/jantimon/html-webpack-plugin/issues) and/or [pull requests](https://github.com/jantimon/html-webpack-plugin/pulls). This project is test-driven, so keep in mind that every change and new feature should be covered by tests.
642
643This project uses the [semistandard code style](https://github.com/Flet/semistandard).
644
645<a href="https://github.com/jantimon/html-webpack-plugin/graphs/contributors"><img src="https://opencollective.com/html-webpack-plugin/contributors.svg?width=890&button=false" /></a>
646
647
648[npm]: https://img.shields.io/npm/v/html-webpack-plugin.svg
649[npm-url]: https://npmjs.com/package/html-webpack-plugin
650
651[node]: https://img.shields.io/node/v/html-webpack-plugin.svg
652[node-url]: https://nodejs.org
653
654[deps]: https://david-dm.org/jantimon/html-webpack-plugin.svg
655[deps-url]: https://david-dm.org/jantimon/html-webpack-plugin
656
657[tests]: http://img.shields.io/travis/jantimon/html-webpack-plugin.svg
658[tests-url]: https://travis-ci.org/jantimon/html-webpack-plugin