Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
H

html-webpack-plugin

> 前端框架
Open source

Simplifies creation of HTML files to serve your webpack bundles

10.7K stars0 likes2 views
WebsiteGitHub

About

Simplifies creation of HTML files to serve your webpack bundles

[![npm][npm]][npm-url] [![node][node]][node-url]

[![tests][tests]][tests-url]

HTML Webpack Plugin

Plugin that simplifies creation of HTML files to serve your bundles

[!WARNING]

html-webpack-plugin is 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.

Install

Webpack 5

bash
  npm i --save-dev html-webpack-plugin
bash
  yarn add --dev html-webpack-plugin

Webpack 4

bash
  npm i --save-dev html-webpack-plugin@4
bash
  yarn add --dev html-webpack-plugin@4

This 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.

Sponsors

Thanks for supporting the ongoing improvements to the html-webpack-plugin!

Zero Config

The html-webpack-plugin works without configuration.
It's a great addition to the ⚙️ webpack-config-plugins.

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

  • webpack-subresource-integrity for enhanced asset security
  • appcache-webpack-plugin for iOS and Android offline usage
  • favicons-webpack-plugin which generates favicons and icons for iOS, Android and desktop browsers
  • html-webpack-harddisk-plugin can be used to always write to disk the html file, useful when webpack-dev-server / HMR are being used
  • html-webpack-inline-svg-plugin to inline SVGs in the resulting HTML file.
  • html-webpack-exclude-assets-plugin for excluding assets using regular expressions
  • html-webpack-include-assets-plugin for including lists of js or css file paths (such as those copied by the copy-webpack-plugin).
  • html-webpack-injector to inject chunks in head or body (different locations ) of same html document.
  • resource-hints-webpack-plugin to add resource hints for faster initial page loads using <link rel='preload'> and <link rel='prefetch'>
  • link-media-html-webpack-plugin allows for injected stylesheet <link /> tags to have their media attribute set automatically; useful for providing specific desktop/mobile/print etc. stylesheets that the browser will conditionally download
  • html-webpack-inline-style-plugin for inlining styles to HTML elements using juice. Useful for email generation automatisation.
  • html-webpack-exclude-empty-assets-plugin removes empty assets from being added to the html. This fixes some problems with extract-text-plugin with webpack 4.
  • webpack-concat-plugin for concat and uglify files that needn't to be webpack bundles(for legacy files) and inject to html-webpack-plugin.
  • html-webpack-link-type-plugin adds a configurable mimetype to resources injected as links (such as adding type="text/css" to external stylesheets) for compatibility with "strict mode".
  • csp-html-webpack-plugin to add Content Security Policy meta tags to the HTML output
  • strict-csp-html-webpack-plugin to add a strict Content-Security-Policy (CSP) as a meta tag to the HTML output. A strict CSP is specifically efficient against XSS attacks.
  • webpack-nomodule-plugin allows you to add a nomodule attribute to specific injected scripts, which prevents the scripts from being loaded by newer browsers. Good for limiting loads of polyfills.
  • html-webpack-skip-assets-plugin Skip adding certain output files to the html file. Built as a drop-in replacement for html-webpack-exclude-assets-plugin and works with newer html-webpack-plugin versions
  • html-webpack-inject-preload allows to add preload links <link rel='preload'> anywhere you want.
  • inject-body-webpack-plugin is a simple method of injecting a custom HTML string into the body.
  • html-webpack-plugin-django a Webpack plugin to inject Django static tags.
  • html-webpack-inject-attributes-plugin add extra attributes to inject assetTags.
  • js-entry-webpack-plugin creates webpack bundles into your js entry

Usage

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

javascript
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

xml
<!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.

Options

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

Issues· 49 open

View all issuesOpen on GitHub
  • #1872

    CVE-2022-37620 html-minifier-terser dependency vulnerability

    Updated Sep 23, 2026
  • #1885

    Security: HTML attribute injection in generated tags due to missing escaping

    Updated Mar 17, 2026
  • #1883

    Delete existing links for css and js

    Updated Jan 28, 2026
  • #1638

    [contenthash] doesn't match between the assets in hooks and emitted output

    Updated Jan 2, 2026
  • #1701

    Support opt-out the automatically added trailing slash for publicPath

    wontfixUpdated Jan 2, 2026
  • #1394

    Reconsider lang attribute

    wontfixUpdated Jan 2, 2026
  • #1723

    HtmlWebpackPLugin ignores Rule.generator.publicPath value

    wontfixUpdated Jan 2, 2026
  • #1685

    Possible to control placement order of base tags?

    wontfixUpdated Jan 2, 2026
  • #1369

    Can't get assets by entry point chunk name in v4

    wontfixUpdated Jan 2, 2026
  • #1745

    Dev server is not rebuilding the HTML file when using templating

    wontfixUpdated Jan 2, 2026

> Tags

JavaScripthtml-webpack-pluginwebpack

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架