Skip to content

Commit bd65d3d

Browse files
author
Jannik Zschiesche
authored
Merge pull request #104 from Becklyn/per-entry-compiler
Per entry compiler
2 parents 66b83b6 + 7c9c5ff commit bd65d3d

File tree

5 files changed

+136
-183
lines changed

5 files changed

+136
-183
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* (improvement) Bump required node version to 12.
3333
* (internal) Bumped all dependencies.
3434
* (improvement) Allow `++` in JS/TS code.
35+
* (improvement) The code is now always compiled per entry file
3536

3637

3738
8.1.0

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ declare namespace kaba
2626
{
2727
sass: SassBuildConfig;
2828
js?: {
29-
common: WebpackBuildConfig;
30-
module: WebpackBuildConfig|null;
31-
legacy: WebpackBuildConfig;
29+
watch: boolean,
30+
configs: WebpackBuildConfig[];
3231
javaScriptDependenciesFileName: string;
3332
basePath: string;
3433
};

src/Kaba.ts

+124-143
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export class Kaba
4848
private publicPath: string = "/assets/app/js/";
4949
private externals: Externals = {};
5050
private moduleConcatenationEnabled: boolean = false;
51-
private plugins: webpack.Plugin[] = [];
5251
private javaScriptDependenciesFileName: string = "_dependencies";
5352
private hashFileNames: boolean = true;
5453
private buildModern: boolean = true;
@@ -62,22 +61,6 @@ export class Kaba
6261
{
6362
this.cwd = process.cwd();
6463
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-
];
8164

8265
// set defaults
8366
this.setOutputPath("build");
@@ -274,12 +257,20 @@ export class Kaba
274257

275258
if (Object.keys(this.jsEntries).length)
276259
{
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+
277271
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,
283274
javaScriptDependenciesFileName: this.javaScriptDependenciesFileName,
284275
basePath: path.join(this.outputPaths.base, this.outputPaths.js),
285276
};
@@ -299,11 +290,34 @@ export class Kaba
299290

300291

301292
/**
302-
* Builds the common webpack config, that is common between legacy & module
293+
* Builds the specialized webpack config for a legacy / module build
303294
*/
304-
private buildWebpackCommon (cliConfig: kaba.CliConfig): Partial<webpack.Configuration>
295+
private buildWebpackConfig (entry: string, entryFile: string, cliConfig: kaba.CliConfig, isModule: boolean): Partial<webpack.Configuration>
305296
{
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+
307321
// mode
308322
mode: cliConfig.debug ? "development" : "production",
309323

@@ -328,9 +342,47 @@ export class Kaba
328342
],
329343
},
330344

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+
331354
// module
332355
module: {
333-
rules: [],
356+
rules: [
357+
// TypeScript
358+
{
359+
test: /\.tsx?$/,
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?jsx?$/,
377+
use: ['cache-loader', babelLoader],
378+
},
379+
380+
// content files
381+
{
382+
test: /\.(svg|txt)$/,
383+
loader: "raw-loader",
384+
},
385+
] as webpack.RuleSetRule[],
334386
},
335387

336388
// optimization
@@ -339,8 +391,6 @@ export class Kaba
339391
minimizer: [],
340392
},
341393

342-
// performance
343-
344394
// devtool (source maps)
345395
devtool: cliConfig.debug
346396
? "inline-cheap-source-map"
@@ -361,12 +411,35 @@ export class Kaba
361411
stats: {
362412
// hide children information (like from the ExtractTextPlugin)
363413
children: false,
414+
hash: !isModule,
415+
version: !isModule,
416+
modules: !isModule,
364417
},
365418

366-
// devServer
367-
368419
// 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+
],
370443

371444
// watch
372445
watch: cliConfig.watch,
@@ -375,11 +448,11 @@ export class Kaba
375448
// don't automatically polyfill certain node libraries
376449
// as we don't care about these implementations and they just add weight
377450
node: this.nodeSettings,
378-
};
451+
} as Partial<webpack.Configuration>;
379452

380453
if (!cliConfig.debug)
381454
{
382-
(config.optimization as any).minimizer.push(new TerserPlugin({
455+
(configTemplate.optimization as any).minimizer.push(new TerserPlugin({
383456
cache: true,
384457
parallel: true,
385458
sourceMap: true,
@@ -395,7 +468,7 @@ export class Kaba
395468
try
396469
{
397470
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
398-
(config.plugins as any[]).push(new BundleAnalyzerPlugin());
471+
(configTemplate.plugins as any[]).push(new BundleAnalyzerPlugin());
399472
}
400473
catch (e)
401474
{
@@ -416,119 +489,27 @@ export class Kaba
416489

417490
}
418491

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)
441493
{
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?jsx?$/,
497+
// only lint files that are in the project dir & exclude tests, vendor and node_modules
498+
include: (path) => path.startsWith(this.cwd) && !/node_modules|tests|vendor/.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,
447508
},
448-
);
509+
});
449510
}
450511

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: /\.tsx?$/,
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?jsx?$/,
493-
use: ['cache-loader', babelLoader],
494-
},
495-
496-
// content files
497-
{
498-
test: /\.(svg|txt)$/,
499-
loader: "raw-loader",
500-
},
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-
},
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;
532513
}
533514
}
534515

0 commit comments

Comments
 (0)