Skip to content

Commit cea4cc5

Browse files
author
Jannik Zschiesche
authored
Merge pull request #107 from Becklyn/next
Next: build into separate dirs, compile only specific node_modules and reluctantly compile SCSS
2 parents cfe8359 + 1b6f4cf commit cea4cc5

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
* (internal) Bumped all dependencies.
3434
* (improvement) Allow `++` in JS/TS code.
3535
* (improvement) The code is now always compiled per entry file
36-
* (improvement) Ignore `.css` files when compiling webpack. You need to import them manually in your SCSS.
3736
* (improvement) Build TypeScript for the `esnext` module system, that (amongst other things) allows to use `import()`.
37+
* (bug) Compile every entry into a separate directory, to avoid issues with the clean plugin.
38+
* (bc) Now only specific modules in `node_modules` are transformed. You need to set your packages explicity via `.compileNpmPackages(...)`.
39+
* (feature) Even if not recommended (use SCSS!), we now support compiling CSS via webpack (not as entry point though). The CSS will be injected into the head dynamically.
3840

3941

4042
8.1.0

UPGRADE.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Always build a legacy and a modern JS build.
66
* Removed the ability to use the project's `tsconfig.json`.
77
* Removed `disableChunkSplitting()`, chunk splitting is now always disabled. Use code splitting instead and remove the call to this method.
8+
* Now no npm package is built anymore. Define your npm packages that need a transformation via `.compileNpmPackages(...)` (for now only top-level entries support, so either complete vendor or global package name).
89

910

1011
7.x to 8.0

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737
"eslint-plugin-react": "^7.17.0",
3838
"eslint-plugin-react-hooks": "^2.3.0",
3939
"fs-extra": "^8.1.0",
40-
"ignore-loader": "^0.1.2",
4140
"kaba-babel-preset": "^4.1.1",
4241
"kleur": "^3.0.3",
42+
"postcss-loader": "^3.0.0",
4343
"pretty-hrtime": "^1.0.3",
4444
"progress-bar-webpack-plugin": "^2.1.0",
4545
"raw-loader": "^4.0.0",
4646
"sade": "^1.7.0",
47+
"style-loader": "^1.1.3",
4748
"terser-webpack-plugin": "^2.3.2",
4849
"ts-loader": "^6.2.1",
4950
"typescript": "^3.7.4",

src/Kaba.ts

+68-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ 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}
16+
interface PostCssLoaderOptions {[key: string]: any}
1417

1518
interface Entries
1619
{
@@ -29,6 +32,22 @@ interface Externals
2932
[name: string]: string;
3033
}
3134

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

3352
/**
3453
* Main Kaba class
@@ -52,6 +71,13 @@ export class Kaba
5271
private hashFileNames: boolean = true;
5372
private buildModern: boolean = true;
5473
private nodeSettings: webpack.Node|false = false;
74+
private compiledNpmPackages: CompiledNpmPackagesMapping = {
75+
'@becklyn': true,
76+
'@mayd': true,
77+
mojave: true,
78+
preact: true,
79+
};
80+
private postCssLoaderOptions: PostCssLoaderOptions = {};
5581

5682

5783
/**
@@ -77,6 +103,16 @@ export class Kaba
77103
}
78104

79105

106+
/**
107+
* Defines which npm packages are compiled
108+
*/
109+
public compileNpmPackages (modules: string[]): this
110+
{
111+
modules.forEach(module => this.compiledNpmPackages[module] = true);
112+
return this;
113+
}
114+
115+
80116
/**
81117
* Adds Sass entries
82118
*/
@@ -219,6 +255,16 @@ export class Kaba
219255
}
220256

221257

258+
/**
259+
* Sets the loader options for the postcss loader
260+
*/
261+
public setPostCssLoaderOptions (options: PostCssLoaderOptions): this
262+
{
263+
this.postCssLoaderOptions = options;
264+
return this;
265+
}
266+
267+
222268
/**
223269
* Returns the kaba config
224270
*
@@ -294,7 +340,7 @@ export class Kaba
294340
*/
295341
private buildWebpackConfig (entry: string, entryFile: string, cliConfig: kaba.CliConfig, isModule: boolean): Partial<webpack.Configuration>
296342
{
297-
const babelLoader = {
343+
const babelLoader: webpack.RuleSetUseItem = {
298344
loader: "babel-loader?cacheDirectory",
299345
options: {
300346
babelrc: false,
@@ -312,8 +358,8 @@ export class Kaba
312358

313359
const entryName = isModule ? `_modern.${entry}` : entry;
314360

315-
let configTemplate = {
316-
name: isModule ? "modern" : "legacy",
361+
let configTemplate: webpack.Configuration = {
362+
name: `${entry}: ${isModule ? "modern" : "legacy"}`,
317363
entry: {
318364
[entryName]: entryFile,
319365
},
@@ -344,10 +390,10 @@ export class Kaba
344390

345391
// output
346392
output: {
347-
path: path.join(this.outputPaths.base, this.outputPaths.js, isModule ? "modern" : "legacy"),
393+
path: path.join(this.outputPaths.base, this.outputPaths.js, entry, isModule ? "modern" : "legacy"),
348394
filename: this.hashFileNames ? '[name].[chunkhash].js' : '[name].js',
349395
// the slash at the end is required of the public path entries
350-
publicPath: path.join(this.publicPath, isModule ? "modern/" : "legacy/"),
396+
publicPath: path.join(this.publicPath, entry, isModule ? "modern/" : "legacy/"),
351397
pathinfo: cliConfig.debug,
352398
},
353399

@@ -369,12 +415,14 @@ export class Kaba
369415
},
370416
},
371417
],
418+
include: (path: string) => isAllowedPath(path, this.compiledNpmPackages),
372419
},
373420

374421
// Babel
375422
{
376423
test: /\.m?jsx?$/,
377424
use: ['cache-loader', babelLoader],
425+
include: (path: string) => isAllowedPath(path, this.compiledNpmPackages),
378426
},
379427

380428
// content files
@@ -383,12 +431,23 @@ export class Kaba
383431
loader: "raw-loader",
384432
},
385433

386-
// ignore CSS files
434+
// Try to avoid compiling CSS, but if there is CSS then inject it into the head
387435
{
388436
test: /\.css$/,
389-
loader: "ignore-loader",
390-
}
391-
] as webpack.RuleSetRule[],
437+
use: [
438+
{
439+
loader: 'style-loader',
440+
options: {
441+
injectType: 'singletonStyleTag',
442+
},
443+
},
444+
{
445+
loader: 'postcss-loader',
446+
options: this.postCssLoaderOptions,
447+
},
448+
],
449+
},
450+
],
392451
},
393452

394453
// optimization

0 commit comments

Comments
 (0)