Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-legacy): prevent dead code elimination from env.LEGACY marker #2000

Closed

Conversation

jonaskuske
Copy link
Contributor

@jonaskuske jonaskuske commented Feb 13, 2021

fix #1999

close #2004 (also includes typo fix)

TL;DR:

Currently if (import.meta.env.LEGACY) is treated as if ('__VITE_IS_LEGACY__') so it's always true.
Likewise, if (import.meta.env.LEGACY === true) is always false because it's treated as if ('__VITE_IS_LEGACY__' === true).

This is due to esbuild's constant folding – using an array (or object) as marker the way it's implemented in this PR circumvents that.

@jonaskuske jonaskuske force-pushed the fix/env-legacy-constant-folding branch from 9c94168 to b4b9538 Compare February 13, 2021 12:12
@yyx990803
Copy link
Member

Thanks for the suggested fix! I think even with a constant Array, this is not fundamentally safe since esbuild may one day improve its constant folding to even detect array literals containing only constants.

This also made me realize that the previous define behavior is problematic because it forces all defined values into string constants (without a way to define raw expressions).

I've refactored the define behavior in 059070e and fixed the plugin-legacy compat in bace724.

@yyx990803 yyx990803 closed this Feb 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

import.meta.env.LEGACY broken due to esbuild's constant folding
2 participants