Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(define): inconsistent env values in build mode #12058

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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