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', }, ], }, };By default every local <img src="image.png"> is required (require('./image.png')). You may need to specify loaders for images in your configuration (recommended file-loader or url-loader).
| Name | Type | Default | Description |
|---|---|---|---|
attributes | {Array|String} | [':srcset', 'img:src', 'audio:src', 'video:src', 'track:src', 'embed:src', 'source:src','input:src', 'object:data'] | Enables/Disables attributes handling |
root | {String} | undefiend | Allow to handle root-relative attributes |
interpolate | {Boolean|String} | false | Allow to use expressions in HTML syntax |
minimize | {Boolean|Object} | true in production mode, otherwise false | Tell html-loader to minimize HTML |
esModule | {Boolean} | false | Use ES modules syntax |
Type: Array|String Default: [':srcset', 'img:src', 'audio:src', 'video:src', 'track:src', 'embed:src', 'source:src', 'input:src', 'object:data']
You can specify which tag-attribute combination should be processed by this loader via the query parameter attributes. Pass an array or a space-separated list of <tag>:<attribute> combinations. (Default: attributes=img:src)
If you use <custom-elements>, and lots of them make use of a custom-src attribute, you don't have to specify each combination <tag>:<attribute>: just specify an empty tag like attributes=:custom-src and it will match every element.
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: 'html-loader', options: { attributes: [':data-src'], }, }, ], }, };To completely disable tag-attribute processing (for instance, if you're handling image loading on the client side) you can pass set false value.
Type: String Default: undefined
For urls that start with a /, the default behavior is to not translate them. If a root query parameter is set, however, it will be prepended to the url and then translated.
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: 'html-loader', options: { root: './file.html', }, }, ], }, };Type: Boolean|String Default: false
Allow to use expressions in HTML syntax.
You can use interpolate flag to enable interpolation syntax for ES6 template strings, like so:
require('html-loader?interpolate!./file.html');<img src="${require(`./images/gallery.png`)}" /> <div>${require('./components/gallery.html')}</div>webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: 'html-loader', options: { interpolate: true, }, }, ], }, };If you only want to use require in template and any other ${} are not to be translated, you can set interpolate flag to require, like so:
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: 'html-loader', options: { interpolate: 'require', }, }, ], }, };This may be useful for template syntaxes. For example:
<#list list as list> <a href="${list.href!}" />${list.name}</a> </#list> <img src="${require(`./images/gallery.png`)}"> <div>${require('./components/gallery.html')}</div>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:
- collapseWhitespace
- conservativeCollapse
- keepClosingSlash
- minifyCSS
- minifyJS
- removeAttributeQuotes
- removeComments
- removeScriptTypeAttributes
- removeStyleTypeAttributes
- useShortDoctype
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: 'html-loader', options: { minimize: true, }, }, ], }, };webpack.config.js
See html-minifier's documentation for more information on the available options.
The rules can be disabled 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, }, }, }, ], }, };Type: Boolean Default: false
By default, html-loader generates JS modules that use the CommonJS 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 ES module syntax using:
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, loader: 'html-loader', options: { esModule: true, }, }, ], }, };webpack.config.js
module.exports = { module: { rules: [ { test: /\.jpg$/, loader: 'file-loader' }, { test: /\.png$/, loader: 'url-loader' }, ], }, output: { publicPath: 'http://cdn.example.com/[hash]/', }, };<!-- file.html --> <img src="image.png" data-src="image2x.png" />require('html-loader!./file.html'); // => '<img src="http://cdn.example.com/49eba9f/a992ca.png" data-src="image2x.png">'require('html-loader?attributes=img:data-src!./file.html'); // => '<img src="image.png" data-src="data:image/png;base64,..." >'require('html-loader?attributes=img:src img:data-src!./file.html'); require('html-loader?attributes[]=img:src&attributes[]=img:data-src!./file.html'); // => '<img src="http://cdn.example.com/49eba9f/a992ca.png" data-src="data:image/png;base64,..." >'require('html-loader?-attributes!./file.html'); // => '<img src="image.jpg" data-src="image2x.png" >''<img src=http://cdn.example.com/49eba9f/a9f92ca.jpg data-src=data:image/png;base64,...>'With the same configuration as above:
<!-- file.html --> <img src="/image.jpg" />require('html-loader!./file.html'); // => '<img src="/image.jpg">'require('html-loader?root=.!./file.html'); // => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg">'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 3 loaders:
- file-loader
- extract-loader
- html-loader
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 file loader will write the .html file for you. Example:
webpack.config.js
module.exports = { module: { rules: [ { test: /\.html$/i, use: ['file-loader?name=[name].[ext]', 'extract-loader', 'html-loader'], }, ], }, };Please take a moment to read our contributing guidelines if you haven't yet done so.