Skip to content

Commit 404c018

Browse files
author
Jannik Zschiesche
committed
Switch from include-list to exclude-list in npm package compilation
1 parent dad78ef commit 404c018

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/Kaba.ts

+33-16
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const DuplicatePackageCheckerPlugin = require("duplicate-package-checker-webpack
1111
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
1212
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
1313

14-
const PACKAGE_MATCHER = /\/node_modules\/(?<package>[^\/]+)\//;
15-
interface CompiledNpmPackagesMapping {[name: string]: true}
14+
const PACKAGE_MATCHER = /\/node_modules\/(?<package>(?:@[^@\/]+\/)?[^@\/]+)\//;
15+
type IgnoredNpmPackagesMapping = Array<RegExp|string>;
1616
interface PostCssLoaderOptions {[key: string]: any}
1717

1818
interface Entries
@@ -35,7 +35,7 @@ interface Externals
3535
/**
3636
* Determines whether a file should processed by the asset pipeline.
3737
*/
38-
function isAllowedPath (path: string, allowedPaths: CompiledNpmPackagesMapping) : boolean
38+
function isAllowedPath (path: string, ignoredPackages: IgnoredNpmPackagesMapping) : boolean
3939
{
4040
const match = PACKAGE_MATCHER.exec(path);
4141

@@ -45,8 +45,27 @@ function isAllowedPath (path: string, allowedPaths: CompiledNpmPackagesMapping)
4545
return true;
4646
}
4747

48-
// only allow the allowed package names
49-
return true === allowedPaths[(match.groups as any).package];
48+
const packageNameToCompile = (match.groups as any).package;
49+
50+
const length = ignoredPackages.length;
51+
for (let i = 0; i < length; ++i)
52+
{
53+
const ignoredPackage = ignoredPackages[i];
54+
55+
if (typeof ignoredPackage == "string")
56+
{
57+
if (ignoredPackage === packageNameToCompile)
58+
{
59+
return false;
60+
}
61+
}
62+
else if (ignoredPackage.test(packageNameToCompile))
63+
{
64+
return false;
65+
}
66+
}
67+
68+
return true;
5069
}
5170

5271
/**
@@ -71,12 +90,10 @@ export class Kaba
7190
private hashFileNames: boolean = true;
7291
private buildModern: boolean = true;
7392
private nodeSettings: webpack.Node|false = false;
74-
private compiledNpmPackages: CompiledNpmPackagesMapping = {
75-
'@becklyn': true,
76-
'@mayd': true,
77-
mojave: true,
78-
preact: true,
79-
};
93+
private ignoredNpmPackages: IgnoredNpmPackagesMapping = [
94+
/^@babel/,
95+
/^regenerator-/,
96+
];
8097
private postCssLoaderOptions: PostCssLoaderOptions = {};
8198

8299

@@ -104,11 +121,11 @@ export class Kaba
104121

105122

106123
/**
107-
* Defines which npm packages are compiled
124+
* Defines which npm packages are NOT compiled with babel
108125
*/
109-
public compileNpmPackages (modules: string[]): this
126+
public ignoreNpmPackages (modules: Array<string|RegExp>): this
110127
{
111-
modules.forEach(module => this.compiledNpmPackages[module] = true);
128+
this.ignoredNpmPackages = this.ignoredNpmPackages.concat(modules);
112129
return this;
113130
}
114131

@@ -415,14 +432,14 @@ export class Kaba
415432
},
416433
},
417434
],
418-
include: (path: string) => isAllowedPath(path, this.compiledNpmPackages),
435+
include: (path: string) => isAllowedPath(path, this.ignoredNpmPackages),
419436
},
420437

421438
// Babel
422439
{
423440
test: /\.m?jsx?$/,
424441
use: ['cache-loader', babelLoader],
425-
include: (path: string) => isAllowedPath(path, this.compiledNpmPackages),
442+
include: (path: string) => isAllowedPath(path, this.ignoredNpmPackages),
426443
},
427444

428445
// content files

0 commit comments

Comments
 (0)