Webpack不会更新我的软件包,并且在使用npm run build
时我总是得到与我的网站相同的旧版本。
我的目录:
- - 我的项目 -------------- SRC ------------------ index.js ------------------的index.html -------- webpack.config.js --------的package.json
我尝试重新安装webpack,它曾帮助过一次,但无论多少次重新安装它都不会再次更新。
webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
entry: path.join(__dirname, "src", "index.js"),
output: {
path: path.join(__dirname, "build"),
filename: "bundle.js",
publicPath: "/"
},
devServer: {
historyApiFallback:true
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(css|scss)$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"postcss-loader",
"sass-loader"
]
},
{
test: /\.(jpg|jpeg|png|gif|mp3|svg)$/,
use: [
{
loader: "file-loader",
options: {
name: "[path][name]-[hash:8].[ext]"
}
}
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
filename: "index.html",
template: path.join(__dirname, "src", "index.html")
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
]
};
的package.json
{
"name": "webpack-babel-react-starter",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@babel/core": "7.2.2",
"@babel/plugin-proposal-class-properties": "7.2.3",
"@babel/plugin-transform-runtime": "7.2.0",
"@babel/preset-env": "7.2.3",
"@babel/preset-react": "7.0.0",
"autoprefixer": "9.4.3",
"babel-loader": "8.0.4",
"css-loader": "2.1.0",
"file-loader": "3.0.1",
"html-webpack-plugin": "3.2.0",
"mini-css-extract-plugin": "0.5.0",
"node-sass": "4.11.0",
"postcss-loader": "3.0.0",
"sass-loader": "7.1.0",
"style-loader": "0.23.1",
"webpack": "^4.30.0",
"webpack-cli": "3.1.2",
"webpack-dev-server": "^3.3.1"
},
"scripts": {
"start": "node server.js --hot",
"dev": "webpack-dev-server --mode production --hot",
"build": "webpack --config webpack.config.js",
"heroku-postbuild": "npm run build"
},
"dependencies": {
"@babel/runtime": "7.2.0",
"bootstrap": "^4.3.1",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.8",
"react-bootstrap-webpack": "^1.0.4",
"react-dom": "^16.8.6",
"react-router": "^5.0.0",
"react-router-dom": "^4.3.1",
"react-router-transition": "^1.3.0"
},
"browserslist": [
"> 0.2%",
"last 15 versions",
"not dead"
]
}
您的问题与浏览器缓存bundle.js
的时间有关。
传统的开发方法是使用devserver,并根据源文件内容何时更改自动重新加载所做的更改。
对于生产,您需要将contenthash
添加到输出文件名中
output: {
path: path.join(__dirname, "build"),
filename: "bundle.[contenthash].js",
publicPath: "/"
}
这将导致文件被命名为类似于bundle.3f2b48a634975adb9b7e.js
,这将再次提示浏览器在下次加载网站时重新加载包,因为包文件名与缓存的包文件名不匹配。
我建议阅读the Webpack caching documentation了解更多详情