bugsnag / bugsnag-js

JavaScript error handling tool for BugSnag. Monitor and report JavaScript bugs & errors.
https://docs.bugsnag.com/platforms/javascript
MIT License
855 stars 252 forks source link

Add ESM build #1397

Open irahopkinson opened 3 years ago

irahopkinson commented 3 years ago

Description

Since about Angular 9 or 10 it has been reporting "CommonJS or AMD dependencies can cause optimization bailouts." It has been reporting this for this BugSnag dependency.

Describe the solution you'd like Add an ESM (ES Module) build that we can use instead of the CommonJS one. I recently did this for one of our libraries that was also reporting the same problem and found it easy to follow along with this article https://blog.logrocket.com/publishing-node-modules-typescript-es-modules/.

Describe alternatives you've considered Use a different system that supports ESM.

mattdyoung commented 3 years ago

Hi @irahopkinson

This was previously raised here https://github.com/bugsnag/bugsnag-js/issues/1276 and there is a lot of previous discussion on this issue: https://github.com/bugsnag/bugsnag-js/issues/463#issuecomment-454693484

Despite the "CommonJS or AMD dependencies can cause optimization bailouts." warning you get in recent Angular versions, our analysis has shown that moving the Bugsnag library to ES Modules would not provide any size benefit from tree shaking.

We're therefore unlikely to add an ESM build in the near future due to the effort and maintenance overhead involved.

I'll keep this open as a feature request as it's something that we may revisit if any future changes to the library make this worthwhile.

tleunen commented 2 years ago

I believe that because of the way Bugsnag-js is built and given to consumers, some optimizations are impossible on their side. For example, I think the library is not tree shaked properly when @bugsnag/js is imported in a file without being used. (In a situation where the entire library should not be present in the output bundle)

tleunen commented 2 years ago

I created a small repo to show the issue I've seen https://github.com/tleunen/bugsnag-treeshaking-issue

The entry point is this file: https://github.com/tleunen/bugsnag-treeshaking-issue/blob/main/src/index.ts You'll see that no bugsnag code is ever used. But bugsnag is still present in the output bundle. I built the project twice (with ncc / webpack 5) to get a minified version and an un-minified version of it. You'll see the build files in the dist folder (I pushed them on git to be visible easily as well) https://github.com/tleunen/bugsnag-treeshaking-issue/tree/main/dist

Hope this can help @mattdyoung

cc @imjoehaines

evelant commented 2 years ago

More and more libraries are being ESM only these days. When popular libraries like Bugsnag don't provide ESM builds it makes the transition harder for everyone. Please consider adding an ESM build sooner rather than later as it will inevtiably become necessary anyway.

pepicrft commented 1 year ago

We are also interested in this one. We are moving our Typescript Node project to the NodeNext module resolution and the type-checking fails because @bugsnag/js doesn't comply with Node's conventions.

m-i-c-h-a-l commented 3 months ago

Because of this issue, Bugsnag is not usable with some new nodejs frameworks, which use NodeNext moduleResolution, e.g. Adonis 6.

hannah-smartbear commented 3 months ago

Hi all,

We are continuing to monitor this thread and feed comments back to our Product Team, as we have an item on our backlog to publish the JS notifier as ES modules. While we don't currently have any further updates on this, we will make sure to keep this thread posted with any developments.