【问题标题】:Webpack dev middleware react hot reload too slowWebpack 开发中间件反应热重载太慢
【发布时间】:2016-08-18 00:11:13
【问题描述】:

我有一个带有 webpack-dev-middlewarewebpack-hot-middleware 的简单配置,它使用热重载 (HMR) 和反应。

一切正常,除了我对代码所做的每一次更改都需要 2 3-4 秒!!!直到我在浏览器中看到它。 难道我做错了什么 ?它应该是这样的?

我的代码相当大,我的包缩小到 841kb(200kb gzipped)是这个原因吗?代码库越大,包创建越慢?

快递服务器:

var webpack = require('webpack');
var webpackConfig = require('./webpack.hot.config');
var compiler = webpack(webpackConfig);

app.use(require("webpack-dev-middleware")(compiler, {
  noInfo: true,
  publicPath: webpackConfig.output.publicPath,
  watchOptions: {
    poll: true
  }
 }));
app.use(require("webpack-hot-middleware")(compiler, {
  log: console.log,
  path: '/__webpack_hmr',
  heartbeat: 10 * 1000
 }));

webpack.hot.config.js

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

module.exports = {

context: __dirname,
entry: [
    'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
    './src/js/index'
],
module: {
    loaders: [{
        test: /\.jsx?$/,
        include: path.join(__dirname, 'src/js'),
        //exclude: /node_modules/,
        loader: 'react-hot!babel'
    },
        {
            // Test expects a RegExp! Note the slashes!
            test: /\.css$/,
            loaders: ['style', 'css'],
            // Include accepts either a path or an array of paths.
            include: path.join(__dirname, 'src/css')
        }
    ]
},
resolve: {
    extensions: ['', '.js', '.jsx']
},
output: {
    path: __dirname + '/public',
    publicPath: '/',
    filename: 'js/app.js'
},
plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
]
};

这就是我在代码中更改某些内容时在控制台中得到的结果:

[HMR] App is up to date.
app.js:73223 [HMR] bundle rebuilding
app.js:73226 [HMR] bundle rebuilt in 335ms
app.js:73289 [HMR] Checking for updates on the server...
app.js:73362 [HMR] Updated modules:
app.js:73364 [HMR]  - ./src/js/components/header.jsx
app.js:73369 [HMR] App is up to date.

【问题讨论】:

  • 所以exclude: /node_modules/ 被注释掉了。在配置中构建仍然很慢吗?除此之外,我建议删除 OccurrenceOrderPlugin。该插件旨在帮助您处理似乎没有实现的分块(除非您在不同的配置文件中)。
  • @garrettmaring,使用include 代替exclude 就足够了,因为它是更明确的变体。过去,我想我还需要使用OccurenceOrderPlugin 来确保webpack-dev-middleware 的热重载。

标签: express reactjs webpack-hmr webpack-hot-middleware hot-module-replacement


【解决方案1】:

专业提示:webpack.config.js 中的模式更改为开发。如果您不使用此属性,它将默认为生产环境,这意味着它会减慢生产速度并使您的热重载变得很糟糕。

module.exports = {
    mode: 'development'
};

【讨论】:

    【解决方案2】:

    您应该启用缓存:

        ...
        plugins: [
            new webpack.optimize.OccurenceOrderPlugin(),
            new webpack.HotModuleReplacementPlugin(),
            new webpack.NoErrorsPlugin()
        ],
        cache: true
    };
    

    【讨论】:

    【解决方案3】:

    考虑在中间件中将轮询切换为 false。我发现轮询可能是 CPU 密集型的。

    在您的 webpack 配置中,您可能还想尝试添加 devtool: false 以避免创建源映射。

    【讨论】:

    • 这似乎没有帮助。
    • 在 Windows 上使用 docker 时,这不是一个选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2018-09-15
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2019-04-24
    • 2016-09-27
    相关资源
    最近更新 更多