From 0573c0d3346c080fc115fba66ffb25480a0c9fa6 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 31 Jan 2024 12:29:08 +0100 Subject: [PATCH] chore: review feedback --- docs/guide/api-vite-runtime.md | 11 +--- .../vite/src/node/ssr/runtime/hmrHandler.ts | 4 +- packages/vite/src/node/ssr/runtime/index.ts | 2 - packages/vite/src/node/ssr/runtime/runtime.ts | 14 ++--- packages/vite/src/node/ssr/runtime/types.ts | 7 +-- packages/vite/src/node/ssr/runtime/utils.ts | 63 +------------------ playground/hmr-ssr/package.json | 2 +- 7 files changed, 16 insertions(+), 87 deletions(-) diff --git a/docs/guide/api-vite-runtime.md b/docs/guide/api-vite-runtime.md index 7cec0c91d6d556..18a93549b5cfc8 100644 --- a/docs/guide/api-vite-runtime.md +++ b/docs/guide/api-vite-runtime.md @@ -89,13 +89,10 @@ export interface ViteRuntimeOptions { * For other runtime use cases, Vite also exposes `fetchModule` from its main entry point. */ fetchModule: FetchFunction - /** - * Custom environment variables available on `import.meta.env`. This doesn't modify the actual `process.env`. - */ - environmentVariables?: Record /** * Configure how source maps are resolved. Prefers `node` if `process.setSourceMapsEnabled` is available. - * Otherwise it will use `prepareStackTrace` by default. + * Otherwise it will use `prepareStackTrace` by default which overrides `Error.prepareStackTrace` method. + * You can provide an object to configure how file contents and source maps are resolved for files that were not processed by Vite. */ sourcemapInterceptor?: | false @@ -121,10 +118,6 @@ export interface ViteRuntimeOptions { * Custom module cache. If not provided, it creates a separate module cache for each ViteRuntime instance. */ moduleCache?: ModuleCacheMap - /** - * Resolved modules that will be returned instead of executing the code. - */ - requestStubs?: Record } ``` diff --git a/packages/vite/src/node/ssr/runtime/hmrHandler.ts b/packages/vite/src/node/ssr/runtime/hmrHandler.ts index 48b27636391562..413d355c2f51a0 100644 --- a/packages/vite/src/node/ssr/runtime/hmrHandler.ts +++ b/packages/vite/src/node/ssr/runtime/hmrHandler.ts @@ -7,10 +7,10 @@ export function createHMRHandler( runtime: ViteRuntime, ): (payload: HMRPayload) => Promise { const queue = new Queue() - return (payload) => queue.enqueue(() => handleHMRUpdate(runtime, payload)) + return (payload) => queue.enqueue(() => handleHMRPayload(runtime, payload)) } -export async function handleHMRUpdate( +export async function handleHMRPayload( runtime: ViteRuntime, payload: HMRPayload, ): Promise { diff --git a/packages/vite/src/node/ssr/runtime/index.ts b/packages/vite/src/node/ssr/runtime/index.ts index 6887d0c9cb279d..f2b5b83f0fda5d 100644 --- a/packages/vite/src/node/ssr/runtime/index.ts +++ b/packages/vite/src/node/ssr/runtime/index.ts @@ -4,8 +4,6 @@ export { ModuleCacheMap } from './moduleCache' export { ViteRuntime } from './runtime' export { ESModulesRunner } from './esmRunner' -export { handleHMRUpdate, createHMRHandler } from './hmrHandler' - export type { HMRLogger, HMRConnection } from '../../../shared/hmr' export type { ViteModuleRunner, diff --git a/packages/vite/src/node/ssr/runtime/runtime.ts b/packages/vite/src/node/ssr/runtime/runtime.ts index 239d143c38a14f..77b647623d63d8 100644 --- a/packages/vite/src/node/ssr/runtime/runtime.ts +++ b/packages/vite/src/node/ssr/runtime/runtime.ts @@ -3,7 +3,6 @@ import { HMRClient, HMRContext } from '../../../shared/hmr' import { ModuleCacheMap } from './moduleCache' import type { FetchResult, - ImportMetaEnv, ModuleCache, ResolvedResult, SSRImportMetadata, @@ -14,7 +13,6 @@ import type { } from './types' import { cleanUrl, - createImportMetaEnvProxy, isPrimitive, isWindows, posixDirname, @@ -50,7 +48,13 @@ export class ViteRuntime { private idToUrlMap = new Map() private fileToIdMap = new Map() - private envProxy: ImportMetaEnv + private envProxy = new Proxy({} as any, { + get(_, p) { + throw new Error( + `[vite-runtime] Dynamic access of "import.meta.env" is not supported. Please, use "import.meta.env.${String(p)}" instead.`, + ) + }, + }) private _destroyed = false private _resetSourceMapSupport?: () => void @@ -61,7 +65,6 @@ export class ViteRuntime { private debug?: ViteRuntimeDebugger, ) { this.moduleCache = options.moduleCache ?? new ModuleCacheMap(options.root) - this.envProxy = createImportMetaEnvProxy(options.environmentVariables) if (typeof options.hmr === 'object') { this.hmrClient = new HMRClient( options.hmr.logger === false @@ -317,9 +320,6 @@ export class ViteRuntime { return request(dep, { isDynamicImport: true }) } - const requestStubs = this.options.requestStubs || {} - if (id in requestStubs) return requestStubs[id] - if ('externalize' in fetchResult) { const { externalize } = fetchResult this.debug?.('[vite-runtime] externalizing', externalize) diff --git a/packages/vite/src/node/ssr/runtime/types.ts b/packages/vite/src/node/ssr/runtime/types.ts index 6d055f8514d659..8da395093feead 100644 --- a/packages/vite/src/node/ssr/runtime/types.ts +++ b/packages/vite/src/node/ssr/runtime/types.ts @@ -143,7 +143,8 @@ export interface ViteRuntimeOptions { environmentVariables?: Record /** * Configure how source maps are resolved. Prefers `node` if `process.setSourceMapsEnabled` is available. - * Otherwise it will use `prepareStackTrace` by default. + * Otherwise it will use `prepareStackTrace` by default which overrides `Error.prepareStackTrace` method. + * You can provide an object to configure how file contents and source maps are resolved for files that were not processed by Vite. */ sourcemapInterceptor?: | false @@ -169,10 +170,6 @@ export interface ViteRuntimeOptions { * Custom module cache. If not provided, creates a separate module cache for each ViteRuntime instance. */ moduleCache?: ModuleCacheMap - /** - * Resolved modules that will be returned instead of executing the code. - */ - requestStubs?: Record } export interface ImportMetaEnv { diff --git a/packages/vite/src/node/ssr/runtime/utils.ts b/packages/vite/src/node/ssr/runtime/utils.ts index acc22dc5c22beb..f2eca533b350fe 100644 --- a/packages/vite/src/node/ssr/runtime/utils.ts +++ b/packages/vite/src/node/ssr/runtime/utils.ts @@ -1,5 +1,3 @@ -import type { ImportMetaEnv } from './types' - export const isWindows = typeof process !== 'undefined' && process.platform === 'win32' @@ -29,66 +27,9 @@ export function slash(p: string): string { return p.replace(windowsSlashRE, '/') } -export const queryRE = /\?.*$/s -export const hashRE = /#.*$/s - +const postfixRE = /[?#].*$/s export function cleanUrl(url: string): string { - return url.replace(hashRE, '').replace(queryRE, '') -} - -const _envShim = Object.create(null) - -const _getEnv = (environmentVariables?: Record) => - globalThis.process?.env || - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore "env" in meta is not typed for SSR code - import.meta.env || - // @ts-expect-error Deno global is not typed - globalThis.Deno?.env.toObject() || - environmentVariables - -export function createImportMetaEnvProxy( - environmentVariables?: Record, -): ImportMetaEnv { - const booleanKeys = ['DEV', 'PROD', 'SSR'] - return new Proxy( - {}, - { - get(_, key) { - if (typeof key !== 'string') return undefined - const env = _getEnv(environmentVariables) - if (booleanKeys.includes(key)) return !!env[key] - return env[key] ?? _envShim[key] - }, - has(_, key) { - const env = _getEnv(environmentVariables) - return key in env || key in _envShim - }, - set(_, key, value) { - if (typeof key !== 'string') return true - - if (booleanKeys.includes(key)) { - value = value ? '1' : '' - } - - const env = _getEnv(environmentVariables) || _envShim - env[key] = value - return true - }, - deleteProperty(_, prop) { - if (!prop) { - return false - } - const env = _getEnv(environmentVariables) || _envShim - delete env[prop as any] - return true - }, - ownKeys() { - const env = _getEnv(environmentVariables) || _envShim - return Object.keys(env) - }, - }, - ) as ImportMetaEnv + return url.replace(postfixRE, '') } export function isPrimitive(value: unknown): boolean { diff --git a/playground/hmr-ssr/package.json b/playground/hmr-ssr/package.json index 35e0799c262738..52a5646e2da7a4 100644 --- a/playground/hmr-ssr/package.json +++ b/playground/hmr-ssr/package.json @@ -1,5 +1,5 @@ { - "name": "@vitejs/test-hmr", + "name": "@vitejs/test-hmr-ssr", "private": true, "version": "0.0.0", "type": "module",