Skip to content

Commit 96b7e59

Browse files
author
Jannik Zschiesche
committed
Only compile specific node_modules packages
1 parent c89d59d commit 96b7e59

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* (improvement) Ignore `.css` files when compiling webpack. You need to import them manually in your SCSS.
3737
* (improvement) Build TypeScript for the `esnext` module system, that (amongst other things) allows to use `import()`.
3838
* (bug) Compile every entry into a separate directory, to avoid issues with the clean plugin.
39+
* (bc) Now only specific modules in `node_modules` are transformed. You need to set your packages explicity via `.compileNpmPackages()`.
3940

4041

4142
8.1.0

src/Kaba.ts

+49-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +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}
1416

1517
interface Entries
1618
{
@@ -29,6 +31,22 @@ interface Externals
2931
[name: string]: string;
3032
}
3133

34+
/**
35+
* Determines whether a file should processed by the asset pipeline.
36+
*/
37+
function isAllowedPath (path: string, allowedPaths: CompiledNpmPackagesMapping) : boolean
38+
{
39+
const match = PACKAGE_MATCHER.exec(path);
40+
41+
// not in node_modules, so always process
42+
if (!match)
43+
{
44+
return true;
45+
}
46+
47+
// only allow the allowed package names
48+
return true === allowedPaths[(match.groups as any).package];
49+
}
3250

3351
/**
3452
* Main Kaba class
@@ -52,6 +70,11 @@ export class Kaba
5270
private hashFileNames: boolean = true;
5371
private buildModern: boolean = true;
5472
private nodeSettings: webpack.Node|false = false;
73+
private compiledNpmPackages: CompiledNpmPackagesMapping = {
74+
preact: true,
75+
mojave: true,
76+
'@mayd': true,
77+
};
5578

5679

5780
/**
@@ -77,6 +100,16 @@ export class Kaba
77100
}
78101

79102

103+
/**
104+
* Defines which npm packages are compiled
105+
*/
106+
public compileNpmPackages (modules: string[]): this
107+
{
108+
modules.forEach(module => this.compiledNpmPackages[module] = true);
109+
return this;
110+
}
111+
112+
80113
/**
81114
* Adds Sass entries
82115
*/
@@ -369,12 +402,14 @@ export class Kaba
369402
},
370403
},
371404
],
405+
include: (path: string) => isAllowedPath(path, this.compiledNpmPackages),
372406
},
373407

374408
// Babel
375409
{
376410
test: /\.m?jsx?$/,
377411
use: ['cache-loader', babelLoader],
412+
include: (path: string) => isAllowedPath(path, this.compiledNpmPackages),
378413
},
379414

380415
// content files
@@ -383,11 +418,22 @@ export class Kaba
383418
loader: "raw-loader",
384419
},
385420

386-
// ignore CSS files
421+
// Try to avoid compiling CSS, but if there is CSS then inject it into the head
387422
{
388423
test: /\.css$/,
389-
loader: "ignore-loader",
390-
}
424+
use: [
425+
{
426+
loader: 'style-loader',
427+
options: {
428+
injectType: 'singletonStyleTag',
429+
},
430+
},
431+
{
432+
loader: 'postcss-loader',
433+
options: this.postCssLoaderOptions,
434+
},
435+
],
436+
},
391437
],
392438
},
393439

0 commit comments

Comments
 (0)