@@ -36,7 +36,18 @@ function parseModel(response, json) {
3636// eslint-disable-next-line no-unused-vars
3737function resolveModuleReference ( bundlerConfig , moduleData ) {
3838 if ( bundlerConfig ) {
39- return bundlerConfig [ moduleData . id ] [ moduleData . name ] ;
39+ var resolvedModuleData = bundlerConfig [ moduleData . id ] [ moduleData . name ] ;
40+
41+ if ( moduleData . async ) {
42+ return {
43+ id : resolvedModuleData . id ,
44+ chunks : resolvedModuleData . chunks ,
45+ name : resolvedModuleData . name ,
46+ async : true
47+ } ;
48+ } else {
49+ return resolvedModuleData ;
50+ }
4051 }
4152
4253 return moduleData ;
@@ -45,11 +56,13 @@ function resolveModuleReference(bundlerConfig, moduleData) {
4556// in Webpack but unfortunately it's not exposed so we have to
4657// replicate it in user space. null means that it has already loaded.
4758
48- var chunkCache = new Map ( ) ; // Start preloading the modules since we might need them soon.
59+ var chunkCache = new Map ( ) ;
60+ var asyncModuleCache = new Map ( ) ; // Start preloading the modules since we might need them soon.
4961// This function doesn't suspend.
5062
5163function preloadModule ( moduleData ) {
5264 var chunks = moduleData . chunks ;
65+ var promises = [ ] ;
5366
5467 for ( var i = 0 ; i < chunks . length ; i ++ ) {
5568 var chunkId = chunks [ i ] ;
@@ -58,31 +71,62 @@ function preloadModule(moduleData) {
5871 if ( entry === undefined ) {
5972 var thenable = globalThis . __next_chunk_load__ ( chunkId ) ;
6073
74+ promises . push ( thenable ) ;
6175 var resolve = chunkCache . set . bind ( chunkCache , chunkId , null ) ;
6276 var reject = chunkCache . set . bind ( chunkCache , chunkId ) ;
6377 thenable . then ( resolve , reject ) ;
6478 chunkCache . set ( chunkId , thenable ) ;
6579 }
6680 }
81+
82+ if ( moduleData . async ) {
83+ var modulePromise = Promise . all ( promises ) . then ( function ( ) {
84+ return globalThis . __next_require__ ( moduleData . id ) ;
85+ } ) ;
86+ modulePromise . then ( function ( value ) {
87+ modulePromise . status = 'fulfilled' ;
88+ modulePromise . value = value ;
89+ } , function ( reason ) {
90+ modulePromise . status = 'rejected' ;
91+ modulePromise . reason = reason ;
92+ } ) ;
93+ asyncModuleCache . set ( moduleData . id , modulePromise ) ;
94+ }
6795} // Actually require the module or suspend if it's not yet ready.
6896// Increase priority if necessary.
6997
7098function requireModule ( moduleData ) {
71- var chunks = moduleData . chunks ;
99+ var moduleExports ;
100+
101+ if ( moduleData . async ) {
102+ // We assume that preloadModule has been called before, which
103+ // should have added something to the module cache.
104+ var promise = asyncModuleCache . get ( moduleData . id ) ;
105+
106+ if ( promise . status === 'fulfilled' ) {
107+ moduleExports = promise . value ;
108+ } else if ( promise . status === 'rejected' ) {
109+ throw promise . reason ;
110+ } else {
111+ throw promise ;
112+ }
113+ } else {
114+ var chunks = moduleData . chunks ;
72115
73- for ( var i = 0 ; i < chunks . length ; i ++ ) {
74- var chunkId = chunks [ i ] ;
75- var entry = chunkCache . get ( chunkId ) ;
116+ for ( var i = 0 ; i < chunks . length ; i ++ ) {
117+ var chunkId = chunks [ i ] ;
118+ var entry = chunkCache . get ( chunkId ) ;
76119
77- if ( entry !== null ) {
78- // We assume that preloadModule has been called before.
79- // So we don't expect to see entry being undefined here, that's an error.
80- // Let's throw either an error or the Promise.
81- throw entry ;
120+ if ( entry !== null ) {
121+ // We assume that preloadModule has been called before.
122+ // So we don't expect to see entry being undefined here, that's an error.
123+ // Let's throw either an error or the Promise.
124+ throw entry ;
125+ }
82126 }
83- }
84127
85- var moduleExports = globalThis . __next_require__ ( moduleData . id ) ;
128+ moduleExports = globalThis . __next_require__ ( moduleData . id ) ;
129+ }
86130
87131 if ( moduleData . name === '*' ) {
88132 // This is a placeholder value that represents that the caller imported this
0 commit comments