Skip to content

Commit

Permalink
feat(vite-node): support debug flag to emit additional info
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 29, 2022
1 parent b5d97b0 commit 6dd7f53
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
47 changes: 37 additions & 10 deletions packages/vite-node/src/client.ts
Expand Up @@ -51,15 +51,18 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
export class ViteNodeRunner {
root: string

debug: boolean

/**
* Holds the cache of modules
* Keys of the map are filepaths, or plain package names
*/
moduleCache: ModuleCacheMap

constructor(public options: ViteNodeRunnerOptions) {
this.root = options.root || process.cwd()
this.moduleCache = options.moduleCache || new ModuleCacheMap()
this.root = options.root ?? process.cwd()
this.moduleCache = options.moduleCache ?? new ModuleCacheMap()
this.debug = options.debug ?? (typeof process !== 'undefined' ? !!process.env.VITE_NODE_DEBUG : false)
}

async executeFile(file: string) {
Expand All @@ -85,23 +88,41 @@ export class ViteNodeRunner {
}

/** @internal */
async directRequest(id: string, fsPath: string, callstack: string[]) {
callstack = [...callstack, normalizeModuleId(id)]
async directRequest(id: string, fsPath: string, _callstack: string[]) {
const callstack = [..._callstack, normalizeModuleId(id)]
const request = async(dep: string) => {
const getStack = () => {
return `stack:\n${[...callstack, dep].reverse().map(p => `- ${p}`).join('\n')}`
}

// probably means it was passed as variable
// and wasn't transformed by Vite
if (this.options.resolveId && this.shouldResolveId(dep)) {
const resolvedDep = await this.options.resolveId(dep, id)
dep = resolvedDep?.id?.replace(this.root, '') || dep
}

if (callstack.includes(normalizeModuleId(dep))) {
const depExports = this.moduleCache.get(dep)?.exports
if (depExports)
return depExports
throw new Error(`[vite-node] Circular dependency detected\nStack:\n${[...callstack, dep].reverse().map(p => `- ${p}`).join('\n')}`)
let debugTimer: any
if (this.debug)
debugTimer = setTimeout(() => this.debugLog(() => `module ${dep} takes over 2s to load.\n${getStack()}`), 2000)

try {
if (callstack.includes(normalizeModuleId(dep))) {
this.debugLog(() => `circular dependency, ${getStack()}`)
const depExports = this.moduleCache.get(dep)?.exports
if (depExports)
return depExports
throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`)
}

const mod = await this.cachedRequest(dep, callstack)

return mod
}
finally {
if (debugTimer)
clearTimeout(debugTimer)
}
return this.cachedRequest(dep, callstack)
}

const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS
Expand Down Expand Up @@ -212,6 +233,12 @@ export class ViteNodeRunner {
hasNestedDefault(target: any) {
return '__esModule' in target && target.__esModule && 'default' in target.default
}

private debugLog(msg: () => string) {
if (this.debug)
// eslint-disable-next-line no-console
console.log(`[vite-node] ${msg()}`)
}
}

function proxyMethod(name: 'get' | 'set' | 'has' | 'deleteProperty', tryDefault: boolean) {
Expand Down
1 change: 1 addition & 0 deletions packages/vite-node/src/types.ts
Expand Up @@ -47,6 +47,7 @@ export interface ViteNodeRunnerOptions {
moduleCache?: ModuleCacheMap
interopDefault?: boolean
requestStubs?: Record<string, any>
debug?: boolean
}

export interface ViteNodeResolveId {
Expand Down

0 comments on commit 6dd7f53

Please sign in to comment.