40e9596c866da4acebd273bbb3084d5c293e8d57a9fcbece5ddadafa199910038729c2c6dd8de0899820533c623cffbe794eddec74bc8206bb6355870effe5 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. ### `Plugins`
  30. 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
  31. * [webpack-subresource-integrity](https://www.npmjs.com/package/webpack-subresource-integrity) for enhanced asset security
  32. * [appcache-webpack-plugin](https://github.com/lettertwo/appcache-webpack-plugin) for iOS and Android offline usage
  33. * [favicons-webpack-plugin](https://github.com/jantimon/favicons-webpack-plugin) which generates favicons and icons for iOS, Android and desktop browsers
  34. * [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
  35. * [html-webpack-inline-source-plugin](https://github.com/DustinJackson/html-webpack-inline-source-plugin) to inline your assets in the resulting HTML file
  36. * [html-webpack-inline-svg-plugin](https://github.com/thegc/html-webpack-inline-svg-plugin) to inline SVGs in the resulting HTML file.
  37. * [html-webpack-exclude-assets-plugin](https://github.com/jamesjieye/html-webpack-exclude-assets-plugin) for excluding assets using regular expressions
  38. * [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).
  39. * [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
  40. * [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
  41. * [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'>`
  42. * [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
  43. * [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
  44. * [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>`
  45. * [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.
  46. * [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.
  47. * [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.
  48. <h2 align="center">Usage</h2>
  49. The plugin will generate an HTML5 file for you that includes all your `webpack`
  50. bundles in the body using `script` tags. Just add the plugin to your `webpack`
  51. config as follows:
  52. **webpack.config.js**
  53. ```js
  54. const HtmlWebpackPlugin = require('html-webpack-plugin')
  55. module.exports = {
  56. entry: 'index.js',
  57. output: {
  58. path: __dirname + '/dist',
  59. filename: 'index_bundle.js'
  60. },
  61. plugins: [
  62. new HtmlWebpackPlugin()
  63. ]
  64. }
  65. ```
  66. This will generate a file `dist/index.html` containing the following
  67. ```html
  68. <!DOCTYPE html>
  69. <html>
  70. <head>
  71. <meta charset="UTF-8">
  72. <title>Webpack App</title>
  73. </head>
  74. <body>
  75. <script src="index_bundle.js"></script>
  76. </body>
  77. </html>
  78. ```
  79. If you have multiple `webpack` entry points, they will all be included with `script` tags in the generated HTML.
  80. If you have any CSS assets in webpack's output (for example, CSS extracted with the [ExtractTextPlugin](https://github.com/webpack/extract-text-webpack-plugin))
  81. then these will be included with `<link>` tags in the HTML head.
  82. If you have plugins that make use of it, `html-webpack-plugin` should be ordered first before any of the integrated plugins.
  83. <h2 align="center">Options</h2>
  84. You can pass a hash of configuration options to `html-webpack-plugin`.
  85. Allowed values are as follows
  86. |Name|Type|Default|Description|
  87. |:--:|:--:|:-----:|:----------|
  88. |**[`title`](#)**|`{String}`|``|The title to use for the generated HTML document|
  89. |**[`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`)|
  90. |**[`template`](#)**|`{String}`|``|`webpack` require path to the template. Please see the [docs](https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md) for details|
  91. |**[`templateParameters`](#)**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template |
  92. |**[`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|
  93. |**[`favicon`](#)**|`{String}`|``|Adds the given favicon path to the output HTML|
  94. |**[`meta`](#)**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|
  95. |**[`minify`](#)**|`{Boolean\|Object}`|`true`|Pass [html-minifier](https://github.com/kangax/html-minifier#options-quick-reference)'s options as object to minify the output|
  96. |**[`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|
  97. |**[`cache`](#)**|`{Boolean}`|`true`|Emit the file only if it was changed|
  98. |**[`showErrors`](#)**|`{Boolean}`|`true`|Errors details will be written into the HTML page|
  99. |**[`chunks`](#)**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|
  100. |**[`chunksSortMode`](#plugins)**|`{String\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \| 'auto' \| 'dependency' \| 'manual' \| {Function}`|
  101. |**[`excludeChunks`](#)**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|
  102. |**[`xhtml`](#)**|`{Boolean}`|`false`|If `true` render the `link` tags as self-closing (XHTML compliant)|
  103. Here's an example webpack config illustrating how to use these options
  104. **webpack.config.js**
  105. ```js
  106. {
  107. entry: 'index.js',
  108. output: {
  109. path: __dirname + '/dist',
  110. filename: 'index_bundle.js'
  111. },
  112. plugins: [
  113. new HtmlWebpackPlugin({
  114. title: 'My App',
  115. filename: 'assets/admin.html'
  116. })
  117. ]
  118. }
  119. ```
  120. ### `Generating Multiple HTML Files`
  121. To generate more than one HTML file, declare the plugin more than
  122. once in your plugins array
  123. **webpack.config.js**
  124. ```js
  125. {
  126. entry: 'index.js',
  127. output: {
  128. path: __dirname + '/dist',
  129. filename: 'index_bundle.js'
  130. },
  131. plugins: [
  132. new HtmlWebpackPlugin(), // Generates default index.html
  133. new HtmlWebpackPlugin({ // Also generate a test.html
  134. filename: 'test.html',
  135. template: 'src/assets/test.html'
  136. })
  137. ]
  138. }
  139. ```
  140. ### `Writing Your Own Templates`
  141. If the default generated HTML doesn't meet your needs you can supply
  142. your own template. The easiest way is to use the `template` option and pass a custom HTML file.
  143. The html-webpack-plugin will automatically inject all necessary CSS, JS, manifest
  144. and favicon files into the markup.
  145. ```js
  146. plugins: [
  147. new HtmlWebpackPlugin({
  148. title: 'Custom template',
  149. // Load a custom template (lodash by default see the FAQ for details)
  150. template: 'index.html'
  151. })
  152. ]
  153. ```
  154. **index.html**
  155. ```html
  156. <!DOCTYPE html>
  157. <html>
  158. <head>
  159. <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
  160. <title><%= htmlWebpackPlugin.options.title %></title>
  161. </head>
  162. <body>
  163. </body>
  164. </html>
  165. ```
  166. If you already have a template loader, you can use it to parse the template.
  167. Please note that this will also happen if you specifiy the html-loader and use `.html` file as template.
  168. **webpack.config.js**
  169. ```js
  170. module: {
  171. loaders: [
  172. { test: /\.hbs$/, loader: "handlebars" }
  173. ]
  174. },
  175. plugins: [
  176. new HtmlWebpackPlugin({
  177. title: 'Custom template using Handlebars',
  178. template: 'index.hbs'
  179. })
  180. ]
  181. ```
  182. 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.
  183. The following variables are available in the template:
  184. - `htmlWebpackPlugin`: data specific to this plugin
  185. - `htmlWebpackPlugin.files`: a massaged representation of the
  186. `assetsByChunkName` attribute of webpack's [stats](https://github.com/webpack/docs/wiki/node.js-api#stats)
  187. object. It contains a mapping from entry point name to the bundle filename, eg:
  188. ```json
  189. "htmlWebpackPlugin": {
  190. "files": {
  191. "css": [ "main.css" ],
  192. "js": [ "assets/head_bundle.js", "assets/main_bundle.js"],
  193. "chunks": {
  194. "head": {
  195. "entry": "assets/head_bundle.js",
  196. "css": [ "main.css" ]
  197. },
  198. "main": {
  199. "entry": "assets/main_bundle.js",
  200. "css": []
  201. },
  202. }
  203. }
  204. }
  205. ```
  206. If you've set a publicPath in your webpack config this will be reflected
  207. correctly in this assets hash.
  208. - `htmlWebpackPlugin.options`: the options hash that was passed to
  209. the plugin. In addition to the options actually used by this plugin,
  210. you can use this hash to pass arbitrary data through to your template.
  211. - `webpack`: the webpack [stats](https://github.com/webpack/docs/wiki/node.js-api#stats)
  212. object. Note that this is the stats object as it was at the time the HTML template
  213. was emitted and as such may not have the full set of stats that are available
  214. after the webpack run is complete.
  215. - `webpackConfig`: the webpack configuration that was used for this compilation. This
  216. can be used, for example, to get the `publicPath` (`webpackConfig.output.publicPath`).
  217. - `compilation`: the webpack [compilation](https://webpack.js.org/api/compilation/) object.
  218. This can be used, for example, to get the contents of processed assets and inline them
  219. directly in the page, through `compilation.assets[...].source()`
  220. (see [the inline template example](examples/inline/template.jade)).
  221. ### `Filtering Chunks`
  222. To include only certain chunks you can limit the chunks being used
  223. **webpack.config.js**
  224. ```js
  225. plugins: [
  226. new HtmlWebpackPlugin({
  227. chunks: ['app']
  228. })
  229. ]
  230. ```
  231. It is also possible to exclude certain chunks by setting the `excludeChunks` option
  232. **webpack.config.js**
  233. ```js
  234. plugins: [
  235. new HtmlWebpackPlugin({
  236. excludeChunks: [ 'dev-helper' ]
  237. })
  238. ]
  239. ```
  240. ### `Events`
  241. To allow other [plugins](https://github.com/webpack/docs/wiki/plugins) to alter the HTML this plugin executes the following events:
  242. #### `Sync`
  243. * `html-webpack-plugin-alter-chunks`
  244. #### `Async`
  245. * `html-webpack-plugin-before-html-generation`
  246. * `html-webpack-plugin-before-html-processing`
  247. * `html-webpack-plugin-alter-asset-tags`
  248. * `html-webpack-plugin-after-html-processing`
  249. * `html-webpack-plugin-after-emit`
  250. Example implementation: [html-webpack-harddisk-plugin](https://github.com/jantimon/html-webpack-harddisk-plugin)
  251. **plugin.js**
  252. ```js
  253. function MyPlugin(options) {
  254. // Configure your plugin with options...
  255. }
  256. MyPlugin.prototype.apply = function (compiler) {
  257. compiler.plugin('compilation', (compilation) => {
  258. console.log('The compiler is starting a new compilation...');
  259. compilation.plugin(
  260. 'html-webpack-plugin-before-html-processing',
  261. (data, cb) => {
  262. data.html += 'The Magic Footer'
  263. cb(null, data)
  264. }
  265. )
  266. })
  267. }
  268. module.exports = MyPlugin
  269. ```
  270. **webpack.config.js**
  271. ```js
  272. plugins: [
  273. new MyPlugin({ options: '' })
  274. ]
  275. ```
  276. Note that the callback must be passed the HtmlWebpackPluginData in order to pass this onto any other plugins listening on the same `html-webpack-plugin-before-html-processing` event
  277. <h2 align="center">Maintainers</h2>
  278. <table>
  279. <tbody>
  280. <tr>
  281. <td align="center">
  282. <img width="150" height="150"
  283. src="https://avatars3.githubusercontent.com/u/4113649?v=3&s=150">
  284. </br>
  285. <a href="https://github.com/jantimon">Jan Nicklas</a>
  286. </td>
  287. <td align="center">
  288. <img width="150" height="150"
  289. src="https://avatars2.githubusercontent.com/u/4112409?v=3&s=150">
  290. </br>
  291. <a href="https://github.com/mastilver">Thomas Sileghem</a>
  292. </td>
  293. </tr>
  294. <tbody>
  295. </table>
  296. [npm]: https://img.shields.io/npm/v/html-webpack-plugin.svg
  297. [npm-url]: https://npmjs.com/package/html-webpack-plugin
  298. [node]: https://img.shields.io/node/v/html-webpack-plugin.svg
  299. [node-url]: https://nodejs.org
  300. [deps]: https://david-dm.org/jantimon/html-webpack-plugin.svg
  301. [deps-url]: https://david-dm.org/jantimon/html-webpack-plugin
  302. [tests]: http://img.shields.io/travis/jantimon/html-webpack-plugin.svg
  303. [tests-url]: https://travis-ci.org/jantimon/html-webpack-plugin
  304. ## Contributors
  305. This project exists thanks to all the people who contribute.
  306. 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.
  307. This project uses the [semistandard code style](https://github.com/Flet/semistandard).
  308. <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>
  309. ## Backers
  310. Thank you to all our backers! 🙏 [Become a backer](https://opencollective.com/html-webpack-plugin#backer)
  311. <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>
  312. <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>
  313. <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>
  314. <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>
  315. <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>
  316. <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>
  317. <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>
  318. <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>
  319. <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>
  320. <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>
  321. ## Sponsors
  322. Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor](https://opencollective.com/html-webpack-plugin#sponsor)
  323. <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>
  324. <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>
  325. <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>
  326. <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>
  327. <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>
  328. <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>
  329. <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>
  330. <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>
  331. <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>
  332. <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>