html-loader webpack
[![npm][npm]][npm-url] [![node][node]][node-url] [![tests][tests]][tests-url] [![coverage][cover]][cover-url] [![discussion][discussion]][discussion-url] [![size][size]][size-url] # html-loader Exports HTML as string. HTML is minimized when the compiler demands. ## Getting Started To begin, you'll need to install `html-loader`: ```console npm install --save-dev html-loader ``` or ```console yarn add -D html-loader ``` or ```console pnpm add -D html-loader ``` Then add the plugin to your `webpack` config. For example: **file.js** ```js import html from "./file.html"; ``` **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", }, ], }, }; ``` ## Options - **[`sources`](#sources)** - **[`preprocessor`](#preprocessor)** - **[`postprocessor`](#postprocessor)** - **[`minimize`](#minimize)** - **[`esModule`](#esmodule)** ### `sources` Type: ```ts type sources = | boolean | { list?: Array<{ tag?: string; attribute?: string; type?: string; filter?: ( tag: string, attribute: string, attributes: string, resourcePath: string, ) => boolean; }>; urlFilter?: ( attribute: string, value: string, resourcePath: string, ) => boolean; scriptingEnabled?: boolean; }; ``` Default: `true` By default every loadable attribute (for example - ``) is imported (`const img = require('./image.png')` or `new URL("./image.png", import.meta.url)`). You may need to specify loaders for images in your configuration (recommended [`asset modules`](https://webpack.js.org/guides/asset-modules/)). Supported tags and attributes: - the `src` attribute of the `audio` tag - the `src` attribute of the `embed` tag - the `src` attribute of the `img` tag - the `srcset` attribute of the `img` tag - the `src` attribute of the `input` tag - the `data` attribute of the `object` tag - the `src` attribute of the `script` tag - the `href` attribute of the `script` tag - the `xlink:href` attribute of the `script` tag - the `src` attribute of the `source` tag - the `srcset` attribute of the `source` tag - the `src` attribute of the `track` tag - the `poster` attribute of the `video` tag - the `src` attribute of the `video` tag - the `xlink:href` attribute of the `image` tag - the `href` attribute of the `image` tag - the `xlink:href` attribute of the `use` tag - the `href` attribute of the `use` tag - the `href` attribute of the `link` tag when the `rel` attribute contains `stylesheet`, `icon`, `shortcut icon`, `mask-icon`, `apple-touch-icon`, `apple-touch-icon-precomposed`, `apple-touch-startup-image`, `manifest`, `prefetch`, `preload` or when the `itemprop` attribute is `image`, `logo`, `screenshot`, `thumbnailurl`, `contenturl`, `downloadurl`, `duringmedia`, `embedurl`, `installurl`, `layoutimage` - the `imagesrcset` attribute of the `link` tag when the `rel` attribute contains `stylesheet`, `icon`, `shortcut icon`, `mask-icon`, `apple-touch-icon`, `apple-touch-icon-precomposed`, `apple-touch-startup-image`, `manifest`, `prefetch`, `preload` - the `content` attribute of the `meta` tag when the `name` attribute is `msapplication-tileimage`, `msapplication-square70x70logo`, `msapplication-square150x150logo`, `msapplication-wide310x150logo`, `msapplication-square310x310logo`, `msapplication-config`, `twitter:image` or when the `property` attribute is `og:image`, `og:image:url`, `og:image:secure_url`, `og:audio`, `og:audio:secure_url`, `og:video`, `og:video:secure_url`, `vk:image` or when the `itemprop` attribute is `image`, `logo`, `screenshot`, `thumbnailurl`, `contenturl`, `downloadurl`, `duringmedia`, `embedurl`, `installurl`, `layoutimage` - the `icon-uri` value component in `content` attribute of the `meta` tag when the `name` attribute is `msapplication-task` #### `boolean` The `true` value enables the processing of all default elements and attributes, the `false` value disables the processing of all attributes. **webpack.config.js** ```js module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { // Disables attributes processing sources: false, }, }, ], }, }; ``` #### `object` Allows you to specify which tags and attributes to process, filter them, filter urls and process sources starting with `/`. For example: **webpack.config.js** ```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; }, }, }, }, ], }, }; ``` #### `list` Type: ```ts type list = Array<{ tag?: string; attribute?: string; type?: string; filter?: ( tag: string, attribute: string, attributes: string, resourcePath: string, ) => boolean; }>; ``` Default: [supported tags and attributes](#sources). Allows to setup which tags and attributes to process and how, as well as the ability to filter some of them. Using `...` syntax allows you to extend [default supported tags and attributes](#sources). For example: **webpack.config.js** ```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** ```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: ```js 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: ```js module.exports = { module: { rules: [ { test: /\.html$/i, loader: "html-loader", options: { sources: { list: [ "...", { tag: "img", attribute: "src", type: "src", filter: () => false, }, ], }, }, }, ], }, }; ``` #### `urlFilter` Type: ```ts type urlFilter = ( attribute: string, value: string, resourcePath: string, ) => boolean; ``` Default: `undefined` Allow to filter urls. All filtered urls will not be resolved (left in the code as they were written). Non-requestable sources (for example ``) are not handled by default. ```js 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; }, }, }, }, ], }, }; ``` #### `scriptingEnabled` Type: ```ts type scriptingEnabled = boolean; ``` Default: `true` By default, the parser in `html-loader` interprets content inside `