TheHtmlWebpackPlugin
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 usinglodash templates, or use your ownloader.
npminstall --save-dev html-webpack-plugin
The plugin will generate an HTML5 file for you that includes all your webpackbundles in the body usingscript
tags. Add the plugin to your webpackconfiguration as follows:
const HtmlWebpackPlugin=require('html-webpack-plugin');const path=require('path');module.exports={ entry:'index.js', output:{ path: path.resolve(__dirname,'./dist'), filename:'index_bundle.js',}, plugins:[newHtmlWebpackPlugin()],};
This will generate a filedist/index.html
containing the following:
<!DOCTYPEhtml><html><head><metacharset="UTF-8"/><title>webpack App</title></head><body><scriptsrc="index_bundle.js"></script></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 theMiniCssExtractPlugin) then these will be included with<link>
tags in the<head>
element of generated HTML.
For all configuration options, please see theplugin documentation.
The plugin supports addons. For a list see thedocumentation.