Skip to content

Commit

Permalink
Merge branch 'main' into pr/simon-abbott/1868
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 18, 2022
2 parents 1efdd2d + 7ba6b20 commit 0ded073
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/vite-node/src/client.ts
Expand Up @@ -46,7 +46,10 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
return normalizeModuleId(fsPath)
}

set(fsPath: string, mod: Partial<ModuleCache>) {
/**
* Assign partial data to the map
*/
update(fsPath: string, mod: Partial<ModuleCache>) {
fsPath = this.normalizePath(fsPath)
if (!super.has(fsPath))
super.set(fsPath, mod)
Expand All @@ -55,6 +58,11 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
return this
}

set(fsPath: string, mod: ModuleCache) {
fsPath = this.normalizePath(fsPath)
return super.set(fsPath, mod)
}

get(fsPath: string) {
fsPath = this.normalizePath(fsPath)
return super.get(fsPath)
Expand Down Expand Up @@ -105,27 +113,28 @@ export class ViteNodeRunner {
return this.moduleCache.get(fsPath)?.promise

const promise = this.directRequest(id, fsPath, callstack)
this.moduleCache.set(fsPath, { promise })
this.moduleCache.update(fsPath, { promise })

return await promise
}

/** @internal */
async directRequest(id: string, fsPath: string, _callstack: string[]) {
const callstack = [..._callstack, fsPath]

const request = async (dep: string) => {
const fsPath = toFilePath(normalizeRequestId(dep, this.options.base), this.root)
const depFsPath = toFilePath(normalizeRequestId(dep, this.options.base), this.root)
const getStack = () => {
return `stack:\n${[...callstack, fsPath].reverse().map(p => `- ${p}`).join('\n')}`
return `stack:\n${[...callstack, depFsPath].reverse().map(p => `- ${p}`).join('\n')}`
}

let debugTimer: any
if (this.debug)
debugTimer = setTimeout(() => console.warn(() => `module ${fsPath} takes over 2s to load.\n${getStack()}`), 2000)
debugTimer = setTimeout(() => console.warn(() => `module ${depFsPath} takes over 2s to load.\n${getStack()}`), 2000)

try {
if (callstack.includes(fsPath)) {
const depExports = this.moduleCache.get(fsPath)?.exports
if (callstack.includes(depFsPath)) {
const depExports = this.moduleCache.get(depFsPath)?.exports
if (depExports)
return depExports
throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`)
Expand Down Expand Up @@ -171,7 +180,7 @@ export class ViteNodeRunner {
if (externalize) {
debugNative(externalize)
const mod = await this.interopedImport(externalize)
this.moduleCache.set(fsPath, { exports: mod })
this.moduleCache.update(fsPath, { exports: mod })
return mod
}

Expand All @@ -188,7 +197,7 @@ export class ViteNodeRunner {
configurable: false,
})

this.moduleCache.set(fsPath, { code: transformed, exports })
this.moduleCache.update(fsPath, { code: transformed, exports })

const __filename = fileURLToPath(url)
const moduleProxy = {
Expand Down
1 change: 1 addition & 0 deletions packages/vite-node/src/server.ts
@@ -1,3 +1,4 @@
import { performance } from 'perf_hooks'
import { resolve } from 'pathe'
import type { TransformResult, ViteDevServer } from 'vite'
import createDebug from 'debug'
Expand Down

0 comments on commit 0ded073

Please sign in to comment.