Skip to content

Webpack reresolves modules that shouldn't be dirty #1530

Closed

Description

@tvararu

I've spent a fair amount of time scouring the documentation and source code but I'm unable to speed up this usecase.

We're using webpack in a huge application, bordering on 1000 components, all of them fairly substantial. Our initial compile times are slow (40s), and our incremental builds are also slow (5s).

Here's a repository that seeks to emulate a test case for what I'm dealing with: https://github.com/tvararu/webpack-test.

  • The entry point is A.js, which requires B.js and C.js.
  • B.js is tiny and doesn't have a lot of dependencies.
  • C.js is monolithic and has thousands of requires.

My expectation is that when using webpack-dev-server in the test project, whenever I save B.js, webpack should recognize that C.js and none of its dependencies have been touched. It should compile B.js swiftly (in <10ms), replace it in the cache, and output the compiled A.js using the cached version of C.js from the initial compile.

However, webpack compiles 3002 hidden modules every time I save B.js, leading to a compile time of 960ms. This isn't bad on its own, but spirals out of control if you add some loaders like react-hot and babel, which is the case in our real project.

I do have a solution: on the same test project there is a dll branch. On that branch, you can run webpack --config webpack.dll.config.js to generate two DLLs from B.js and C.js which will then get leveraged when compiling A.js. Whenever you save B.js, its DLL will get recompiled, A.js will notice that one of its DLLs has updated and it'll just take the old DLL of C.js and the new DLL of B.js and conjoin them together into one quick happy bundle.

I could go further on that branch and do directory reads or dependency graph walks to generate a DLL for every component. That should in theory make compiling as fast as I would like it. But at that point it seems like I've just reimplemented (poorly) what the caching layer in webpack should do on its own, so what's going on here?

What blindingly obvious thing am I missing?

Thanks for reading. :)

Activity

  1. tvararu commented on Nov 4, 2015

    @tvararu
    Author

    I've just submitted a question on Stack Overflow as well for this issue.

  2. sokra commented on Nov 14, 2015

    @sokra
    Member

    It doesn't recompile all the modules. It only reresolves all modules.

  3. changed the title [-]Webpack recompiles modules that shouldn't be dirty[/-] [+]Webpack reresolves modules that shouldn't be dirty[/+] on Nov 15, 2015
  4. tvararu commented on Nov 15, 2015

    @tvararu
    Author

    Thanks for the input! I have edited the issue title to reflect this.

    This issue has been marked with the [bug] label; would profiling webpack using node-inspector like in this issue be useful to determining the cause of the performance regression for this usecase?

  5. donaldpipowitch commented on Feb 2, 2016

    @donaldpipowitch

    Has this ever been resolved? Can I use webpack watch mode and get incremental builds? I see the same problem. I use browser-sync as my development server (as I need CSS live reloading and style-loader produces an initial flash effect when the styles are injected with a slight delay). You can clearly see that all source files are written down to file system even if just one file changes.

    I have a small example:

    - package.json
    - start.js
    - src/
      - index.html
      - app.js
      - style.less
    

    package.json:

    {
      "name": "webpack-example",
      "private": true,
      "scripts": {
        "start": "node ./start.js"
      },
      "devDependencies": {
        "browser-sync": "^2.11.1",
        "css-loader": "^0.23.1",
        "extract-text-webpack-plugin": "^1.0.1",
        "html-loader": "^0.4.0",
        "html-webpack-plugin": "^2.7.2",
        "less": "^2.6.0",
        "less-loader": "^2.2.2",
        "string-replace-webpack-plugin": "0.0.3",
        "webpack": "^1.12.12"
      }
    }
    

    start.js:

    var path = require('path');
    var webpack = require('webpack');
    var HtmlWebpackPlugin = require('html-webpack-plugin');
    var StringReplacePlugin = require('string-replace-webpack-plugin');
    var ExtractTextPlugin = require('extract-text-webpack-plugin');
    var bs = require('browser-sync').create();
    
    var TRANSLATION_REG_EXP = /<%=\s*(\S+)\s*%>/g;
    
    var translations = {
      'foo.bar': 'Hello world! Replaced.'
    };
    
    var compiler = webpack({
      entry: {
        app: [
          './src/app.js'
        ]
      },
      output: {
        path: path.join(__dirname, 'dist'), // must be absolute
        filename: 'app.js'
      },
      module: {
        loaders: [
          {
            test: /\.less$/,
            loader: ExtractTextPlugin.extract('css?sourceMap!less?sourceMap')
          },
          {
            test: /\.(js|html)$/,
            exclude: /(node_modules|bower_components)/,
            loader: StringReplacePlugin.replace({
              replacements: [
                {
                  pattern: TRANSLATION_REG_EXP,
                  replacement: (match, key, offset, string) => translations[key]
                }
              ]
            })
          }
        ]
      },
      plugins: [
        new HtmlWebpackPlugin({
          template: 'html!./src/index.html'
        }),
        new ExtractTextPlugin('styles.css'),
        new StringReplacePlugin()
      ],
      devtool: 'inline-source-map'
    });
    
    compiler.watch({}, function(err, stats) {
      if (err) {
        console.error('webpack build error');
      } else {
        console.log('webpack build', stats.endTime);
      }
    });
    
    bs.init({
      server: {
        baseDir: './dist'
      }
    });
    
    bs.watch('./dist/**/*.(html|js|css)').on('change', bs.reload);
    

    src/index.html:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Example</title>
    
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
      <!-- styles will be injected here -->
    </head>
    <body>
      <h1><%= foo.bar %></h1>
    
      <!-- scripts will be injected here -->
    </body>
    </html>
    

    src/app.js:

    // styles
    require('./style.less');
    
    // scripts
    console.log('<%= foo.bar %>');
    

    src/style.less:

    h1 {
      color: red;
    }
    

    When I run $ npm start now I get:

    ...
    [BS] Serving files from: ./dist
    webpack build 1454393629861
    [BS] File changed: dist/styles.css
    [BS] File changed: dist/app.js
    [BS] File changed: dist/index.html
    

    When I edit src/style.less now I'd expect that webpack would only compile src/style.less to dist/style.css, but all files are rewritten:

    # changing src/style.less
    webpack build 1454393633888
    [BS] File changed: dist/styles.css # expected
    [BS] File changed: dist/index.html # unexpected
    [BS] File changed: dist/app.js # unexpected
    
  6. donaldpipowitch commented on Feb 4, 2016

    @donaldpipowitch

    @sokra This happens in ^2.0.6-beta, too.

  7. donaldpipowitch commented on Feb 4, 2016

    @donaldpipowitch

    While webpack writes unchanged files to the file system, so bs.watch('./dist/**/*.(html|js|css)').on('change', bs.reload); is unnecessarily called, the stats object of the watch callback actually contains information about which files have changed (module.built === true).

    I can now rewrite my example to get the actual loading behaviour I want:

    compiler.watch({}, function(err, stats) {
      if (err) {
        console.error('webpack build error');
      } else {
        console.log('webpack build', stats.endTime);
    
    +    var changedModules = stats.compilation.modules.filter(function(module) {
    +      return module.built && module.resource;
    +    });
    +    var changedStyleModules = changedModules.filter(function(module) {
    +      return module.resource.match(/\.(css|less|sass)$/);
    +    });
    +    var hasOnlyStyleChanges = changedModules.length === changedStyleModules.length;
    +    if (hasOnlyStyleChanges) {
    +      bs.reload('*.css');
    +    } else {
    +      bs.reload();
    +    }
      }
    });
    
    bs.init({
      server: {
        baseDir: './dist'
      }
    });
    
    -bs.watch('./dist/**/*.(html|js|css)').on('change', bs.reload);

    Caveats:

    1. The files are still unnecessarily written.
    2. module.resource gives me the source file, but I think there is no way to see which files be generated out of this source file. I need to make a manual mapping (e.g. assume that .less, .sass and .css files output some other .css file).
  8. AlexGalays commented on Sep 8, 2016

    @AlexGalays

    @donaldpipowitch Thanks a lot for providing a working, complete workaround to this very annoying behavior.

  9. webpack-bot commented on Aug 11, 2017

    @webpack-bot
    Contributor

    This issue had no activity for at least half a year.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  10. added
    status: stale
    on Aug 11, 2017
  11. dmnd commented on Aug 15, 2017

    @dmnd

    To prevent the @webpack-bot from closing this, I just did some quick testing on my app that has multiple entry points. I changed a module that is only reachable by a single entry point and recorded how long it took to incrementally build.

    Time taken with all entry points active: 11159ms
    Time take with only one entry point active (e.g. commented out the rest): 1017ms

    This is with webpack 2.2 because I haven't upgraded yet. But I can confirm the issue is not fixed as of 2.2.

  12. removed
    status: stale
    on Aug 15, 2017
  13. webpack-bot commented on Feb 14, 2018

    @webpack-bot
    Contributor

    This issue had no activity for at least half a year.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  14. added
    status: stale
    on Feb 14, 2018
  15. warrenseine commented on Feb 26, 2018

    @warrenseine

    @webpack-bot Please don’t close.

  16. removed
    status: stale
    on Feb 26, 2018
  17. BobbyBabes commented on Jul 5, 2018

    @BobbyBabes

    @donaldpipowitch

    The files are still unnecessarily written.

    Isn't this because Webpack always injects certain boilerplate to files ?
    Just a wild guess though.

  18. webpack-bot commented on Jan 4, 2019

    @webpack-bot
    Contributor

    This issue had no activity for at least half a year.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  19. added
    status: stale
    on Jan 4, 2019
  20. webpack-bot commented on Jan 19, 2019

    @webpack-bot
    Contributor

    Issue was closed because of inactivity.

    If you think this is still a valid issue, please file a new issue with additional information.

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

    status: stale

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions