developit / microbundle

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

in compress mode, micro bundle causing to trim the code. #1029

Closed Code-Crash closed 1 year ago

Code-Crash commented 1 year ago

In one of my builds, when I'm using the compress flag, it's causing the issue which is trimming the value from window.parent.document to window only, which is causing a failure in expected behavior.

Please see the attached images for reference.

Below this image, we can see window.parent.document getting changed to window only, please see line number 3835 in the first image and in the second 2259.

uncompress

Please see the window

compress
rschristian commented 1 year ago

This is an intentional effect of Terser's pure_getters compression option:

pure_getters (default: "strict") -- If you pass true for this, Terser will assume that object property access (e.g. foo.bar or foo["bar"]) doesn't have any side effects. Specify "strict" to treat foo.bar as side-effect-free only when foo is certain to not throw, i.e. not null or undefined.

https://github.com/developit/microbundle/blob/bb8713c74706f466672b3109552c7d3e675b5325/src/index.js#L597-L601

Is there any particular reason you're relying on side effects (throwing) rather than just doing the following?

passEmitsToParent = window.parent.document ? false : true;
Code-Crash commented 1 year ago

Yes @rschristian, I was relying on the catch block if I'm not able to access window.parent.document, however as you said adding if statement or ternary operator, it's working.

so if it's expected behavior, then we are good.

Thanks for clarification.