简体   繁体   English

React&Webpack Dev Server:热重装不起作用

[英]React & Webpack Dev Server: Hot Reloading not working

I've got a React app with a fairly simple Webpack setup, I'm wanting to use Webpack Dev Server and enable Hot Reloading. 我有一个带有相当简单的Webpack设置的React应用,我想使用Webpack Dev Server并启用热重载。 It feels like I've read nearly all answers for solving this... 好像我已经阅读了解决此问题的几乎所有答案...

package.json package.json

"dev": "NODE_ENV=dev webpack-dev-server --hot --inline --open --progress --mode development --content-base dist --config webpack.dev.js",

webpack.common.js webpack.common.js

devtool: "source-map",
  entry: [
    "babel-polyfill",
    "./src/client/index.js"
  ],
  output: {
    path: path.join(__dirname, "dist"),
    filename: "bundle.js",
    publicPath: "/assets/"
  }

webpack.dev.js webpack.dev.js

const common = require("./webpack.common.js");

module.exports = merge(common, {
  devtool: "inline-source-map",
  devServer: {
    compress: true,
    contentBase: path.resolve(__dirname, "dist"),
    index: "index.html",
    stats: "normal",
    inline: true,
    clientLogLevel: "info"
  },
  mode: "development"
});

To run the project, I run yarn run dev and it will load the webpack-dev-server and open the React app in the browser, however, it doesn't hot reload. 为了运行项目,我运行yarn run dev ,它将加载webpack-dev-server并在浏览器中打开React应用,但是,它不会热重载。 I'm not sure if it re-builds the bundle.js file, because when re-freshing the browser, it still loads the old version of the code. 我不确定是否会重新生成bundle.js文件,因为在刷新浏览器时,它仍会加载旧版本的代码。

You are loading the static configured page. 您正在加载静态配置页面。 Static bundle is the file on your disk and it is recreated after you run webpack build command. 静态捆绑包是磁盘上的文件,并且在您运行webpack build命令后会重新创建它。 You will use static configuration in production when it is ready to release. 准备发布时,您将在生产中使用静态配置。

You need to load dynamic bundle wich is server from webpack virtual file systems and which is accessible from url where webpack dev server is binded. 您需要从webpack虚拟文件系统中加载服务器中的动态捆绑包,并且可以从绑定了webpack dev服务器的url访问该捆绑包。 For example http://localhost:8080/dist/bundle.js . 例如http://localhost:8080/dist/bundle.js This will be updated by webpack dev server each time when sources is changed. 每次更改源时,webpack开发服务器都会对此进行更新。

While hot module reloading might be enabled in webpack, it also require some changes in codebase, have you used module.hot in your application? 虽然可以在webpack中启用热模块重新加载,但它也需要对代码库进行一些更改,您module.hot在应用程序中使用了module.hot ( https://webpack.js.org/guides/hot-module-replacement/#enabling-hmr take a look at index.js part). https://webpack.js.org/guides/hot-module-replacement/#enabling-hmr看看index.js部分)。 As a react-specific alternative, react-hot-loader can be used 作为特定于反应的替代方案,可以使用react-hot-loader

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM