Skip to content

Commit ba6efce

Browse files
committed
combine modern and legacy build configs into one config array
1 parent be395f3 commit ba6efce

File tree

3 files changed

+61
-100
lines changed

3 files changed

+61
-100
lines changed

src/@types/kaba.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ declare namespace kaba
2727
sass: SassBuildConfig;
2828
js?: {
2929
common: WebpackBuildConfig;
30-
module: WebpackBuildConfig[]|null;
31-
legacy: WebpackBuildConfig;
30+
configs: WebpackBuildConfig[];
3231
javaScriptDependenciesFileName: string;
3332
basePath: string;
3433
};

src/Kaba.ts

+43-56
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,24 @@ export class Kaba
274274

275275
if (Object.keys(this.jsEntries).length)
276276
{
277+
const compilerConfigs: kaba.WebpackBuildConfig[] = [];
278+
279+
Object.keys(this.jsEntries).forEach((entry: string) => {
280+
if (this.buildModern)
281+
{
282+
compilerConfigs.push(this.buildWebpackConfig(entry, this.jsEntries[entry], cliConfig, true));
283+
}
284+
});
285+
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+
277292
jsConfig = {
278293
common: this.buildWebpackCommon(cliConfig),
279-
module: this.buildModern
280-
? this.buildWebpackConfigs(cliConfig, true)
281-
: null,
282-
legacy: this.buildWebpackConfigs(cliConfig, false),
294+
configs: compilerConfigs,
283295
javaScriptDependenciesFileName: this.javaScriptDependenciesFileName,
284296
basePath: path.join(this.outputPaths.base, this.outputPaths.js),
285297
};
@@ -423,7 +435,7 @@ export class Kaba
423435
/**
424436
* Builds the specialized webpack config for a legacy / module build
425437
*/
426-
private buildWebpackConfigs (cliConfig: kaba.CliConfig, isModule: boolean): Partial<webpack.Configuration>[]
438+
private buildWebpackConfig (entry: string, entryFile: string, cliConfig: kaba.CliConfig, isModule: boolean): Partial<webpack.Configuration>
427439
{
428440
const babelLoader = {
429441
loader: "babel-loader?cacheDirectory",
@@ -435,26 +447,20 @@ export class Kaba
435447
},
436448
};
437449

438-
let entries = this.jsEntries;
439-
440-
if (isModule)
441-
{
442-
entries = {};
443-
Object.keys(this.jsEntries).forEach(
444-
entry =>
445-
{
446-
entries[`_modern.${entry}`] = this.jsEntries[entry];
447-
},
448-
);
449-
}
450-
451450
let typeScriptConfig = path.join(
452451
this.libRoot,
453452
"configs",
454453
isModule ? "tsconfig.modern.json" : "tsconfig.legacy.json",
455454
);
456455

456+
const entryObjKey = isModule ? `_modern.${entry}` : entry;
457+
457458
let configTemplate = {
459+
name: isModule ? "modern" : "legacy",
460+
entry: {
461+
[entryObjKey]: entryFile,
462+
},
463+
458464
// output
459465
output: {
460466
path: path.join(this.outputPaths.base, this.outputPaths.js, isModule ? "modern" : "legacy"),
@@ -495,7 +501,7 @@ export class Kaba
495501
test: /\.(svg|txt)$/,
496502
loader: "raw-loader",
497503
},
498-
],
504+
] as webpack.RuleSetRule[],
499505
},
500506

501507
// plugins
@@ -510,45 +516,26 @@ export class Kaba
510516
],
511517
};
512518

513-
return Object.keys(entries).map(entryFile =>
514-
{
515-
let config = Object.assign({}, configTemplate, {
516-
entry: {
517-
[entryFile]: entries[entryFile],
519+
if (!isModule) {
520+
configTemplate.module.rules.push({
521+
// ESLint
522+
test: /\.m?jsx?$/,
523+
// only lint files that are in the project dir & exclude tests, vendor and node_modules
524+
include: (path) => path.startsWith(this.cwd) && !/node_modules|tests|vendor/.test(path),
525+
loader: "eslint-loader",
526+
options: {
527+
cache: true,
528+
configFile: path.join(this.libRoot, "configs/.eslintrc.yml"),
529+
fix: cliConfig.fix,
530+
parser: "babel-eslint",
531+
quiet: !cliConfig.lint,
532+
// always only emit a warning, so to actually never fail the webpack build
533+
emitWarning: true,
518534
},
519-
}) as Partial<webpack.Configuration>;
520-
521-
if (!isModule && undefined !== config.module)
522-
{
523-
config.module = Object.assign({}, config.module);
524-
let rules: webpack.RuleSetRule[] = [];
525-
526-
config.module.rules.forEach(rule => {
527-
rules.push(rule);
528-
});
529-
530-
rules.push({
531-
// ESLint
532-
test: /\.m?jsx?$/,
533-
// only lint files that are in the project dir & exclude tests, vendor and node_modules
534-
include: (path) => path.startsWith(this.cwd) && !/node_modules|tests|vendor/.test(path),
535-
loader: "eslint-loader",
536-
options: {
537-
cache: true,
538-
configFile: path.join(this.libRoot, "configs/.eslintrc.yml"),
539-
fix: cliConfig.fix,
540-
parser: "babel-eslint",
541-
quiet: !cliConfig.lint,
542-
// always only emit a warning, so to actually never fail the webpack build
543-
emitWarning: true,
544-
},
545-
});
546-
547-
config.module.rules = rules;
548-
}
535+
});
536+
}
549537

550-
return config;
551-
});
538+
return configTemplate;
552539
}
553540
}
554541

src/runner/WebpackRunner.ts

+17-42
Original file line numberDiff line numberDiff line change
@@ -47,53 +47,28 @@ export class WebpackRunner
4747

4848
let configs: webpack.Configuration[] = [];
4949

50-
if (Array.isArray(this.buildConfig.js.legacy))
50+
51+
this.buildConfig.js.configs.forEach(config =>
5152
{
52-
this.buildConfig.js.legacy.forEach(legacyConfig =>
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)
5355
{
54-
// For some reason TypeScript thinks that `this.buildConfig.js` might become `undefined`, even though we have this check already in line 34
55-
if (!this.buildConfig.js)
56-
{
57-
return;
58-
}
56+
return;
57+
}
5958

60-
configs.push(
61-
Object.assign(
62-
{},
63-
this.buildConfig.js.common,
64-
legacyConfig,
65-
{
66-
plugins: (this.buildConfig.js.common.plugins as any[]).concat(legacyConfig.plugins),
67-
name: "legacy",
68-
}
69-
)
70-
);
71-
});
72-
}
73-
74-
if (Array.isArray(this.buildConfig.js.module))
75-
{
76-
this.buildConfig.js.module.forEach(moduleConfig =>
59+
if (undefined !== config.plugins && this.buildConfig.js.common.plugins)
7760
{
78-
// For some reason TypeScript thinks that `this.buildConfig.js` might become `undefined`, even though we have this check already in line 34
79-
if (!this.buildConfig.js)
80-
{
81-
return;
82-
}
61+
config.plugins.push(...this.buildConfig.js.common.plugins)
62+
}
8363

84-
configs.push(
85-
Object.assign(
86-
{},
87-
this.buildConfig.js.common,
88-
moduleConfig,
89-
{
90-
plugins: (this.buildConfig.js.common.plugins as any[]).concat(moduleConfig.plugins),
91-
name: "modern",
92-
}
93-
)
94-
);
95-
});
96-
}
64+
configs.push(
65+
Object.assign(
66+
{},
67+
this.buildConfig.js.common,
68+
config
69+
)
70+
);
71+
});
9772

9873
let compiler = webpack(configs) as webpack.MultiCompiler;
9974

0 commit comments

Comments
 (0)