You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the issue is related to 1.x -> 2.0 upgrade, read the Migration Guide first.
Describe the bug
Works:
console.log(import.meta.env.LEGACY)// true or false depending on chunk
Doesn't work:
if(import.meta.env.LEGACY){console.log(true)}else{console.log(false)}// true in both chunks
esbuild runs before the value is replaced so it still sees the '__VITE_IS_LEGACY__' marker, then – even with minification disabled – does its constant folding aka dead code elimination. Because '__VITE_IS_LEGACY__' is truthy, the entire else branch is dropped and both the legacy and the modern chunk log true.
Likewise, this will only ever run the else branch because '__VITE_IS_LEGACY__' === true is folded into false:
Describe the bug
Works:
Doesn't work:
esbuild
runs before the value is replaced so it still sees the'__VITE_IS_LEGACY__'
marker, then – even with minification disabled – does its constant folding aka dead code elimination. Because'__VITE_IS_LEGACY__'
is truthy, the entire else branch is dropped and both the legacy and the modern chunk logtrue
.Likewise, this will only ever run the
else
branch because'__VITE_IS_LEGACY__' === true
is folded intofalse
:To fix this, you can use an array as marker because array and object expressions don't participate in constant folding: esbuild.github.io/api/#define
I've submitted a PR :)
Reproduction
See the updated
playground/legacy/main.js
that uses if/else branching – if you run it with the current plugin code from upstream, the test will fail.System Info
vite
version:vite@2.0.0-beta.69
The text was updated successfully, but these errors were encountered: