@@ -136,58 +136,61 @@ export class Bundler {
136
136
}
137
137
138
138
const bundlingFile = util . trimSuffix ( mod . jsFile , '.js' ) + '.bundling.js'
139
-
139
+ this . #compiled . set ( mod . url , bundlingFile ) !
140
140
if ( existsFileSync ( bundlingFile ) ) {
141
141
return bundlingFile
142
142
}
143
143
144
144
const source = await this . #app. readModule ( mod . url )
145
145
if ( source === null ) {
146
+ this . #compiled. delete ( mod . url ) !
146
147
throw new Error ( `Unsupported module '${ mod . url } '` )
147
148
}
148
149
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
+ )
172
163
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 ( ',' ) } }` )
179
170
}
180
171
}
181
- }
182
172
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
+ } ) )
184
182
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
+ }
186
189
}
187
190
188
191
private async createMainJS ( ) {
189
192
const bundled = Array . from ( this . #bundled. entries ( ) )
190
- . filter ( ( [ name ] ) => ! [ 'polyfill ' , 'deps' , 'shared' ] . includes ( name ) )
193
+ . filter ( ( [ name ] ) => ! [ 'polyfills ' , 'deps' , 'shared' ] . includes ( name ) )
191
194
. reduce ( ( r , [ name , filename ] ) => {
192
195
r [ name ] = filename
193
196
return r
0 commit comments