Skip to content

Commit

Permalink
fix(define): should not stringify vite internal env (#12120)
Browse files Browse the repository at this point in the history
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
sun0day and bluwy committed Feb 21, 2023
1 parent 3551f75 commit 73c3999
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/vite/src/node/plugins/define.ts
Expand Up @@ -31,16 +31,24 @@ export function definePlugin(config: ResolvedConfig): Plugin {
})
}

const env = { ...config.env }
const userDefine: Record<string, string> = {}
const userDefineEnv: Record<string, string> = {}
for (const key in config.define) {
const val = config.define[key]
userDefine[key] = typeof val === 'string' ? val : JSON.stringify(val)

// make sure `import.meta.env` object has user define properties
const match = key.match(metaEnvRe)
if (match) {
env[match[1]] = val
if (isBuild) {
const match = key.match(metaEnvRe)
if (match) {
userDefineEnv[match[1]] =
// test if value is raw identifier to wrap with __vite__ so when
// stringified for `import.meta.env`, we can remove the quotes and
// retain being an identifier
typeof val === 'string' && /^[\p{L}_$]/u.test(val.trim())
? `__vite__${val}__vite__`
: val
}
}
}

Expand All @@ -49,16 +57,21 @@ export function definePlugin(config: ResolvedConfig): Plugin {
const importMetaKeys: Record<string, string> = {}
const importMetaFallbackKeys: Record<string, string> = {}
if (isBuild) {
env.SSR = !!config.build.ssr

const env: Record<string, any> = {
...config.env,
SSR: !!config.build.ssr,
}
// set here to allow override with config.define
importMetaKeys['import.meta.hot'] = `undefined`
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}
Object.assign(importMetaFallbackKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(env),
'import.meta.env': JSON.stringify({ ...env, ...userDefineEnv }).replace(
/"__vite__(.+?)__vite__"/g,
(_, val) => val,
),
})
}

Expand Down
1 change: 1 addition & 0 deletions playground/legacy/__tests__/legacy.spec.ts
Expand Up @@ -23,6 +23,7 @@ test('import.meta.env.LEGACY', async () => {
isBuild ? 'true' : 'false',
true,
)
await untilUpdated(() => page.textContent('#env-equal'), 'true', true)
})

// https://github.com/vitejs/vite/issues/3400
Expand Down
1 change: 1 addition & 0 deletions playground/legacy/index.html
@@ -1,5 +1,6 @@
<h1 id="app"></h1>
<div id="env"></div>
<div id="env-equal"></div>
<div id="iterators"></div>
<div id="features-after-corejs-3"></div>
<div id="async-generator"></div>
Expand Down
3 changes: 3 additions & 0 deletions playground/legacy/main.js
Expand Up @@ -20,6 +20,9 @@ if (import.meta.env.LEGACY) {

text('#env', `is legacy: ${isLegacy}`)

const metaEnvObj = import.meta.env
text('#env-equal', import.meta.env.LEGACY === metaEnvObj.LEGACY)

// Iterators
text('#iterators', [...new Set(['hello'])].join(''))

Expand Down

0 comments on commit 73c3999

Please sign in to comment.