Skip to content

Commit bace724

Browse files
committed
fix(plugin-legacy): prevent constant folding for import.meta.env.LEGACY
close #1999
1 parent 059070e commit bace724

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

packages/playground/legacy/main.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ async function run() {
55

66
run()
77

8-
document.getElementById('env').textContent = `is legacy: ${
9-
import.meta.env.LEGACY
10-
}`
8+
let isLegacy
9+
10+
// make sure that branching works despite esbuild's constant folding (#1999)
11+
if (import.meta.env.LEGACY) {
12+
if (import.meta.env.LEGACY === true) isLegacy = true
13+
} else {
14+
if (import.meta.env.LEGACY === false) isLegacy = false
15+
}
16+
17+
document.getElementById('env').textContent = `is legacy: ${isLegacy}`

packages/plugin-legacy/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function viteLegacyPlugin(options = {}) {
189189
}
190190

191191
if (raw.includes(legacyEnvVarMarker)) {
192-
const re = new RegExp(`"${legacyEnvVarMarker}"`, 'g')
192+
const re = new RegExp(legacyEnvVarMarker, 'g')
193193
if (config.build.sourcemap) {
194194
const s = new MagicString(raw)
195195
let match
@@ -550,8 +550,8 @@ function replaceLegacyEnvBabelPlugin() {
550550
return ({ types: t }) => ({
551551
name: 'vite-replace-env-legacy',
552552
visitor: {
553-
StringLiteral(path) {
554-
if (path.node.value === legacyEnvVarMarker) {
553+
Identifier(path) {
554+
if (path.node.name === legacyEnvVarMarker) {
555555
path.replaceWith(t.booleanLiteral(true))
556556
}
557557
}

packages/plugin-legacy/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
"systemjs": "^6.8.3"
3333
},
3434
"peerDependencies": {
35-
"vite": "^2.0.0-beta.12"
35+
"vite": "^2.0.0-beta.70"
3636
}
3737
}

0 commit comments

Comments
 (0)