Skip to content

Commit

Permalink
fix: ensure define overrides import.meta in build
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 3, 2022
1 parent 8970f16 commit 08fe0fa
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/define.ts
Expand Up @@ -61,8 +61,8 @@ export function definePlugin(config: ResolvedConfig): Plugin {

const replacements: Record<string, string> = {
...(replaceProcessEnv ? processNodeEnv : {}),
...userDefine,
...importMetaKeys,
...userDefine,
...(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 08fe0fa

Please sign in to comment.