Skip to content

Commit

Permalink
perf(@ngtools/webpack): avoid bootstrap conversion AST traversal wher…
Browse files Browse the repository at this point in the history
…e possible

To support AOT compilation the Angular bootstrap call needs to be converted from
using `platform-browser-dynamic` to `platform-browser`. This transform was previously
being executed against every source file within the program regardless of the presence of
`platformBrowserDynamic`. An initial check is now performed that can avoid AST traversal
when the bootstrapping call is not present in a file.

(cherry picked from commit 4fa8392)
  • Loading branch information
clydin authored and alan-agius4 committed Sep 9, 2022
1 parent 3184eb0 commit 1e3ecbd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/ngtools/webpack/src/ivy/transformation.ts
Expand Up @@ -74,6 +74,12 @@ export function mergeTransformers(
return result;
}

/**
* The name of the Angular platform that should be replaced within
* bootstrap call expressions to support AOT.
*/
const PLATFORM_BROWSER_DYNAMIC_NAME = 'platformBrowserDynamic';

export function replaceBootstrap(
getTypeChecker: () => ts.TypeChecker,
): ts.TransformerFactory<ts.SourceFile> {
Expand All @@ -86,7 +92,7 @@ export function replaceBootstrap(
const visitNode: ts.Visitor = (node: ts.Node) => {
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
const target = node.expression;
if (target.text === 'platformBrowserDynamic') {
if (target.text === PLATFORM_BROWSER_DYNAMIC_NAME) {
if (!bootstrapNamespace) {
bootstrapNamespace = nodeFactory.createUniqueName('__NgCli_bootstrap_');
bootstrapImport = nodeFactory.createImportDeclaration(
Expand Down Expand Up @@ -115,6 +121,10 @@ export function replaceBootstrap(
};

return (sourceFile: ts.SourceFile) => {
if (!sourceFile.text.includes(PLATFORM_BROWSER_DYNAMIC_NAME)) {
return sourceFile;
}

let updatedSourceFile = ts.visitEachChild(sourceFile, visitNode, context);

if (bootstrapImport) {
Expand Down

0 comments on commit 1e3ecbd

Please sign in to comment.