-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(@ngtools/webpack): improve rebuild performance
We need to run full static analysis on the first build to discover routes in node_modules, but in rebuilding the app we just need to look for the changed files. This introduces a subtle bug though; if the module structure changes, we might be missing lazy routes if those are in modules imported but weren't in the original structure. This is, for now, considered "okay" as it's a relatively rare case. We should probably output a warning though.
- Loading branch information
Showing
6 changed files
with
195 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as ts from 'typescript'; | ||
|
||
import {TypeScriptFileRefactor} from './refactor'; | ||
|
||
|
||
function _getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): string { | ||
if (node.kind == ts.SyntaxKind.Identifier) { | ||
return (node as ts.Identifier).text; | ||
} else if (node.kind == ts.SyntaxKind.StringLiteral) { | ||
return (node as ts.StringLiteral).text; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
|
||
export interface LazyRouteMap { | ||
[path: string]: string; | ||
} | ||
|
||
|
||
export function findLazyRoutes(filePath: string, | ||
program: ts.Program, | ||
host: ts.CompilerHost): LazyRouteMap { | ||
const refactor = new TypeScriptFileRefactor(filePath, host, program); | ||
|
||
return refactor | ||
// Find all object literals in the file. | ||
.findAstNodes(null, ts.SyntaxKind.ObjectLiteralExpression, true) | ||
// Get all their property assignments. | ||
.map((node: ts.ObjectLiteralExpression) => { | ||
return refactor.findAstNodes(node, ts.SyntaxKind.PropertyAssignment, false); | ||
}) | ||
// Take all `loadChildren` elements. | ||
.reduce((acc: ts.PropertyAssignment[], props: ts.PropertyAssignment[]) => { | ||
return acc.concat(props.filter(literal => { | ||
return _getContentOfKeyLiteral(refactor.sourceFile, literal) == 'loadChildren'; | ||
})); | ||
}, []) | ||
// Get only string values. | ||
.filter((node: ts.PropertyAssignment) => node.initializer.kind == ts.SyntaxKind.StringLiteral) | ||
// Get the string value. | ||
.map((node: ts.PropertyAssignment) => (node.initializer as ts.StringLiteral).text) | ||
// Map those to either [path, absoluteModulePath], or [path, null] if the module pointing to | ||
// does not exist. | ||
.map((routePath: string) => { | ||
const moduleName = routePath.split('#')[0]; | ||
const resolvedModuleName = ts.resolveModuleName( | ||
moduleName, filePath, program.getCompilerOptions(), host); | ||
if (resolvedModuleName.resolvedModule | ||
&& resolvedModuleName.resolvedModule.resolvedFileName | ||
&& host.fileExists(resolvedModuleName.resolvedModule.resolvedFileName)) { | ||
return [routePath, resolvedModuleName]; | ||
} else { | ||
return [routePath, null]; | ||
} | ||
}) | ||
// Reduce to the LazyRouteMap map. | ||
.reduce((acc: LazyRouteMap, [routePath, resolvedModuleName]: [string, string | null]) => { | ||
acc[routePath] = resolvedModuleName; | ||
return acc; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {expectFileToMatch} from '../../utils/fs'; | ||
import {expectGitToBeClean} from '../../utils/git'; | ||
import {killAllProcesses, exec, waitForAnyProcessOutputToMatch} from '../../utils/process'; | ||
import {ngServe} from '../../utils/project'; | ||
import {request} from '../../utils/http'; | ||
|
||
|
||
export default function() { | ||
if (process.platform.startsWith('win')) { | ||
return Promise.resolve(); | ||
} | ||
|
||
return ngServe() | ||
.then(() => exec('touch', 'src/main.ts')) | ||
.then(() => waitForAnyProcessOutputToMatch(/webpack: bundle is now VALID/, 10000)) | ||
.then(() => request('http://localhost:4200/')) | ||
.then(() => killAllProcesses(), (err: any) => { | ||
killAllProcesses(); | ||
throw err; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters