developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.04k stars 362 forks source link

microbundle bundles of the "debug" package with "--target node" results on ReferenceError: navigator is not defined #880

Closed asasvirtuais closed 3 years ago

asasvirtuais commented 3 years ago

microbundle bundles of the "debug" package with "--target node" results on ReferenceError: navigator is not defined

https://github.com/icaro-capobianco/debug-microbundle-repro

Workaround is to have debug included as external either through the --external flag or by avoiding unknown dependencies altogether.

asasvirtuais commented 3 years ago

I don't think this is a problem with microbundle at all,

debug should probably just point their main export to the node version https://github.com/visionmedia/debug/blob/master/package.json#L54

I will close the issue now, just wanted to leave it here for future devs looking for a solution

asasvirtuais commented 3 years ago

Reopening because it was contested

https://github.com/visionmedia/debug/issues/834#issuecomment-901514044

Is there anything @developit can do here?

On my case it's not a problem because I only bundle my own code and keep dependencies imported, but for anyone wanting to really bundle everything, this would likely be an issue.

developit commented 3 years ago

Is there a reason why you'd be wanting to bundle the debug package? That seems like the kind of dependency that should never be bundled, since it varies by environment.

developit commented 3 years ago

Looking at the code, this seems like it would be an issue in any Rollup bundle that targets Node. The problem is not that the browser target is being ignored, it's that debug attempts to differentiate between Node and Electron/NW.js at runtime - that means bundlers have to bundle both the browser and the Node version of debug if targeting Node (which is bad).

I don't think there's a durable fix that can be implemented in microbundle, since we'd have to inline all 4 conditions from here, 3 of which aren't really knowable at build time:

(eg: someone could be using --target node for Electron, and this would break that use-case)

Instead, what about aliasing?

microbundle --target node --alias debug=debug/src/node.js

Personally I'd still recommend having debug be an external dependency instead of bundled. To do that, move it from "dependencies" to "peerDependencies" + "devDependencies".

asasvirtuais commented 3 years ago

Is there a reason why you'd be wanting to bundle the debug package? That seems like the kind of dependency that should never be bundled since it varies by environment.

I stumbled upon this issue by accident, when some of my internal libraries had nested dependencies I wasn't aware of (which included debug).

To provide context: I bundle my backend code because I run a couple of projects that have a couple of in-house libraries that I manage all by myself, so to avoid having issues arising whenever I update something, I just take a snapshot (bundle) of how the code was like and deploy it, that way I just need to worry about one project at a time.

asasvirtuais commented 3 years ago

Personally I'd still recommend having debug be an external dependency instead of bundled. To do that, move it from "dependencies" to "peerDependencies" + "devDependencies".

That is what I did! Thank you for the input! @developit