Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setImmediate causes analyzer to only start after webpack's done callback is invoked #232

Closed
liangchunn opened this issue Dec 5, 2018 · 3 comments

Comments

@liangchunn
Copy link

liangchunn commented Dec 5, 2018

Issue description

This issue stems from parallel-webpack#85 and is related to #152, and a PR in parallel-webpack#89 in an attempt to 'hack around' the faulty behaviour.

webpack-bundle-analyzer is enqueueing the main analyze operation with setImmediate, which affects this plugin to execute only after a webpack build (which is executed with the Node.js API) has invoked the done callback.

I suspect that when webpack is running, there are a lot of I/O operations being spawned by webpack (like reading and writing), therefore the actions that are defined and called using setImmediate are deferred after the webpack build has finished. Since there is no way to 'listen' when this plugin has successfully completed, this causes issues with tools that exits as soon as the done callback is fired (for example: tooling that spawns multiple webpack builds in parallel and hooks into the done callback to determine if they are done):

const webpack = require('webpack')
const config = require('./webpack.config')

const compiler = webpack(config)

compiler.run(/* done callback */ (err, stats) => {
    if (err) {
        console.log(err)
        process.exit(1)
    }
    console.log('build succeeded')
    process.exit(0)
    // now the analyzer will not be able to emit anything here
})

Reproducible repository

I've created a minimal repository that reproduces this issue, and a detailed explanation here: https://github.com/liangchunn/webpack-bundle-analyzer-set-immediate-bug

Please read the README as it contains important things to watch out in order to understand how it actually works.

Possible fixes

  • Don't enqueue operations with setImmediate, make it execute whenever the line is reached. This would change the behaviour of how people use it, and might echo logs between builds (Reference setTimeout -> setImmediate #21)
  • Have an option like { deferAnalyzer: boolean = true} (true by default), and explicitly opt-out enqueuing operations with setImmediate by setting { deferAnalyzer: false }, which won't break everyone's build or change the way people are already using this plugin

In any case, I am open for a PR addressing this issue. Just let me know which route to go (or if there are any other possibilities to fix this issue).

Technical info

  • Webpack Bundle Analyzer version: 3.0.3
  • Webpack version: 4.27.1
  • Node.js version: 8.11.3
  • yarn version: 1.12.3
  • OS: macOS 10.14.1

Debug info

How do you use this module? As CLI utility or as plugin?
plugin

If plugin, what options were provided? (e.g. new BundleAnalyzerPlugin({ analyzerMode: 'disabled', generateStatsFile: true }))

{
    analyzerMode: 'static',
    reportFilename: './report.html',
    openAnalyzer: false
}

What other Webpack plugins were used?

  • clean-webpack-plugin
@liangchunn liangchunn changed the title setImmediate causes webpack to call done callback prematurely setImmediate causes webpack to invoke done callback prematurely Dec 5, 2018
@liangchunn liangchunn changed the title setImmediate causes webpack to invoke done callback prematurely setImmediate causes analyzer to only start after webpack's 'done' callback is invoked Dec 5, 2018
@liangchunn liangchunn changed the title setImmediate causes analyzer to only start after webpack's 'done' callback is invoked setImmediate causes analyzer to only start after webpack's done callback is invoked Dec 5, 2018
@valscion
Copy link
Member

valscion commented Dec 7, 2018

Thanks for the thorough issue report! We will get to this when we have time ☺️

@mareolan
Copy link
Contributor

mareolan commented Feb 2, 2019

I've hit this issue too. There's another solution by using Webpack's tapAsync instead of tap which will make Webpack's "done" hook wait until the bundle analyzer finishes its async operation.

@valscion
Copy link
Member

This should be fixed in v3.0.4 now, let us know if that's not the case.

rohit-gohri added a commit to smartprix/sm-webpack-config that referenced this issue May 10, 2019
No need for it now as bundle-analyzer handles it now: webpack-contrib/webpack-bundle-analyzer#232
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants