Exports HTML as string. HTML is minimized when the compiler demands.
To begin, you'll need to install html-loader:
npm install --save-dev html-loaderThen add the plugin to your webpack config. For example:
file.js
import html from "./file.html";webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", }, ], }, };| Name | Type | Default | Description |
|---|---|---|---|
sources | {Boolean|Object} | true | Enables/Disables sources handling |
preprocessor | {Function} | undefined | Allows pre-processing of content before handling |
minimize | {Boolean|Object} | true in production mode, otherwise false | Tell html-loader to minimize HTML |
esModule | {Boolean} | true | Enable/disable ES modules syntax |
Type: Boolean|Object Default: true
By default every loadable attributes (for example - <img src="image.png">) is imported (const img = require('./image.png') or import img from "./image.png""). You may need to specify loaders for images in your configuration (recommended asset modules).
Supported tags and attributes:
- the
srcattribute of theaudiotag - the
srcattribute of theembedtag - the
srcattribute of theimgtag - the
srcsetattribute of theimgtag - the
srcattribute of theinputtag - the
dataattribute of theobjecttag - the
srcattribute of thescripttag - the
hrefattribute of thescripttag - the
xlink:hrefattribute of thescripttag - the
srcattribute of thesourcetag - the
srcsetattribute of thesourcetag - the
srcattribute of thetracktag - the
posterattribute of thevideotag - the
srcattribute of thevideotag - the
xlink:hrefattribute of theimagetag - the
hrefattribute of theimagetag - the
xlink:hrefattribute of theusetag - the
hrefattribute of theusetag - the
hrefattribute of thelinktag when therelattribute containsstylesheet,icon,shortcut icon,mask-icon,apple-touch-icon,apple-touch-icon-precomposed,apple-touch-startup-image,manifest,prefetch,preloador when theitempropattribute isimage,logo,screenshot,thumbnailurl,contenturl,downloadurl,duringmedia,embedurl,installurl,layoutimage - the
imagesrcsetattribute of thelinktag when therelattribute containsstylesheet,icon,shortcut icon,mask-icon,apple-touch-icon,apple-touch-icon-precomposed,apple-touch-startup-image,manifest,prefetch,preload - the
contentattribute of themetatag when thenameattribute ismsapplication-tileimage,msapplication-square70x70logo,msapplication-square150x150logo,msapplication-wide310x150logo,msapplication-square310x310logo,msapplication-config,twitter:imageor when thepropertyattribute isog:image,og:image:url,og:image:secure_url,og:audio,og:audio:secure_url,og:video,og:video:secure_url,vk:imageor when theitempropattribute isimage,logo,screenshot,thumbnailurl,contenturl,downloadurl,duringmedia,embedurl,installurl,layoutimage - the
icon-urivalue component incontentattribute of themetatag when thenameattribute ismsapplication-task
The true value enables processing of all default elements and attributes, the false disable processing of all attributes.
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { // Disables attributes processing sources: false, }, }, ], }, };Allows you to specify which tags and attributes to process, filter them, filter urls and process sources starts with /.
For example:
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { sources: { list: [ // All default supported tags and attributes "...", { tag: "img", attribute: "data-src", type: "src", }, { tag: "img", attribute: "data-srcset", type: "srcset", }, ], urlFilter: (attribute, value, resourcePath) => { // The `attribute` argument contains a name of the HTML attribute. // The `value` argument contains a value of the HTML attribute. // The `resourcePath` argument contains a path to the loaded HTML file. if (/example\.pdf$/.test(value)) { return false; } return true; }, }, }, }, ], }, };Type: Array Default: supported tags and attributes.
Allows to setup which tags and attributes to process and how, and the ability to filter some of them.
Using ... syntax allows you to extend default supported tags and attributes.
For example:
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { sources: { list: [ // All default supported tags and attributes "...", { tag: "img", attribute: "data-src", type: "src", }, { tag: "img", attribute: "data-srcset", type: "srcset", }, { // Tag name tag: "link", // Attribute name attribute: "href", // Type of processing, can be `src` or `scrset` type: "src", // Allow to filter some attributes filter: (tag, attribute, attributes, resourcePath) => { // The `tag` argument contains a name of the HTML tag. // The `attribute` argument contains a name of the HTML attribute. // The `attributes` argument contains all attributes of the tag. // The `resourcePath` argument contains a path to the loaded HTML file. if (/my-html\.html$/.test(resourcePath)) { return false; } if (!/stylesheet/i.test(attributes.rel)) { return false; } if ( attributes.type && attributes.type.trim().toLowerCase() !== "text/css" ) { return false; } return true; }, }, ], }, }, }, ], }, };If the tag name is not specified it will process all the tags.
You can use your custom filter to specify html elements to be processed.
For example:
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { sources: { list: [ { // Attribute name attribute: "src", // Type of processing, can be `src` or `scrset` type: "src", // Allow to filter some attributes (optional) filter: (tag, attribute, attributes, resourcePath) => { // The `tag` argument contains a name of the HTML tag. // The `attribute` argument contains a name of the HTML attribute. // The `attributes` argument contains all attributes of the tag. // The `resourcePath` argument contains a path to the loaded HTML file. // choose all HTML tags except img tag return tag.toLowerCase() !== "img"; }, }, ], }, }, }, ], }, };Filter can also be used to extend the supported elements and attributes.
For example, filter can help process meta tags that reference assets:
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { sources: { list: [ { tag: "meta", attribute: "content", type: "src", filter: (tag, attribute, attributes, resourcePath) => { if ( attributes.value === "og:image" || attributes.name === "twitter:image" ) { return true; } return false; }, }, ], }, }, }, ], }, };Note: source with a tag option takes precedence over source without.
Filter can be used to disable default sources.
For example:
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { sources: { list: [ "...", { tag: "img", attribute: "src", type: "src", filter: () => false, }, ], }, }, }, ], }, };Type: Function Default: undefined
Allow to filter urls. All filtered urls will not be resolved (left in the code as they were written). All non requestable sources (for example <img src="javascript:void(0)">) do not handle by default.
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { sources: { urlFilter: (attribute, value, resourcePath) => { // The `attribute` argument contains a name of the HTML attribute. // The `value` argument contains a value of the HTML attribute. // The `resourcePath` argument contains a path to the loaded HTML file. if (/example\.pdf$/.test(value)) { return false; } return true; }, }, }, }, ], }, };Type: Function Default: undefined
Allows pre-processing of content before handling.
âš You should always return valid HTML
file.hbs
You can set the preprocessor option as a Function instance.
webpack.config.js
const Handlebars = require("handlebars"); module.exports = { module: { rules: [ { test: /\.hbs$/i, loader: "html-loader", options: { preprocessor: (content, loaderContext) => { let result; try { result = Handlebars.compile(content)({ firstname: "Value", lastname: "OtherValue", }); } catch (error) { loaderContext.emitError(error); return content; } return result; }, }, }, ], }, };You can also set the preprocessor option as an asynchronous function instance.
For example:
webpack.config.js
const Handlebars = require("handlebars"); module.exports = { module: { rules: [ { test: /\.hbs$/i, loader: "html-loader", options: { preprocessor: async (content, loaderContext) => { let result; try { result = await Handlebars.compile(content)({ firstname: "Value", lastname: "OtherValue", }); } catch (error) { await loaderContext.emitError(error); return content; } return result; }, }, }, ], }, };Type: Boolean|Object Default: true in production mode, otherwise false
Tell html-loader to minimize HTML.
The enabled rules for minimizing by default are the following ones:
({ caseSensitive: true, collapseWhitespace: true, conservativeCollapse: true, keepClosingSlash: true, minifyCSS: true, minifyJS: true, removeComments: true, removeRedundantAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, });webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { minimize: true, }, }, ], }, };webpack.config.js
See html-minifier-terser's documentation for more information on the available options.
The default rules can be overridden using the following options in your webpack.conf.js
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { minimize: { removeComments: false, collapseWhitespace: false, }, }, }, ], }, };The default rules can be extended:
webpack.config.js
const { defaultMinimizerOptions } = require("html-loader"); module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { minimize: { ...defaultMinimizerOptions, removeComments: false, collapseWhitespace: false, }, }, }, ], }, };Type: Boolean Default: true
By default, html-loader generates JS modules that use the ES modules syntax. There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.
You can enable a CommonJS modules syntax using:
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { esModule: false, }, }, ], }, };With <!-- webpackIgnore: true --> comment, can to disable sources handling for next tag.
<!-- Disabled url handling for the src attribute --> <!-- webpackIgnore: true --> <img src="image.png" /> <!-- Disabled url handling for the src and srcset attributes --> <!-- webpackIgnore: true --> <img srcset="image.png 480w, image.png 768w" src="image.png" alt="Elva dressed as a fairy" /> <!-- Disabled url handling for the content attribute --> <!-- webpackIgnore: true --> <meta itemprop="image" content="./image.png" /> <!-- Disabled url handling for the href attribute --> <!-- webpackIgnore: true --> <link rel="icon" type="image/png" sizes="192x192" href="./image.png" />With resolve.roots can specify a list of directories where requests of server-relative URLs (starting with '/') are resolved.
webpack.config.js
module.exports = { context: __dirname, module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: {}, }, { test: /\.jpg$/, type: "asset/resource", }, ], }, resolve: { roots: [path.resolve(__dirname, "fixtures")], }, };file.html
<img src="/image.jpg" />// => image.jpg in __dirname/fixtures will be resolvedwebpack.config.js
module.exports = { module: { rules: [ { test: /\.jpg$/, type: "asset/resource", }, { test: /\.png$/, type: "asset/inline", }, ], }, output: { publicPath: "http://cdn.example.com/[fullhash]/", }, };file.html
<img src="image.jpg" data-src="image2x.png" />index.js
require("html-loader!./file.html"); // => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg" data-src="image2x.png">'require('html-loader?{"sources":{"list":[{"tag":"img","attribute":"data-src","type":"src"}]}}!./file.html'); // => '<img src="image.jpg" data-src="data:image/png;base64,..." >'require('html-loader?{"sources":{"list":[{"tag":"img","attribute":"src","type":"src"},{"tag":"img","attribute":"data-src","type":"src"}]}}!./file.html'); // => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg" data-src="data:image/png;base64,..." >'script.file.js
console.log(document);style.file.css
a { color: red; }file.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Title of the document</title> <link rel="stylesheet" type="text/css" href="./style.file.css" /> </head> <body> Content of the document...... <script src="./script.file.js"></script> </body> </html>webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/, type: "asset/resource", generator: { filename: "[name][ext]", }, }, { test: /\.html$/i, use: ["extract-loader", "html-loader"], }, { test: /\.js$/i, exclude: /\.file.js$/i, loader: "babel-loader", }, { test: /\.file.js$/i, type: "asset/resource", }, { test: /\.css$/i, exclude: /\.file.css$/i, loader: "css-loader", }, { test: /\.file.css$/i, type: "asset/resource", }, ], }, };You can use any template system. Below is an example for handlebars.
file.hbs
webpack.config.js
const Handlebars = require("handlebars"); module.exports = { module: { rules: [ { test: /\.hbs$/i, loader: "html-loader", options: { preprocessor: (content, loaderContext) => { let result; try { result = Handlebars.compile(content)({ firstname: "Value", lastname: "OtherValue", }); } catch (error) { loaderContext.emitError(error); return content; } return result; }, }, }, ], }, };You can use PostHTML without any additional loaders.
file.html
<img src="image.jpg" />webpack.config.js
const posthtml = require("posthtml"); const posthtmlWebp = require("posthtml-webp"); module.exports = { module: { rules: [ { test: /\.hbs$/i, loader: "html-loader", options: { preprocessor: (content, loaderContext) => { let result; try { result = posthtml().use(plugin).process(content, { sync: true }); } catch (error) { loaderContext.emitError(error); return content; } return result.html; }, }, }, ], }, };A very common scenario is exporting the HTML into their own .html file, to serve them directly instead of injecting with javascript. This can be achieved with a combination of 2 loaders:
- extract-loader
- html-loader
and asset modules
The html-loader will parse the URLs, require the images and everything you expect. The extract loader will parse the javascript back into a proper html file, ensuring images are required and point to proper path, and the asset modules will write the .html file for you. Example:
webpack.config.js
module.exports = { output: { assetModuleFilename: "[name][ext]", }, module: { rules: [ { test: /\.html$/, type: "asset/resource", generator: { filename: "[name][ext]", }, }, { test: /\.html$/i, use: ["extract-loader", "html-loader"], }, ], }, };Please take a moment to read our contributing guidelines if you haven't yet done so.