@@ -244,6 +244,8 @@ impl JsPlugin {
244244 let mut startup: Vec < Cow < str > > = Vec :: new ( ) ;
245245 let mut allow_inline_startup = true ;
246246 let mut mf_async_startup = false ;
247+ let mf_async_startup_flag =
248+ runtime_requirements. contains ( RuntimeGlobals :: ASYNC_FEDERATION_STARTUP ) ;
247249 let supports_arrow_function = compilation
248250 . options
249251 . output
@@ -330,12 +332,20 @@ impl JsPlugin {
330332
331333 if !runtime_requirements. contains ( RuntimeGlobals :: STARTUP_NO_DEFAULT ) {
332334 if chunk. has_entry_module ( & compilation. chunk_graph ) {
333- let use_federation_async =
334- runtime_requirements. contains ( RuntimeGlobals :: ASYNC_FEDERATION_STARTUP ) ;
335+ let use_federation_async = mf_async_startup_flag;
336+
337+ if cfg ! ( debug_assertions) && mf_async_startup_flag {
338+ tracing:: debug!(
339+ "render_startup async MF chunk={:?} has_entry_modules={} reqs={:?}" ,
340+ chunk_ukey,
341+ chunk. has_entry_module( & compilation. chunk_graph) ,
342+ runtime_requirements
343+ ) ;
344+ }
335345
336346 if use_federation_async {
337347 let startup_fn = RuntimeGlobals :: STARTUP_ENTRYPOINT ;
338- let needs_on_chunks_loaded =
348+ let _needs_on_chunks_loaded =
339349 runtime_requirements. contains ( RuntimeGlobals :: ON_CHUNKS_LOADED ) ;
340350 mf_async_startup = true ;
341351 let mut buf2: Vec < Cow < str > > = Vec :: new ( ) ;
@@ -353,7 +363,7 @@ impl JsPlugin {
353363 buf2. push ( "// Call federation runtime initialization" . into ( ) ) ;
354364 buf2. push ( "var runtimeInitialization = undefined;" . into ( ) ) ;
355365 buf2. push ( format ! ( "if (typeof {} === \" function\" ) {{" , startup_fn) . into ( ) ) ;
356- buf2. push ( format ! ( " runtimeInitialization = {}() ;" , startup_fn) . into ( ) ) ;
366+ buf2. push ( format ! ( " runtimeInitialization = {};" , startup_fn) . into ( ) ) ;
357367 buf2. push ( "} else {" . into ( ) ) ;
358368 buf2. push (
359369 format ! (
@@ -488,7 +498,7 @@ impl JsPlugin {
488498 // ESM output with top-level await
489499 buf2. push (
490500 format ! (
491- "const {}Promise = Promise.resolve(runtimeInitialization).then(async () => {{" ,
501+ "const {}Promise = Promise.resolve().then(() => {{ return typeof runtimeInitialization === \" function \" ? runtimeInitialization() : runtimeInitialization; }}).then(() => {{ return typeof __webpack_require__.I === \" function \" ? __webpack_require__.I( \" default \" ) : undefined; }} ).then(async () => {{" ,
492502 RuntimeGlobals :: EXPORTS
493503 )
494504 . into ( ) ,
@@ -518,26 +528,22 @@ impl JsPlugin {
518528 buf2. push ( format ! ( " return {};" , entry_fn_body) . into ( ) ) ;
519529 }
520530 buf2. push ( "});" . into ( ) ) ;
521- if needs_on_chunks_loaded {
522- buf2. push (
523- format ! (
524- "export default await {}Promise.then(res => {}(res));" ,
525- RuntimeGlobals :: EXPORTS ,
526- RuntimeGlobals :: ON_CHUNKS_LOADED
527- )
528- . into ( ) ,
529- ) ;
530- } else {
531- buf2. push (
532- format ! ( "export default await {}Promise;" , RuntimeGlobals :: EXPORTS ) . into ( ) ,
533- ) ;
534- }
531+ buf2. push ( "export default await " . into ( ) ) ;
532+ buf2. push (
533+ format ! (
534+ "{}Promise.then(res => typeof {} === \" function\" ? {}(res) : res);" ,
535+ RuntimeGlobals :: EXPORTS ,
536+ RuntimeGlobals :: ON_CHUNKS_LOADED ,
537+ RuntimeGlobals :: ON_CHUNKS_LOADED
538+ )
539+ . into ( ) ,
540+ ) ;
535541 } else {
536542 // CJS output with Promise chain
537- buf2. push ( "// Wrap startup in Promise.all with federation handlers" . into ( ) ) ;
543+ buf2. push ( "// Wrap startup in Promise chain with federation handlers" . into ( ) ) ;
538544 buf2. push (
539545 format ! (
540- "var {} = Promise.resolve(runtimeInitialization).then(function() {{" ,
546+ "var {} = Promise.resolve().then(function() {{ return typeof runtimeInitialization === \" function \" ? runtimeInitialization() : runtimeInitialization; }}).then(function() {{ return typeof __webpack_require__.I === \" function \" ? __webpack_require__.I( \" default \" ) : undefined; }} ).then(function() {{" ,
541547 RuntimeGlobals :: EXPORTS
542548 )
543549 . into ( ) ,
@@ -571,23 +577,22 @@ impl JsPlugin {
571577 } else {
572578 buf2. push ( format ! ( " return {};" , entry_fn_body) . into ( ) ) ;
573579 }
574- if needs_on_chunks_loaded {
575- buf2. push (
576- format ! (
577- "}}).then(function(res) {{ return {}(res); }});" ,
578- RuntimeGlobals :: ON_CHUNKS_LOADED
579- )
580- . into ( ) ,
581- ) ;
582- } else {
583- buf2. push ( "});" . into ( ) ) ;
584- }
580+ buf2. push ( "}).then(function(res) {" . into ( ) ) ;
581+ buf2. push (
582+ format ! (
583+ " return typeof {} === \" function\" ? {}(res) : res;" ,
584+ RuntimeGlobals :: ON_CHUNKS_LOADED ,
585+ RuntimeGlobals :: ON_CHUNKS_LOADED
586+ )
587+ . into ( ) ,
588+ ) ;
589+ buf2. push ( "});" . into ( ) ) ;
585590 }
586591 } else {
587- buf2. push ( "// Wrap startup in Promise.all with federation handlers" . into ( ) ) ;
592+ buf2. push ( "// Wrap startup in Promise chain with federation handlers" . into ( ) ) ;
588593 buf2. push (
589594 format ! (
590- "var {} = Promise.resolve(runtimeInitialization).then(function() {{" ,
595+ "var {} = Promise.resolve().then(function() {{ return typeof runtimeInitialization === \" function \" ? runtimeInitialization() : runtimeInitialization; }}).then(function() {{ return typeof __webpack_require__.I === \" function \" ? __webpack_require__.I( \" default \" ) : undefined; }} ).then(function() {{" ,
591596 RuntimeGlobals :: EXPORTS
592597 )
593598 . into ( ) ,
@@ -621,6 +626,15 @@ impl JsPlugin {
621626 } else {
622627 buf2. push ( format ! ( " return {};" , entry_fn_body) . into ( ) ) ;
623628 }
629+ buf2. push ( "}).then(function(res) {" . into ( ) ) ;
630+ buf2. push (
631+ format ! (
632+ " return typeof {} === \" function\" ? {}(res) : res;" ,
633+ RuntimeGlobals :: ON_CHUNKS_LOADED ,
634+ RuntimeGlobals :: ON_CHUNKS_LOADED
635+ )
636+ . into ( ) ,
637+ ) ;
624638 buf2. push ( "});" . into ( ) ) ;
625639 }
626640
@@ -1235,15 +1249,22 @@ impl JsPlugin {
12351249 startup_global
12361250 ) ) ) ;
12371251 result. add ( RawStringSource :: from ( format ! (
1238- " runtimeInitialization = {}() ;\n " ,
1252+ " runtimeInitialization = {};\n " ,
12391253 startup_global
12401254 ) ) ) ;
12411255 result. add ( RawStringSource :: from ( "}\n " ) ) ;
12421256 result. add ( RawStringSource :: from ( "var promises = [];\n " ) ) ;
12431257 result. add ( RawStringSource :: from ( format ! (
1244- "var {} = Promise.resolve(runtimeInitialization).then(function() {{\n " ,
1258+ "var {} = Promise.resolve().then(function() {{ return typeof runtimeInitialization === 'function' ? runtimeInitialization() : runtimeInitialization; }}).then(function() {{ return typeof __webpack_require__.I === 'function' ? __webpack_require__.I('default') : undefined; }} ).then(function() {{\n " ,
12451259 RuntimeGlobals :: EXPORTS . name( )
12461260 ) ) ) ;
1261+ result. add ( RawStringSource :: from (
1262+ " if (__webpack_require__.federation && __webpack_require__.federation.bundlerRuntime && typeof __webpack_require__.federation.bundlerRuntime.flushInitialConsumes === 'function') {\n " ,
1263+ ) ) ;
1264+ result. add ( RawStringSource :: from (
1265+ " __webpack_require__.federation.bundlerRuntime.flushInitialConsumes();\n " ,
1266+ ) ) ;
1267+ result. add ( RawStringSource :: from ( " }\n " ) ) ;
12471268 result. add ( RawStringSource :: from ( " var handlers = [\n " ) ) ;
12481269 result. add ( RawStringSource :: from ( " function(chunkId, promises) {\n " ) ) ;
12491270 result. add ( RawStringSource :: from ( " return (__webpack_require__.f.consumes || function(chunkId, promises) {})(chunkId, promises);\n " ) ) ;
0 commit comments