將 HTML 匯出為字串。當編譯器需要時,HTML 會進行最小化。
首先,您需要安裝 html-loader
npm install --save-dev html-loader
或
yarn add -D html-loader
或
pnpm add -D html-loader
然後將外掛程式新增到您的 webpack
設定檔中。例如
file.js
import html from "./file.html";
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
},
],
},
};
sources
類型
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;
};
預設值:true
預設情況下,每個可載入屬性(例如 - <img src="image.png"/>
)都會被匯入(const img = require('./image.png')
或 import img from "./image.png""
)。您可能需要在設定檔中為圖片指定載入器(建議 asset modules
)。
支援的標籤和屬性
audio
標籤的 src
屬性embed
標籤的 src
屬性img
標籤的 src
屬性img
標籤的 srcset
屬性input
標籤的 src
屬性object
標籤的 data
屬性script
標籤的 src
屬性script
標籤的 href
屬性script
標籤的 xlink:href
屬性source
標籤的 src
屬性source
標籤的 srcset
屬性track
標籤的 src
屬性video
標籤的 poster
屬性video
標籤的 src
屬性image
標籤的 xlink:href
屬性image
標籤的 href
屬性use
標籤的 xlink:href
屬性use
標籤的 href
屬性link
標籤的 href
屬性,當 rel
屬性包含 stylesheet
、icon
、shortcut icon
、mask-icon
、apple-touch-icon
、apple-touch-icon-precomposed
、apple-touch-startup-image
、manifest
、prefetch
、preload
或當 itemprop
屬性為 image
、logo
、screenshot
、thumbnailurl
、contenturl
、downloadurl
、duringmedia
、embedurl
、installurl
、layoutimage
時link
標籤的 imagesrcset
屬性,當 rel
屬性包含 stylesheet
、icon
、shortcut icon
、mask-icon
、apple-touch-icon
、apple-touch-icon-precomposed
、apple-touch-startup-image
、manifest
、prefetch
、preload
時meta
標籤的 content
屬性,當 name
屬性為 msapplication-tileimage
、msapplication-square70x70logo
、msapplication-square150x150logo
、msapplication-wide310x150logo
、msapplication-square310x310logo
、msapplication-config
、twitter:image
或當 property
屬性為 og:image
、og:image:url
、og:image:secure_url
、og:audio
、og:audio:secure_url
、og:video
、og:video:secure_url
、vk:image
或當 itemprop
屬性為 image
、logo
、screenshot
、thumbnailurl
、contenturl
、downloadurl
、duringmedia
、embedurl
、installurl
、layoutimage
時name
屬性為 msapplication-task
時,meta
標籤的 content
屬性中的 icon-uri
值元件boolean
true
值啟用所有預設元素和屬性的處理,false
值停用所有屬性的處理。
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
// Disables attributes processing
sources: false,
},
},
],
},
};
object
讓您可以指定要處理的標籤和屬性,過濾它們,過濾網址,並處理以 /
開頭的來源。
例如
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;
},
},
},
},
],
},
};
list
類型
type list = Array<{
tag?: string;
attribute?: string;
type?: string;
filter?: (
tag: string,
attribute: string,
attributes: string,
resourcePath: string,
) => boolean;
}>;
預設:支援的標籤和屬性。
允許設定要處理的標籤和屬性以及處理方式,以及過濾其中一些標籤和屬性的能力。
使用 ...
語法讓您可以擴充 預設支援的標籤和屬性。
例如
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;
},
},
],
},
},
},
],
},
};
如果未指定標籤名稱,它將處理所有標籤。
您可以使用自訂過濾器來指定要處理的 HTML 元素。
例如
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";
},
},
],
},
},
},
],
},
};
過濾器也可以用來擴充支援的元素和屬性。
例如,過濾器可以協助處理參照資源的 meta 標籤
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;
},
},
],
},
},
},
],
},
};
注意
具有
tag
選項的來源優先於沒有該選項的來源。
過濾器可用於停用預設來源。
例如
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
sources: {
list: [
"...",
{
tag: "img",
attribute: "src",
type: "src",
filter: () => false,
},
],
},
},
},
],
},
};
urlFilter
類型
type urlFilter = (
attribute: string,
value: string,
resourcePath: string,
) => boolean;
預設:undefined
允許過濾網址。所有過濾的網址都不會解析(保留在程式碼中,就像它們被寫入一樣)。預設情況下,不處理不可請求的來源(例如 <img src="javascript:void(0)"/>
)。
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 scriptingEnabled = boolean;
預設值:true
預設情況下,html-loader
中的剖析器會將 <noscript>
標籤內的內容詮釋為 #text
,因此會略過處理此標籤內的內容。
若要讓剖析器將 <noscript>
內的內容處理為 #AST
,請將此參數設定為:false
其他資訊:scriptingEnabled
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
sources: {
// Enables processing inside the <noscript> tag
scriptingEnabled: false,
},
},
},
],
},
};
preprocessor
類型
type preprocessor = (
content: string | Buffer,
loaderContext: LoaderContext,
) => HTMLElement;
預設:undefined
允許在處理前預先處理內容。
警告
您應該始終傳回有效的 HTML
file.hbs
<div>
<p>{{firstname}} {{lastname}}</p>
<img src="image.png" alt="alt" />
<div>
function
您可以將 preprocessor
選項設定為 function
執行個體。
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;
},
},
},
],
},
};
您也可以將 preprocessor
選項設定為非同步函式執行個體。
例如
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;
},
},
},
],
},
};
minimize
類型
type minimize =
| boolean
| {
caseSensitive?: boolean;
collapseWhitespace?: boolean;
conservativeCollapse?: boolean;
keepClosingSlash?: boolean;
minifyCSS?: boolean;
minifyJS?: boolean;
removeComments?: boolean;
removeRedundantAttributes?: boolean;
removeScriptTypeAttributes?: boolean;
removeStyleLinkTypeAttributes?: boolean;
};
預設值:生產模式為 true
,否則為 false
指示 html-loader
最小化 HTML。
boolean
預設啟用的最小化規則如下
({
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,
},
},
],
},
};
object
webpack.config.js
請參閱 html-minifier-terser 的文件,以取得更多關於可用選項的資訊。
可以在 webpack.conf.js
中使用下列選項覆寫預設規則
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: {
removeComments: false,
collapseWhitespace: false,
},
},
},
],
},
};
可以延伸預設規則
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,
},
},
},
],
},
};
esModule
類型
type esModule = boolean;
預設值:true
預設情況下,html-loader
會產生使用 ES 模組語法的 JS 模組。在某些情況下,使用 ES 模組是有利的,例如 模組串接 和 樹狀搖晃。
你可以使用以下方式啟用 CommonJS 模組語法
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
esModule: false,
},
},
],
},
};
<!-- webpackIgnore: true -->
註解停用 URL 解析使用 <!-- webpackIgnore: true -->
註解,可以停用下一個標籤的來源處理。
<!-- 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" />
使用 resolve.roots
可以指定一個目錄清單,其中包含伺服器相關 URL (從 '/' 開始) 的請求解析。
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 resolved
webpack.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
和 link
標籤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: ["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",
},
],
},
};
你可以使用任何範本系統。以下是 handlebars 的範例。
file.hbs
<div>
<p>{{firstname}} {{lastname}}</p>
<img src="image.png" alt="alt" />
<div>
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;
},
},
},
],
},
};
你可以使用 PostHTML,而不需要任何其他載入器。
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;
},
},
},
],
},
};
一個非常常見的場景是將 HTML 匯出到它們自己的 .html 檔案中,以便直接提供服務,而不是使用 javascript 注入。這可以使用 html-loader 和 資產模組
的組合來實現。
html-loader 會剖析 URL,載入影像,並執行您預期的所有動作。extract loader 會將 javascript 剖析回正確的 html 檔案,確保載入影像並指向正確路徑,而 asset modules
會為您撰寫 .html 檔案。範例
webpack.config.js
module.exports = {
output: {
assetModuleFilename: "[name][ext]",
},
module: {
rules: [
{
test: /\.html$/,
type: "asset/resource",
generator: {
filename: "[name][ext]",
},
},
{
test: /\.html$/i,
use: ["html-loader"],
},
],
},
};
如果您尚未閱讀,請花點時間閱讀我們的貢獻指南。