Replacements for html-webpack-plugin
Motivation: Since webpack 5.109.0, webpack can generate HTML pages and use HTML files as entry points itself.
output.html (built-in, since webpack 5.109.0)
output.html generates a page for each JavaScript entry and injects its scripts and styles, like the plugin does without a template. Pages are named after their entry, so set output.htmlFilename to keep index.html.
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
plugins: [new HtmlWebpackPlugin({ title: 'My app' })],
output: {
htmlFilename: 'index.html',
html: {
title: 'My app',
meta: { charset: 'utf-8', viewport: 'width=device-width, initial-scale=1' }
}
}
}output.html also covers favicon, inject, scriptLoading, base, and more. See the output.html docs.
HTML entry points (built-in, since webpack 5.109.0)
If you use a template, make the HTML file the entry instead, and reference your script from it. webpack bundles the scripts and styles the page references, and rewrites their URLs. Remove html-loader or any other loader for .html files from module.rules, otherwise webpack skips its built-in HTML handling and emits no page.
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
plugins: [new HtmlWebpackPlugin({ template: './src/index.html' })]
entry: './src/index.html'
}<!-- src/index.html -->
<body>
<div id="root"></div>
<script src="./index.js"></script>
</body>The HTML is not run through a template engine, so templates using <%= htmlWebpackPlugin.options.title %> or templateParameters need to be rewritten as plain HTML, or set through output.html instead.