|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import * as ts from 'typescript'; |
| 9 | + |
| 10 | +export function importFactory(): ts.TransformerFactory<ts.SourceFile> { |
| 11 | + return (context: ts.TransformationContext) => { |
| 12 | + const visitNode: ts.Visitor = (node: ts.Node) => { |
| 13 | + // Only try to transform 'loadChildren' property assignments. |
| 14 | + if (ts.isPropertyAssignment(node)) { |
| 15 | + return replaceImport(node, context); |
| 16 | + } |
| 17 | + |
| 18 | + return ts.visitEachChild(node, visitNode, context); |
| 19 | + }; |
| 20 | + |
| 21 | + // Only transform files that contain `loadChildren`. |
| 22 | + return (sourceFile: ts.SourceFile) => ( |
| 23 | + /\bloadChildren\b/.test(sourceFile.text) |
| 24 | + ? ts.visitNode(sourceFile, visitNode) |
| 25 | + : sourceFile |
| 26 | + ); |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function replaceImport(node: ts.PropertyAssignment, context: ts.TransformationContext): ts.Node { |
| 31 | + // This ONLY matches the format below: |
| 32 | + // loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME) |
| 33 | + // It will not match nor alter variations, for instance: |
| 34 | + // - not using arrow functions |
| 35 | + // - not using `m` as the module argument |
| 36 | + // - using `await` instead of `then` |
| 37 | + // - using a default export (https://github.com/angular/angular/issues/11402) |
| 38 | + // The only parts that can change are the ones in caps: IMPORT_STRING and EXPORT_NAME. |
| 39 | + |
| 40 | + |
| 41 | + // Exit early if the structure is not what we expect. |
| 42 | + |
| 43 | + // loadChildren: something |
| 44 | + const name = node.name; |
| 45 | + if (!( |
| 46 | + ts.isIdentifier(name) |
| 47 | + && name.text == 'loadChildren' |
| 48 | + )) { |
| 49 | + return node; |
| 50 | + } |
| 51 | + |
| 52 | + // loadChildren: () => something |
| 53 | + const initializer = node.initializer; |
| 54 | + if (!( |
| 55 | + ts.isArrowFunction(initializer) |
| 56 | + && initializer.parameters.length === 0 |
| 57 | + )) { |
| 58 | + return node; |
| 59 | + } |
| 60 | + |
| 61 | + // loadChildren: () => something.then(something) |
| 62 | + const topArrowFnBody = initializer.body; |
| 63 | + if (!ts.isCallExpression(topArrowFnBody)) { return node; } |
| 64 | + |
| 65 | + const topArrowFnBodyExpr = topArrowFnBody.expression; |
| 66 | + if (!( |
| 67 | + ts.isPropertyAccessExpression(topArrowFnBodyExpr) |
| 68 | + && ts.isIdentifier(topArrowFnBodyExpr.name) |
| 69 | + )) { |
| 70 | + return node; |
| 71 | + } |
| 72 | + if (topArrowFnBodyExpr.name.text != 'then') { return node; } |
| 73 | + |
| 74 | + // loadChildren: () => import('IMPORT_STRING').then(something) |
| 75 | + const importCall = topArrowFnBodyExpr.expression; |
| 76 | + if (!( |
| 77 | + ts.isCallExpression(importCall) |
| 78 | + && importCall.expression.kind === ts.SyntaxKind.ImportKeyword |
| 79 | + && importCall.arguments.length === 1 |
| 80 | + && ts.isStringLiteral(importCall.arguments[0]) |
| 81 | + )) { |
| 82 | + return node; |
| 83 | + } |
| 84 | + |
| 85 | + // loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME) |
| 86 | + if (!( |
| 87 | + topArrowFnBody.arguments.length === 1 |
| 88 | + && ts.isArrowFunction(topArrowFnBody.arguments[0]) |
| 89 | + )) { |
| 90 | + return node; |
| 91 | + } |
| 92 | + |
| 93 | + const thenArrowFn = topArrowFnBody.arguments[0] as ts.ArrowFunction; |
| 94 | + if (!( |
| 95 | + thenArrowFn.parameters.length === 1 |
| 96 | + && ts.isPropertyAccessExpression(thenArrowFn.body) |
| 97 | + && ts.isIdentifier(thenArrowFn.body.name) |
| 98 | + )) { |
| 99 | + return node; |
| 100 | + } |
| 101 | + |
| 102 | + // At this point we know what are the nodes we need to replace. |
| 103 | + const importStringLit = importCall.arguments[0] as ts.StringLiteral; |
| 104 | + const exportNameId = thenArrowFn.body.name; |
| 105 | + |
| 106 | + // The easiest way to alter them is with a simple visitor. |
| 107 | + const replacementVisitor: ts.Visitor = (node: ts.Node) => { |
| 108 | + if (node === importStringLit) { |
| 109 | + // Transform the import string. |
| 110 | + return ts.createStringLiteral(importStringLit.text + '.ngfactory'); |
| 111 | + } else if (node === exportNameId) { |
| 112 | + // Transform the export name. |
| 113 | + return ts.createIdentifier(exportNameId.text + 'NgFactory'); |
| 114 | + } |
| 115 | + |
| 116 | + return ts.visitEachChild(node, replacementVisitor, context); |
| 117 | + }; |
| 118 | + |
| 119 | + return ts.visitEachChild(node, replacementVisitor, context); |
| 120 | +} |
0 commit comments