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