[英]Webpack doesn't rendering html partials in another html partial
[英]Included partial in Webpack 4 rendering as literal text instead of HTML
I'm trying to include a header.html in my index.html, as a partial via html-loader but header.html is rendering as literal text instead of HTML. 使用此处和此处提到的插值似乎适用于 Webpack v2。 我还注意到html-loader URL中的#interpolate hash 不起作用; 意味着从 Webpack v4 开始,插值已失效? Webpack 如果我包含选项,则会发出有关无效选项 object的错误: { 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
}
}
]
}
}
索引.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
的 v1.0.0 中不起作用,基于这个答案
我的下一个问题是在 v1.0.0 中我有什么替代方案来代替interpolate
?
我将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(...) %>
中的路径是错误的。 我想知道那是不是笔误。 我还将部分从 a.html 更改为 .ejs (实时的实现在html-webpack-plugin
v3.2.0 的.html 格式中有部分,我降级到但仍然没有工作。我不知道为什么它不起作用)
我还冒昧地将html-loader
升级到 v1.1.0,正如@IVO GELOV 所建议的那样,我的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 有什么问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.