Skip to content

Commit

Permalink
fix: allow running --ui (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 18, 2022
1 parent 8213446 commit 924ba44
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
32 changes: 32 additions & 0 deletions packages/vitest/src/node/plugins/envRelacer.ts
@@ -0,0 +1,32 @@
import MagicString from 'magic-string'
import type { Plugin } from 'vite'

// so people can reassign envs at runtime
// import.meta.env.VITE_NAME = 'app' -> process.env.VITE_NAME = 'app'
export const EnvReplacerPlugin = (): Plugin => {
return {
name: 'vitest:env-replacer',
enforce: 'pre',
transform(code) {
let s: MagicString | null = null

const envs = code.matchAll(/\bimport\.meta\.env\b/g)

for (const env of envs) {
s ||= new MagicString(code)

const startIndex = env.index!
const endIndex = startIndex + env[0].length

s.overwrite(startIndex, endIndex, 'process.env')
}

if (s) {
return {
code: s.toString(),
map: s.generateMap({ hires: true }),
}
}
},
}
}
20 changes: 12 additions & 8 deletions packages/vitest/src/node/plugins/index.ts
Expand Up @@ -4,6 +4,7 @@ import type { ResolvedConfig, UserConfig } from '../../types'
import { deepMerge, ensurePackageInstalled, notNullish } from '../../utils'
import { resolveApiConfig } from '../config'
import { Vitest } from '../core'
import { EnvReplacerPlugin } from './envRelacer'
import { GlobalSetupPlugin } from './globalSetup'
import { MocksPlugin } from './mock'

Expand Down Expand Up @@ -66,14 +67,6 @@ 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 All @@ -99,6 +92,16 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
)
options.api = resolveApiConfig(options)

// we cannot replace them, because esbuild will throw an error,
// so we are removing them to allow reassigning
const define = viteConfig.define

if (define) {
delete define['process.env.NODE_ENV']
delete define['global.process.env.NODE_ENV']
delete define['globalThis.process.env.NODE_ENV']
}

// we replace every "import.meta.env" with "process.env"
// to allow reassigning, so we need to put all envs on process.env
const { PROD, DEV, ...envs } = viteConfig.env
Expand All @@ -125,6 +128,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
await server.watcher.close()
},
},
EnvReplacerPlugin(),
MocksPlugin(),
GlobalSetupPlugin(ctx),
options.ui
Expand Down

0 comments on commit 924ba44

Please sign in to comment.