Skip to content

Commit 9f754cd

Browse files
committed
refactor(parsing): don't create ts program for each child on a file, 300% boost !
1 parent 1e70f88 commit 9f754cd

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/app/compiler/dependencies.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,23 @@ export class Dependencies {
144144

145145
private getSourceFileDecorators(srcFile: ts.SourceFile, outputSymbols: Object): void {
146146

147+
let cleaner = (process.cwd() + path.sep).replace(/\\/g, '/');
148+
let file = srcFile.fileName.replace(cleaner, '');
149+
150+
this.programComponent = ts.createProgram([file], {});
151+
let sourceFile = this.programComponent.getSourceFile(file);
152+
this.typeCheckerComponent = this.programComponent.getTypeChecker(true);
153+
147154
ts.forEachChild(srcFile, (node: ts.Node) => {
148155

149156
let deps: Deps = <Deps>{};
150-
let cleaner = (process.cwd() + path.sep).replace(/\\/g, '/');
151-
let file = srcFile.fileName.replace(cleaner, '');
152157
if (node.decorators) {
153158
let visitNode = (visitedNode, index) => {
154159

155160
let metadata = node.decorators.pop();
156161
let name = this.getSymboleName(node);
157162
let props = this.findProps(visitedNode);
158-
let IO = this.getComponentIO(file);
163+
let IO = this.getComponentIO(file, sourceFile);
159164

160165
if (this.isModule(metadata)) {
161166
deps = {
@@ -252,7 +257,7 @@ export class Dependencies {
252257
else if (node.symbol) {
253258
if(node.symbol.flags === ts.SymbolFlags.Class) {
254259
let name = this.getSymboleName(node);
255-
let IO = this.getComponentIO(file);
260+
let IO = this.getComponentIO(file, sourceFile);
256261
deps = {
257262
name,
258263
file: file,
@@ -270,7 +275,7 @@ export class Dependencies {
270275
outputSymbols['classes'].push(deps);
271276
}
272277
} else {
273-
let IO = this.getRouteIO(file);
278+
let IO = this.getRouteIO(file, sourceFile);
274279
if(IO.routes) {
275280
let newRoutes;
276281
try {
@@ -695,14 +700,10 @@ export class Dependencies {
695700
return [];
696701
}
697702

698-
private getRouteIO(filename: string) {
703+
private getRouteIO(filename, sourceFile) {
699704
/**
700705
* Copyright https://github.com/ng-bootstrap/ng-bootstrap
701706
*/
702-
this.programComponent = ts.createProgram([filename], {});
703-
let sourceFile = this.programComponent.getSourceFile(filename);
704-
this.typeCheckerComponent = this.programComponent.getTypeChecker(true);
705-
706707
var res = sourceFile.statements.reduce((directive, statement) => {
707708

708709
if (statement.kind === ts.SyntaxKind.VariableStatement) {
@@ -715,14 +716,10 @@ export class Dependencies {
715716
return res[0] || {};
716717
}
717718

718-
private getComponentIO(filename: string) {
719+
private getComponentIO(filename: string, sourceFile) {
719720
/**
720721
* Copyright https://github.com/ng-bootstrap/ng-bootstrap
721722
*/
722-
this.programComponent = ts.createProgram([filename], {});
723-
let sourceFile = this.programComponent.getSourceFile(filename);
724-
this.typeCheckerComponent = this.programComponent.getTypeChecker(true);
725-
726723
var res = sourceFile.statements.reduce((directive, statement) => {
727724

728725
if (statement.kind === ts.SyntaxKind.ClassDeclaration) {

0 commit comments

Comments
 (0)