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