Skip to content

Commit

Permalink
chore: review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 31, 2024
1 parent fea94b5 commit 0573c0d
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 87 deletions.
11 changes: 2 additions & 9 deletions docs/guide/api-vite-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>
/**
* 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
Expand All @@ -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<string, any>
}
```

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/ssr/runtime/hmrHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export function createHMRHandler(
runtime: ViteRuntime,
): (payload: HMRPayload) => Promise<void> {
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<void> {
Expand Down
2 changes: 0 additions & 2 deletions packages/vite/src/node/ssr/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions packages/vite/src/node/ssr/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HMRClient, HMRContext } from '../../../shared/hmr'
import { ModuleCacheMap } from './moduleCache'
import type {
FetchResult,
ImportMetaEnv,
ModuleCache,
ResolvedResult,
SSRImportMetadata,
Expand All @@ -14,7 +13,6 @@ import type {
} from './types'
import {
cleanUrl,
createImportMetaEnvProxy,
isPrimitive,
isWindows,
posixDirname,
Expand Down Expand Up @@ -50,7 +48,13 @@ export class ViteRuntime {

private idToUrlMap = new Map<string, string>()
private fileToIdMap = new Map<string, string[]>()
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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions packages/vite/src/node/ssr/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ export interface ViteRuntimeOptions {
environmentVariables?: Record<string, any>
/**
* 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
Expand All @@ -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<string, any>
}

export interface ImportMetaEnv {
Expand Down
63 changes: 2 additions & 61 deletions packages/vite/src/node/ssr/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { ImportMetaEnv } from './types'

export const isWindows =
typeof process !== 'undefined' && process.platform === 'win32'

Expand Down Expand Up @@ -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<string, any>) =>
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<string, any>,
): 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 {
Expand Down
2 changes: 1 addition & 1 deletion playground/hmr-ssr/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@vitejs/test-hmr",
"name": "@vitejs/test-hmr-ssr",
"private": true,
"version": "0.0.0",
"type": "module",
Expand Down

0 comments on commit 0573c0d

Please sign in to comment.