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

feat: allow reassigning define globals #769

Merged
merged 5 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 0 additions & 30 deletions packages/vitest/src/node/plugins/envReplacer.ts

This file was deleted.

43 changes: 32 additions & 11 deletions packages/vitest/src/node/plugins/index.ts
Expand Up @@ -6,7 +6,6 @@ import { resolveApiConfig } from '../config'
import { Vitest } from '../core'
import { GlobalSetupPlugin } from './globalSetup'
import { MocksPlugin } from './mock'
import { EnvReplacerPlugin } from './envReplacer'

export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest()): Promise<VitePlugin[]> {
let haveStarted = false
Expand All @@ -27,6 +26,30 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
const preOptions = deepMerge({}, options, viteConfig.test ?? {})
preOptions.api = resolveApiConfig(preOptions)

// make user defines globals, if possible
// so people can reassign them
for (const key in viteConfig.define) {
const val = viteConfig.define[key]
let replacement: any
try {
replacement = typeof val === 'string' ? JSON.parse(val) : val
}
catch {
// probably means it contains reference to some variable,
// like this: "__VAR__": "process.env.VAR"
continue
}
if (key.startsWith('import.meta.env.')) {
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
const envKey = key.slice('import.meta.env.'.length)
process.env[envKey] = replacement
delete viteConfig.define[key]
}
else if (!key.includes('.')) {
(globalThis as any)[key] = replacement
delete viteConfig.define[key]
}
}

return {
// we are setting NODE_ENV when running CLI to 'test',
// but it can be overridden
Expand All @@ -37,6 +60,14 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
// setting this option can bypass that and fallback to cjs version
mainFields: [],
},
define: {
'process.env.NODE_ENV': 'process.env.NODE_ENV',
'global.process.env.NODE_ENV': 'global.process.env.NODE_ENV',
'globalThis.process.env.NODE_ENV': 'globalThis.process.env.NODE_ENV',
// so people can reassign envs at runtime
// import.meta.env.VITE_NAME = 'app' -> process.env.VITE_NAME = 'app'
'import.meta.env': 'process.env',
},
server: {
...preOptions.api,
open: preOptions.ui && preOptions.open
Expand Down Expand Up @@ -69,15 +100,6 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
process.env.PROD ??= viteConfig.env.PROD ? '1' : ''
process.env.DEV ??= viteConfig.env.DEV ? '1' : ''
process.env.SSR ??= '1'

// account for user env defines
for (const key in viteConfig.define) {
if (key.startsWith('import.meta.env.')) {
const val = viteConfig.define[key]
const envKey = key.slice('import.meta.env.'.length)
process.env[envKey] = typeof val === 'string' ? JSON.parse(val) : val
}
}
},
async configureServer(server) {
if (haveStarted)
Expand All @@ -92,7 +114,6 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
await server.watcher.close()
},
},
EnvReplacerPlugin(),
MocksPlugin(),
GlobalSetupPlugin(ctx),
options.ui
Expand Down