@@ -48,7 +48,6 @@ export class Kaba
48
48
private publicPath : string = "/assets/app/js/" ;
49
49
private externals : Externals = { } ;
50
50
private moduleConcatenationEnabled : boolean = false ;
51
- private plugins : webpack . Plugin [ ] = [ ] ;
52
51
private javaScriptDependenciesFileName : string = "_dependencies" ;
53
52
private hashFileNames : boolean = true ;
54
53
private buildModern : boolean = true ;
@@ -62,22 +61,6 @@ export class Kaba
62
61
{
63
62
this . cwd = process . cwd ( ) ;
64
63
this . libRoot = path . dirname ( __dirname ) ;
65
- this . plugins = [
66
- new ProgressBarPlugin ( {
67
- complete : green ( "─" ) ,
68
- incomplete : gray ( "─" ) ,
69
- width : 50 ,
70
- format : ` ${ cyan ( "build" ) } :bar ${ green ( ":percent" ) } ${ gray ( ":msg" ) } ` ,
71
- } ) ,
72
- new DuplicatePackageCheckerPlugin ( {
73
- emitError : true ,
74
- strict : true ,
75
- } ) ,
76
- new ProvidePlugin ( {
77
- h : [ "preact" , "h" ] ,
78
- Fragment : [ "preact" , "Fragment" ] ,
79
- } ) ,
80
- ] ;
81
64
82
65
// set defaults
83
66
this . setOutputPath ( "build" ) ;
@@ -274,12 +257,20 @@ export class Kaba
274
257
275
258
if ( Object . keys ( this . jsEntries ) . length )
276
259
{
260
+ const compilerConfigs : kaba . WebpackBuildConfig [ ] = [ ] ;
261
+
262
+ Object . keys ( this . jsEntries ) . forEach ( ( entry : string ) => {
263
+ compilerConfigs . push ( this . buildWebpackConfig ( entry , this . jsEntries [ entry ] , cliConfig , false ) ) ;
264
+
265
+ if ( this . buildModern )
266
+ {
267
+ compilerConfigs . push ( this . buildWebpackConfig ( entry , this . jsEntries [ entry ] , cliConfig , true ) ) ;
268
+ }
269
+ } ) ;
270
+
277
271
jsConfig = {
278
- common : this . buildWebpackCommon ( cliConfig ) ,
279
- module : this . buildModern
280
- ? this . buildWebpackConfig ( cliConfig , true )
281
- : null ,
282
- legacy : this . buildWebpackConfig ( cliConfig , false ) ,
272
+ watch : cliConfig . watch ,
273
+ configs : compilerConfigs ,
283
274
javaScriptDependenciesFileName : this . javaScriptDependenciesFileName ,
284
275
basePath : path . join ( this . outputPaths . base , this . outputPaths . js ) ,
285
276
} ;
@@ -299,11 +290,34 @@ export class Kaba
299
290
300
291
301
292
/**
302
- * Builds the common webpack config, that is common between legacy & module
293
+ * Builds the specialized webpack config for a legacy / module build
303
294
*/
304
- private buildWebpackCommon ( cliConfig : kaba . CliConfig ) : Partial < webpack . Configuration >
295
+ private buildWebpackConfig ( entry : string , entryFile : string , cliConfig : kaba . CliConfig , isModule : boolean ) : Partial < webpack . Configuration >
305
296
{
306
- const config : Partial < webpack . Configuration > = {
297
+ const babelLoader = {
298
+ loader : "babel-loader?cacheDirectory" ,
299
+ options : {
300
+ babelrc : false ,
301
+ presets : [
302
+ [ isModule ? kabaBabelPreset . modern : kabaBabelPreset . legacy ] ,
303
+ ] ,
304
+ } ,
305
+ } ;
306
+
307
+ let typeScriptConfig = path . join (
308
+ this . libRoot ,
309
+ "configs" ,
310
+ isModule ? "tsconfig.modern.json" : "tsconfig.legacy.json" ,
311
+ ) ;
312
+
313
+ const entryName = isModule ? `_modern.${ entry } ` : entry ;
314
+
315
+ let configTemplate = {
316
+ name : isModule ? "modern" : "legacy" ,
317
+ entry : {
318
+ [ entryName ] : entryFile ,
319
+ } ,
320
+
307
321
// mode
308
322
mode : cliConfig . debug ? "development" : "production" ,
309
323
@@ -328,9 +342,47 @@ export class Kaba
328
342
] ,
329
343
} ,
330
344
345
+ // output
346
+ output : {
347
+ path : path . join ( this . outputPaths . base , this . outputPaths . js , isModule ? "modern" : "legacy" ) ,
348
+ filename : this . hashFileNames ? '[name].[chunkhash].js' : '[name].js' ,
349
+ // the slash at the end is required of the public path entries
350
+ publicPath : path . join ( this . publicPath , isModule ? "modern/" : "legacy/" ) ,
351
+ pathinfo : cliConfig . debug ,
352
+ } ,
353
+
331
354
// module
332
355
module : {
333
- rules : [ ] ,
356
+ rules : [
357
+ // TypeScript
358
+ {
359
+ test : / \. t s x ? $ / ,
360
+ use : [
361
+ 'cache-loader' ,
362
+ babelLoader ,
363
+ {
364
+ loader : "ts-loader" ,
365
+ options : {
366
+ context : this . cwd ,
367
+ configFile : typeScriptConfig ,
368
+ errorFormatter : ( message , colors ) => typeScriptErrorFormatter ( message , colors , this . cwd ) ,
369
+ } ,
370
+ } ,
371
+ ] ,
372
+ } ,
373
+
374
+ // Babel
375
+ {
376
+ test : / \. m ? j s x ? $ / ,
377
+ use : [ 'cache-loader' , babelLoader ] ,
378
+ } ,
379
+
380
+ // content files
381
+ {
382
+ test : / \. ( s v g | t x t ) $ / ,
383
+ loader : "raw-loader" ,
384
+ } ,
385
+ ] as webpack . RuleSetRule [ ] ,
334
386
} ,
335
387
336
388
// optimization
@@ -339,8 +391,6 @@ export class Kaba
339
391
minimizer : [ ] ,
340
392
} ,
341
393
342
- // performance
343
-
344
394
// devtool (source maps)
345
395
devtool : cliConfig . debug
346
396
? "inline-cheap-source-map"
@@ -361,12 +411,35 @@ export class Kaba
361
411
stats : {
362
412
// hide children information (like from the ExtractTextPlugin)
363
413
children : false ,
414
+ hash : ! isModule ,
415
+ version : ! isModule ,
416
+ modules : ! isModule ,
364
417
} ,
365
418
366
- // devServer
367
-
368
419
// plugins
369
- plugins : this . plugins ,
420
+ plugins : [
421
+ new ProgressBarPlugin ( {
422
+ complete : green ( "─" ) ,
423
+ incomplete : gray ( "─" ) ,
424
+ width : 50 ,
425
+ format : ` ${ cyan ( "build" ) } :bar ${ green ( ":percent" ) } ${ gray ( ":msg" ) } ` ,
426
+ } ) ,
427
+ new DuplicatePackageCheckerPlugin ( {
428
+ emitError : true ,
429
+ strict : true ,
430
+ } ) ,
431
+ new ProvidePlugin ( {
432
+ h : [ "preact" , "h" ] ,
433
+ Fragment : [ "preact" , "Fragment" ] ,
434
+ } ) ,
435
+ new CleanWebpackPlugin ( ) ,
436
+ new DefinePlugin ( {
437
+ 'process.env.MODERN_BUILD' : isModule ,
438
+ 'MODERN_BUILD' : isModule ,
439
+ 'process.env.DEBUG' : cliConfig . debug ,
440
+ 'DEBUG' : cliConfig . debug ,
441
+ } ) ,
442
+ ] ,
370
443
371
444
// watch
372
445
watch : cliConfig . watch ,
@@ -375,11 +448,11 @@ export class Kaba
375
448
// don't automatically polyfill certain node libraries
376
449
// as we don't care about these implementations and they just add weight
377
450
node : this . nodeSettings ,
378
- } ;
451
+ } as Partial < webpack . Configuration > ;
379
452
380
453
if ( ! cliConfig . debug )
381
454
{
382
- ( config . optimization as any ) . minimizer . push ( new TerserPlugin ( {
455
+ ( configTemplate . optimization as any ) . minimizer . push ( new TerserPlugin ( {
383
456
cache : true ,
384
457
parallel : true ,
385
458
sourceMap : true ,
@@ -395,7 +468,7 @@ export class Kaba
395
468
try
396
469
{
397
470
const BundleAnalyzerPlugin = require ( 'webpack-bundle-analyzer' ) . BundleAnalyzerPlugin ;
398
- ( config . plugins as any [ ] ) . push ( new BundleAnalyzerPlugin ( ) ) ;
471
+ ( configTemplate . plugins as any [ ] ) . push ( new BundleAnalyzerPlugin ( ) ) ;
399
472
}
400
473
catch ( e )
401
474
{
@@ -416,119 +489,27 @@ export class Kaba
416
489
417
490
}
418
491
419
- return config ;
420
- }
421
-
422
-
423
- /**
424
- * Builds the specialized webpack config for a legacy / module build
425
- */
426
- private buildWebpackConfig ( cliConfig : kaba . CliConfig , isModule : boolean ) : Partial < webpack . Configuration >
427
- {
428
- const babelLoader = {
429
- loader : "babel-loader?cacheDirectory" ,
430
- options : {
431
- babelrc : false ,
432
- presets : [
433
- [ isModule ? kabaBabelPreset . modern : kabaBabelPreset . legacy ] ,
434
- ] ,
435
- } ,
436
- } ;
437
-
438
- let entries = this . jsEntries ;
439
-
440
- if ( isModule )
492
+ if ( ! isModule )
441
493
{
442
- entries = { } ;
443
- Object . keys ( this . jsEntries ) . forEach (
444
- entry =>
445
- {
446
- entries [ `_modern.${ entry } ` ] = this . jsEntries [ entry ] ;
494
+ ( configTemplate . module as webpack . Module ) . rules . push ( {
495
+ // ESLint
496
+ test : / \. m ? j s x ? $ / ,
497
+ // only lint files that are in the project dir & exclude tests, vendor and node_modules
498
+ include : ( path ) => path . startsWith ( this . cwd ) && ! / n o d e _ m o d u l e s | t e s t s | v e n d o r / . test ( path ) ,
499
+ loader : "eslint-loader" ,
500
+ options : {
501
+ cache : true ,
502
+ configFile : path . join ( this . libRoot , "configs/.eslintrc.yml" ) ,
503
+ fix : cliConfig . fix ,
504
+ parser : "babel-eslint" ,
505
+ quiet : ! cliConfig . lint ,
506
+ // always only emit a warning, so to actually never fail the webpack build
507
+ emitWarning : true ,
447
508
} ,
448
- ) ;
509
+ } ) ;
449
510
}
450
511
451
- let typeScriptConfig = path . join (
452
- this . libRoot ,
453
- "configs" ,
454
- isModule ? "tsconfig.modern.json" : "tsconfig.legacy.json" ,
455
- ) ;
456
-
457
- return {
458
- // entry
459
- entry : entries ,
460
-
461
- // output
462
- output : {
463
- path : path . join ( this . outputPaths . base , this . outputPaths . js , isModule ? "modern" : "legacy" ) ,
464
- filename : this . hashFileNames ? '[name].[chunkhash].js' : '[name].js' ,
465
- // the slash at the end is required of the public path entries
466
- publicPath : path . join ( this . publicPath , isModule ? "modern/" : "legacy/" ) ,
467
- pathinfo : cliConfig . debug ,
468
- } ,
469
-
470
- // module
471
- module : {
472
- rules : [
473
- // TypeScript
474
- {
475
- test : / \. t s x ? $ / ,
476
- use : [
477
- 'cache-loader' ,
478
- babelLoader ,
479
- {
480
- loader : "ts-loader" ,
481
- options : {
482
- context : this . cwd ,
483
- configFile : typeScriptConfig ,
484
- errorFormatter : ( message , colors ) => typeScriptErrorFormatter ( message , colors , this . cwd ) ,
485
- } ,
486
- } ,
487
- ] ,
488
- } ,
489
-
490
- // Babel
491
- {
492
- test : / \. m ? j s x ? $ / ,
493
- use : [ 'cache-loader' , babelLoader ] ,
494
- } ,
495
-
496
- // content files
497
- {
498
- test : / \. ( s v g | t x t ) $ / ,
499
- loader : "raw-loader" ,
500
- } ,
501
-
502
- // ESLint
503
- {
504
- test : / \. m ? j s x ? $ / ,
505
- // only lint files that are in the project dir & exclude tests, vendor and node_modules
506
- include : ( path ) => path . startsWith ( this . cwd ) && ! / n o d e _ m o d u l e s | t e s t s | v e n d o r / . test ( path ) ,
507
- loader : "eslint-loader" ,
508
- options : {
509
- cache : true ,
510
- configFile : path . join ( this . libRoot , "configs/.eslintrc.yml" ) ,
511
- fix : cliConfig . fix ,
512
- parser : "babel-eslint" ,
513
- quiet : ! cliConfig . lint ,
514
- // always only emit a warning, so to actually never fail the webpack build
515
- emitWarning : true ,
516
- } ,
517
- } ,
518
- ] ,
519
- } ,
520
-
521
- // plugins
522
- plugins : [
523
- new CleanWebpackPlugin ( ) ,
524
- new DefinePlugin ( {
525
- 'process.env.MODERN_BUILD' : isModule ,
526
- 'MODERN_BUILD' : isModule ,
527
- 'process.env.DEBUG' : cliConfig . debug ,
528
- 'DEBUG' : cliConfig . debug ,
529
- } ) ,
530
- ] ,
531
- } ;
512
+ return configTemplate ;
532
513
}
533
514
}
534
515
0 commit comments