Skip to content

Commit 0a50c59

Browse files
authored
fix(define): inconsistent env values in build mode (#12058)
1 parent 081c27f commit 0a50c59

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { isCSSRequest } from './css'
66
import { isHTMLRequest } from './html'
77

88
const nonJsRe = /\.json(?:$|\?)/
9+
const metaEnvRe = /import\.meta\.env\.(.+)/
910
const isNonJsRequest = (request: string): boolean => nonJsRe.test(request)
1011

1112
export function definePlugin(config: ResolvedConfig): Plugin {
@@ -30,29 +31,34 @@ export function definePlugin(config: ResolvedConfig): Plugin {
3031
})
3132
}
3233

34+
const env = { ...config.env }
3335
const userDefine: Record<string, string> = {}
3436
for (const key in config.define) {
3537
const val = config.define[key]
3638
userDefine[key] = typeof val === 'string' ? val : JSON.stringify(val)
39+
40+
// make sure `import.meta.env` object has user define properties
41+
const match = key.match(metaEnvRe)
42+
if (match) {
43+
env[match[1]] = val
44+
}
3745
}
3846

3947
// during dev, import.meta properties are handled by importAnalysis plugin.
4048
// ignore replace import.meta.env in lib build
4149
const importMetaKeys: Record<string, string> = {}
4250
const importMetaFallbackKeys: Record<string, string> = {}
4351
if (isBuild) {
44-
const env: Record<string, any> = {
45-
...config.env,
46-
SSR: !!config.build.ssr,
47-
}
52+
env.SSR = !!config.build.ssr
53+
4854
// set here to allow override with config.define
4955
importMetaKeys['import.meta.hot'] = `undefined`
5056
for (const key in env) {
5157
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
5258
}
5359
Object.assign(importMetaFallbackKeys, {
5460
'import.meta.env.': `({}).`,
55-
'import.meta.env': JSON.stringify(config.env),
61+
'import.meta.env': JSON.stringify(env),
5662
})
5763
}
5864

playground/env/__tests__/env.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ test('expand', async () => {
4949
expect(await page.textContent('.expand')).toBe('expand')
5050
})
5151

52+
test('ssr', async () => {
53+
expect(await page.textContent('.ssr')).toBe('false')
54+
})
55+
5256
test('env object', async () => {
5357
const envText = await page.textContent('.env-object')
5458
expect(JSON.parse(envText)).toMatchObject({
5559
VITE_EFFECTIVE_MODE_FILE_NAME: `.env.${mode}`,
5660
CUSTOM_PREFIX_ENV_VARIABLE: '1',
5761
VITE_CUSTOM_ENV_VARIABLE: '1',
5862
BASE_URL: '/env/',
63+
VITE_BOOL: true,
64+
SSR: false,
5965
MODE: mode,
6066
DEV: !isBuild,
6167
PROD: isBuild,

playground/env/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ <h1>Environment Variables</h1>
1515
<p>typeof import.meta.env.VITE_BOOL: <code class="bool"></code></p>
1616
<p>process.env.NODE_ENV: <code class="node-env"></code></p>
1717
<p>import.meta.env.VITE_EXPAND: <code class="expand"></code></p>
18+
<p>import.meta.env.SSR: <code class="ssr"></code></p>
1819
<p>import.meta.env: <span class="pre env-object"></span></p>
1920
<p>import.meta.url: <span class="pre url"></span></p>
2021

@@ -28,6 +29,7 @@ <h1>Environment Variables</h1>
2829
text('.mode-file', import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME)
2930
text('.inline', import.meta.env.VITE_INLINE)
3031
text('.bool', typeof import.meta.env.VITE_BOOL)
32+
text('.ssr', import.meta.env.SSR)
3133
text('.node-env', process.env.NODE_ENV)
3234
text('.env-object', JSON.stringify(import.meta.env, null, 2))
3335
text('.expand', import.meta.env.VITE_EXPAND)

0 commit comments

Comments
 (0)