next.js icon indicating copy to clipboard operation
next.js copied to clipboard

webpack-bundle-analyzer sever mode is broken because of two sets of bundles

Open PengyuanZhao opened this issue 8 years ago • 6 comments

webpack-bundle-analyzer server mode is broken after I upgraded to Next.js 5. There are two sets of bundles: .next/bundles(uglified) and .next/dist/bundles. webpack-bundle-analyzer will generate report.html for each of them and seems to try to open two servers on the same port which then crashes. Any way to get around this?

Thanks!

PengyuanZhao avatar Feb 27 '18 15:02 PengyuanZhao

In the webpack bundle analyzer config set the analyzerMode to static. It will still generate and open two reports, but it won't crash.

webpackConfig.plugins.push(
  { analyzerMode: 'static' }
);

ghost avatar Feb 27 '18 15:02 ghost

@shane805 Good call. That's actually what I'm using instead for now. But still wondering if there is a way to make the server mode work.

PengyuanZhao avatar Feb 27 '18 15:02 PengyuanZhao

You can now use next-bundle-analyzer to handle this. It automatically configures the correct ports to open 2 compilations. https://github.com/zeit/next-plugins/tree/master/packages/next-bundle-analyzer

timneutkens avatar Feb 27 '18 16:02 timneutkens

Hi! Just for the record, the next-bundle-analyzer has hardcoded config which does not suit me. But I stole its code and make it work with server mode:

// nex.config.js
module.exports = {
  webpack: (config, options) => {
    if (ANALYZE) {
      const { isServer } = options
      config.plugins.push(new BundleAnalyzerPlugin({
        analyzerMode: 'server',
        //  The host changed from 127.0.0.0 to make it work when running it in docker container. Maybe you do not need that change.
        analyzerHost: '0.0.0.0', 
        analyzerPort: isServer ? '8881': '8882',
        openAnalyzer: false
      }))
    }

    return config
  }
}

lependu avatar Mar 02 '18 11:03 lependu

@lependu Thanks! It's great to know that isServer is the key here.

PengyuanZhao avatar Mar 02 '18 14:03 PengyuanZhao

we should fix examples in documentation too. I met this problem today while learn next.js.

meotimdihia avatar Dec 05 '18 11:12 meotimdihia