简体   繁体   English

Webpack 正在编译成功,但更改未反映在 React App 中

[英]Webpack is compiling successfully but changes are not reflected in React App

I am using Django to host the react app which is being compiled by babel and bundled using webpack.我正在使用 Django 来托管由 babel 编译并使用 webpack 捆绑的反应应用程序。 The webpack shows me a compiled successfully message like below but the changes aren't reflected. webpack 向我显示编译成功的消息,如下所示,但未反映更改。 The funny thing is when I wait for a bit, it works but that too sometimes with no linear pattern.有趣的是,当我稍等片刻时,它确实有效,但有时也没有线性模式。 I am getting mad about this, please help, I saw that, since I am using OSX: it is getting corrupted as the github issue says: https://github.com/webpack/webpack-dev-server/issues/24 but it is not helping!我对此很生气,请帮忙,我看到了,因为我使用的是 OSX:它正在损坏,因为 github 问题说: https://github.com/webpack/webpack-dev-server/issues/24但是它没有帮助!

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 config: 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 config通天塔配置

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "10"
        }
      }
    ],
    "@babel/preset-react"
  ],
  "plugins": ["@babel/plugin-proposal-class-properties"]
}

package.json 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"
  }
}

You can try this!你可以试试这个!

module.exports = {
  mode: 'development',
  devServer: {
    port:  process.env.PORT || 3000,
    historyApiFallback: true,
    open: true,
    compress: true,
    
    // Setup output directory
    static: {
      directory: path.join(__dirname, 'public'),
      watch: {
        ignored: /node_modules/,
      },
    },
  },
}

I have faced this problem before and our webpack is quite similar.我以前遇到过这个问题,我们的 webpack 非常相似。 for me, I'm using Chrome and I just clicked hard reload in the browser then the problem was gone.对我来说,我使用的是 Chrome,我只是在浏览器中单击了硬重新加载,然后问题就消失了。 your browser might keep your cache.您的浏览器可能会保留您的缓存。

for more information: https://stackoverflow.com/a/3951207/12439451更多信息: https://stackoverflow.com/a/3951207/12439451

If it's not fixed, please check your path in webpack again.如果没有修复,请再次检查您在 webpack 中的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack无法自动编译React文件中的代码更改 - Webpack not compiling changes to code in React file automatically 当代码更改时,webpack热加载器会重新编译,但不会反映在输出中(app.js) - The webpack hot loader recompiles when the code changes But it is not reflected in output (app.js) React Redux-更改未反映在组件中 - React Redux - changes aren't reflected in component Webpack 开发服务器编译成功但重新加载或重建不起作用 - Webpack dev server compiling successfully but reload or rebuild not working 反应状态与道具-为什么状态更改未反映在组件中? - React State vs. Props - Why are state changes not reflected in components? 尽管在我的控制台中成功编译,为什么我的反应应用程序在导入和使用材料 ui 中的图标后显示空白屏幕? - Why is my react app displaying a blank screen after importing and using an icon from material ui despite compiling successfully in my console? SCSS文件没有编译成CSS使用Webpack做出反应 - SCSS files not compiling to CSS in react using Webpack 使用webpack部署react app - Deploying react app with webpack 状态更改,但未反映在DOM中 - State Changes but not reflected in DOM 未反映合并选项更改 - mergeOptions changes not reflected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM