Skip to content

Commit

Permalink
fix(plugin-legacy): prevent constant folding for import.meta.env.LEGACY
Browse files Browse the repository at this point in the history
close #1999
  • Loading branch information
yyx990803 committed Feb 13, 2021
1 parent 059070e commit bace724
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
13 changes: 10 additions & 3 deletions packages/playground/legacy/main.js
Expand Up @@ -5,6 +5,13 @@ async function run() {

run()

document.getElementById('env').textContent = `is legacy: ${
import.meta.env.LEGACY
}`
let isLegacy

// make sure that branching works despite esbuild's constant folding (#1999)
if (import.meta.env.LEGACY) {
if (import.meta.env.LEGACY === true) isLegacy = true
} else {
if (import.meta.env.LEGACY === false) isLegacy = false
}

document.getElementById('env').textContent = `is legacy: ${isLegacy}`
6 changes: 3 additions & 3 deletions packages/plugin-legacy/index.js
Expand Up @@ -189,7 +189,7 @@ function viteLegacyPlugin(options = {}) {
}

if (raw.includes(legacyEnvVarMarker)) {
const re = new RegExp(`"${legacyEnvVarMarker}"`, 'g')
const re = new RegExp(legacyEnvVarMarker, 'g')
if (config.build.sourcemap) {
const s = new MagicString(raw)
let match
Expand Down Expand Up @@ -550,8 +550,8 @@ function replaceLegacyEnvBabelPlugin() {
return ({ types: t }) => ({
name: 'vite-replace-env-legacy',
visitor: {
StringLiteral(path) {
if (path.node.value === legacyEnvVarMarker) {
Identifier(path) {
if (path.node.name === legacyEnvVarMarker) {
path.replaceWith(t.booleanLiteral(true))
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-legacy/package.json
Expand Up @@ -32,6 +32,6 @@
"systemjs": "^6.8.3"
},
"peerDependencies": {
"vite": "^2.0.0-beta.12"
"vite": "^2.0.0-beta.70"
}
}

0 comments on commit bace724

Please sign in to comment.