我试图在我的 index.html 中包含一个 header.html,作为部分通过html-loader但 header.html 呈现为文字文本而不是 HTML。使用此处和此处提到的插值似乎适用于 Webpack v2。我还注意到html-loader URL中的#interpolate 哈希不起作用;意味着从 Webpack v4 开始 interpolate 已失效?如果我包含选项,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",
}
}
}
},
索引.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
所以我认为这在这个答案的interpolatev1.0.0 中不起作用html-loader
interpolate我的下一个问题是在 v1.0.0中我有哪些替代方案?
侃侃无极
相关分类