|
1 | 1 | import * as acorn from 'acorn'; |
2 | 2 | import ExternalModule from './ExternalModule'; |
3 | 3 | import Graph from './Graph'; |
4 | | -import Module from './Module'; |
| 4 | +import Module, { DynamicImport } from './Module'; |
5 | 5 | import { |
6 | 6 | CustomPluginOptions, |
7 | 7 | EmittedChunk, |
@@ -276,27 +276,27 @@ export class ModuleLoader { |
276 | 276 | return loadNewModulesPromise; |
277 | 277 | } |
278 | 278 |
|
279 | | - private async fetchDynamicDependencies(module: Module): Promise<void> { |
| 279 | + private async fetchDynamicDependencies( |
| 280 | + module: Module, |
| 281 | + resolveDynamicImportPromises: Promise< |
| 282 | + [dynamicImport: DynamicImport, resolvedId: ResolvedId | string | null] |
| 283 | + >[] |
| 284 | + ): Promise<void> { |
280 | 285 | const dependencies = await Promise.all( |
281 | | - module.dynamicImports.map(async dynamicImport => { |
282 | | - const resolvedId = await this.resolveDynamicImport( |
283 | | - module, |
284 | | - typeof dynamicImport.argument === 'string' |
285 | | - ? dynamicImport.argument |
286 | | - : dynamicImport.argument.esTreeNode, |
287 | | - module.id |
288 | | - ); |
289 | | - if (resolvedId === null) return null; |
290 | | - if (typeof resolvedId === 'string') { |
291 | | - dynamicImport.resolution = resolvedId; |
292 | | - return null; |
293 | | - } |
294 | | - return (dynamicImport.resolution = await this.fetchResolvedDependency( |
295 | | - relativeId(resolvedId.id), |
296 | | - module.id, |
297 | | - resolvedId |
298 | | - )); |
299 | | - }) |
| 286 | + resolveDynamicImportPromises.map(resolveDynamicImportPromise => |
| 287 | + resolveDynamicImportPromise.then(async ([dynamicImport, resolvedId]) => { |
| 288 | + if (resolvedId === null) return null; |
| 289 | + if (typeof resolvedId === 'string') { |
| 290 | + dynamicImport.resolution = resolvedId; |
| 291 | + return null; |
| 292 | + } |
| 293 | + return (dynamicImport.resolution = await this.fetchResolvedDependency( |
| 294 | + relativeId(resolvedId.id), |
| 295 | + module.id, |
| 296 | + resolvedId |
| 297 | + )); |
| 298 | + }) |
| 299 | + ) |
300 | 300 | ); |
301 | 301 | for (const dependency of dependencies) { |
302 | 302 | if (dependency) { |
@@ -336,10 +336,15 @@ export class ModuleLoader { |
336 | 336 | this.modulesById.set(id, module); |
337 | 337 | this.graph.watchFiles[id] = true; |
338 | 338 | await this.addModuleSource(id, importer, module); |
339 | | - await this.pluginDriver.hookParallel('moduleParsed', [module.info]); |
| 339 | + const resolveStaticDependencyPromises = this.getResolveStaticDependencyPromises(module); |
| 340 | + const resolveDynamicImportPromises = this.getResolveDynamicImportPromises(module); |
| 341 | + Promise.all([ |
| 342 | + ...(resolveStaticDependencyPromises as Promise<never>[]), |
| 343 | + ...(resolveDynamicImportPromises as Promise<never>[]) |
| 344 | + ]).then(() => this.pluginDriver.hookParallel('moduleParsed', [module.info])); |
340 | 345 | await Promise.all([ |
341 | | - this.fetchStaticDependencies(module), |
342 | | - this.fetchDynamicDependencies(module) |
| 346 | + this.fetchStaticDependencies(module, resolveStaticDependencyPromises), |
| 347 | + this.fetchDynamicDependencies(module, resolveDynamicImportPromises) |
343 | 348 | ]); |
344 | 349 | module.linkImports(); |
345 | 350 | return module; |
@@ -375,19 +380,14 @@ export class ModuleLoader { |
375 | 380 | } |
376 | 381 | } |
377 | 382 |
|
378 | | - private async fetchStaticDependencies(module: Module): Promise<void> { |
| 383 | + private async fetchStaticDependencies( |
| 384 | + module: Module, |
| 385 | + resolveStaticDependencyPromises: Promise<[source: string, resolvedId: ResolvedId]>[] |
| 386 | + ): Promise<void> { |
379 | 387 | for (const dependency of await Promise.all( |
380 | | - Array.from(module.sources, async source => |
381 | | - this.fetchResolvedDependency( |
382 | | - source, |
383 | | - module.id, |
384 | | - (module.resolvedIds[source] = |
385 | | - module.resolvedIds[source] || |
386 | | - this.handleResolveId( |
387 | | - await this.resolveId(source, module.id, EMPTY_OBJECT), |
388 | | - source, |
389 | | - module.id |
390 | | - )) |
| 388 | + resolveStaticDependencyPromises.map(resolveStaticDependencyPromise => |
| 389 | + resolveStaticDependencyPromise.then(([source, resolvedId]) => |
| 390 | + this.fetchResolvedDependency(source, module.id, resolvedId) |
391 | 391 | ) |
392 | 392 | ) |
393 | 393 | )) { |
@@ -450,6 +450,43 @@ export class ModuleLoader { |
450 | 450 | }; |
451 | 451 | } |
452 | 452 |
|
| 453 | + private getResolveDynamicImportPromises( |
| 454 | + module: Module |
| 455 | + ): Promise<[dynamicImport: DynamicImport, resolvedId: ResolvedId | string | null]>[] { |
| 456 | + return module.dynamicImports.map(async dynamicImport => { |
| 457 | + const resolvedId = await this.resolveDynamicImport( |
| 458 | + module, |
| 459 | + typeof dynamicImport.argument === 'string' |
| 460 | + ? dynamicImport.argument |
| 461 | + : dynamicImport.argument.esTreeNode, |
| 462 | + module.id |
| 463 | + ); |
| 464 | + if (resolvedId && typeof resolvedId === 'object') { |
| 465 | + dynamicImport.id = resolvedId.id; |
| 466 | + } |
| 467 | + return [dynamicImport, resolvedId] as [DynamicImport, ResolvedId | string | null]; |
| 468 | + }); |
| 469 | + } |
| 470 | + |
| 471 | + private getResolveStaticDependencyPromises( |
| 472 | + module: Module |
| 473 | + ): Promise<[source: string, resolvedId: ResolvedId]>[] { |
| 474 | + return Array.from( |
| 475 | + module.sources, |
| 476 | + async source => |
| 477 | + [ |
| 478 | + source, |
| 479 | + (module.resolvedIds[source] = |
| 480 | + module.resolvedIds[source] || |
| 481 | + this.handleResolveId( |
| 482 | + await this.resolveId(source, module.id, EMPTY_OBJECT), |
| 483 | + source, |
| 484 | + module.id |
| 485 | + )) |
| 486 | + ] as [string, ResolvedId] |
| 487 | + ); |
| 488 | + } |
| 489 | + |
453 | 490 | private handleResolveId( |
454 | 491 | resolvedId: ResolvedId | null, |
455 | 492 | source: string, |
|
0 commit comments