Webpack编译成功但React应用变更不生效(Django部署场景)
问题描述
我用Django托管经Babel编译、Webpack打包的React应用,Webpack显示编译成功,但代码变更无法实时反映到应用中。有时等待很久才生效,但完全没有规律,这让我十分困扰。我了解到OSX系统下可能存在相关问题,尝试过相关解决方案但未能解决。
终端运行npm run dev输出
npm run dev > frontend@1.0.0 dev > webpack --mode development --watch asset main.js 1.32 MiB [compared for emit] [minimized] (name: main) 1 related asset runtime modules 1.04 KiB 5 modules modules by path ./node_modules/ 1.2 MiB modules by path ./node_modules/axios/ 56.8 KiB 32 modules modules by path ./node_modules/react-dom/ 1000 KiB 3 modules modules by path ./node_modules/react/ 85.7 KiB 2 modules modules by path ./node_modules/scheduler/ 17.3 KiB 2 modules + 5 modules modules by path ./src/ 36.6 KiB modules by path ./src/components/*.js 30.3 KiB ./src/components/App.js 1.58 KiB [built] [code generated] + 9 modules modules by path ./src/*.js 6.33 KiB ./src/index.js 35 bytes [built] [code generated] + 2 modules webpack 5.74.0 compiled successfully in 2567 ms assets by status 1.32 MiB [cached] 1 asset cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules ./src/components/ProfilePage.js 4.05 KiB [built] webpack 5.74.0 compiled successfully in 209 ms assets by status 1.32 MiB [cached] 1 asset cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules ./src/components/ProfilePage.js 4.05 KiB [built] webpack 5.74.0 compiled successfully in 96 ms assets by status 1.32 MiB [cached] 1 asset cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules ./src/components/ProfilePage.js 4.05 KiB [built] webpack 5.74.0 compiled successfully in 119 ms assets by status 1.32 MiB [cached] 1 asset cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules ./src/components/ProfilePage.js 4.05 KiB [built] webpack 5.74.0 compiled successfully in 91 ms assets by status 1.32 MiB [cached] 1 asset cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules
Webpack配置
const path = require("path"); const webpack = require("webpack"); module.exports = { entry: "./src/index.js", output: { path: path.resolve(__dirname, "./static/frontend/"), filename: "[name].js", publicPath: "/static/frontend/", }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: "babel-loader", }, }, ], }, optimization: { minimize: true, }, plugins: [ new webpack.DefinePlugin({ "process.env": { // This has effect on the react lib size NODE_ENV: JSON.stringify("development"), }, }), ], };
Babel配置
{ "presets": [ [ "@babel/preset-env", { "targets": { "node": "10" } } ], "@babel/preset-react" ], "plugins": ["@babel/plugin-proposal-class-properties"] }
package.json
{ "name": "frontend", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "dev": "webpack --mode development --watch", "build": "webpack --mode production" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@babel/core": "^7.18.9", "@babel/preset-env": "^7.18.9", "@babel/preset-react": "^7.18.6", "babel-loader": "^8.2.5", "react": "^18.2.0", "react-dom": "^18.2.0", "webpack": "^5.74.0", "webpack-cli": "^4.10.0" }, "dependencies": { "@babel/plugin-proposal-class-properties": "^7.18.6", "@emotion/react": "^11.10.0", "@emotion/styled": "^11.10.0", "@mui/icons-material": "^5.8.4", "@mui/material": "^5.9.2", "axios": "^0.27.2", "bootstrap-icons": "^1.9.1", "jwt-decode": "^3.1.2", "react-router-dom": "^6.3.0" } }
解决方案
1. 关闭开发环境的代码压缩
开发环境下开启代码压缩不仅会减慢编译速度,还容易引发缓存问题。修改Webpack配置:
optimization: { minimize: false, },
2. 修复OSX下Webpack监听延迟问题
OSX文件系统的原生监听存在延迟,给Webpack添加强制轮询配置:
module.exports = { // ...其他配置 watchOptions: { poll: 1000, // 每秒轮询一次文件变化 aggregateTimeout: 300, // 等待300ms再执行编译,避免频繁触发 }, };
3. 禁用Django静态文件缓存
开发环境下Django默认会缓存静态文件,修改settings.py:
DEBUG = True # 禁用静态文件缓存 if DEBUG: CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', } } # 确保静态文件目录配置正确 STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
4. 浏览器端禁用缓存
打开浏览器开发者工具(F12),切换到Network选项卡,勾选「Disable cache」,避免浏览器缓存旧的JS文件。
5. 改用Webpack Dev Server代理(最优方案)
直接使用Webpack Dev Server的热更新功能,绕过Django静态文件缓存问题:
- 安装依赖:
npm install webpack-dev-server --save-dev
- 修改Webpack配置,添加devServer:
module.exports = { // ...其他配置 devServer: { hot: true, // 开启热模块替换 port: 3000, proxy: { '/': 'http://localhost:8000', // 代理请求到Django服务 }, }, };
- 更新package.json的dev脚本:
"scripts": { "dev": "webpack serve --mode development", "build": "webpack --mode production" }
- 先启动Django服务(
python manage.py runserver),再运行npm run dev,访问http://localhost:3000即可实现代码变更实时热更新。
内容的提问来源于stack exchange,提问作者Shivam Arora
相关产品推荐
相关产品推荐

