forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (angular#4188)
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
1 parent
dbb3175
commit e82feab
Showing
7 changed files
with
250 additions
and
20 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,66 @@ | ||
import {dirname, join} from 'path'; | ||
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.name) == '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.ResolvedModuleWithFailedLookupLocations = moduleName[0] == '.' | ||
? { resolvedModule: { resolvedFileName: join(dirname(filePath), moduleName) + '.ts' }, | ||
failedLookupLocations: [] } | ||
: ts.resolveModuleName(moduleName, filePath, program.getCompilerOptions(), host); | ||
if (resolvedModuleName.resolvedModule | ||
&& resolvedModuleName.resolvedModule.resolvedFileName | ||
&& host.fileExists(resolvedModuleName.resolvedModule.resolvedFileName)) { | ||
return [routePath, resolvedModuleName.resolvedModule.resolvedFileName]; | ||
} 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,73 @@ | ||
import { | ||
killAllProcesses, | ||
exec, | ||
waitForAnyProcessOutputToMatch, | ||
silentExecAndWaitForOutputToMatch, | ||
ng, execAndWaitForOutputToMatch, | ||
} from '../../utils/process'; | ||
import {writeFile} from '../../utils/fs'; | ||
import {wait} from '../../utils/utils'; | ||
|
||
|
||
export default function() { | ||
if (process.platform.startsWith('win')) { | ||
return Promise.resolve(); | ||
} | ||
|
||
let oldNumberOfChunks = 0; | ||
const chunkRegExp = /chunk\s+\{/g; | ||
|
||
return execAndWaitForOutputToMatch('ng', ['serve'], /webpack: bundle is now VALID/) | ||
// Should trigger a rebuild. | ||
.then(() => exec('touch', 'src/main.ts')) | ||
.then(() => waitForAnyProcessOutputToMatch(/webpack: bundle is now INVALID/, 1000)) | ||
.then(() => waitForAnyProcessOutputToMatch(/webpack: bundle is now VALID/, 5000)) | ||
// Count the bundles. | ||
.then((stdout: string) => { | ||
oldNumberOfChunks = stdout.split(chunkRegExp).length; | ||
}) | ||
// Add a lazy module. | ||
.then(() => ng('generate', 'module', 'lazy', '--routing')) | ||
// Just wait for the rebuild, otherwise we might be validating this build. | ||
.then(() => wait(1000)) | ||
.then(() => writeFile('src/app/app.module.ts', ` | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { NgModule } from '@angular/core'; | ||
import { FormsModule } from '@angular/forms'; | ||
import { HttpModule } from '@angular/http'; | ||
import { AppComponent } from './app.component'; | ||
import { RouterModule } from '@angular/router'; | ||
@NgModule({ | ||
declarations: [ | ||
AppComponent | ||
], | ||
imports: [ | ||
BrowserModule, | ||
FormsModule, | ||
HttpModule, | ||
RouterModule.forRoot([ | ||
{ path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule' } | ||
]) | ||
], | ||
providers: [], | ||
bootstrap: [AppComponent] | ||
}) | ||
export class AppModule { } | ||
`)) | ||
// Should trigger a rebuild with a new bundle. | ||
.then(() => waitForAnyProcessOutputToMatch(/webpack: bundle is now INVALID/, 1000)) | ||
.then(() => waitForAnyProcessOutputToMatch(/webpack: bundle is now VALID/, 5000)) | ||
// Count the bundles. | ||
.then((stdout: string) => { | ||
let newNumberOfChunks = stdout.split(chunkRegExp).length; | ||
if (oldNumberOfChunks >= newNumberOfChunks) { | ||
throw new Error('Expected webpack to create a new chunk, but did not.'); | ||
} | ||
}) | ||
.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
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