HTML Bundler Webpack Plugin

4.23.0 · active · verified Tue Apr 21

The `html-bundler-webpack-plugin` is a Webpack plugin designed to streamline the creation of single-page or multi-page websites directly from HTML templates. Unlike `html-webpack-plugin` which primarily injects bundled assets, this plugin treats HTML files as entry points, resolving all linked assets (scripts, styles, images, fonts) within the HTML and CSS itself. It handles asset processing and ensures correct output URLs after Webpack's build. Key differentiators include built-in support for numerous template engines like Eta, EJS, Handlebars, Nunjucks, Pug, Tempura, TwigJS, LiquidJS, and Markdown out-of-the-box. It also simplifies asset resolution in CSS without requiring `resolve-url-loader`. The plugin is currently in a very active development state, with version 4.23.0 being the latest stable release, and frequent updates addressing features, fixes, and Webpack compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic Webpack configuration to compile two HTML entry points (`index.html`, `about.html`), processing linked SCSS, JavaScript, and SVG/image assets. It demonstrates aliased paths and hashed output filenames.

import path from 'path';
import { fileURLToPath } from 'url';
import { HtmlBundlerPlugin } from 'html-bundler-webpack-plugin';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default {
  mode: 'development',
  output: {
    path: path.join(__dirname, 'dist'),
    clean: true,
  },
  resolve: {
    alias: {
      '@images': path.join(__dirname, 'src/images'),
      '@styles': path.join(__dirname, 'src/styles'),
      '@scripts': path.join(__dirname, 'src/scripts'),
    },
  },
  plugins: [
    new HtmlBundlerPlugin({
      entry: {
        index: './src/views/index.html',
        about: './src/views/about.html',
      },
      js: {
        filename: 'js/[name].[contenthash:8].js',
      },
      css: {
        filename: 'css/[name].[contenthash:8].css',
      },
    }),
  ],
  module: {
    rules: [
      {
        test: /\.(png|jpe?g|svg|ico)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'img/[name].[hash:8][ext]',
        },
      },
      {
        test: /\.(scss|css)$/,
        use: ['css-loader', 'sass-loader'],
      },
      {
        test: /\.ejs$/,
        loader: 'html-bundler-webpack-plugin/preprocessor/ejs',
      }
    ],
  },
};

// src/views/index.html
// <!DOCTYPE html>
// <html lang="en">
// <head>
//     <meta charset="UTF-8">
//     <meta name="viewport" content="width=device-width, initial-scale=1.0">
//     <title>Home</title>
//     <link href="@styles/main.scss" rel="stylesheet">
// </head>
// <body>
//     <h1>Welcome to the Homepage</h1>
//     <img src="@images/logo.svg" alt="Logo">
//     <script src="@scripts/main.js"></script>
// </body>
// </html>

// src/scripts/main.js
// console.log('Hello from main.js!');

// src/styles/main.scss
// body { font-family: sans-serif; .logo { width: 100px; } }

view raw JSON →