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: allow running --ui #787

Merged
merged 2 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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']
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking comment: Are we deleting these entries in case the user added them by hand, no? Should we instead filter them out where they are causing issues? (with a patch in Vite if necessary). Or maybe I'm missing some context, and the comment could be expanded a bit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they will be deleted if user added them by hand, yes

to be honest, this is not needed, until Vite fixes replacing NODE_ENV, then we would need to remove them to allow reassigning on our side. Or don't add them in Vite for SSR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I kinda agree not to delete them. If user define them and get the error, it's more like their responsiblity to either fix it or find a workaround

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I kinda agree not to delete them. If user define them and get the error, it's more like their responsiblity to either fix it or find a workaround

Then we shouldn't add them in Vite


// 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