in 2023 can be used the html-bundler-webpack-plugin instead of html-webpack-plugin
. This plugin extracts JS, CSS, assets from source files used in HTML. Using this plugin, the entrypoint
is the HTML file and all used source resources are extracted automatically.
You can load individual source scripts, styles and images directly in HTML.
The path to source file must be relative by HTML file or an Webpack alias.
index.html
<!DOCTYPE html>
<html
<head>
...
<!-- load source file of style -->
<link rel="stylesheet" href="./style/default.scss">
</head>
<body>
<h1>Home</h1>
<!-- load source file of image -->
<img src="./images/homepage.png">
<!-- load source file of script -->
<script src="./js/main.js"></script>
</body>
</html>
example.html
<!DOCTYPE html>
<html
<head>
...
<!-- load source file of style -->
<link rel="stylesheet" href="./style/default.scss">
</head>
<body>
<h1>Example</h1>
<!-- load source file of script -->
<script src="./js/example.js"></script>
</body>
</html>
Install plugin:
npm install html-bundler-webpack-plugin --save-dev
Change your webpack.config.js according to the following minimal configuration:
const path = require('path');
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
module.exports = {
output: {
path: path.join(__dirname, 'dist/'),
publicPath: '/',
},
entry: {
// define HTML files here
index: './src/index.html', // => dist/index.html
example: './src/example.html', // => dist/example.html
// ...
},
plugins: [
new HtmlBundlerPlugin({
js: {
// output filename of extracted JS from source script loaded in HTML via `<script>` tag
filename: 'assets/js/[name].[contenthash:8].js',
},
css: {
// output filename of extracted CSS from source style loaded in HTML via `<link>` tag
filename: 'assets/css/[name].[contenthash:8].css',
},
}),
],
module: {
rules: [
// Note: enable processing of HTML files from entry
{
test: /\.html$/,
loader: HtmlBundlerPlugin.loader, // HTML loader
},
// styles
{
test: /\.(css|sass|scss)$/,
use: ['css-loader', 'sass-loader'],
},
// images
{
test: /\.(png|jpe?g|svg|ico)/,
type: 'asset/resource',
generator: {
filename: 'assets/img/[name].[hash:8][ext]',
},
},
],
},
};
The generated HTML contains output hashed filenames. Source styles, scripts and images are automatically processed and placed in the output directory dist/
.
The generated index.html:
<!DOCTYPE html>
<html>
<head>
...
<link href="/assets/css/default.f57966f4.css" rel="stylesheet">
</head>
<body>
<h1>Home</h1>
<img src="/assets/img/homepage.d2f4b855.png">
<script src="/assets/js/main.b855d8f4.js"></script>
</body>
</html>
Note
No longer needed to import styles in JavaScript.
No longer needed to define a JS file as the entrypoint.
Now it works intuitively, right in the HTML.