Skip to content

Commit 73c3999

Browse files
sun0daybluwy
andauthored
fix(define): should not stringify vite internal env (#12120)
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
1 parent 3551f75 commit 73c3999

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

packages/vite/src/node/plugins/define.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,24 @@ export function definePlugin(config: ResolvedConfig): Plugin {
3131
})
3232
}
3333

34-
const env = { ...config.env }
3534
const userDefine: Record<string, string> = {}
35+
const userDefineEnv: Record<string, string> = {}
3636
for (const key in config.define) {
3737
const val = config.define[key]
3838
userDefine[key] = typeof val === 'string' ? val : JSON.stringify(val)
3939

4040
// make sure `import.meta.env` object has user define properties
41-
const match = key.match(metaEnvRe)
42-
if (match) {
43-
env[match[1]] = val
41+
if (isBuild) {
42+
const match = key.match(metaEnvRe)
43+
if (match) {
44+
userDefineEnv[match[1]] =
45+
// test if value is raw identifier to wrap with __vite__ so when
46+
// stringified for `import.meta.env`, we can remove the quotes and
47+
// retain being an identifier
48+
typeof val === 'string' && /^[\p{L}_$]/u.test(val.trim())
49+
? `__vite__${val}__vite__`
50+
: val
51+
}
4452
}
4553
}
4654

@@ -49,16 +57,21 @@ export function definePlugin(config: ResolvedConfig): Plugin {
4957
const importMetaKeys: Record<string, string> = {}
5058
const importMetaFallbackKeys: Record<string, string> = {}
5159
if (isBuild) {
52-
env.SSR = !!config.build.ssr
53-
60+
const env: Record<string, any> = {
61+
...config.env,
62+
SSR: !!config.build.ssr,
63+
}
5464
// set here to allow override with config.define
5565
importMetaKeys['import.meta.hot'] = `undefined`
5666
for (const key in env) {
5767
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
5868
}
5969
Object.assign(importMetaFallbackKeys, {
6070
'import.meta.env.': `({}).`,
61-
'import.meta.env': JSON.stringify(env),
71+
'import.meta.env': JSON.stringify({ ...env, ...userDefineEnv }).replace(
72+
/"__vite__(.+?)__vite__"/g,
73+
(_, val) => val,
74+
),
6275
})
6376
}
6477

playground/legacy/__tests__/legacy.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ test('import.meta.env.LEGACY', async () => {
2323
isBuild ? 'true' : 'false',
2424
true,
2525
)
26+
await untilUpdated(() => page.textContent('#env-equal'), 'true', true)
2627
})
2728

2829
// https://github.com/vitejs/vite/issues/3400

playground/legacy/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<h1 id="app"></h1>
22
<div id="env"></div>
3+
<div id="env-equal"></div>
34
<div id="iterators"></div>
45
<div id="features-after-corejs-3"></div>
56
<div id="async-generator"></div>

playground/legacy/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ if (import.meta.env.LEGACY) {
2020

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

23+
const metaEnvObj = import.meta.env
24+
text('#env-equal', import.meta.env.LEGACY === metaEnvObj.LEGACY)
25+
2326
// Iterators
2427
text('#iterators', [...new Set(['hello'])].join(''))
2528

0 commit comments

Comments
 (0)