Skip to content
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(ngcc): shortcircuit tokenizing in ESM dependency host #37639

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,10 +47,14 @@ export class EsmDependencyHost extends DependencyHostBase {
const templateStack: ts.SyntaxKind[] = [];
let lastToken: ts.SyntaxKind = ts.SyntaxKind.Unknown;
let currentToken: ts.SyntaxKind = ts.SyntaxKind.Unknown;
const stopAtIndex = findLastPossibleImportOrReexport(fileContents);

this.scanner.setText(fileContents);

while ((currentToken = this.scanner.scan()) !== ts.SyntaxKind.EndOfFileToken) {
if (this.scanner.getTokenPos() > stopAtIndex) {
break;
}
switch (currentToken) {
case ts.SyntaxKind.TemplateHead:
// TemplateHead indicates the beginning of a backticked string
Expand Down Expand Up @@ -266,6 +270,9 @@ export function hasImportOrReexportStatements(source: string): boolean {
return /(?:import|export)[\s\S]+?(["'])(?:\\\1|.)+?\1/.test(source);
}

function findLastPossibleImportOrReexport(source: string): number {
return Math.max(source.lastIndexOf('import'), source.lastIndexOf(' from '));
}

/**
* Check whether the given statement is an import with a string literal module specifier.
Expand Down