@@ -136,58 +136,61 @@ export class Bundler {
136136 }
137137
138138 const bundlingFile = util . trimSuffix ( mod . jsFile , '.js' ) + '.bundling.js'
139-
139+ this . #compiled . set ( mod . url , bundlingFile ) !
140140 if ( existsFileSync ( bundlingFile ) ) {
141141 return bundlingFile
142142 }
143143
144144 const source = await this . #app. readModule ( mod . url )
145145 if ( source === null ) {
146+ this . #compiled. delete ( mod . url ) !
146147 throw new Error ( `Unsupported module '${ mod . url } '` )
147148 }
148149
149- let { code, starExports } = await transform (
150- mod . url ,
151- source . code ,
152- {
153- ...this . #app. sharedCompileOptions ,
154- swcOptions : {
155- sourceType : source . type ,
156- } ,
157- bundleMode : true ,
158- bundleExternal : external ,
159- }
160- )
161-
162- if ( starExports && starExports . length > 0 ) {
163- for ( let index = 0 ; index < starExports . length ; index ++ ) {
164- const url = starExports [ index ]
165- const names = await this . #app. parseModuleExportNames ( url )
166- code = code . replace ( `export * from "[${ url } ]:` , `export {${ names . filter ( name => name !== 'default' ) . join ( ',' ) } } from "` )
167- code = code . replace ( `export const $$star_${ index } ` , `export const {${ names . filter ( name => name !== 'default' ) . join ( ',' ) } }` )
168- }
169- }
170-
171- this . #compiled. set ( mod . url , bundlingFile ) !
150+ try {
151+ let { code, starExports } = await transform (
152+ mod . url ,
153+ source . code ,
154+ {
155+ ...this . #app. sharedCompileOptions ,
156+ swcOptions : {
157+ sourceType : source . type ,
158+ } ,
159+ bundleMode : true ,
160+ bundleExternal : external ,
161+ }
162+ )
172163
173- // compile deps
174- for ( const dep of mod . deps ) {
175- if ( ! dep . url . startsWith ( '#' ) && ! external . includes ( dep . url ) ) {
176- const depMod = this . #app. getModule ( dep . url )
177- if ( depMod !== null ) {
178- await this . compile ( depMod , external )
164+ if ( starExports && starExports . length > 0 ) {
165+ for ( let index = 0 ; index < starExports . length ; index ++ ) {
166+ const url = starExports [ index ]
167+ const names = await this . #app. parseModuleExportNames ( url )
168+ code = code . replace ( `export * from "[ ${ url } ]:` , `export { ${ names . filter ( name => name !== 'default' ) . join ( ',' ) } } from "` )
169+ code = code . replace ( `export const $$star_ ${ index } ` , `export const { ${ names . filter ( name => name !== 'default' ) . join ( ',' ) } }` )
179170 }
180171 }
181- }
182172
183- await ensureTextFile ( bundlingFile , code )
173+ // compile deps
174+ await Promise . all ( mod . deps . map ( async dep => {
175+ if ( ! dep . url . startsWith ( '#' ) && ! external . includes ( dep . url ) ) {
176+ const depMod = this . #app. getModule ( dep . url )
177+ if ( depMod !== null ) {
178+ await this . compile ( depMod , external )
179+ }
180+ }
181+ } ) )
184182
185- return bundlingFile
183+ await ensureTextFile ( bundlingFile , code )
184+ return bundlingFile
185+ } catch ( e ) {
186+ this . #compiled. delete ( mod . url ) !
187+ throw new Error ( `Can't compile module '${ mod . url } ': ${ e . message } ` )
188+ }
186189 }
187190
188191 private async createMainJS ( ) {
189192 const bundled = Array . from ( this . #bundled. entries ( ) )
190- . filter ( ( [ name ] ) => ! [ 'polyfill ' , 'deps' , 'shared' ] . includes ( name ) )
193+ . filter ( ( [ name ] ) => ! [ 'polyfills ' , 'deps' , 'shared' ] . includes ( name ) )
191194 . reduce ( ( r , [ name , filename ] ) => {
192195 r [ name ] = filename
193196 return r
0 commit comments