Skip to content

Commit 818163e

Browse files
committed
perf(compiler): improve loading performance
1 parent 914b238 commit 818163e

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

packages/compiler/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "dist/index.js",
66
"scripts": {
7-
"build": "rm -rf ./dist && tsc --build"
7+
"build": "rm -rf ./dist && tsc --build tsconfig.build.json"
88
},
99
"keywords": [
1010
"compiler",
@@ -20,7 +20,7 @@
2020
"dependencies": {
2121
"@lesy/core": "^1.0.0-beta.7",
2222
"@lesy/lesy-plugin-sidekick": "^1.0.0-beta.7",
23-
"ts-node": "^8.10.1",
23+
"ts-node": "^9.0.0",
2424
"tsconfig-paths": "^3.9.0"
2525
},
2626
"publishConfig": {

packages/compiler/src/index.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync } from "fs";
2-
2+
import { LesyCoreClass } from "@lesy/core";
33
class LesyCompiler {
44
opts: any = {
55
isTypescriptApp: false,
@@ -18,24 +18,24 @@ class LesyCompiler {
1818
private tsConfigPath = null;
1919

2020
exec(opts = {}) {
21-
this.opts = { ...this.opts, ...opts };
22-
const {
23-
isTypescriptApp,
24-
root,
25-
srcFilePath,
26-
tsFlag,
27-
loadDefaultPlugins,
28-
customTsConfig,
29-
...src
30-
} = this.opts;
31-
const hasSrcData = Object.keys(src).length > 0;
32-
const hasBuildDir = existsSync(`${root}/dist`);
33-
const shouldRunTSCode = process.argv.includes(tsFlag);
21+
Object.assign(this.opts, opts);
22+
const src = {
23+
commands: this.opts.commands || [],
24+
middlewares: this.opts.middlewares || [],
25+
features: this.opts.features || [],
26+
plugins: this.opts.plugins || [],
27+
config: this.opts.config || {},
28+
validators: this.opts.validators || [],
29+
};
30+
const hasSrcData = this.opts.commands;
3431
let mainFilePath: string;
3532
process.env.LESY_LANG = "js";
36-
if (!isTypescriptApp) {
37-
mainFilePath = !hasSrcData && require.resolve(root);
33+
if (!this.opts.isTypescriptApp) {
34+
mainFilePath = !hasSrcData && require.resolve(this.opts.root);
3835
} else {
36+
const { root, tsFlag, srcFilePath, customTsConfig } = this.opts;
37+
const hasBuildDir = existsSync(`${root}/dist`);
38+
const shouldRunTSCode = process.argv.includes(tsFlag);
3939
if ((hasBuildDir && shouldRunTSCode) || !hasBuildDir) {
4040
// run src
4141
process.env.LESY_LANG = "ts";
@@ -48,7 +48,6 @@ class LesyCompiler {
4848
mainFilePath = !hasSrcData && require.resolve(root);
4949
}
5050
}
51-
5251
return this.runApp(hasSrcData ? src : require(mainFilePath));
5352
}
5453

@@ -71,33 +70,30 @@ class LesyCompiler {
7170
}
7271

7372
private runApp(appData) {
74-
const { LesyCore, LesyCoreClass } = require("@lesy/core");
75-
const { root } = this.opts;
76-
Object.assign(appData, this.includeEssentials(appData));
77-
73+
this.includeEssentials(appData);
74+
appData.root = this.opts.root;
7875
const lesy = new LesyCoreClass()
79-
.bootstrap({ ...appData, root })
76+
.bootstrap(appData)
8077
.catch((e: any) => console.log("LESY ERROR:", e));
81-
8278
return {
8379
parse: (argv: string[]) =>
84-
lesy.then((l) => {
80+
lesy.then((l: any) => {
8581
if (global["lesyWorkspace"]) return l;
8682
return l.run(argv || process.argv.slice(2));
8783
}),
8884
};
8985
}
9086

9187
private includeEssentials(appData) {
92-
let { plugins = [], config = {} } = appData;
9388
if (this.opts.loadDefaultPlugins) {
94-
plugins = [
89+
appData.plugins = [
9590
...this.defaultPlugins.map((p: string) => require.resolve(p)),
96-
...plugins,
91+
...appData.plugins,
9792
];
9893
}
99-
config = { ...this.defaultConfig, ...config };
100-
return { ...appData, plugins, config };
94+
const config = this.defaultConfig;
95+
for (const prop in appData.config) config[prop] = appData.config[prop];
96+
appData.config = config;
10197
}
10298
}
10399

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"paths": {}
5+
}
6+
}

packages/compiler/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
"importHelpers": false,
1313
"moduleResolution": "node",
1414
"allowSyntheticDefaultImports": true,
15-
"esModuleInterop": true
15+
"esModuleInterop": true,
16+
"baseUrl": "./",
17+
"paths": {
18+
"@lesy/core": ["../core/src/index.ts"]
19+
}
1620
},
1721
"exclude": ["node_modules", "__tests__"],
1822
"compileOnSave": false

0 commit comments

Comments
 (0)