Skip to content

Commit

Permalink
refactor: rename to EvaluatedModules
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Sep 24, 2024
1 parent c70b9c0 commit 784ee9b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/guide/api-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()
public imports = new Set<string>()
public evaluated = false
Expand All @@ -26,10 +26,10 @@ export class ModuleRunnerNode {
}
}

export class ModuleRunnerGraph {
public readonly idToModuleMap = new Map<string, ModuleRunnerNode>()
public readonly fileToModulesMap = new Map<string, Set<ModuleRunnerNode>>()
public readonly urlToIdModuleMap = new Map<string, ModuleRunnerNode>()
export class EvaluatedModules {
public readonly idToModuleMap = new Map<string, EvaluatedModuleNode>()
public readonly fileToModulesMap = new Map<string, Set<EvaluatedModuleNode>>()
public readonly urlToIdModuleMap = new Map<string, EvaluatedModuleNode>()

/**
* Returns the module node by the resolved module ID. Usually, module ID is
Expand All @@ -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)
}

Expand All @@ -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<ModuleRunnerNode> | undefined {
public getModulesByFile(file: string): Set<EvaluatedModuleNode> | undefined {
return this.fileToModulesMap.get(file)
}

Expand All @@ -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))
}

Expand All @@ -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)

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/module-runner/hmrHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 []
}
Expand All @@ -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
}
Expand All @@ -156,7 +156,7 @@ function findAllEntrypoints(
runner: ModuleRunner,
entrypoints = new Set<string>(),
): Set<string> {
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)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/module-runner/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
32 changes: 16 additions & 16 deletions packages/vite/src/module-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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, {
Expand All @@ -51,7 +51,7 @@ export class ModuleRunner {
private readonly root: string
private readonly concurrentModuleNodePromises = new Map<
string,
Promise<ModuleRunnerNode>
Promise<EvaluatedModuleNode>
>()

private destroyed = false
Expand All @@ -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(
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
Expand All @@ -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 &&
Expand All @@ -164,7 +164,7 @@ export class ModuleRunner {

private async cachedRequest(
url: string,
mod: ModuleRunnerNode,
mod: EvaluatedModuleNode,
callstack: string[] = [],
metadata?: SSRImportMetadata,
): Promise<any> {
Expand Down Expand Up @@ -219,12 +219,12 @@ export class ModuleRunner {
private async cachedModule(
url: string,
importer?: string,
): Promise<ModuleRunnerNode> {
): Promise<EvaluatedModuleNode> {
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)
Expand All @@ -241,8 +241,8 @@ export class ModuleRunner {
private async getModuleInformation(
url: string,
importer: string | undefined,
cachedModule: ModuleRunnerNode | undefined,
): Promise<ModuleRunnerNode> {
cachedModule: EvaluatedModuleNode | undefined,
): Promise<EvaluatedModuleNode> {
if (this.destroyed) {
throw new Error(`Vite module runner has been destroyed.`)
}
Expand Down Expand Up @@ -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
Expand All @@ -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<any> {
const fetchResult = mod.meta!
Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/module-runner/sourcemap/interceptor.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -21,7 +21,7 @@ export interface InterceptorOptions {
const sourceMapCache: Record<string, CachedMapEntry> = {}
const fileContentsCache: Record<string, string> = {}

const moduleGraphs = new Set<ModuleRunnerGraph>()
const evaluatedModulesCache = new Set<EvaluatedModules>()
const retrieveFileHandlers = new Set<RetrieveFileHandler>()
const retrieveSourceMapHandlers = new Set<RetrieveSourceMapHandler>()

Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/module-runner/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 784ee9b

Please sign in to comment.