Skip to content

Commit 33f05b8

Browse files
Kai Eichingerjesko-plitt
Kai Eichinger
authored andcommitted
Run a separate instance of Webpack per JS entry file
1 parent 614b304 commit 33f05b8

File tree

4 files changed

+79
-47
lines changed

4 files changed

+79
-47
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"scripts": {
1616
"build": "node_modules/.bin/tsc --noEmitOnError --noErrorTruncation --listEmittedFiles --pretty --noUnusedLocals && chmod +x bin/run.js",
17-
"dev": "node_modules/.bin/tsc --noEmitOnError --noErrorTruncation --listEmittedFiles --pretty",
17+
"dev": "node_modules/.bin/tsc --noEmitOnError --noErrorTruncation --listEmittedFiles --pretty --watch",
1818
"prepublishOnly": "npm run-script build",
1919
"test": "npm run-script build && ava"
2020
},

src/@types/kaba.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare namespace kaba
2727
sass: SassBuildConfig;
2828
js?: {
2929
common: WebpackBuildConfig;
30-
module: WebpackBuildConfig|null;
30+
module: WebpackBuildConfig[]|null;
3131
legacy: WebpackBuildConfig;
3232
javaScriptDependenciesFileName: string;
3333
basePath: string;

src/Kaba.ts

+35-24
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ export class Kaba
277277
jsConfig = {
278278
common: this.buildWebpackCommon(cliConfig),
279279
module: this.buildModern
280-
? this.buildWebpackConfig(cliConfig, true)
280+
? this.buildWebpackConfigs(cliConfig, true)
281281
: null,
282-
legacy: this.buildWebpackConfig(cliConfig, false),
282+
legacy: this.buildWebpackConfigs(cliConfig, false),
283283
javaScriptDependenciesFileName: this.javaScriptDependenciesFileName,
284284
basePath: path.join(this.outputPaths.base, this.outputPaths.js),
285285
};
@@ -423,7 +423,7 @@ export class Kaba
423423
/**
424424
* Builds the specialized webpack config for a legacy / module build
425425
*/
426-
private buildWebpackConfig (cliConfig: kaba.CliConfig, isModule: boolean): Partial<webpack.Configuration>
426+
private buildWebpackConfigs (cliConfig: kaba.CliConfig, isModule: boolean): Partial<webpack.Configuration>[]
427427
{
428428
const babelLoader = {
429429
loader: "babel-loader?cacheDirectory",
@@ -454,10 +454,7 @@ export class Kaba
454454
isModule ? "tsconfig.modern.json" : "tsconfig.legacy.json",
455455
);
456456

457-
return {
458-
// entry
459-
entry: entries,
460-
457+
let configTemplate = {
461458
// output
462459
output: {
463460
path: path.join(this.outputPaths.base, this.outputPaths.js, isModule ? "modern" : "legacy"),
@@ -498,23 +495,6 @@ export class Kaba
498495
test: /\.(svg|txt)$/,
499496
loader: "raw-loader",
500497
},
501-
502-
// ESLint
503-
{
504-
test: /\.m?jsx?$/,
505-
// only lint files that are in the project dir & exclude tests, vendor and node_modules
506-
include: (path) => path.startsWith(this.cwd) && !/node_modules|tests|vendor/.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-
},
518498
],
519499
},
520500

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

src/runner/WebpackRunner.ts

+42-21
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,52 @@ export class WebpackRunner
4747

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

50-
configs.push(
51-
Object.assign(
52-
{},
53-
this.buildConfig.js.common,
54-
this.buildConfig.js.legacy,
50+
if (Array.isArray(this.buildConfig.js.legacy))
51+
{
52+
this.buildConfig.js.legacy.forEach(legacyConfig =>
53+
{
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)
5556
{
56-
plugins: (this.buildConfig.js.common.plugins as any[]).concat(this.buildConfig.js.legacy.plugins),
57-
name: "legacy",
57+
return;
5858
}
59-
)
60-
);
6159

62-
if (this.buildConfig.js.module)
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))
6375
{
64-
configs.push(
65-
Object.assign(
66-
{},
67-
this.buildConfig.js.common,
68-
this.buildConfig.js.module,
69-
{
70-
plugins: (this.buildConfig.js.common.plugins as any[]).concat(this.buildConfig.js.module.plugins),
71-
name: "modern",
72-
}
73-
)
74-
);
76+
this.buildConfig.js.module.forEach(moduleConfig =>
77+
{
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+
}
83+
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+
});
7596
}
7697

7798
let compiler = webpack(configs) as webpack.MultiCompiler;

0 commit comments

Comments
 (0)