From e73ca9edfe1b2a2a333b1f0d3c831780c3eaf5fd Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 15 Aug 2023 20:50:04 +0800 Subject: [PATCH] fix: define process variable and using import.meta.env together did not work (#3944) --- packages/vite-node/src/client.ts | 6 ++-- packages/vite-node/src/utils.ts | 29 +++++++++++++++++++ .../vitest/src/node/plugins/ssrReplacer.ts | 2 +- test/core/test/env.test.ts | 15 ++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/packages/vite-node/src/client.ts b/packages/vite-node/src/client.ts index 2c97f24f648b..53358657dd91 100644 --- a/packages/vite-node/src/client.ts +++ b/packages/vite-node/src/client.ts @@ -6,7 +6,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url' import vm from 'node:vm' import { resolve } from 'pathe' import createDebug from 'debug' -import { VALID_ID_PREFIX, cleanUrl, isInternalRequest, isNodeBuiltin, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils' +import { VALID_ID_PREFIX, cleanUrl, createImportMetaEnvProxy, isInternalRequest, isNodeBuiltin, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils' import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types' import { extractSourceMap } from './source-map' @@ -32,6 +32,8 @@ const clientStub = { removeStyle: () => {}, } +const env = createImportMetaEnvProxy() + export const DEFAULT_REQUEST_STUBS: Record> = { '/@vite/client': clientStub, '@vite/client': clientStub, @@ -299,7 +301,7 @@ export class ViteNodeRunner { const modulePath = cleanUrl(moduleId) // disambiguate the `:/` on windows: see nodejs/node#31710 const href = pathToFileURL(modulePath).href - const meta = { url: href } + const meta = { url: href, env } const exports = Object.create(null) Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module', diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index fdd349db9291..f781e2984ef5 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -199,3 +199,32 @@ function traverseBetweenDirs( longerDir = dirname(longerDir) } } + +export function createImportMetaEnvProxy() { + // packages/vitest/src/node/plugins/index.ts:146 + const booleanKeys = [ + 'DEV', + 'PROD', + 'SSR', + ] + return new Proxy(process.env, { + get(_, key) { + if (typeof key !== 'string') + return undefined + if (booleanKeys.includes(key)) + return !!process.env[key] + return process.env[key] + }, + set(_, key, value) { + if (typeof key !== 'string') + return true + + if (booleanKeys.includes(key)) + process.env[key] = value ? '1' : '' + else + process.env[key] = value + + return true + }, + }) +} diff --git a/packages/vitest/src/node/plugins/ssrReplacer.ts b/packages/vitest/src/node/plugins/ssrReplacer.ts index 217ceff188a6..01792969962a 100644 --- a/packages/vitest/src/node/plugins/ssrReplacer.ts +++ b/packages/vitest/src/node/plugins/ssrReplacer.ts @@ -23,7 +23,7 @@ export function SsrReplacerPlugin(): Plugin { const startIndex = env.index! const endIndex = startIndex + env[0].length - s.overwrite(startIndex, endIndex, 'process.env') + s.overwrite(startIndex, endIndex, '__vite_ssr_import_meta__.env') } if (s) { diff --git a/test/core/test/env.test.ts b/test/core/test/env.test.ts index c90d22526a55..d5fa3b56d584 100644 --- a/test/core/test/env.test.ts +++ b/test/core/test/env.test.ts @@ -47,3 +47,18 @@ test('custom env', () => { test('ignores import.meta.env in string literals', () => { expect('import.meta.env').toBe('import' + '.meta.env') }) + +test('define process and using import.meta.env together', () => { + const process = {} + expect(process).toMatchObject({}) + expect(import.meta.env.MODE).toEqual('test') +}) + +test('PROD, DEV, SSR should be boolean', () => { + expect(typeof import.meta.env.PROD).toEqual('boolean') + expect(typeof import.meta.env.DEV).toEqual('boolean') + expect(typeof import.meta.env.SSR).toEqual('boolean') + + import.meta.env.SSR = false + expect(import.meta.env.SSR).toEqual(false) +})