webpack-contrib / webpack-bundle-analyzer

Webpack plugin and CLI utility that represents bundle content as convenient interactive zoomable treemap
MIT License
12.53k stars 479 forks source link

Bundle parsing breaks if bundled code creates a sequence #604

Closed jameshoward closed 1 year ago

jameshoward commented 1 year ago

Issue description

We have begun using the Sentry Webpack plugin, which injects a small script at the top of the bundle output. This seems to break the parseBundle function in parseUtils.js because, after minification, the bundle ends up starting like this:

!function(){ /* Sentry code here */ }(),(()=>{ /* webpack module here */})();

When walking the AST in parseBundle, this is matched as a single ExpressionStatement but it doesn't match any of the conditions for finding the Webpack module.

This in turn means that node_modules are not correctly split out from the src in our project when using this plugin.

There's an example at https://github.com/jameshoward/webpack-sentry-demo but an even simpler reproduction is to inject code with a BannerPlugin, provided that code won't be overly-stripped-out by Terser:

// webpack.config.js
  …
  plugins: [
    new webpack.BannerPlugin({
      banner: '(function() { console.log(window); })();',
      raw: true
    })
  ]

Also raised as https://github.com/getsentry/sentry-javascript-bundler-plugins/issues/335 in case it could be fixed their side.

Technical info

  System:
    OS: macOS 13.4
    CPU: (8) arm64 Apple M2
    Memory: 1.03 GB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.20.0 - /opt/homebrew/opt/node@16/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.4 - /opt/homebrew/opt/node@16/bin/npm
  npmPackages:
    webpack: ^5.88.0 => 5.88.0
    webpack-bundle-analyzer: ^4.9.0 => 4.9.0
    webpack-cli: ^5.1.4 => 5.1.4

Debug info

Used as a CLI with webpack-bundle-analyzer stats.json dist/

valscion commented 1 year ago

Thanks for opening this issue! This looks similar to the feature request here: https://github.com/webpack-contrib/webpack-bundle-analyzer/discussions/601 — that is, we don't currently support BannerPlugin.

valscion commented 1 year ago

Could you share your details in #601 and close this issue? Let's keep the feature request discussion in the same place ☺️

jameshoward commented 1 year ago

Yes, sorry, I'll add a comment to the FR. I had checked through the issues to make sure I wasn't duplicating one but didn't think to check the feature requests. Thanks.

valscion commented 1 year ago

Yeah no worries! I haven't done much work directing people to discussions so it's easy to miss ☺️