@@ -143,23 +143,40 @@ function loadChunkPath(chunkPath: ChunkPath, source?: SourceInfo): void {
143
143
}
144
144
}
145
145
146
- function loadChunkUncached ( chunkPath : ChunkPath ) {
146
+ async function loadChunkAsyncUncached (
147
+ source : SourceInfo ,
148
+ chunkPath : ChunkPath
149
+ ) : Promise < void > {
147
150
// resolve to an absolute path to simplify `require` handling
148
151
const resolved = path . resolve ( RUNTIME_ROOT , chunkPath )
149
152
150
- const chunkModules : CompressedModuleFactories = require ( resolved )
151
- for ( const [ moduleId , moduleFactory ] of Object . entries ( chunkModules ) ) {
152
- if ( ! moduleFactories [ moduleId ] ) {
153
- if ( Array . isArray ( moduleFactory ) ) {
154
- const [ moduleFactoryFn , otherIds ] = moduleFactory
155
- moduleFactories [ moduleId ] = moduleFactoryFn
156
- for ( const otherModuleId of otherIds ) {
157
- moduleFactories [ otherModuleId ] = moduleFactoryFn
153
+ try {
154
+ // The chunk we are loading is a CJS module, when importing it via import()
155
+ // the module.exports become the `default` property of the esm module
156
+ // namespace.
157
+ const chunkModules : CompressedModuleFactories = ( await import ( resolved ) )
158
+ . default
159
+ for ( const [ moduleId , moduleFactory ] of Object . entries ( chunkModules ) ) {
160
+ if ( ! moduleFactories [ moduleId ] ) {
161
+ if ( Array . isArray ( moduleFactory ) ) {
162
+ const [ moduleFactoryFn , otherIds ] = moduleFactory
163
+ moduleFactories [ moduleId ] = moduleFactoryFn
164
+ for ( const otherModuleId of otherIds ) {
165
+ moduleFactories [ otherModuleId ] = moduleFactoryFn
166
+ }
167
+ } else {
168
+ moduleFactories [ moduleId ] = moduleFactory
158
169
}
159
- } else {
160
- moduleFactories [ moduleId ] = moduleFactory
161
170
}
162
171
}
172
+ } catch ( e ) {
173
+ let errorMessage = `Failed to load chunk ${ chunkPath } `
174
+ if ( source ) {
175
+ errorMessage += ` from ${ stringifySourceInfo ( source ) } `
176
+ }
177
+ throw new Error ( errorMessage , {
178
+ cause : e ,
179
+ } )
163
180
}
164
181
}
165
182
@@ -176,23 +193,11 @@ function loadChunkAsync(
176
193
177
194
let entry = chunkCache . get ( chunkPath )
178
195
if ( entry === undefined ) {
179
- try {
180
- // Load the chunk synchronously
181
- loadChunkUncached ( chunkPath )
182
- entry = loadedChunk
183
- } catch ( e ) {
184
- let errorMessage = `Failed to load chunk ${ chunkPath } `
185
- if ( source ) {
186
- errorMessage += ` from ${ stringifySourceInfo ( source ) } `
187
- }
188
-
189
- // Cache the failure promise, future requests will also get this same rejection
190
- entry = Promise . reject (
191
- new Error ( errorMessage , {
192
- cause : e ,
193
- } )
194
- )
195
- }
196
+ // A new Promise ensures callers that don't handle rejection will still trigger one unhandled rejection.
197
+ // Handling the rejection will not trigger unhandled rejections.
198
+ entry = loadChunkAsyncUncached ( source , chunkPath ) . then (
199
+ ( ) => void chunkCache . set ( chunkPath , loadedChunk )
200
+ )
196
201
chunkCache . set ( chunkPath , entry )
197
202
}
198
203
// TODO: Return an instrumented Promise that React can use instead of relying on referential equality.
0 commit comments