@@ -123,17 +123,17 @@ export function scanImports(environment: ScanEnvironment): {
123
123
} >
124
124
} {
125
125
const start = performance . now ( )
126
- const deps : Record < string , string > = { }
127
- const missing : Record < string , string > = { }
128
- let entries : string [ ]
129
-
130
126
const { config } = environment
127
+
131
128
const scanContext = { cancelled : false }
132
- const esbuildContext : Promise < BuildContext | undefined > = computeEntries (
133
- environment ,
134
- ) . then ( ( computedEntries ) => {
135
- entries = computedEntries
129
+ let esbuildContext : Promise < BuildContext | undefined > | undefined
130
+ async function cancel ( ) {
131
+ scanContext . cancelled = true
132
+ return esbuildContext ?. then ( ( context ) => context ?. cancel ( ) )
133
+ }
136
134
135
+ async function scan ( ) {
136
+ const entries = await computeEntries ( environment )
137
137
if ( ! entries . length ) {
138
138
if ( ! config . optimizeDeps . entries && ! config . optimizeDeps . include ) {
139
139
environment . logger . warn (
@@ -153,82 +153,73 @@ export function scanImports(environment: ScanEnvironment): {
153
153
. map ( ( entry ) => `\n ${ colors . dim ( entry ) } ` )
154
154
. join ( '' ) } `,
155
155
)
156
- return prepareEsbuildScanner (
157
- environment ,
158
- entries ,
159
- deps ,
160
- missing ,
161
- scanContext ,
162
- )
163
- } )
156
+ const deps : Record < string , string > = { }
157
+ const missing : Record < string , string > = { }
158
+
159
+ let context : BuildContext | undefined
160
+ try {
161
+ esbuildContext = prepareEsbuildScanner (
162
+ environment ,
163
+ entries ,
164
+ deps ,
165
+ missing ,
166
+ )
167
+ context = await esbuildContext
168
+ if ( scanContext . cancelled ) return
164
169
165
- const result = esbuildContext
166
- . then ( ( context ) => {
167
- function disposeContext ( ) {
168
- return context ?. dispose ( ) . catch ( ( e ) => {
169
- environment . logger . error ( 'Failed to dispose esbuild context' , {
170
- error : e ,
171
- } )
172
- } )
173
- }
174
- if ( ! context || scanContext . cancelled ) {
175
- disposeContext ( )
176
- return { deps : { } , missing : { } }
177
- }
178
- return context
179
- . rebuild ( )
180
- . then ( ( ) => {
181
- return {
182
- // Ensure a fixed order so hashes are stable and improve logs
183
- deps : orderedDependencies ( deps ) ,
184
- missing,
185
- }
186
- } )
187
- . finally ( ( ) => {
188
- return disposeContext ( )
189
- } )
190
- } )
191
- . catch ( async ( e ) => {
192
- if ( e . errors && e . message . includes ( 'The build was canceled' ) ) {
193
- // esbuild logs an error when cancelling, but this is expected so
194
- // return an empty result instead
195
- return { deps : { } , missing : { } }
196
- }
170
+ try {
171
+ await context ! . rebuild ( )
172
+ return {
173
+ // Ensure a fixed order so hashes are stable and improve logs
174
+ deps : orderedDependencies ( deps ) ,
175
+ missing,
176
+ }
177
+ } catch ( e ) {
178
+ if ( e . errors && e . message . includes ( 'The build was canceled' ) ) {
179
+ // esbuild logs an error when cancelling, but this is expected so
180
+ // return an empty result instead
181
+ return
182
+ }
197
183
198
- const prependMessage = colors . red ( `\
184
+ const prependMessage = colors . red ( `\
199
185
Failed to scan for dependencies from entries:
200
186
${ entries . join ( '\n' ) }
201
187
202
188
` )
203
- if ( e . errors ) {
204
- const msgs = await formatMessages ( e . errors , {
205
- kind : 'error' ,
206
- color : true ,
207
- } )
208
- e . message = prependMessage + msgs . join ( '\n' )
209
- } else {
210
- e . message = prependMessage + e . message
211
- }
212
- throw e
213
- } )
214
- . finally ( ( ) => {
215
- if ( debug ) {
216
- const duration = ( performance . now ( ) - start ) . toFixed ( 2 )
217
- const depsStr =
218
- Object . keys ( orderedDependencies ( deps ) )
219
- . sort ( )
220
- . map ( ( id ) => `\n ${ colors . cyan ( id ) } -> ${ colors . dim ( deps [ id ] ) } ` )
221
- . join ( '' ) || colors . dim ( 'no dependencies found' )
222
- debug ( `Scan completed in ${ duration } ms: ${ depsStr } ` )
189
+ if ( e . errors ) {
190
+ const msgs = await formatMessages ( e . errors , {
191
+ kind : 'error' ,
192
+ color : true ,
193
+ } )
194
+ e . message = prependMessage + msgs . join ( '\n' )
195
+ } else {
196
+ e . message = prependMessage + e . message
197
+ }
198
+ throw e
199
+ } finally {
200
+ if ( debug ) {
201
+ const duration = ( performance . now ( ) - start ) . toFixed ( 2 )
202
+ const depsStr =
203
+ Object . keys ( orderedDependencies ( deps ) )
204
+ . sort ( )
205
+ . map ( ( id ) => `\n ${ colors . cyan ( id ) } -> ${ colors . dim ( deps [ id ] ) } ` )
206
+ . join ( '' ) || colors . dim ( 'no dependencies found' )
207
+ debug ( `Scan completed in ${ duration } ms: ${ depsStr } ` )
208
+ }
223
209
}
224
- } )
210
+ } finally {
211
+ context ?. dispose ( ) . catch ( ( e ) => {
212
+ environment . logger . error ( 'Failed to dispose esbuild context' , {
213
+ error : e ,
214
+ } )
215
+ } )
216
+ }
217
+ }
218
+ const result = scan ( )
225
219
226
220
return {
227
- cancel : async ( ) => {
228
- scanContext . cancelled = true
229
- return esbuildContext . then ( ( context ) => context ?. cancel ( ) )
230
- } ,
231
- result,
221
+ cancel,
222
+ result : result . then ( ( res ) => res ?? { deps : { } , missing : { } } ) ,
232
223
}
233
224
}
234
225
@@ -283,10 +274,7 @@ async function prepareEsbuildScanner(
283
274
entries : string [ ] ,
284
275
deps : Record < string , string > ,
285
276
missing : Record < string , string > ,
286
- scanContext : { cancelled : boolean } ,
287
- ) : Promise < BuildContext | undefined > {
288
- if ( scanContext . cancelled ) return
289
-
277
+ ) : Promise < BuildContext > {
290
278
const plugin = esbuildScanPlugin ( environment , deps , missing , entries )
291
279
292
280
const { plugins = [ ] , ...esbuildOptions } =
0 commit comments