我正在尝试将{.3}中的header.html作为部分内容包含在html-loader中,但是header.html呈现为文字文本而不是HTML。使用here和here所述的插值似乎适用于Webpack v2。我还注意到html-loader URL中的#interpolate哈希无效;意味着从Webpack v4开始插值已失效?如果我包含以下选项,则Webpack会发出有关无效选项对象的错误:{ interpolate: true }
树
--dist
--node_modules
--src
----js
------index.js
----partials
------header.html
--templates
----index.html
--package.json
--webpack.config.json
webpack.config.json
const path = require("path"),
webpack = require('webpack'),
{ CleanWebpackPlugin } = require("clean-webpack-plugin"),
HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
mode: "development",
entry: {
index: "./src/js/index.js"
},
plugins: [
// new CleanWebpackPlugin(['dist/*']) for < v2 versions of CleanWebpackPlugin
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: "Home",
filename: "index.html",
template: "templates/index.html",
inject: true,
minify: true
})
],
devtool: "source-map",
devServer: {
contentBase: "./dist"
},
output: {
// filename: "[name].bundle.js",
filename: "[name].[contenthash].js",
path: path.resolve(__dirname, "dist"),
// publicPath: "/"
},
optimization: {
moduleIds: "hashed",
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
chunks: "all",
}
}
}
},
module: {
rules: [
{
test: /\.(html)$/,
loader: "html-loader",
options: {
minimize: true
}
}
]
}
}
index.html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<%= require("html-loader!./src/partials/header.html") %>
</body>
</html>
编辑1
所以我认为interpolate
在html-loader
基础this answer的v1.0.0版本中不起作用
我的下一个问题是在v1.0.0中我可以用什么替代interpolate
?
答案 0 :(得分:0)
我将html-loader
降级至v0.5.5,因为interpolate
选项不适用于html-loader
v1.0.0。另外,我将 index.html 更改为
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<%= require("html-loader!../src/partials/header.ejs") %>
</body>
</html>
初始<%= require(...) %>
中的路径错误。我想知道那是不是错字。我也将部分代码从.html更改为.ejs(实时实现的部分代码是html-webpack-plugin
v3.2.0的.html格式,我将该版本降级为但仍然没有用。我不知道为什么它不起作用)
我还按照@IVO GELOV的建议随意将html-loader
升级到v1.1.0,这样我的 package.json 看起来像这样:
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"csv-loader": "^3.0.3",
"express": "^4.17.1",
"file-loader": "^6.0.0",
"html-loader": "^1.1.0",
"html-webpack-plugin": "^4.3.0",
"webpack": "^4.42.0",
"webpack-cli": "^3.3.11",
"webpack-dev-middleware": "^3.7.2",
"webpack-dev-server": "^3.10.3",
"xml-loader": "^1.2.1"
},
并且,插值有效。不确定html-loader
v1.0.0