Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: start tracking module resolution as soon as possible for easier tracking #2560

Merged
merged 2 commits into from Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/vite-node/src/client.ts
Expand Up @@ -197,7 +197,7 @@ export class ViteNodeRunner {
return !isInternalRequest(id) && !isNodeBuiltin(id)
}

async resolveUrl(id: string, importee?: string): Promise<[url: string, fsPath: string]> {
private async _resolveUrl(id: string, importee?: string): Promise<[url: string, fsPath: string]> {
if (!this.shouldResolveId(id))
return [id, id]
// we don't pass down importee here, because otherwise Vite doesn't resolve it correctly
Expand All @@ -215,6 +215,18 @@ export class ViteNodeRunner {
return [resolvedId, fsPath]
}

async resolveUrl(id: string, importee?: string) {
const resolveKey = `resolve:${id}`
// put info about new import as soon as possible, so we can start tracking it
this.moduleCache.set(resolveKey, { resolving: true })
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
try {
return await this._resolveUrl(id, importee)
}
finally {
this.moduleCache.delete(resolveKey)
}
}

/** @internal */
async dependencyRequest(id: string, fsPath: string, callstack: string[]) {
const getStack = () => {
Expand Down
1 change: 1 addition & 0 deletions packages/vite-node/src/types.ts
Expand Up @@ -45,6 +45,7 @@ export interface ModuleCache {
promise?: Promise<any>
exports?: any
evaluated?: boolean
resolving?: boolean
code?: string
map?: RawSourceMap
/**
Expand Down
20 changes: 11 additions & 9 deletions packages/vitest/src/utils/import.ts
@@ -1,21 +1,23 @@
import { getWorkerState } from './global'
import { setTimeout } from './timers'

export async function waitForImportsToResolve(tries = 0) {
await new Promise(resolve => setTimeout(resolve, 0))
function waitNextTick() {
return new Promise(resolve => setTimeout(resolve, 0))
}

export async function waitForImportsToResolve() {
await waitNextTick()
const state = getWorkerState()
const promises: Promise<unknown>[] = []
let resolvingCount = 0
for (const mod of state.moduleCache.values()) {
if (mod.promise && !mod.evaluated)
promises.push(mod.promise)
if (mod.resolving)
resolvingCount++
}
if (!promises.length && tries >= 3)
if (!promises.length && !resolvingCount)
return
await Promise.allSettled(promises)
// wait until the end of the loop, so `.then` on modules is called,
// like in import('./example').then(...)
// also call dynamicImportSettled again in case new imports were added
await new Promise(resolve => setTimeout(resolve, 1))
.then(() => Promise.resolve())
.then(() => waitForImportsToResolve(tries + 1))
await waitForImportsToResolve()
}