web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

660 lines
29 KiB

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