Skip to content

Commit fda837c

Browse files
committed
define build config in a single place
1 parent ba6efce commit fda837c

File tree

3 files changed

+96
-141
lines changed

3 files changed

+96
-141
lines changed

src/@types/kaba.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ declare namespace kaba
2626
{
2727
sass: SassBuildConfig;
2828
js?: {
29-
common: WebpackBuildConfig;
29+
watch: boolean,
3030
configs: WebpackBuildConfig[];
3131
javaScriptDependenciesFileName: string;
3232
basePath: string;

src/Kaba.ts

+87-108
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export class Kaba
7777
h: ["preact", "h"],
7878
Fragment: ["preact", "Fragment"],
7979
}),
80+
new CleanWebpackPlugin(),
8081
];
8182

8283
// set defaults
@@ -277,20 +278,16 @@ export class Kaba
277278
const compilerConfigs: kaba.WebpackBuildConfig[] = [];
278279

279280
Object.keys(this.jsEntries).forEach((entry: string) => {
281+
compilerConfigs.push(this.buildWebpackConfig(entry, this.jsEntries[entry], cliConfig, false));
282+
280283
if (this.buildModern)
281284
{
282285
compilerConfigs.push(this.buildWebpackConfig(entry, this.jsEntries[entry], cliConfig, true));
283286
}
284287
});
285288

286-
// The log output gets really big with multiple entry files.
287-
// Therefore the linted (legacy) entries are added after the modern ones.
288-
Object.keys(this.jsEntries).forEach((entry: string) => {
289-
compilerConfigs.push(this.buildWebpackConfig(entry, this.jsEntries[entry], cliConfig, false));
290-
});
291-
292289
jsConfig = {
293-
common: this.buildWebpackCommon(cliConfig),
290+
watch: cliConfig.watch,
294291
configs: compilerConfigs,
295292
javaScriptDependenciesFileName: this.javaScriptDependenciesFileName,
296293
basePath: path.join(this.outputPaths.base, this.outputPaths.js),
@@ -311,11 +308,34 @@ export class Kaba
311308

312309

313310
/**
314-
* Builds the common webpack config, that is common between legacy & module
311+
* Builds the specialized webpack config for a legacy / module build
315312
*/
316-
private buildWebpackCommon (cliConfig: kaba.CliConfig): Partial<webpack.Configuration>
313+
private buildWebpackConfig (entry: string, entryFile: string, cliConfig: kaba.CliConfig, isModule: boolean): Partial<webpack.Configuration>
317314
{
318-
const config: Partial<webpack.Configuration> = {
315+
const babelLoader = {
316+
loader: "babel-loader?cacheDirectory",
317+
options: {
318+
babelrc: false,
319+
presets: [
320+
[isModule ? kabaBabelPreset.modern : kabaBabelPreset.legacy],
321+
],
322+
},
323+
};
324+
325+
let typeScriptConfig = path.join(
326+
this.libRoot,
327+
"configs",
328+
isModule ? "tsconfig.modern.json" : "tsconfig.legacy.json",
329+
);
330+
331+
const entryName = isModule ? `_modern.${entry}` : entry;
332+
333+
let configTemplate = {
334+
name: isModule ? "modern" : "legacy",
335+
entry: {
336+
[entryName]: entryFile,
337+
},
338+
319339
// mode
320340
mode: cliConfig.debug ? "development" : "production",
321341

@@ -340,9 +360,47 @@ export class Kaba
340360
],
341361
},
342362

363+
// output
364+
output: {
365+
path: path.join(this.outputPaths.base, this.outputPaths.js, isModule ? "modern" : "legacy"),
366+
filename: this.hashFileNames ? '[name].[chunkhash].js' : '[name].js',
367+
// the slash at the end is required of the public path entries
368+
publicPath: path.join(this.publicPath, isModule ? "modern/" : "legacy/"),
369+
pathinfo: cliConfig.debug,
370+
},
371+
343372
// module
344373
module: {
345-
rules: [],
374+
rules: [
375+
// TypeScript
376+
{
377+
test: /\.tsx?$/,
378+
use: [
379+
'cache-loader',
380+
babelLoader,
381+
{
382+
loader: "ts-loader",
383+
options: {
384+
context: this.cwd,
385+
configFile: typeScriptConfig,
386+
errorFormatter: (message, colors) => typeScriptErrorFormatter(message, colors, this.cwd),
387+
},
388+
},
389+
],
390+
},
391+
392+
// Babel
393+
{
394+
test: /\.m?jsx?$/,
395+
use: ['cache-loader', babelLoader],
396+
},
397+
398+
// content files
399+
{
400+
test: /\.(svg|txt)$/,
401+
loader: "raw-loader",
402+
},
403+
] as webpack.RuleSetRule[],
346404
},
347405

348406
// optimization
@@ -351,8 +409,6 @@ export class Kaba
351409
minimizer: [],
352410
},
353411

354-
// performance
355-
356412
// devtool (source maps)
357413
devtool: cliConfig.debug
358414
? "inline-cheap-source-map"
@@ -373,10 +429,11 @@ export class Kaba
373429
stats: {
374430
// hide children information (like from the ExtractTextPlugin)
375431
children: false,
432+
hash: !isModule,
433+
version: !isModule,
434+
modules: !isModule,
376435
},
377436

378-
// devServer
379-
380437
// plugins
381438
plugins: this.plugins,
382439

@@ -387,11 +444,20 @@ export class Kaba
387444
// don't automatically polyfill certain node libraries
388445
// as we don't care about these implementations and they just add weight
389446
node: this.nodeSettings,
390-
};
447+
} as Partial<webpack.Configuration>;
448+
449+
(configTemplate.plugins as webpack.Plugin[]).push(
450+
new DefinePlugin({
451+
'process.env.MODERN_BUILD': isModule,
452+
'MODERN_BUILD': isModule,
453+
'process.env.DEBUG': cliConfig.debug,
454+
'DEBUG': cliConfig.debug,
455+
})
456+
);
391457

392458
if (!cliConfig.debug)
393459
{
394-
(config.optimization as any).minimizer.push(new TerserPlugin({
460+
(configTemplate.optimization as any).minimizer.push(new TerserPlugin({
395461
cache: true,
396462
parallel: true,
397463
sourceMap: true,
@@ -407,7 +473,7 @@ export class Kaba
407473
try
408474
{
409475
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
410-
(config.plugins as any[]).push(new BundleAnalyzerPlugin());
476+
(configTemplate.plugins as any[]).push(new BundleAnalyzerPlugin());
411477
}
412478
catch (e)
413479
{
@@ -428,96 +494,9 @@ export class Kaba
428494

429495
}
430496

431-
return config;
432-
}
433-
434-
435-
/**
436-
* Builds the specialized webpack config for a legacy / module build
437-
*/
438-
private buildWebpackConfig (entry: string, entryFile: string, cliConfig: kaba.CliConfig, isModule: boolean): Partial<webpack.Configuration>
439-
{
440-
const babelLoader = {
441-
loader: "babel-loader?cacheDirectory",
442-
options: {
443-
babelrc: false,
444-
presets: [
445-
[isModule ? kabaBabelPreset.modern : kabaBabelPreset.legacy],
446-
],
447-
},
448-
};
449-
450-
let typeScriptConfig = path.join(
451-
this.libRoot,
452-
"configs",
453-
isModule ? "tsconfig.modern.json" : "tsconfig.legacy.json",
454-
);
455-
456-
const entryObjKey = isModule ? `_modern.${entry}` : entry;
457-
458-
let configTemplate = {
459-
name: isModule ? "modern" : "legacy",
460-
entry: {
461-
[entryObjKey]: entryFile,
462-
},
463-
464-
// output
465-
output: {
466-
path: path.join(this.outputPaths.base, this.outputPaths.js, isModule ? "modern" : "legacy"),
467-
filename: this.hashFileNames ? '[name].[chunkhash].js' : '[name].js',
468-
// the slash at the end is required of the public path entries
469-
publicPath: path.join(this.publicPath, isModule ? "modern/" : "legacy/"),
470-
pathinfo: cliConfig.debug,
471-
},
472-
473-
// module
474-
module: {
475-
rules: [
476-
// TypeScript
477-
{
478-
test: /\.tsx?$/,
479-
use: [
480-
'cache-loader',
481-
babelLoader,
482-
{
483-
loader: "ts-loader",
484-
options: {
485-
context: this.cwd,
486-
configFile: typeScriptConfig,
487-
errorFormatter: (message, colors) => typeScriptErrorFormatter(message, colors, this.cwd),
488-
},
489-
},
490-
],
491-
},
492-
493-
// Babel
494-
{
495-
test: /\.m?jsx?$/,
496-
use: ['cache-loader', babelLoader],
497-
},
498-
499-
// content files
500-
{
501-
test: /\.(svg|txt)$/,
502-
loader: "raw-loader",
503-
},
504-
] as webpack.RuleSetRule[],
505-
},
506-
507-
// plugins
508-
plugins: [
509-
new CleanWebpackPlugin(),
510-
new DefinePlugin({
511-
'process.env.MODERN_BUILD': isModule,
512-
'MODERN_BUILD': isModule,
513-
'process.env.DEBUG': cliConfig.debug,
514-
'DEBUG': cliConfig.debug,
515-
}),
516-
],
517-
};
518-
519-
if (!isModule) {
520-
configTemplate.module.rules.push({
497+
if (!isModule)
498+
{
499+
(configTemplate.module as webpack.Module).rules.push({
521500
// ESLint
522501
test: /\.m?jsx?$/,
523502
// only lint files that are in the project dir & exclude tests, vendor and node_modules

src/runner/WebpackRunner.ts

+8-32
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,14 @@ export class WebpackRunner
4040
this.logger.log("Launching webpack...");
4141
const start = process.hrtime();
4242

43-
if (this.buildConfig.js.common.watch)
43+
if (this.buildConfig.js.watch)
4444
{
4545
this.resolveCallback = resolve;
4646
}
4747

48-
let configs: webpack.Configuration[] = [];
48+
let compiler = webpack(this.buildConfig.js.configs) as webpack.MultiCompiler;
4949

50-
51-
this.buildConfig.js.configs.forEach(config =>
52-
{
53-
// For some reason TypeScript thinks that `this.buildConfig.js` might become `undefined`, even though we have this check already in line 34
54-
if (!this.buildConfig.js)
55-
{
56-
return;
57-
}
58-
59-
if (undefined !== config.plugins && this.buildConfig.js.common.plugins)
60-
{
61-
config.plugins.push(...this.buildConfig.js.common.plugins)
62-
}
63-
64-
configs.push(
65-
Object.assign(
66-
{},
67-
this.buildConfig.js.common,
68-
config
69-
)
70-
);
71-
});
72-
73-
let compiler = webpack(configs) as webpack.MultiCompiler;
74-
75-
if (this.buildConfig.js.common.watch)
50+
if (this.buildConfig.js.watch)
7651
{
7752
this.resolveCallback = resolve;
7853

@@ -113,12 +88,13 @@ export class WebpackRunner
11388
stats.stats.forEach(
11489
singleStats =>
11590
{
116-
console.log("");
11791
let type = singleStats.compilation.compiler.options.name as string;
92+
let statConfig = singleStats.compilation.compiler.options.stats as any;
93+
statConfig.colors = true;
94+
95+
console.log("");
11896
console.log(`${bgCyan(black(" webpack "))} ${cyan(type)}`);
119-
console.log(singleStats.toString({
120-
colors: true,
121-
}));
97+
console.log(singleStats.toString(statConfig));
12298
}
12399
);
124100

0 commit comments

Comments
 (0)