diff --git a/docs/guide/api-environment.md b/docs/guide/api-environment.md index 6d9043652349b7..6023384af599df 100644 --- a/docs/guide/api-environment.md +++ b/docs/guide/api-environment.md @@ -704,7 +704,7 @@ export interface ModuleRunnerOptions { /** * Custom module cache. If not provided, it creates a separate module cache for each module runner instance. */ - moduleGraph?: ModuleRunnerGraph + evaluatedModules?: EvaluatedModules } ``` diff --git a/packages/vite/src/module-runner/moduleCache.ts b/packages/vite/src/module-runner/evaluatedModules.ts similarity index 86% rename from packages/vite/src/module-runner/moduleCache.ts rename to packages/vite/src/module-runner/evaluatedModules.ts index 7ed91da171983d..a6e5ec446cc769 100644 --- a/packages/vite/src/module-runner/moduleCache.ts +++ b/packages/vite/src/module-runner/evaluatedModules.ts @@ -8,7 +8,7 @@ const MODULE_RUNNER_SOURCEMAPPING_REGEXP = new RegExp( `//# ${SOURCEMAPPING_URL}=data:application/json;base64,(.+)`, ) -export class ModuleRunnerNode { +export class EvaluatedModuleNode { public importers = new Set() public imports = new Set() public evaluated = false @@ -26,10 +26,10 @@ export class ModuleRunnerNode { } } -export class ModuleRunnerGraph { - public readonly idToModuleMap = new Map() - public readonly fileToModulesMap = new Map>() - public readonly urlToIdModuleMap = new Map() +export class EvaluatedModules { + public readonly idToModuleMap = new Map() + public readonly fileToModulesMap = new Map>() + public readonly urlToIdModuleMap = new Map() /** * Returns the module node by the resolved module ID. Usually, module ID is @@ -38,7 +38,7 @@ export class ModuleRunnerGraph { * Module runner graph will have 1 to 1 mapping with the server module graph. * @param id Resolved module ID */ - public getModuleById(id: string): ModuleRunnerNode | undefined { + public getModuleById(id: string): EvaluatedModuleNode | undefined { return this.idToModuleMap.get(id) } @@ -48,7 +48,7 @@ export class ModuleRunnerGraph { * multiple modules for the same file. * @param file The file system path of the module */ - public getModulesByFile(file: string): Set | undefined { + public getModulesByFile(file: string): Set | undefined { return this.fileToModulesMap.get(file) } @@ -57,7 +57,7 @@ export class ModuleRunnerGraph { * Unlike module graph on the server, the URL is not resolved and is used as is. * @param url Server URL that was used in the import statement */ - public getModuleByUrl(url: string): ModuleRunnerNode | undefined { + public getModuleByUrl(url: string): EvaluatedModuleNode | undefined { return this.urlToIdModuleMap.get(unwrapId(url)) } @@ -68,14 +68,14 @@ export class ModuleRunnerGraph { * @param id Resolved module ID * @param url URL that was used in the import statement */ - public ensureModule(id: string, url: string): ModuleRunnerNode { + public ensureModule(id: string, url: string): EvaluatedModuleNode { id = normalizeModuleId(id) if (this.idToModuleMap.has(id)) { const moduleNode = this.idToModuleMap.get(id)! this.urlToIdModuleMap.set(url, moduleNode) return moduleNode } - const moduleNode = new ModuleRunnerNode(id, url) + const moduleNode = new EvaluatedModuleNode(id, url) this.idToModuleMap.set(id, moduleNode) this.urlToIdModuleMap.set(url, moduleNode) @@ -85,7 +85,7 @@ export class ModuleRunnerGraph { return moduleNode } - public invalidateModule(node: ModuleRunnerNode): void { + public invalidateModule(node: EvaluatedModuleNode): void { node.evaluated = false node.meta = undefined node.map = undefined diff --git a/packages/vite/src/module-runner/hmrHandler.ts b/packages/vite/src/module-runner/hmrHandler.ts index 0a08c0cde47e0f..c75407b6ec2e61 100644 --- a/packages/vite/src/module-runner/hmrHandler.ts +++ b/packages/vite/src/module-runner/hmrHandler.ts @@ -54,7 +54,7 @@ export async function handleHotPayload( hmrClient.logger.debug(`program reload`) await hmrClient.notifyListeners('vite:beforeFullReload', payload) - runner.moduleGraph.clear() + runner.evaluatedModules.clear() for (const url of clearEntrypointUrls) { await runner.import(url) @@ -121,7 +121,7 @@ class Queue { } function getModulesByFile(runner: ModuleRunner, file: string): string[] { - const nodes = runner.moduleGraph.getModulesByFile(file) + const nodes = runner.evaluatedModules.getModulesByFile(file) if (!nodes) { return [] } @@ -137,7 +137,7 @@ function getModulesEntrypoints( for (const moduleId of modules) { if (visited.has(moduleId)) continue visited.add(moduleId) - const module = runner.moduleGraph.getModuleById(moduleId) + const module = runner.evaluatedModules.getModuleById(moduleId) if (!module) { continue } @@ -156,7 +156,7 @@ function findAllEntrypoints( runner: ModuleRunner, entrypoints = new Set(), ): Set { - for (const mod of runner.moduleGraph.idToModuleMap.values()) { + for (const mod of runner.evaluatedModules.idToModuleMap.values()) { if (mod.importers && !mod.importers.size) { entrypoints.add(mod.url) } diff --git a/packages/vite/src/module-runner/index.ts b/packages/vite/src/module-runner/index.ts index 3ba0b8b90b1dd6..7130795f862767 100644 --- a/packages/vite/src/module-runner/index.ts +++ b/packages/vite/src/module-runner/index.ts @@ -1,6 +1,6 @@ // this file should re-export only things that don't rely on Node.js or other runner features -export { ModuleRunnerGraph, type ModuleRunnerNode } from './moduleCache' +export { EvaluatedModules, type EvaluatedModuleNode } from './evaluatedModules' export { ModuleRunner } from './runner' export { ESModulesEvaluator } from './esmEvaluator' export { RemoteRunnerTransport } from './runnerTransport' diff --git a/packages/vite/src/module-runner/runner.ts b/packages/vite/src/module-runner/runner.ts index aefc6bd0a9eba9..1c502b0e1880f3 100644 --- a/packages/vite/src/module-runner/runner.ts +++ b/packages/vite/src/module-runner/runner.ts @@ -2,8 +2,8 @@ import type { ViteHotContext } from 'types/hot' import { HMRClient, HMRContext } from '../shared/hmr' import { cleanUrl, isPrimitive, isWindows } from '../shared/utils' import { analyzeImportedModDifference } from '../shared/ssrTransform' -import type { ModuleRunnerNode } from './moduleCache' -import { ModuleRunnerGraph } from './moduleCache' +import type { EvaluatedModuleNode } from './evaluatedModules' +import { EvaluatedModules } from './evaluatedModules' import type { ModuleEvaluator, ModuleRunnerContext, @@ -36,7 +36,7 @@ interface ModuleRunnerDebugger { } export class ModuleRunner { - public moduleGraph: ModuleRunnerGraph + public evaluatedModules: EvaluatedModules public hmrClient?: HMRClient private readonly envProxy = new Proxy({} as any, { @@ -51,7 +51,7 @@ export class ModuleRunner { private readonly root: string private readonly concurrentModuleNodePromises = new Map< string, - Promise + Promise >() private destroyed = false @@ -63,7 +63,7 @@ export class ModuleRunner { ) { const root = this.options.root this.root = root[root.length - 1] === '/' ? root : `${root}/` - this.moduleGraph = options.moduleGraph ?? new ModuleRunnerGraph() + this.evaluatedModules = options.evaluatedModules ?? new EvaluatedModules() this.transport = options.transport if (typeof options.hmr === 'object') { this.hmrClient = new HMRClient( @@ -92,7 +92,7 @@ export class ModuleRunner { * Clear all caches including HMR listeners. */ public clearCache(): void { - this.moduleGraph.clear() + this.evaluatedModules.clear() this.hmrClient?.clear() } @@ -128,7 +128,7 @@ export class ModuleRunner { return exports } - private isCircularModule(mod: ModuleRunnerNode) { + private isCircularModule(mod: EvaluatedModuleNode) { for (const importedFile of mod.imports) { if (mod.importers.has(importedFile)) { return true @@ -150,7 +150,7 @@ export class ModuleRunner { if (importer === moduleUrl) { return true } - const mod = this.moduleGraph.getModuleById(importer) + const mod = this.evaluatedModules.getModuleById(importer) if ( mod && mod.importers.size && @@ -164,7 +164,7 @@ export class ModuleRunner { private async cachedRequest( url: string, - mod: ModuleRunnerNode, + mod: EvaluatedModuleNode, callstack: string[] = [], metadata?: SSRImportMetadata, ): Promise { @@ -219,12 +219,12 @@ export class ModuleRunner { private async cachedModule( url: string, importer?: string, - ): Promise { + ): Promise { url = normalizeAbsoluteUrl(url, this.root) let cached = this.concurrentModuleNodePromises.get(url) if (!cached) { - const cachedModule = this.moduleGraph.getModuleByUrl(url) + const cachedModule = this.evaluatedModules.getModuleByUrl(url) cached = this.getModuleInformation(url, importer, cachedModule).finally( () => { this.concurrentModuleNodePromises.delete(url) @@ -241,8 +241,8 @@ export class ModuleRunner { private async getModuleInformation( url: string, importer: string | undefined, - cachedModule: ModuleRunnerNode | undefined, - ): Promise { + cachedModule: EvaluatedModuleNode | undefined, + ): Promise { if (this.destroyed) { throw new Error(`Vite module runner has been destroyed.`) } @@ -274,10 +274,10 @@ export class ModuleRunner { ? fetchedModule.externalize : fetchedModule.id const moduleUrl = 'url' in fetchedModule ? fetchedModule.url : url - const module = this.moduleGraph.ensureModule(moduleId, moduleUrl) + const module = this.evaluatedModules.ensureModule(moduleId, moduleUrl) if ('invalidate' in fetchedModule && fetchedModule.invalidate) { - this.moduleGraph.invalidateModule(module) + this.evaluatedModules.invalidateModule(module) } fetchedModule.url = moduleUrl @@ -290,7 +290,7 @@ export class ModuleRunner { // override is allowed, consider this a public API protected async directRequest( url: string, - mod: ModuleRunnerNode, + mod: EvaluatedModuleNode, _callstack: string[], ): Promise { const fetchResult = mod.meta! diff --git a/packages/vite/src/module-runner/sourcemap/interceptor.ts b/packages/vite/src/module-runner/sourcemap/interceptor.ts index 88117d7b8b62b8..eb4f41f83e68a2 100644 --- a/packages/vite/src/module-runner/sourcemap/interceptor.ts +++ b/packages/vite/src/module-runner/sourcemap/interceptor.ts @@ -1,7 +1,7 @@ import type { OriginalMapping } from '@jridgewell/trace-mapping' import type { ModuleRunner } from '../runner' import { posixDirname, posixResolve } from '../utils' -import type { ModuleRunnerGraph } from '../moduleCache' +import type { EvaluatedModules } from '../evaluatedModules' import { slash } from '../../shared/utils' import { DecodedMap, getOriginalPosition } from './decoder' @@ -21,7 +21,7 @@ export interface InterceptorOptions { const sourceMapCache: Record = {} const fileContentsCache: Record = {} -const moduleGraphs = new Set() +const evaluatedModulesCache = new Set() const retrieveFileHandlers = new Set() const retrieveSourceMapHandlers = new Set() @@ -46,11 +46,11 @@ let overridden = false const originalPrepare = Error.prepareStackTrace function resetInterceptor(runner: ModuleRunner, options: InterceptorOptions) { - moduleGraphs.delete(runner.moduleGraph) + evaluatedModulesCache.delete(runner.evaluatedModules) if (options.retrieveFile) retrieveFileHandlers.delete(options.retrieveFile) if (options.retrieveSourceMap) retrieveSourceMapHandlers.delete(options.retrieveSourceMap) - if (moduleGraphs.size === 0) { + if (evaluatedModulesCache.size === 0) { Error.prepareStackTrace = originalPrepare overridden = false } @@ -64,7 +64,7 @@ export function interceptStackTrace( Error.prepareStackTrace = prepareStackTrace overridden = true } - moduleGraphs.add(runner.moduleGraph) + evaluatedModulesCache.add(runner.evaluatedModules) if (options.retrieveFile) retrieveFileHandlers.add(options.retrieveFile) if (options.retrieveSourceMap) retrieveSourceMapHandlers.add(options.retrieveSourceMap) @@ -102,7 +102,7 @@ function supportRelativeURL(file: string, url: string) { } function getRunnerSourceMap(position: OriginalMapping): CachedMapEntry | null { - for (const moduleGraph of moduleGraphs) { + for (const moduleGraph of evaluatedModulesCache) { const sourceMap = moduleGraph.getModuleSourceMapById(position.source!) if (sourceMap) { return { diff --git a/packages/vite/src/module-runner/types.ts b/packages/vite/src/module-runner/types.ts index e33610131759f8..cce95b5cba816f 100644 --- a/packages/vite/src/module-runner/types.ts +++ b/packages/vite/src/module-runner/types.ts @@ -5,7 +5,7 @@ import type { DefineImportMetadata, SSRImportMetadata, } from '../shared/ssrTransform' -import type { ModuleRunnerGraph } from './moduleCache' +import type { EvaluatedModules } from './evaluatedModules' import type { ssrDynamicImportKey, ssrExportAllKey, @@ -168,7 +168,7 @@ export interface ModuleRunnerOptions { /** * Custom module cache. If not provided, creates a separate module cache for each ModuleRunner instance. */ - moduleGraph?: ModuleRunnerGraph + evaluatedModules?: EvaluatedModules } export interface ImportMetaEnv { diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts index 1c4925550e299e..758072f25a83be 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts +++ b/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts @@ -52,7 +52,7 @@ describe('module runner initialization', async () => { resolvePath(import.meta.url, './fixtures/throws-error-method.ts'), (code) => '\n\n\n\n\n' + code + '\n', ) - runner.moduleGraph.clear() + runner.evaluatedModules.clear() server.environments.ssr.moduleGraph.invalidateAll() const methodErrorNew = await getError(async () => {