-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(@ngtools/webpack): improve rebuild performance #4188
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result
will always be an empty object, it's not modified after it's created.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops.