Simplifies creation of HTML files to serve your webpack bundles
Simplifies creation of HTML files to serve your webpack bundles
[![npm][npm]][npm-url] [![node][node]][node-url]
[![tests][tests]][tests-url]
Plugin that simplifies creation of HTML files to serve your bundles
[!WARNING]
html-webpack-pluginis deprecated — webpack scaffolds the HTML around your bundles itself and no longer needs it.Read the Native HTML guide and follow its migration guide.
Existing setups keep working: webpack only emits a page when you ask for one with
output.html, so this plugin keeps owning your HTML until you migrate.
npm i --save-dev html-webpack-plugin yarn add --dev html-webpack-plugin npm i --save-dev html-webpack-plugin@4 yarn add --dev html-webpack-plugin@4This is a webpack 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
your own template using lodash templates or use your own loader.
Thanks for supporting the ongoing improvements to the html-webpack-plugin!
The html-webpack-plugin works without configuration.
It's a great addition to the ⚙️ webpack-config-plugins.
The html-webpack-plugin provides hooks to extend it to your needs. There are already some really powerful plugins which can be integrated with zero configuration
head or body (different locations ) of same html document.<link rel='preload'> and <link rel='prefetch'><link /> tags to have their media attribute set automatically; useful for providing specific desktop/mobile/print etc. stylesheets that the browser will conditionally downloadmeta tag to the HTML output. A strict CSP is specifically efficient against XSS attacks.nomodule attribute to specific injected scripts, which prevents the scripts from being loaded by newer browsers. Good for limiting loads of polyfills.The plugin will generate an HTML5 file for you that includes all your webpack
bundles in the head using script tags. Just add the plugin to your webpack
config as follows:
webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "index.js",
output: {
path: __dirname + "/dist",
filename: "index_bundle.js",
},
plugins: [new HtmlWebpackPlugin()],
};This will generate a file dist/index.html containing the following
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Webpack App</title>
<script defer src="index_bundle.js"></script>
</head>
<body></body>
</html>If you have multiple webpack entry points, they will all be included with script tags in the generated HTML.
If you have any CSS assets in webpack's output (for example, CSS extracted with the mini-css-extract-plugin)
then these will be included with <link> tags in the HTML head.
If you have plugins that make use of it, html-webpack-plugin should be ordered first before any of the integrated Plugins.
You can pass a hash of configuration options to html-webpack-plugin.
Allowed values are as follows:
| Name | Type | Default | Description |
|---|---|---|---|
title |
{String} |
Webpack App |
The title to use for the generated HTML document |
filename |
{String|Function} |
'index.html' |
The file to write the HTML to. Defaults to index.html. You can specify a subdirectory here too (eg: assets/admin.html). The [name] placeholder will be replaced with the entry name. Can also be a function e.g. (entryName) => entryName + '.html'. |
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 for details |
CVE-2022-37620 html-minifier-terser dependency vulnerability
Security: HTML attribute injection in generated tags due to missing escaping
Delete existing links for css and js
[contenthash] doesn't match between the assets in hooks and emitted output
Support opt-out the automatically added trailing slash for publicPath
Reconsider lang attribute
HtmlWebpackPLugin ignores Rule.generator.publicPath value
Possible to control placement order of base tags?
Can't get assets by entry point chunk name in v4
Dev server is not rebuilding the HTML file when using templating