@@ -33,11 +33,17 @@ class Bundler {
33
33
34
34
private bundlerOptions : BundlerOptions ;
35
35
36
- constructor ( generalConfig : GeneralConfig = { } , bundlerOptions : BundlerOptions ) {
36
+ constructor (
37
+ generalConfig : GeneralConfig = { } ,
38
+ bundlerOptions : BundlerOptions
39
+ ) {
37
40
this . entryPath = getEntryPath ( ) ;
38
41
this . generalConfig = generalConfig ;
39
42
40
- this . config = this . resolveConfig ( this . entryPath , generalConfig . config ?? { } ) ;
43
+ this . config = this . resolveConfig (
44
+ this . entryPath ,
45
+ generalConfig . config ?? { }
46
+ ) ;
41
47
42
48
this . bundlerOptions = bundlerOptions ;
43
49
}
@@ -49,7 +55,11 @@ class Bundler {
49
55
*/
50
56
private resolveRegex ( pattern : string | RegExp ) {
51
57
if ( isString ( pattern ) ) {
52
- return globToRegex ( pattern , { extended : true , globstar : true , flags : 'i' } ) ;
58
+ return globToRegex ( pattern , {
59
+ extended : true ,
60
+ globstar : true ,
61
+ flags : 'i' ,
62
+ } ) ;
53
63
} else {
54
64
return pattern ;
55
65
}
@@ -58,7 +68,7 @@ class Bundler {
58
68
private mergeConfig (
59
69
prop : keyof CommonConfig ,
60
70
config : Config ,
61
- target : CJSConfig | ESMConfig | DistConfig ,
71
+ target : CJSConfig | ESMConfig | DistConfig
62
72
) {
63
73
const configValue = config [ prop ] ;
64
74
const targetValue = target [ prop ] ;
@@ -131,7 +141,7 @@ class Bundler {
131
141
if ( err ) {
132
142
reject ( err ) ;
133
143
} else {
134
- resolve ( ) ;
144
+ resolve ( null ) ;
135
145
}
136
146
} ) ;
137
147
} ) ;
@@ -152,7 +162,7 @@ class Bundler {
152
162
entryFile : string ,
153
163
moduleName : string ,
154
164
currentRelativeDir : string ,
155
- extensions : string [ ] ,
165
+ extensions : string [ ]
156
166
) : Promise < Module [ ] > {
157
167
return new Promise ( ( resolve , reject ) => {
158
168
fs . readdir ( resolvedPath , ( err , files ) => {
@@ -171,25 +181,32 @@ class Bundler {
171
181
entryFile ,
172
182
moduleName ,
173
183
path . join ( currentRelativeDir , file ) ,
174
- extensions ,
175
- ) ,
184
+ extensions
185
+ )
176
186
) ;
177
187
} else {
178
188
const firstDotPos =
179
189
file . charAt ( 0 ) === '.' ? file . indexOf ( '.' , 1 ) : file . indexOf ( '.' ) ;
180
- const baseName = firstDotPos > - 1 ? file . substring ( 0 , firstDotPos ) : file ;
190
+ const baseName =
191
+ firstDotPos > - 1 ? file . substring ( 0 , firstDotPos ) : file ;
181
192
const extname = firstDotPos > - 1 ? file . substring ( firstDotPos ) : '' ;
182
193
183
194
const oldRelativePath = path . join ( currentRelativeDir , file ) ;
184
- const newRelativePath = path . join ( currentRelativeDir , baseName + '.js' ) ;
195
+ const newRelativePath = path . join (
196
+ currentRelativeDir ,
197
+ baseName + '.js'
198
+ ) ;
185
199
186
200
modules . push ( {
187
201
id : modules . length + 1 ,
188
202
ext : extname ,
189
203
oldRelativePath,
190
204
newRelativePath,
191
205
filePath,
192
- name : oldRelativePath === entryFile ? moduleName : camelCase ( baseName ) ,
206
+ name :
207
+ oldRelativePath === entryFile
208
+ ? moduleName
209
+ : camelCase ( baseName ) ,
193
210
isBuildFile : extensions . includes ( extname ) ,
194
211
} ) ;
195
212
}
@@ -207,7 +224,7 @@ class Bundler {
207
224
private getModulesFiles (
208
225
modules : Module [ ] ,
209
226
config : Config ,
210
- buildConfig : CJSConfig | ESMConfig | DistConfig ,
227
+ buildConfig : CJSConfig | ESMConfig | DistConfig
211
228
) : ModuleFiles {
212
229
const result : ModuleFiles = {
213
230
assetFiles : [ ] ,
@@ -232,8 +249,10 @@ class Bundler {
232
249
result . assetFiles . push ( current ) ;
233
250
} else if (
234
251
isBuildFile &&
235
- ( buildConfig . include . length === 0 || buildConfig . include . some ( regexMatches ) ) &&
236
- ( buildConfig . exclude . length === 0 || ! buildConfig . exclude . some ( regexMatches ) )
252
+ ( buildConfig . include . length === 0 ||
253
+ buildConfig . include . some ( regexMatches ) ) &&
254
+ ( buildConfig . exclude . length === 0 ||
255
+ ! buildConfig . exclude . some ( regexMatches ) )
237
256
) {
238
257
result . buildFiles . push ( current ) ;
239
258
}
@@ -250,7 +269,7 @@ class Bundler {
250
269
runBuild (
251
270
modules : Module [ ] ,
252
271
mainConfig : Config ,
253
- config : DistConfig | CJSConfig | ESMConfig ,
272
+ config : DistConfig | CJSConfig | ESMConfig
254
273
) {
255
274
const moduleFiles = this . getModulesFiles ( modules , mainConfig , config ) ;
256
275
const promises : Array < Promise < any > > = [ ] ;
@@ -264,58 +283,63 @@ class Bundler {
264
283
? config . externals
265
284
: allExternal ;
266
285
267
- // const onWarn = (warning, warn) => {
268
- // console.log(warning.message);
269
- // warn(warning);
270
- // };
271
-
272
286
const onError = ( ex ) => {
273
287
console . error ( ex ?. message || ex ) ;
274
288
} ;
275
289
276
- buildFiles . forEach ( ( { filePath, newRelativePath, oldRelativePath, name } ) => {
277
- const out = path . resolve ( this . entryPath , config . outDir , newRelativePath ) ;
278
- promises . push (
279
- rollup ( {
280
- input : filePath ,
281
- plugins,
282
- external,
283
- onwarn : ( warning , warn ) => console . log ( warning . message , filePath ) ,
284
- } )
285
- . then ( ( bundler ) =>
286
- bundler . write ( {
287
- file : out ,
288
- format : config . format ,
289
- interop : config . interop ,
290
- sourcemap : config . sourcemap ,
291
- name,
292
- } ) ,
293
- )
294
- . then ( ( ) => {
295
- if ( this . bundlerOptions . generateOutputLogs ) {
296
- log ( chalk . green ( `${ oldRelativePath } ... ${ out } \n` ) ) ;
297
- }
298
- return null ;
290
+ buildFiles . forEach (
291
+ ( { filePath, newRelativePath, oldRelativePath, name } ) => {
292
+ const out = path . resolve (
293
+ this . entryPath ,
294
+ config . outDir ,
295
+ newRelativePath
296
+ ) ;
297
+ promises . push (
298
+ rollup ( {
299
+ input : filePath ,
300
+ plugins,
301
+ external,
302
+ onwarn : ( warning , warn ) => console . log ( warning . message , filePath ) ,
299
303
} )
300
- . catch ( onError ) ,
301
- ) ;
302
- } ) ;
304
+ . then ( ( bundler ) =>
305
+ bundler . write ( {
306
+ file : out ,
307
+ format : config . format ,
308
+ interop : config . interop ,
309
+ sourcemap : config . sourcemap ,
310
+ name,
311
+ } )
312
+ )
313
+ . then ( ( ) => {
314
+ if ( this . bundlerOptions . generateOutputLogs ) {
315
+ log ( chalk . green ( `${ oldRelativePath } ... ${ out } \n` ) ) ;
316
+ }
317
+ return null ;
318
+ } )
319
+ . catch ( onError )
320
+ ) ;
321
+ }
322
+ ) ;
303
323
304
324
assetFiles . forEach ( ( assetFile ) => {
305
325
promises . push (
306
326
this . copyFile (
307
327
assetFile . filePath ,
308
- path . resolve ( this . entryPath , config . outDir , assetFile . oldRelativePath ) ,
309
- ) ,
328
+ path . resolve ( this . entryPath , config . outDir , assetFile . oldRelativePath )
329
+ )
310
330
) ;
311
331
} ) ;
312
332
313
333
typeDefinitionFiles . forEach ( ( typeDefinitionFile ) => {
314
334
promises . push (
315
335
this . copyFile (
316
336
typeDefinitionFile . filePath ,
317
- path . resolve ( this . entryPath , config . outDir , typeDefinitionFile . oldRelativePath ) ,
318
- ) ,
337
+ path . resolve (
338
+ this . entryPath ,
339
+ config . outDir ,
340
+ typeDefinitionFile . oldRelativePath
341
+ )
342
+ )
319
343
) ;
320
344
} ) ;
321
345
@@ -335,7 +359,7 @@ class Bundler {
335
359
config . entryFile ,
336
360
config . moduleName ,
337
361
'' ,
338
- config . extensions ,
362
+ config . extensions
339
363
) ;
340
364
341
365
//run cjs build
0 commit comments