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