@@ -123,17 +123,17 @@ export function scanImports(environment: ScanEnvironment): {
123123 } >
124124} {
125125 const start = performance . now ( )
126- const deps : Record < string , string > = { }
127- const missing : Record < string , string > = { }
128- let entries : string [ ]
129-
130126 const { config } = environment
127+
131128 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+ }
136134
135+ async function scan ( ) {
136+ const entries = await computeEntries ( environment )
137137 if ( ! entries . length ) {
138138 if ( ! config . optimizeDeps . entries && ! config . optimizeDeps . include ) {
139139 environment . logger . warn (
@@ -153,82 +153,73 @@ export function scanImports(environment: ScanEnvironment): {
153153 . map ( ( entry ) => `\n ${ colors . dim ( entry ) } ` )
154154 . join ( '' ) } `,
155155 )
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
164169
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+ }
197183
198- const prependMessage = colors . red ( `\
184+ const prependMessage = colors . red ( `\
199185 Failed to scan for dependencies from entries:
200186 ${ entries . join ( '\n' ) }
201187
202188 ` )
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+ }
223209 }
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 ( )
225219
226220 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 : { } } ) ,
232223 }
233224}
234225
@@ -283,10 +274,7 @@ async function prepareEsbuildScanner(
283274 entries : string [ ] ,
284275 deps : Record < string , string > ,
285276 missing : Record < string , string > ,
286- scanContext : { cancelled : boolean } ,
287- ) : Promise < BuildContext | undefined > {
288- if ( scanContext . cancelled ) return
289-
277+ ) : Promise < BuildContext > {
290278 const plugin = esbuildScanPlugin ( environment , deps , missing , entries )
291279
292280 const { plugins = [ ] , ...esbuildOptions } =
0 commit comments