import.meta.env.LEGACY
broken due to esbuild's constant folding
#1999
Labels
import.meta.env.LEGACY
broken due to esbuild's constant folding
#1999
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: