reactjs - Windows 10/Ubuntu 上的 React 热模块替换

标签 reactjs ubuntu express webpack windows-10

所以我一直在尝试为 React 配置热重载,我将 webpack2 与 express 和 webpack-hot-middleware 用于 HMR。

我使用适用于 Linux 的 Windows 子系统从 ubuntu 运行服务器,并使用 Windows 上的普通 Chrome 作为客户端。

它在我的 Mac 上工作得很好,但在 Windows 上却不行,我尝试将浏览器连接到“localhost”和“127.0.0.1”,还将 express 上的主机名明确设置为“localhost”、“127.0.0.0”。 0.1' 和 '0.0.0.0' 但它们都不起作用。

此外,Webpack 甚至不会在文件更改时重建。

这是我的 webpack.config.js

const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: [
    path.resolve(__dirname, './src/index.jsx'),
    'webpack-hot-middleware/client'
  ],
  devtool: 'inline-source-map',
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  ],
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'app.bundle.js',
    publicPath: '/'
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: [/node_modules/],
        use: [{
          loader: 'babel-loader',
          options: {
            presets: ['react', 'env']
          }
        }]
      }
    ]
  }
}

中间件配置

app.use(webpackDevMiddleware(compiler, {
  publicPath: config.output.publicPath
}));

app.use(webpackHotMiddleware(compiler));

app.listen(port, () => {
  console.log('Listening on port ', port);
});

最佳答案

我也遇到过这种情况,这似乎是 linux 子系统之外的文件更改问题。我正在使用 Atom 进行编辑,但没有看到重建。但是,如果我从 bash 中触摸文件或通过 vi 编辑它们,我会看到重新构建和重新加载。我并没有在 Atom 中使 webpack 通知文件更改太过分,但是如果我在我的环境中设置 CHOKIDAR_USEPOLLING=true,更改最终会在一些短的轮询间隔后被注意到。

关于reactjs - Windows 10/Ubuntu 上的 React 热模块替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603109/

相关文章:

javascript - react native : Change style prop of 3rd party component

javascript - React Native : [Unhandled promise rejection: Error: Invalid hook call. Hooks 只能在函数组件的主体内部调用

javascript - React 中的 setState() 未更新 onClick 状态

node.js - 用于更新 Mongo 文档中数组内对象的 Mongoose 语法

javascript - Node/ express -错误 : Can't set headers after they are sent

reactjs - Material UI Typescript - createStyles 问题

Python 看不到使用 apt-get 安装的模块

ruby-on-rails - 安装了 Ruby,但安装 RubyGems 不起作用

ubuntu - 如何检查 Apache2 是否在 Ubuntu 中停止?

mysql - 无法使用express+mysql登录,注册有效