Webpack不会更新我的捆绑软件,并且在使用npm run build
时,我总是会获得相同的旧版本。
我的目录:
---- MyProject
-------------- 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"
]
}
答案 0 :(得分:0)
您的问题与浏览器将bundle.js
缓存了一段不确定的时间有关。
传统的开发方式是使用devserver,并且应根据源文件内容的更改时间自动重新加载所做的更改。
对于生产环境,您需要像这样将contenthash
添加到输出文件名
output: {
path: path.join(__dirname, "build"),
filename: "bundle.[contenthash].js",
publicPath: "/"
}
这将导致文件被命名为bundle.3f2b48a634975adb9b7e.js
之类的文件,由于捆绑文件名与缓存的捆绑文件名不匹配,因此下次浏览器再次提示浏览器重新加载捆绑文件。
我建议阅读the Webpack caching documentation了解更多详情