【问题标题】:Webpack Dev Server doesn't hot reload, requires manual webpack buildWebpack Dev Server 不会热重载,需要手动构建 webpack
【发布时间】:2019-11-09 21:28:42
【问题描述】:

这是一个小型 React 应用的子组件中的一些示例渲染代码:

<React.Fragment>
  <h1>From Tab Section</h1>
  <h2>update 2</h2> // doesn't display in the browser
  <p>update 3</p> // also doesn't display
</React.Fragment>

不同的是,写完h1标签后我重新构建了Webpack。

进一步的测试证实,重建 bundle.js 并在它注入的 index.html 页面上刷新浏览器是查看更新的唯一方法。

我想我有热重载设置,如文档中所示。根据另一篇 SO 帖子,我在 start 命令中添加了 --inline 。但是仍然需要手动构建和开发服务器重启。

这里还需要什么?

// package.json
"scripts": {
    "test": "jest --watch",
    "watch": "webpack --watch",
    "start": "webpack-dev-server --open --inline",
    "build": "webpack"
  },
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "babel-jest": "^24.8.0",
    "babel-loader": "^8.0.6",
    "jest": "^24.8.0",
    "webpack": "^4.35.0",
    "webpack-cli": "^3.3.5",
    "webpack-php-loader": "^0.5.0"
  },
  "dependencies": {
    "dotenv": "^8.0.0",
    "dotenv-webpack": "^1.7.0",
    "history": "^4.9.0",
    "lodash": "^4.17.11",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^7.1.0",
    "react-router-dom": "^5.0.1",
    "redux": "^4.0.1",
    "save-dev": "^2.0.0",
    "webpack-dev-server": "^3.7.2"
  }

// webpack.config.js
const webpack = require("webpack");
const dotenvWebpack = require("dotenv-webpack");
const path = require("path");

module.exports = {
  entry : {
    './adminSettingsArea' :
      './adminSettingsArea/src/index.jsx'
  },
  output : {
    filename : '[name]/bundle.js',
    path : path.resolve(__dirname),
  },
  devtool: 'inline-source-map',
  devServer : {
    contentBase : './adminSettingsArea',
    hot : true
  },
  plugins : [
    new webpack.HotModuleReplacementPlugin(),
    new dotenvWebpack()
  ],
  module : {
    rules : [
      {
        test : /\.(js|jsx)$/,
        exclude : [/node_modules/, /vendor/],
        use : {
          loader : "babel-loader",
          options : {
            presets : [
              '@babel/preset-env',
              "@babel/preset-react"
            ]
          },
        }
      }
    ],
  },
};

【问题讨论】:

    标签: javascript webpack


    【解决方案1】:

    通过将 index.html 文件移动到与内存中的其余内容 webpack 更新相同的文件夹 ( ./adminSettingsArea/src ) 来解决。

    以前,这个索引文件比 contentBase 值低一级。结果,webpack 最初能够加载 index.html 文件,但不能加载 /src 子文件夹中的 jsx 文件。

    另外,请参阅下面的示例,了解我在输出路径中遇到的另一个怪癖。

    entry : {
        'adminArea' :
          './adminSettingsArea/src/index.jsx'
      },
      output : {
        filename : 'shared/[name].bundle.js', // for some reason I need to assign the subfolder here instead of arg 2 in the next line
        path : path.resolve(__dirname, ''),
      },
      devtool: 'inline-source-map',
      devServer : {
        contentBase : './adminSettingsArea/src',
        hot : true
      },
    

    【讨论】:

    • 你拯救了我的一天
    猜你喜欢
    • 1970-01-01
    • 2018-06-12
    • 2019-10-29
    • 2016-12-28
    • 1970-01-01
    • 2020-11-24
    • 2017-08-07
    • 2017-06-14
    • 2022-11-25
    相关资源
    最近更新 更多