Skip to content

Commit

Permalink
perf(compiler): improve loading performance
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh-coder committed Sep 16, 2020
1 parent 914b238 commit 818163e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "rm -rf ./dist && tsc --build"
"build": "rm -rf ./dist && tsc --build tsconfig.build.json"
},
"keywords": [
"compiler",
Expand All @@ -20,7 +20,7 @@
"dependencies": {
"@lesy/core": "^1.0.0-beta.7",
"@lesy/lesy-plugin-sidekick": "^1.0.0-beta.7",
"ts-node": "^8.10.1",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0"
},
"publishConfig": {
Expand Down
54 changes: 25 additions & 29 deletions packages/compiler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync } from "fs";

import { LesyCoreClass } from "@lesy/core";
class LesyCompiler {
opts: any = {
isTypescriptApp: false,
Expand All @@ -18,24 +18,24 @@ class LesyCompiler {
private tsConfigPath = null;

exec(opts = {}) {
this.opts = { ...this.opts, ...opts };
const {
isTypescriptApp,
root,
srcFilePath,
tsFlag,
loadDefaultPlugins,
customTsConfig,
...src
} = this.opts;
const hasSrcData = Object.keys(src).length > 0;
const hasBuildDir = existsSync(`${root}/dist`);
const shouldRunTSCode = process.argv.includes(tsFlag);
Object.assign(this.opts, opts);
const src = {
commands: this.opts.commands || [],
middlewares: this.opts.middlewares || [],
features: this.opts.features || [],
plugins: this.opts.plugins || [],
config: this.opts.config || {},
validators: this.opts.validators || [],
};
const hasSrcData = this.opts.commands;
let mainFilePath: string;
process.env.LESY_LANG = "js";
if (!isTypescriptApp) {
mainFilePath = !hasSrcData && require.resolve(root);
if (!this.opts.isTypescriptApp) {
mainFilePath = !hasSrcData && require.resolve(this.opts.root);
} else {
const { root, tsFlag, srcFilePath, customTsConfig } = this.opts;
const hasBuildDir = existsSync(`${root}/dist`);
const shouldRunTSCode = process.argv.includes(tsFlag);
if ((hasBuildDir && shouldRunTSCode) || !hasBuildDir) {
// run src
process.env.LESY_LANG = "ts";
Expand All @@ -48,7 +48,6 @@ class LesyCompiler {
mainFilePath = !hasSrcData && require.resolve(root);
}
}

return this.runApp(hasSrcData ? src : require(mainFilePath));
}

Expand All @@ -71,33 +70,30 @@ class LesyCompiler {
}

private runApp(appData) {
const { LesyCore, LesyCoreClass } = require("@lesy/core");
const { root } = this.opts;
Object.assign(appData, this.includeEssentials(appData));

this.includeEssentials(appData);
appData.root = this.opts.root;
const lesy = new LesyCoreClass()
.bootstrap({ ...appData, root })
.bootstrap(appData)
.catch((e: any) => console.log("LESY ERROR:", e));

return {
parse: (argv: string[]) =>
lesy.then((l) => {
lesy.then((l: any) => {
if (global["lesyWorkspace"]) return l;
return l.run(argv || process.argv.slice(2));
}),
};
}

private includeEssentials(appData) {
let { plugins = [], config = {} } = appData;
if (this.opts.loadDefaultPlugins) {
plugins = [
appData.plugins = [
...this.defaultPlugins.map((p: string) => require.resolve(p)),
...plugins,
...appData.plugins,
];
}
config = { ...this.defaultConfig, ...config };
return { ...appData, plugins, config };
const config = this.defaultConfig;
for (const prop in appData.config) config[prop] = appData.config[prop];
appData.config = config;
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"paths": {}
}
}
6 changes: 5 additions & 1 deletion packages/compiler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
"importHelpers": false,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
"esModuleInterop": true,
"baseUrl": "./",
"paths": {
"@lesy/core": ["../core/src/index.ts"]
}
},
"exclude": ["node_modules", "__tests__"],
"compileOnSave": false
Expand Down

0 comments on commit 818163e

Please sign in to comment.