Skip to content

Commit e73ca9e

Browse files
authoredAug 15, 2023
fix: define process variable and using import.meta.env together did not work (#3944)
1 parent eca4b87 commit e73ca9e

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed
 

‎packages/vite-node/src/client.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
66
import vm from 'node:vm'
77
import { resolve } from 'pathe'
88
import createDebug from 'debug'
9-
import { VALID_ID_PREFIX, cleanUrl, isInternalRequest, isNodeBuiltin, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
9+
import { VALID_ID_PREFIX, cleanUrl, createImportMetaEnvProxy, isInternalRequest, isNodeBuiltin, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
1010
import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types'
1111
import { extractSourceMap } from './source-map'
1212

@@ -32,6 +32,8 @@ const clientStub = {
3232
removeStyle: () => {},
3333
}
3434

35+
const env = createImportMetaEnvProxy()
36+
3537
export const DEFAULT_REQUEST_STUBS: Record<string, Record<string, unknown>> = {
3638
'/@vite/client': clientStub,
3739
'@vite/client': clientStub,
@@ -299,7 +301,7 @@ export class ViteNodeRunner {
299301
const modulePath = cleanUrl(moduleId)
300302
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
301303
const href = pathToFileURL(modulePath).href
302-
const meta = { url: href }
304+
const meta = { url: href, env }
303305
const exports = Object.create(null)
304306
Object.defineProperty(exports, Symbol.toStringTag, {
305307
value: 'Module',

‎packages/vite-node/src/utils.ts

+29
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,32 @@ function traverseBetweenDirs(
199199
longerDir = dirname(longerDir)
200200
}
201201
}
202+
203+
export function createImportMetaEnvProxy() {
204+
// packages/vitest/src/node/plugins/index.ts:146
205+
const booleanKeys = [
206+
'DEV',
207+
'PROD',
208+
'SSR',
209+
]
210+
return new Proxy(process.env, {
211+
get(_, key) {
212+
if (typeof key !== 'string')
213+
return undefined
214+
if (booleanKeys.includes(key))
215+
return !!process.env[key]
216+
return process.env[key]
217+
},
218+
set(_, key, value) {
219+
if (typeof key !== 'string')
220+
return true
221+
222+
if (booleanKeys.includes(key))
223+
process.env[key] = value ? '1' : ''
224+
else
225+
process.env[key] = value
226+
227+
return true
228+
},
229+
})
230+
}

‎packages/vitest/src/node/plugins/ssrReplacer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function SsrReplacerPlugin(): Plugin {
2323
const startIndex = env.index!
2424
const endIndex = startIndex + env[0].length
2525

26-
s.overwrite(startIndex, endIndex, 'process.env')
26+
s.overwrite(startIndex, endIndex, '__vite_ssr_import_meta__.env')
2727
}
2828

2929
if (s) {

‎test/core/test/env.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,18 @@ test('custom env', () => {
4747
test('ignores import.meta.env in string literals', () => {
4848
expect('import.meta.env').toBe('import' + '.meta.env')
4949
})
50+
51+
test('define process and using import.meta.env together', () => {
52+
const process = {}
53+
expect(process).toMatchObject({})
54+
expect(import.meta.env.MODE).toEqual('test')
55+
})
56+
57+
test('PROD, DEV, SSR should be boolean', () => {
58+
expect(typeof import.meta.env.PROD).toEqual('boolean')
59+
expect(typeof import.meta.env.DEV).toEqual('boolean')
60+
expect(typeof import.meta.env.SSR).toEqual('boolean')
61+
62+
import.meta.env.SSR = false
63+
expect(import.meta.env.SSR).toEqual(false)
64+
})

0 commit comments

Comments
 (0)
Please sign in to comment.