Skip to content

Webpack 4 hot rebuild time twice as slow #6767

Closed

Description

@drewjenkins

Originally posted this issue on webpack-dev-server's repo, but was told nearly all of HMR is handled by Webpack itself, so I am moving that issue over here.

  • Operating System: Mac OS High Sierra
  • Node Version: 8.10.0
  • NPM Version: 5.6.0
  • webpack Version: 4.1.1
  • webpack-dev-server Version: 3.1.1
  • This is a bug
  • This is a modification request

Code

Note that the webpack file I posted isn't exactly what we have, but anything removed was just company info, me merging a common webpack file with our dev one to produce a single code snippet, and removing some unrelated/redundant info (e.g. multiple templates with similar content).

  // webpack.config.js
const webpack = require('webpack');
const merge = require('webpack-merge');
const config = require('./webpack.common.config.js');
const Jarvis = require('webpack-jarvis');

module.exports = merge(config, {
  mode: 'development',
  devtool: 'cheap-module-eval-source-map',
  output: {
    publicPath: '/',
    filename: '[name]-[hash].js',
    crossOriginLoading: 'anonymous',
    chunkFilename: '[name]-[hash].js'
  },
  optimization: {
    noEmitOnErrors: true,
    namedModules: true,
  },
  plugins: [
    new CopyWebpackPlugin([{
      from: 'app/assets/images/favicon/',
      to: 'favicon/',
    }]),
    new CopyWebpackPlugin([{
      from: 'app/assets/images/square/logo.png',
      to: 'logo.png',
    }]),
    new ExtractTextPlugin({
      filename: '[name]-styles-[contenthash].css',
      allChunks: true,
    }),
    new DeterministicHash(),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
    new HtmlWebpackPlugin({
      template: 'templates/index.ejs',
      filename: 'index.html',
      inject: false,
      chunks: ['common', 'main'],
      appleTouchIconSizes: [57, 72, 114, 120, 144, 152],
    }),
    new Jarvis({port: 7003}),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      _DEVELOPMENT_: true,
    })
  ],
  module: {
    rules: [
      {
        test: /\.(otf|ttf|woff|woff2|png|jpg|svg|mp3|wav|aff|dic)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: 'assets/[name]-[sha1:hash:hex:8].[ext]'
            },
          },
        ]
      },
      {
        test: /\.s?css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            'css-loader',
            {
              loader: 'postcss-loader',
              options: {
                plugins: () => [
                  require('precss'),
                  require('autoprefixer'),
                ]
              }
            },
            'sass-loader'
          ]
        }),
      },
      {
        test: /\.jsx?$/,
        use: ['babel-loader'],
      },
    ]
  },
  entry: {
    main: [
      'babel-polyfill',
      'react-hot-loader/patch',
      'webpack/hot/only-dev-server',
      'webpack-dev-server/client?https://0.0.0.0:7001',
      './app/main.js',
    ],
  }
});
  // dev-server.js
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack.devconfig');
const ssl = require('./devssl');

new WebpackDevServer(webpack(config), {
  publicPath: config.output.publicPath,
  headers: {'Access-Control-Allow-Origin': '*'},
  hot: true,
  https: true,
  clientLogLevel: 'error',
  cert: ssl.cert,
  overlay: true,
  key: ssl.key,
  historyApiFallback: true,
  disableHostCheck: true,
  watchOptions: {
    ignored: /\/node_modules\/.*/,
  },
  stats: {
    assets: false,
    cached: false,
    cachedAssets: false,
    children: false,
    chunks: false,
    chunkModules: false,
    chunkOrigins: false,
    colors: true,
    depth: false,
    entrypoints: true,
    excludeAssets: /app\/assets/,
    hash: false,
    maxModules: 15,
    modules: false,
    performance: true,
    reasons: false,
    source: false,
    timings: true,
    version: false,
    warnings: true,
  },
}).listen(7001, '0.0.0.0', function(err, result) {
  console.log(`Serving chunks at path ${config.output.publicPath}`);
});

Expected Behavior

Similar, if not faster HMR

Actual Behavior

Hot reload time increased from ~5s to ~10s when upgrading from Webpack 3 to Webpack 4.

For Bugs; How can we reproduce the behavior?

Unsure if it reproduces in every case, but in our case we have a large webapp that spits out multiple entry points, with sass processing, along with a lot of other webpack features.

Activity

  1. dylang commented on Mar 16, 2018

    @dylang

    Hot reload time increased from ~5s to ~10s when upgrading from Webpack 3 to Webpack 4.

    Have you tried your Webpack 4 changes in an older version of node, say 8.9.4? For us it makes it fast again.

  2. sokra commented on Mar 17, 2018

    @sokra
    Member

    cc @bmeurer node 8.10 includes a V8 6.2 upgrade.

  3. bmeurer commented on Mar 19, 2018

    @bmeurer

    Interesting. Any idea what it could be related to? 6.2 is a pretty massive update (performance-wise).

    cc @hashseed @psmarshall @mathiasbynens

  4. hashseed commented on Mar 19, 2018

    @hashseed

    Would you be willing to bisect the V8 change that caused this regression? I have some instructions here.

  5. drewjenkins commented on Mar 19, 2018

    @drewjenkins
    Author

    For reference, I did some benchmarks comparing various nodes with various webpack versions. I don't claim to be an expert, I just thought it may be helpful to have more information.

    For my test, I just added and removed the string 'test' to a React component 10 times, saving between each addition/removal. These numbers should be taken with a grain of salt since when upgrading from Webpack 3 to Webpack 4, we had to change our process slightly, however the overarching trend seems clear. Node 8.10.0 definitely seems to be impacting the rebuild time, though Webpack 3->4 seems to be a larger cause of the increase.

    Webpack 3.10.0 - Node 8.9.4
    Build - 60602ms
    Rebuild - 9564ms 4041ms 4401ms 4062ms 4196ms 4902ms 3922ms 3507ms 4388ms 4217ms
    Rebuild Average - 4720ms
    
    Webpack 3.10.0 - Node 8.10.0
    Build - 58165ms
    Rebuild - 9493ms 5112ms 5314ms 4965ms 4754ms 5050ms 5165ms 5059ms 5094ms 5096ms
    Rebuild Average - 5510.2ms
    
    Webpack 4.1.1 - Node 8.9.4
    Build - 52564ms
    Rebuild - 8294ms 8235ms 5260ms 5094ms 7926ms 8109ms 7979ms 8086ms 7721ms 7927ms
    Rebuild Average - 7463.1ms
    
    Webpack 4.1.1 - Node 8.10.0
    Build - 47680ms
    Rebuild - 9977ms 8344ms 8518ms 8205ms 8648ms 8291ms 8320ms 8320ms 7580ms 8227ms
    Rebuild Average - 8443ms
    
  6. Sojborg commented on Apr 3, 2018

    @Sojborg

    I get much worse build results after going from webpack 3 -> 4

    Webpack 3.10.0 - Node 8.9.4
    Build - 61 secs

    Webpack 3.10.0 - Node 9.6.1
    Build 65 secs

    Webpack 4.4.1 - Node 8.9.4
    Build 74 secs

    Webpack 4.4.1 - Node 9.6.1
    Build 106 secs

    Are you guys looking into this decrease in performance from webpack 3 to 4?

  7. montogeek commented on Apr 4, 2018

    @montogeek
    Contributor

    @Sojborg What is your webpack configuration?

  8. Sojborg commented on Apr 4, 2018

    @Sojborg

    I only opgrader webpack version i didn't change the config. So I still use the old plugin API could that affect performance?

  9. zenkilies commented on Apr 5, 2018

    @zenkilies

    @Sojborg let try adding this line to the config:

    "mode": process.env.NODE_ENV === "production" ? "production" : "development",
    

    WARNING in configuration

    The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.

    You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

  10. malixsys commented on Apr 10, 2018

    @malixsys

    Shouldn't you use mini-css-extract-plugin instead of ExtractTextPlugin? 🤔

  11. p3x-robot commented on Apr 18, 2018

    @p3x-robot

    I use development mode and it became a snail. 👎

  12. alexander-akait commented on Apr 18, 2018

    @alexander-akait
    Member

    @drewjenkins Can you create minimum reproducible test repo with same config for webpack@3 and webpack@4 when you have increase hmr time?

  13. alexander-akait commented on Apr 18, 2018

    @alexander-akait
    Member

    /cc @Sojborg @drewjenkins please read comment above

  14. p3x-robot commented on Apr 18, 2018

    @p3x-robot

    It is too hard to update everything, the downgrade is too expensive to play with this.
    I did the same with UglifyJS3, i tried, UglifyJs2.2.something was 22 seconds, then first version 3 was 4 minutes 42 seconds, the in about 2 months, it became 44 seconds. It was ok, for building.

    For me is the same:
    webpack/webpack-dev-server#1346

    In my code you can use I set development mode, here:
    https://github.com/patrikx3/corifeus-builder-angular/blob/master/src/config/webpack/angular/dev.js#L15

    And execute here as the config is generated:
    https://github.com/patrikx3/corifeus-builder-angular/blob/master/src/config/webpack/angular/common.js#L35

    https://github.com/patrikx3/corifeus-builder-angular/blob/master/src/config/webpack/angular/common.js#L161

    But I even did like this, if this is a mistake:

    NODE_ENV=development grunt run
    

    Building is about: 50 seconds
    Changing 1 line takes about : 18 seconds

    Well, actually I dont know, this is kind of ok (giving it is Typescript).
    But I remember it was much faster (i see it is compiling something i have never changed), it was a jiffy to update. I dont know, maybe I am wrong, sorry.

  15. birkir commented on Apr 21, 2018

    @birkir

    I traced my slowdown to the thread-loader... Either disable it or set its timeout to Infinity

  16. 34 remaining items

  17. mran3 commented on Dec 1, 2018

    @mran3

    For my site which generates more than 40 files using htmlwebpack plugin, the comments of @aaronjensen and @edmorley had a big impact, as it was becoming impossible to work on the site, to install the beta you can use: npm i --save-dev html-webpack-plugin@next

  18. Younghun-Jung commented on Dec 18, 2018

    @Younghun-Jung

    Guys.

    I think key solution of this issue, devtool option.
    I change 'inline-source-map' into 'cheap-module-eval-source-map' on devtool option, and
    speed is faster from 15s to 2s.
    thanks @voxmatt
    you save my time.

  19. cute-angelia commented on Jan 3, 2019

    @cute-angelia

    Thanks @searene! thewebpack mode is very important option about webpack speed

  20. kelly-tock commented on Jan 25, 2019

    @kelly-tock

    if you set anything in optimization in your config, does webpack merge it with defaults provided by 'development' mode?

  21. olosegres commented on Jan 31, 2019

    @olosegres

    Guys, this solves the problem! (at least for me)

    optimization: {
      splitChunks: {
        cacheGroups: {
          default: false
        }
      }
    }
    

    It seems, that webpack 4 use inside CommonsChunkPlugin with new default options, so you should disable defaults.

  22. kelly-tock commented on Jan 31, 2019

    @kelly-tock

    We’ve gotten our rebuilds down to around 5 seconds on a large codebase.

    • follow the guides on TS loader and fork-TS
    • set development as the mode
    • use fast sass loader
    • disable any sourmaps in dev for sass
    • only use fast sass, css, and style loader in dec
    • latest beta of html webpack plugin
    • I ended up not putting anything in optimatizatiin in dev. I originally had various combinations as mentioned above but it always slowed it down for me.
    • haven’t tried this yet, but my guess is that Babel typescript would be faster than the 2 loaders we’re looking st this sometime soon
  23. salujaharkirat commented on Feb 28, 2019

    @salujaharkirat

    Setting mode to develop really increases the reload speed but I need to use localhost as I have an environment map as follows:-

    let appEnv;
    
    switch (process.env.NODE_ENV) {
      case "development":
        appEnv = {
          //... dev environ config
        };
        break;
      case "production":
        appEnv = {
          //... prod environ config
        };
        break;
      default:
        appEnv = {
          //... local environ config
        };
    }
    
    export default appEnv;
    

    Specifying development as mode was reloading the changes really fast but the functionality was breaking, as development app environment was returned instead of local.

    Added following to solve the problem:-

        new webpack.DefinePlugin({
          "process.env": {
            NODE_ENV: JSON.stringify("localhost")
          }
        }),
    
  24. illiatoka commented on Sep 18, 2019

    @illiatoka

    @voxmatt Oh man, you've saved my life, thanks a lot!

  25. sibelius commented on Mar 5, 2020

    @sibelius

    what is the proper version of html-webpack-plugin to fix this?

  26. alexander-akait commented on Sep 2, 2020

    @alexander-akait
    Member

    I want to close this issue, for webpack@4 we did everything we could, also almost all loaders and plugins received updates and performance improvements, webpack@5 has more improvements and very fast, anyway if you faced with performance again (in webpack@5 too) please open new issue with reproducible test repo, thanks for using webpack, feel free to feedback

  27. zerofirework commented on Dec 16, 2021

    @zerofirework

    Late to the party but I recently run into a similar issue. We end up finding that WEBPACK_DEV_SERVER env variable is no longer set in the new dev server (webpack/webpack-cli#1973). We use the variable to gate whether to run optimization and other production only configs. The performance is recovered after this is fixed.

  28. Ryan-LT commented on Sep 14, 2022

    @Ryan-LT

    Same issues with our project. What helped:

    Scrolling through all replies, tried out many of them. This is the most effective one. Our project hot loader speed has decreased from 10s to just 200ms. Crazy!

  29. jetani-ravi commented on Jan 11, 2024

    @jetani-ravi

    This works for me

    "mode": process.env.NODE_ENV === "production" ? "production" : "development",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions