【发布时间】:2020-05-08 14:20:34
【问题描述】:
学习 WebPack 并将其设置为从模板创建 index.html 并使用包含唯一内容哈希的捆绑文件注入 <script> 标记(以绕过浏览器缓存并确保加载新内容),例如所以:bundle-[contentHash].js.
我正在使用 json-server 并将 webpack -d -w 作为 NPM 的脚本运行。当我运行脚本并加载页面时,它会不断地向 index.html 文件中添加相同的 <script> 标签。
webpack.config.js:
const webpack = require("webpack")
const path = require("path")
var htmlPlugin = require("html-webpack-plugin")
module.exports = {
// context: path.resolve(__dirname),
entry: "./client/index.jsx",
output: {
path: path.join(__dirname, "/public"),
publicPath: path.join(__dirname, "/public"),
filename: "bundle-[contentHash].js"
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
},
plugins: [
new htmlPlugin({
template: "./public/index.html"
})
]
}
【问题讨论】:
标签: javascript webpack