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: automatically remove define related configuration #3552

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 3 additions & 35 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GlobalSetupPlugin } from './globalSetup'
import { CSSEnablerPlugin } from './cssEnabler'
import { CoverageTransform } from './coverageTransform'
import { MocksPlugin } from './mocks'
import { resolveOptimizerConfig } from './utils'
import { deleteDefineConfig, resolveOptimizerConfig } from './utils'
import { VitestResolver } from './vitestResolver'

export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('test')): Promise<VitePlugin[]> {
Expand Down Expand Up @@ -50,43 +50,11 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
)
testConfig.api = resolveApiServerConfig(testConfig)

if (viteConfig.define) {
delete viteConfig.define['import.meta.vitest']
delete viteConfig.define['process.env']
}

// store defines for globalThis to make them
// reassignable when running in worker in src/runtime/setup.ts
const defines: Record<string, any> = {}

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.')) {
const envKey = key.slice('import.meta.env.'.length)
process.env[envKey] = replacement
delete viteConfig.define[key]
}
else if (key.startsWith('process.env.')) {
const envKey = key.slice('process.env.'.length)
process.env[envKey] = replacement
delete viteConfig.define[key]
}
else if (!key.includes('.')) {
defines[key] = replacement
delete viteConfig.define[key]
}
}
const defines: Record<string, any> = deleteDefineConfig(viteConfig)

(options as ResolvedConfig).defines = defines
;(options as ResolvedConfig).defines = defines

let open: string | boolean | undefined

Expand Down
38 changes: 37 additions & 1 deletion packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { builtinModules } from 'node:module'
import { version as viteVersion } from 'vite'
import type { DepOptimizationOptions } from 'vite'
import type { DepOptimizationOptions, UserConfig as ViteConfig } from 'vite'
import type { DepsOptimizationOptions, InlineConfig } from '../../types'

export function resolveOptimizerConfig(testOptionc: DepsOptimizationOptions | undefined, viteOptions: DepOptimizationOptions | undefined, testConfig: InlineConfig) {
Expand Down Expand Up @@ -32,3 +32,39 @@ export function resolveOptimizerConfig(testOptionc: DepsOptimizationOptions | un
}
return newConfig
}

export function deleteDefineConfig(viteConfig: ViteConfig) {
const defines: Record<string, any> = {}
if (viteConfig.define) {
delete viteConfig.define['import.meta.vitest']
delete viteConfig.define['process.env']
delete viteConfig.define.process
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
}
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.')) {
const envKey = key.slice('import.meta.env.'.length)
process.env[envKey] = replacement
delete viteConfig.define[key]
}
else if (key.startsWith('process.env.')) {
const envKey = key.slice('process.env.'.length)
process.env[envKey] = replacement
delete viteConfig.define[key]
}
else if (!key.includes('.')) {
defines[key] = replacement
delete viteConfig.define[key]
}
}
return defines
}
33 changes: 2 additions & 31 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CSSEnablerPlugin } from './cssEnabler'
import { EnvReplacerPlugin } from './envReplacer'
import { GlobalSetupPlugin } from './globalSetup'
import { MocksPlugin } from './mocks'
import { resolveOptimizerConfig } from './utils'
import { deleteDefineConfig, resolveOptimizerConfig } from './utils'
import { VitestResolver } from './vitestResolver'

interface WorkspaceOptions extends UserWorkspaceConfig {
Expand All @@ -26,37 +26,8 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
options() {
this.meta.watchMode = false
},
// TODO: refactor so we don't have the same code here and in plugins/index.ts
config(viteConfig) {
if (viteConfig.define) {
delete viteConfig.define['import.meta.vitest']
delete viteConfig.define['process.env']
}

const env: Record<string, any> = {}

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.')) {
const envKey = key.slice('import.meta.env.'.length)
env[envKey] = replacement
delete viteConfig.define[key]
}
else if (key.startsWith('process.env.')) {
const envKey = key.slice('process.env.'.length)
env[envKey] = replacement
delete viteConfig.define[key]
}
}
const env: Record<string, any> = deleteDefineConfig(viteConfig)

const testConfig = viteConfig.test || {}

Expand Down