Skip to content

Commit

Permalink
fix: ensure define overrides import.meta in build (#8892)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 3, 2022
1 parent b6d655a commit 7d810a9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/define.ts
Expand Up @@ -39,6 +39,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
// 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,
Expand All @@ -47,7 +48,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}
Object.assign(importMetaKeys, {
Object.assign(importMetaFallbackKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(config.env),
'import.meta.hot': `false`
Expand All @@ -61,8 +62,9 @@ export function definePlugin(config: ResolvedConfig): Plugin {

const replacements: Record<string, string> = {
...(replaceProcessEnv ? processNodeEnv : {}),
...userDefine,
...importMetaKeys,
...userDefine,
...importMetaFallbackKeys,
...(replaceProcessEnv ? processEnv : {})
}

Expand Down
3 changes: 2 additions & 1 deletion playground/env/.env
@@ -1,3 +1,4 @@
VITE_CUSTOM_ENV_VARIABLE=1
CUSTOM_PREFIX_ENV_VARIABLE=1
VITE_EFFECTIVE_MODE_FILE_NAME=.env
VITE_EFFECTIVE_MODE_FILE_NAME=.env
VITE_BOOL=true
4 changes: 4 additions & 0 deletions playground/env/__tests__/env.spec.ts
Expand Up @@ -36,6 +36,10 @@ test('inline variables', async () => {
)
})

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

test('NODE_ENV', async () => {
expect(await page.textContent('.node-env')).toBe(process.env.NODE_ENV)
})
Expand Down
2 changes: 2 additions & 0 deletions playground/env/index.html
Expand Up @@ -12,6 +12,7 @@ <h1>Environment Variables</h1>
import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME: <code class="mode-file"></code>
</p>
<p>import.meta.env.VITE_INLINE: <code class="inline"></code></p>
<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: <span class="pre env-object"></span></p>
<p>import.meta.url: <span class="pre url"></span></p>
Expand All @@ -25,6 +26,7 @@ <h1>Environment Variables</h1>
text('.custom-prefix', import.meta.env.CUSTOM_PREFIX_ENV_VARIABLE)
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('.node-env', process.env.NODE_ENV)
text('.env-object', JSON.stringify(import.meta.env, null, 2))

Expand Down
3 changes: 3 additions & 0 deletions playground/env/vite.config.js
Expand Up @@ -5,5 +5,8 @@ module.exports = defineConfig({
envPrefix: ['VITE_', 'CUSTOM_PREFIX_'],
build: {
outDir: 'dist/env'
},
define: {
'import.meta.env.VITE_BOOL': true
}
})

0 comments on commit 7d810a9

Please sign in to comment.