|
| 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 | + |
| 11 | +/** |
| 12 | + * Given this original source code: |
| 13 | + * |
| 14 | + * import { NgModule } from '@angular/core'; |
| 15 | + * import { Routes, RouterModule } from '@angular/router'; |
| 16 | + * |
| 17 | + * const routes: Routes = [{ |
| 18 | + * path: 'lazy', |
| 19 | + * loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule). |
| 20 | + * }]; |
| 21 | + * |
| 22 | + * @NgModule({ |
| 23 | + * imports: [RouterModule.forRoot(routes)], |
| 24 | + * exports: [RouterModule] |
| 25 | + * }) |
| 26 | + * export class AppRoutingModule { } |
| 27 | + * |
| 28 | + * NGC (View Engine) will process it into: |
| 29 | + * |
| 30 | + * import { Routes } from '@angular/router'; |
| 31 | + * const ɵ0 = () => import('./lazy/lazy.module').then(m => m.LazyModule); |
| 32 | + * const routes: Routes = [{ |
| 33 | + * path: 'lazy', |
| 34 | + * loadChildren: ɵ0 |
| 35 | + * }]; |
| 36 | + * export class AppRoutingModule { |
| 37 | + * } |
| 38 | + * export { ɵ0 }; |
| 39 | + * |
| 40 | + * The importFactory transformation will only see the AST after it is process by NGC. |
| 41 | + * You can confirm this with the code below: |
| 42 | + * |
| 43 | + * const res = ts.createPrinter().printNode(ts.EmitHint.Unspecified, sourceFile, sourceFile); |
| 44 | + * console.log(`### Original source: \n${sourceFile.text}\n###`); |
| 45 | + * console.log(`### Current source: \n${currentText}\n###`); |
| 46 | + * |
| 47 | + * At this point it doesn't yet matter what the target (ES5/ES2015/etc) is, so the original |
| 48 | + * constructs, like `class` and arrow functions, still remain. |
| 49 | + * |
| 50 | + */ |
| 51 | + |
| 52 | +export function importFactory( |
| 53 | + warningCb: (warning: string) => void, |
| 54 | +): ts.TransformerFactory<ts.SourceFile> { |
| 55 | + return (context: ts.TransformationContext) => { |
| 56 | + return (sourceFile: ts.SourceFile) => { |
| 57 | + const warning = ` |
| 58 | +Found 'loadChildren' with a non-string syntax in ${sourceFile.fileName} but could not transform it. |
| 59 | +Make sure it matches the format below: |
| 60 | +
|
| 61 | +loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME) |
| 62 | +
|
| 63 | +Please note that only IMPORT_STRING and EXPORT_NAME can be replaced in this format.`; |
| 64 | + |
| 65 | + const emitWarning = () => warningCb(warning); |
| 66 | + const visitVariableStatement: ts.Visitor = (node: ts.Node) => { |
| 67 | + if (ts.isVariableDeclaration(node)) { |
| 68 | + return replaceImport(node, context, emitWarning); |
| 69 | + } |
| 70 | + |
| 71 | + return ts.visitEachChild(node, visitVariableStatement, context); |
| 72 | + }; |
| 73 | + |
| 74 | + const visitToplevelNodes: ts.Visitor = (node: ts.Node) => { |
| 75 | + // We only care about finding variable declarations, which are found in this structure: |
| 76 | + // VariableStatement -> VariableDeclarationList -> VariableDeclaration |
| 77 | + if (ts.isVariableStatement(node)) { |
| 78 | + return ts.visitEachChild(node, visitVariableStatement, context); |
| 79 | + } |
| 80 | + |
| 81 | + // There's no point in recursing into anything but variable statements, so return the node. |
| 82 | + return node; |
| 83 | + }; |
| 84 | + |
| 85 | + return ts.visitEachChild(sourceFile, visitToplevelNodes, context); |
| 86 | + }; |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +function replaceImport( |
| 91 | + node: ts.VariableDeclaration, |
| 92 | + context: ts.TransformationContext, |
| 93 | + emitWarning: () => void, |
| 94 | +): ts.Node { |
| 95 | + // This ONLY matches the original source code format below: |
| 96 | + // loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME) |
| 97 | + // And expects that source code to be transformed by NGC (see comment for importFactory). |
| 98 | + // It will not match nor alter variations, for instance: |
| 99 | + // - not using arrow functions |
| 100 | + // - not using `m` as the module argument |
| 101 | + // - using `await` instead of `then` |
| 102 | + // - using a default export (https://github.com/angular/angular/issues/11402) |
| 103 | + // The only parts that can change are the ones in caps: IMPORT_STRING and EXPORT_NAME. |
| 104 | + |
| 105 | + // Exit early if the structure is not what we expect. |
| 106 | + |
| 107 | + // ɵ0 = something |
| 108 | + const name = node.name; |
| 109 | + if (!( |
| 110 | + ts.isIdentifier(name) |
| 111 | + && /ɵ\d+/.test(name.text) |
| 112 | + )) { |
| 113 | + return node; |
| 114 | + } |
| 115 | + |
| 116 | + const initializer = node.initializer; |
| 117 | + if (initializer === undefined) { |
| 118 | + return node; |
| 119 | + } |
| 120 | + |
| 121 | + // ɵ0 = () => something |
| 122 | + if (!( |
| 123 | + ts.isArrowFunction(initializer) |
| 124 | + && initializer.parameters.length === 0 |
| 125 | + )) { |
| 126 | + return node; |
| 127 | + } |
| 128 | + |
| 129 | + // ɵ0 = () => something.then(something) |
| 130 | + const topArrowFnBody = initializer.body; |
| 131 | + if (!ts.isCallExpression(topArrowFnBody)) { |
| 132 | + return node; |
| 133 | + } |
| 134 | + |
| 135 | + const topArrowFnBodyExpr = topArrowFnBody.expression; |
| 136 | + if (!( |
| 137 | + ts.isPropertyAccessExpression(topArrowFnBodyExpr) |
| 138 | + && ts.isIdentifier(topArrowFnBodyExpr.name) |
| 139 | + )) { |
| 140 | + return node; |
| 141 | + } |
| 142 | + if (topArrowFnBodyExpr.name.text != 'then') { |
| 143 | + return node; |
| 144 | + } |
| 145 | + |
| 146 | + // ɵ0 = () => import('IMPORT_STRING').then(something) |
| 147 | + const importCall = topArrowFnBodyExpr.expression; |
| 148 | + if (!( |
| 149 | + ts.isCallExpression(importCall) |
| 150 | + && importCall.expression.kind === ts.SyntaxKind.ImportKeyword |
| 151 | + && importCall.arguments.length === 1 |
| 152 | + && ts.isStringLiteral(importCall.arguments[0]) |
| 153 | + )) { |
| 154 | + return node; |
| 155 | + } |
| 156 | + |
| 157 | + // ɵ0 = () => import('IMPORT_STRING').then(m => m.EXPORT_NAME) |
| 158 | + if (!( |
| 159 | + topArrowFnBody.arguments.length === 1 |
| 160 | + && ts.isArrowFunction(topArrowFnBody.arguments[0]) |
| 161 | + )) { |
| 162 | + // Now that we know it's both `ɵ0` (generated by NGC) and a `import()`, start emitting a warning |
| 163 | + // if the structure isn't as expected to help users identify unusable syntax. |
| 164 | + emitWarning(); |
| 165 | + |
| 166 | + return node; |
| 167 | + } |
| 168 | + |
| 169 | + const thenArrowFn = topArrowFnBody.arguments[0] as ts.ArrowFunction; |
| 170 | + if (!( |
| 171 | + thenArrowFn.parameters.length === 1 |
| 172 | + && ts.isPropertyAccessExpression(thenArrowFn.body) |
| 173 | + && ts.isIdentifier(thenArrowFn.body.name) |
| 174 | + )) { |
| 175 | + emitWarning(); |
| 176 | + |
| 177 | + return node; |
| 178 | + } |
| 179 | + |
| 180 | + // At this point we know what are the nodes we need to replace. |
| 181 | + const importStringLit = importCall.arguments[0] as ts.StringLiteral; |
| 182 | + const exportNameId = thenArrowFn.body.name; |
| 183 | + |
| 184 | + // The easiest way to alter them is with a simple visitor. |
| 185 | + const replacementVisitor: ts.Visitor = (node: ts.Node) => { |
| 186 | + if (node === importStringLit) { |
| 187 | + // Transform the import string. |
| 188 | + return ts.createStringLiteral(importStringLit.text + '.ngfactory'); |
| 189 | + } else if (node === exportNameId) { |
| 190 | + // Transform the export name. |
| 191 | + return ts.createIdentifier(exportNameId.text + 'NgFactory'); |
| 192 | + } |
| 193 | + |
| 194 | + return ts.visitEachChild(node, replacementVisitor, context); |
| 195 | + }; |
| 196 | + |
| 197 | + return ts.visitEachChild(node, replacementVisitor, context); |
| 198 | +} |
0 commit comments