Skip to content

Commit

Permalink
fix(define): inconsistent env values in build mode (#12058)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Feb 16, 2023
1 parent 081c27f commit 0a50c59
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isCSSRequest } from './css'
import { isHTMLRequest } from './html'

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

export function definePlugin(config: ResolvedConfig): Plugin {
Expand All @@ -30,29 +31,34 @@ export function definePlugin(config: ResolvedConfig): Plugin {
})
}

const env = { ...config.env }
const userDefine: 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
}
}

// during dev, import.meta properties are handled by importAnalysis plugin.
// ignore replace import.meta.env in lib build
const importMetaKeys: Record<string, string> = {}
const importMetaFallbackKeys: Record<string, string> = {}
if (isBuild) {
const env: Record<string, any> = {
...config.env,
SSR: !!config.build.ssr,
}
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(config.env),
'import.meta.env': JSON.stringify(env),
})
}

Expand Down
6 changes: 6 additions & 0 deletions playground/env/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,19 @@ test('expand', async () => {
expect(await page.textContent('.expand')).toBe('expand')
})

test('ssr', async () => {
expect(await page.textContent('.ssr')).toBe('false')
})

test('env object', async () => {
const envText = await page.textContent('.env-object')
expect(JSON.parse(envText)).toMatchObject({
VITE_EFFECTIVE_MODE_FILE_NAME: `.env.${mode}`,
CUSTOM_PREFIX_ENV_VARIABLE: '1',
VITE_CUSTOM_ENV_VARIABLE: '1',
BASE_URL: '/env/',
VITE_BOOL: true,
SSR: false,
MODE: mode,
DEV: !isBuild,
PROD: isBuild,
Expand Down
2 changes: 2 additions & 0 deletions playground/env/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ <h1>Environment Variables</h1>
<p>typeof import.meta.env.VITE_BOOL: <code class="bool"></code></p>
<p>process.env.NODE_ENV: <code class="node-env"></code></p>
<p>import.meta.env.VITE_EXPAND: <code class="expand"></code></p>
<p>import.meta.env.SSR: <code class="ssr"></code></p>
<p>import.meta.env: <span class="pre env-object"></span></p>
<p>import.meta.url: <span class="pre url"></span></p>

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

0 comments on commit 0a50c59

Please sign in to comment.