Skip to content

Commit 5098b21

Browse files
authoredDec 5, 2022
fix: dynamicImportSettled also waits for nested imports (#2389)
* fix: dynamicImportSettled also waits for nested imports * chore: wait only not evaluated modules * chore: always mark evaluated * chore: mark external modules evaluated
1 parent d050604 commit 5098b21

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed
 

‎packages/vite-node/src/client.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ export class ViteNodeRunner {
247247
debugNative(externalize)
248248
const exports = await this.interopedImport(externalize)
249249
mod.exports = exports
250+
mod.evaluated = true
250251
return exports
251252
}
252253

@@ -286,7 +287,7 @@ export class ViteNodeRunner {
286287
},
287288
})
288289

289-
Object.assign(mod, { code: transformed, exports })
290+
Object.assign(mod, { code: transformed, exports, evaluated: false })
290291

291292
const __filename = fileURLToPath(url)
292293
const moduleProxy = {
@@ -347,7 +348,12 @@ export class ViteNodeRunner {
347348
columnOffset: -codeDefinition.length,
348349
})
349350

350-
await fn(...Object.values(context))
351+
try {
352+
await fn(...Object.values(context))
353+
}
354+
finally {
355+
mod.evaluated = true
356+
}
351357

352358
return exports
353359
}

‎packages/vite-node/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export type CreateHotContextFunction = (runner: ViteNodeRunner, url: string) =>
4444
export interface ModuleCache {
4545
promise?: Promise<any>
4646
exports?: any
47+
evaluated?: boolean
4748
code?: string
4849
map?: RawSourceMap
4950
/**

‎packages/vitest/src/integrations/vi.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,24 @@ class VitestUtils {
247247
/**
248248
* Wait for all imports to load.
249249
* Useful, if you have a synchronous call that starts
250-
* importing a module, that you cannot wait otherwise.
250+
* importing a module that you cannot wait otherwise.
251251
*/
252252
public async dynamicImportSettled() {
253253
const state = getWorkerState()
254254
const promises: Promise<unknown>[] = []
255255
for (const mod of state.moduleCache.values()) {
256-
if (mod.promise)
256+
if (mod.promise && !mod.evaluated)
257257
promises.push(mod.promise)
258258
}
259+
if (!promises.length)
260+
return
259261
await Promise.allSettled(promises)
260-
// wait until the end of the loop, so `.then` on modules called,
262+
// wait until the end of the loop, so `.then` on modules is called,
261263
// like in import('./example').then(...)
262-
await new Promise(resolve => setTimeout(resolve, 1)).then(() => Promise.resolve())
264+
// also call dynamicImportSettled again in case new imports were added
265+
await new Promise(resolve => setTimeout(resolve, 1))
266+
.then(() => Promise.resolve())
267+
.then(() => this.dynamicImportSettled())
263268
}
264269

265270
private _config: null | ResolvedConfig = null

0 commit comments

Comments
 (0)
Please sign in to comment.