66"use strict" ;
77
88const isDependencyKind = require ( "../graph/isDependencyKind" ) ;
9+ const RuntimeGlobals = require ( "../runtime/RuntimeGlobals" ) ;
10+ const InitFragment = require ( "../template/InitFragment" ) ;
11+ const memoize = require ( "../util/memoize" ) ;
12+ const isGeneratorLowered = require ( "./isGeneratorLowered" ) ;
913
1014/** @import ModuleGraph from "../graph/ModuleGraph" */
11- /** @import Module from "../module/Module" */
15+ /** @import Module, { BuildMeta } from "../module/Module" */
16+ /** @import { GenerateContext } from "../module/Generator" */
17+ /** @import ChunkGraph, { ModuleId } from "../graph/ChunkGraph" */
18+ /** @import { DependencyTemplateContext } from "../template/DependencyTemplate" */
19+ /** @import { JavascriptModuleBuildInfo } from "../javascript/JavascriptModule" */
1220
1321/** @typedef {Set<Module> } Modules */
1422
@@ -35,7 +43,12 @@ const getOutgoingAsyncModules = (moduleGraph, module) => {
3543 // Walk the imports in source order: the spec gathers these from
3644 // `[[RequestedModules]]`, and their evaluation order is observable.
3745 for ( const dependency of module . dependencies ) {
38- if ( ! isDependencyKind ( dependency , "esm-import" ) ) continue ;
46+ if (
47+ ! isDependencyKind ( dependency , "esm-import" ) &&
48+ ! isDependencyKind ( dependency , "provided" )
49+ ) {
50+ continue ;
51+ }
3952 const referenced = moduleGraph . getModule ( dependency ) ;
4053 if ( ! referenced || seen . has ( referenced ) ) continue ;
4154 const connections = outgoingConnectionMap . get ( referenced ) ;
@@ -44,7 +57,8 @@ const getOutgoingAsyncModules = (moduleGraph, module) => {
4457 connections . some (
4558 ( c ) =>
4659 c . dependency !== null &&
47- isDependencyKind ( c . dependency , "esm-import" ) &&
60+ ( isDependencyKind ( c . dependency , "esm-import" ) ||
61+ isDependencyKind ( c . dependency , "provided" ) ) &&
4862 c . isTargetActive ( undefined )
4963 )
5064 ) {
@@ -58,3 +72,108 @@ const getOutgoingAsyncModules = (moduleGraph, module) => {
5872} ;
5973
6074module . exports . getOutgoingAsyncModules = getOutgoingAsyncModules ;
75+
76+ // The deferred-namespace runtime imports dependency templates, so load it on demand.
77+ const getDeferredCycleHelpers = memoize ( ( ) =>
78+ require ( "../runtime/MakeDeferredNamespaceObjectRuntime" )
79+ ) ;
80+
81+ /**
82+ * Key shared by the async modules of one strongly-connected component. The spec
83+ * records one `[[EvaluationError]]` per component, so a member that already
84+ * fulfilled still has to report the root's error afterwards.
85+ * @param {ModuleGraph } moduleGraph the module graph
86+ * @param {ChunkGraph } chunkGraph the chunk graph
87+ * @param {Module } module the async module
88+ * @returns {ModuleId | null } the group key, or null when it is not in an async cycle
89+ */
90+ const getAsyncCycleGroupId = ( moduleGraph , chunkGraph , module ) => {
91+ const { getDeferredCycleModules } = getDeferredCycleHelpers ( ) ;
92+ const peers = getDeferredCycleModules ( moduleGraph , module ) ;
93+ if ( peers === null ) return null ;
94+ // The component's smallest async member id names the group, so every member
95+ // emits the same key and none has to carry the whole peer list.
96+ let groupId = null ;
97+ let asyncPeerCount = 0 ;
98+ for ( const peer of peers ) {
99+ if ( ! moduleGraph . isAsync ( peer ) ) continue ;
100+ asyncPeerCount ++ ;
101+ const id = chunkGraph . getModuleId ( peer ) ;
102+ if ( id !== null && ( groupId === null || `${ id } ` < `${ groupId } ` ) ) {
103+ groupId = id ;
104+ }
105+ }
106+ if ( asyncPeerCount === 0 ) return null ;
107+ const ownId = chunkGraph . getModuleId ( module ) ;
108+ if ( ownId !== null && ( groupId === null || `${ ownId } ` < `${ groupId } ` ) ) {
109+ groupId = ownId ;
110+ }
111+ return groupId ;
112+ } ;
113+
114+ /**
115+ * Wraps a module body so its imports can suspend evaluation.
116+ * @param {DependencyTemplateContext } templateContext template context
117+ * @param {boolean= } useModuleExports whether the body can replace CommonJS exports
118+ * @returns {InitFragment<GenerateContext> } async module boundary
119+ */
120+ const createAsyncModuleInitFragment = (
121+ templateContext ,
122+ useModuleExports = false
123+ ) => {
124+ const {
125+ module,
126+ moduleGraph,
127+ chunkGraph,
128+ runtimeTemplate,
129+ runtimeRequirements
130+ } = templateContext ;
131+ runtimeRequirements . add ( RuntimeGlobals . module ) ;
132+ runtimeRequirements . add ( RuntimeGlobals . asyncModule ) ;
133+ // Only strict error handling retains the shared evaluation error of a cycle.
134+ const cycleGroupId = runtimeTemplate . outputOptions . strictModuleErrorHandling
135+ ? getAsyncCycleGroupId ( moduleGraph , chunkGraph , module )
136+ : null ;
137+ if ( cycleGroupId !== null ) runtimeRequirements . add ( RuntimeGlobals . moduleId ) ;
138+ const isAsyncBody = /** @type {BuildMeta } */ ( module . buildMeta ) . async ;
139+ const argumentsSuffix = useModuleExports
140+ ? `, ${ isAsyncBody ? 1 : 0 } , ${ cycleGroupId === null ? "undefined" : JSON . stringify ( cycleGroupId ) } , 1`
141+ : cycleGroupId === null
142+ ? isAsyncBody
143+ ? ", 1"
144+ : ""
145+ : `, ${ isAsyncBody ? 1 : 0 } , ${ JSON . stringify ( cycleGroupId ) } ` ;
146+ // Disposal must finish before dependents can resume evaluation.
147+ const usesTopLevelUsingDeclaration =
148+ /** @type {JavascriptModuleBuildInfo } */ ( module . buildInfo )
149+ . usesTopLevelUsingDeclaration ;
150+ const blockStart = usesTopLevelUsingDeclaration ? " {" : "" ;
151+ const blockEnd = usesTopLevelUsingDeclaration ? "\n}" : "" ;
152+ const parameters = `__webpack_handle_async_dependencies__, __webpack_async_result__${ useModuleExports ? ", __webpack_async_module__" : "" } ` ;
153+ const moduleAssignment = useModuleExports
154+ ? `${ module . moduleArgument } = __webpack_async_module__;\n`
155+ : "" ;
156+ const bindThis = useModuleExports ? ".bind(this)" : "" ;
157+ if ( isGeneratorLowered ( module , moduleGraph , runtimeTemplate ) ) {
158+ runtimeRequirements . add ( RuntimeGlobals . asyncModuleGenerator ) ;
159+ return new InitFragment (
160+ `${ RuntimeGlobals . asyncModule } (${ module . moduleArgument } , ${ RuntimeGlobals . asyncModuleGenerator } (function* (${ parameters } ) { try {${ blockStart } \n${ moduleAssignment } ` ,
161+ InitFragment . STAGE_ASYNC_BOUNDARY ,
162+ 0 ,
163+ "async-module" ,
164+ `${ blockEnd } \n__webpack_async_result__();\n} catch(e) { __webpack_async_result__(e); } }${ bindThis } )${ argumentsSuffix } );`
165+ ) ;
166+ }
167+ return new InitFragment (
168+ ( runtimeTemplate . supportsArrowFunction ( )
169+ ? `${ RuntimeGlobals . asyncModule } (${ module . moduleArgument } , async (${ parameters } ) => { try {${ blockStart } \n`
170+ : `${ RuntimeGlobals . asyncModule } (${ module . moduleArgument } , async function (${ parameters } ) { try {${ blockStart } \n` ) +
171+ moduleAssignment ,
172+ InitFragment . STAGE_ASYNC_BOUNDARY ,
173+ 0 ,
174+ "async-module" ,
175+ `${ blockEnd } \n__webpack_async_result__();\n} catch(e) { __webpack_async_result__(e); } }${ runtimeTemplate . supportsArrowFunction ( ) ? "" : bindThis } ${ argumentsSuffix } );`
176+ ) ;
177+ } ;
178+
179+ module . exports . createAsyncModuleInitFragment = createAsyncModuleInitFragment ;
0 commit comments