@@ -76,52 +76,74 @@ const createBundler = (esBuildUserOptions = {}) => {
7676 outfile : outputPath ,
7777 bundle : true ,
7878 }
79- // our plugin for new watch mode of esbuild 0.17.0
80- const customBuildPlugin = {
81- name : 'cypress-esbuild-watch-plugin' ,
82- setup ( build ) {
83- build . onEnd ( ( result ) => {
84- if ( result . errors . length > 0 ) {
85- debug (
86- 'watch on %s build failed, errors %o' ,
87- filePath ,
88- result . errors ,
89- )
90-
91- bundled [ filePath ] = new Promise ( ( resolve , reject ) => {
92- esbuild
93- . formatMessages ( result . errors , {
79+
80+ let isResolved = false
81+
82+ bundled [ filePath ] = new Promise ( ( resolve , reject ) => {
83+ // our plugin for new watch mode of esbuild 0.17.0
84+ const customBuildPlugin = {
85+ name : 'cypress-esbuild-watch-plugin' ,
86+ setup ( build ) {
87+ build . onEnd ( async ( result ) => {
88+ if ( result . errors . length > 0 ) {
89+ debug (
90+ 'watch on %s build failed, errors %o' ,
91+ filePath ,
92+ result . errors ,
93+ )
94+
95+ result = [
96+ undefined ,
97+ await esbuild . formatMessages ( result . errors , {
9498 kind : 'error' ,
9599 color : false ,
96- } )
97- . then ( ( messages ) => reject ( new Error ( messages . join ( '\n\n' ) ) ) )
98- } )
99-
100- file . emit ( 'rerun' )
101- return
102- }
103-
104- debug (
105- 'watch on %s build succeeded, warnings %o' ,
106- filePath ,
107- result . warnings ,
108- )
109- file . emit ( 'rerun' )
110- } )
111- } ,
112- }
113- const plugins = esBuildOptions . plugins ? [ ...esBuildOptions . plugins , customBuildPlugin ] : [ customBuildPlugin ] ;
114- bundled [ filePath ] = esbuild . context ( { ...esBuildOptions , plugins} ) . then ( ( watcher ) => {
115- watcher . watch ( ) . then ( ( _ ) => {
116- // when the test runner closes this spec
117- file . on ( 'close' , ( ) => {
118- debug ( 'file %s close, removing bundle promise' , filePath )
119- delete bundled [ filePath ]
120- watcher . dispose ( )
100+ } ) ,
101+ ]
102+ } else {
103+ result = [ outputPath , undefined ]
104+
105+ debug (
106+ 'watch on %s build succeeded, warnings %o' ,
107+ filePath ,
108+ result . warnings ,
109+ )
110+ }
111+
112+ if ( isResolved ) {
113+ debug ( 'rerun %s' , filePath )
114+ bundled [ filePath ] = result [ 0 ]
115+ ? Promise . resolve ( result [ 0 ] )
116+ : Promise . reject ( result [ 1 ] )
117+ file . emit ( 'rerun' )
118+ } else {
119+ if ( result [ 0 ] ) {
120+ debug ( 'resolving %s for the 1st time' , filePath )
121+ resolve ( result [ 0 ] )
122+ } else {
123+ debug ( 'rejecting %s for the 1st time' , filePath )
124+ reject ( result [ 1 ] )
125+ }
126+
127+ isResolved = true
128+ }
129+ } )
130+ } ,
131+ }
132+
133+ const plugins = esBuildOptions . plugins
134+ ? [ ...esBuildOptions . plugins , customBuildPlugin ]
135+ : [ customBuildPlugin ]
136+
137+ esbuild . context ( { ...esBuildOptions , plugins } ) . then ( ( watcher ) => {
138+ watcher . watch ( ) . then ( ( _ ) => {
139+ // when the test runner closes this spec
140+ file . on ( 'close' , ( ) => {
141+ debug ( 'file %s close, removing bundle promise' , filePath )
142+ delete bundled [ filePath ]
143+ watcher . dispose ( )
144+ } )
121145 } )
122146 } )
123-
124- return outputPath
125147 } )
126148
127149 return bundled [ filePath ]
0 commit comments