@@ -28,6 +28,7 @@ type SSRModule = Record<string, any>
28
28
29
29
const pendingModules = new Map < string , Promise < SSRModule > > ( )
30
30
const pendingImports = new Map < string , string [ ] > ( )
31
+ const importErrors = new WeakMap < Error , { importee : string } > ( )
31
32
32
33
export async function ssrLoadModule (
33
34
url : string ,
@@ -131,32 +132,39 @@ async function instantiateModule(
131
132
const pendingDeps : string [ ] = [ ]
132
133
133
134
const ssrImport = async ( dep : string ) => {
134
- if ( dep [ 0 ] !== '.' && dep [ 0 ] !== '/' ) {
135
- return nodeImport ( dep , mod . file ! , resolveOptions )
136
- }
137
- // convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
138
- dep = unwrapId ( dep )
139
- if ( ! isCircular ( dep ) && ! pendingImports . get ( dep ) ?. some ( isCircular ) ) {
140
- pendingDeps . push ( dep )
141
- if ( pendingDeps . length === 1 ) {
142
- pendingImports . set ( url , pendingDeps )
135
+ try {
136
+ if ( dep [ 0 ] !== '.' && dep [ 0 ] !== '/' ) {
137
+ return await nodeImport ( dep , mod . file ! , resolveOptions )
143
138
}
144
- const mod = await ssrLoadModule (
145
- dep ,
146
- server ,
147
- context ,
148
- urlStack ,
149
- fixStacktrace ,
150
- )
151
- if ( pendingDeps . length === 1 ) {
152
- pendingImports . delete ( url )
153
- } else {
154
- pendingDeps . splice ( pendingDeps . indexOf ( dep ) , 1 )
139
+ // convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
140
+ dep = unwrapId ( dep )
141
+ if ( ! isCircular ( dep ) && ! pendingImports . get ( dep ) ?. some ( isCircular ) ) {
142
+ pendingDeps . push ( dep )
143
+ if ( pendingDeps . length === 1 ) {
144
+ pendingImports . set ( url , pendingDeps )
145
+ }
146
+ const mod = await ssrLoadModule (
147
+ dep ,
148
+ server ,
149
+ context ,
150
+ urlStack ,
151
+ fixStacktrace ,
152
+ )
153
+ if ( pendingDeps . length === 1 ) {
154
+ pendingImports . delete ( url )
155
+ } else {
156
+ pendingDeps . splice ( pendingDeps . indexOf ( dep ) , 1 )
157
+ }
158
+ // return local module to avoid race condition #5470
159
+ return mod
155
160
}
156
- // return local module to avoid race condition #5470
157
- return mod
161
+ return moduleGraph . urlToModuleMap . get ( dep ) ?. ssrModule
162
+ } catch ( err ) {
163
+ // tell external error handler which mod was imported with error
164
+ importErrors . set ( err , { importee : dep } )
165
+
166
+ throw err
158
167
}
159
- return moduleGraph . urlToModuleMap . get ( dep ) ?. ssrModule
160
168
}
161
169
162
170
const ssrDynamicImport = ( dep : string ) => {
@@ -204,6 +212,7 @@ async function instantiateModule(
204
212
)
205
213
} catch ( e ) {
206
214
mod . ssrError = e
215
+ const errorData = importErrors . get ( e )
207
216
208
217
if ( e . stack && fixStacktrace ) {
209
218
ssrFixStacktrace ( e , moduleGraph )
@@ -212,7 +221,10 @@ async function instantiateModule(
212
221
server . config . logger . error (
213
222
colors . red (
214
223
`Error when evaluating SSR module ${ url } :` +
215
- ( e . importee ? ` failed to import "${ e . importee } "\n` : '\n' ) ,
224
+ ( errorData ?. importee
225
+ ? ` failed to import "${ errorData . importee } "`
226
+ : '' ) +
227
+ `\n|- ${ e . stack } \n` ,
216
228
) ,
217
229
{
218
230
timestamp : true ,
@@ -221,7 +233,6 @@ async function instantiateModule(
221
233
} ,
222
234
)
223
235
224
- delete e . importee
225
236
throw e
226
237
}
227
238
@@ -262,15 +273,8 @@ async function nodeImport(
262
273
}
263
274
}
264
275
265
- try {
266
- const mod = await dynamicImport ( url )
267
- return proxyESM ( mod )
268
- } catch ( err ) {
269
- // tell external error handler which mod was imported with error
270
- err . importee = id
271
-
272
- throw err
273
- }
276
+ const mod = await dynamicImport ( url )
277
+ return proxyESM ( mod )
274
278
}
275
279
276
280
// rollup-style default import interop for cjs
0 commit comments