|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC 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 | + |
| 9 | +import ts from 'typescript'; |
| 10 | +import { generateJitFileUri, generateJitInlineUri } from './uri'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Creates a TypeScript Transformer to transform Angular Component resource references into |
| 14 | + * static import statements. This transformer is used in Angular's JIT compilation mode to |
| 15 | + * support processing of component resources. When in AOT mode, the Angular AOT compiler handles |
| 16 | + * this processing and this transformer is not used. |
| 17 | + * @param getTypeChecker A function that returns a TypeScript TypeChecker instance for the program. |
| 18 | + * @returns A TypeScript transformer factory. |
| 19 | + */ |
| 20 | +export function createJitResourceTransformer( |
| 21 | + getTypeChecker: () => ts.TypeChecker, |
| 22 | +): ts.TransformerFactory<ts.SourceFile> { |
| 23 | + return (context: ts.TransformationContext) => { |
| 24 | + const typeChecker = getTypeChecker(); |
| 25 | + const nodeFactory = context.factory; |
| 26 | + const resourceImportDeclarations: ts.ImportDeclaration[] = []; |
| 27 | + |
| 28 | + const visitNode: ts.Visitor = (node: ts.Node) => { |
| 29 | + if (ts.isClassDeclaration(node)) { |
| 30 | + const decorators = ts.getDecorators(node); |
| 31 | + |
| 32 | + if (!decorators || decorators.length === 0) { |
| 33 | + return node; |
| 34 | + } |
| 35 | + |
| 36 | + return nodeFactory.updateClassDeclaration( |
| 37 | + node, |
| 38 | + [ |
| 39 | + ...decorators.map((current) => |
| 40 | + visitDecorator(nodeFactory, current, typeChecker, resourceImportDeclarations), |
| 41 | + ), |
| 42 | + ...(ts.getModifiers(node) ?? []), |
| 43 | + ], |
| 44 | + node.name, |
| 45 | + node.typeParameters, |
| 46 | + node.heritageClauses, |
| 47 | + node.members, |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + return ts.visitEachChild(node, visitNode, context); |
| 52 | + }; |
| 53 | + |
| 54 | + return (sourceFile) => { |
| 55 | + const updatedSourceFile = ts.visitEachChild(sourceFile, visitNode, context); |
| 56 | + |
| 57 | + if (resourceImportDeclarations.length > 0) { |
| 58 | + return nodeFactory.updateSourceFile( |
| 59 | + updatedSourceFile, |
| 60 | + ts.setTextRange( |
| 61 | + nodeFactory.createNodeArray( |
| 62 | + [...resourceImportDeclarations, ...updatedSourceFile.statements], |
| 63 | + updatedSourceFile.statements.hasTrailingComma, |
| 64 | + ), |
| 65 | + updatedSourceFile.statements, |
| 66 | + ), |
| 67 | + updatedSourceFile.isDeclarationFile, |
| 68 | + updatedSourceFile.referencedFiles, |
| 69 | + updatedSourceFile.typeReferenceDirectives, |
| 70 | + updatedSourceFile.hasNoDefaultLib, |
| 71 | + updatedSourceFile.libReferenceDirectives, |
| 72 | + ); |
| 73 | + } else { |
| 74 | + return updatedSourceFile; |
| 75 | + } |
| 76 | + }; |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +function visitDecorator( |
| 81 | + nodeFactory: ts.NodeFactory, |
| 82 | + node: ts.Decorator, |
| 83 | + typeChecker: ts.TypeChecker, |
| 84 | + resourceImportDeclarations: ts.ImportDeclaration[], |
| 85 | +): ts.Decorator { |
| 86 | + const origin = getDecoratorOrigin(node, typeChecker); |
| 87 | + if (!origin || origin.module !== '@angular/core' || origin.name !== 'Component') { |
| 88 | + return node; |
| 89 | + } |
| 90 | + |
| 91 | + if (!ts.isCallExpression(node.expression)) { |
| 92 | + return node; |
| 93 | + } |
| 94 | + |
| 95 | + const decoratorFactory = node.expression; |
| 96 | + const args = decoratorFactory.arguments; |
| 97 | + if (args.length !== 1 || !ts.isObjectLiteralExpression(args[0])) { |
| 98 | + // Unsupported component metadata |
| 99 | + return node; |
| 100 | + } |
| 101 | + |
| 102 | + const objectExpression = args[0] as ts.ObjectLiteralExpression; |
| 103 | + const styleReplacements: ts.Expression[] = []; |
| 104 | + |
| 105 | + // visit all properties |
| 106 | + let properties = ts.visitNodes(objectExpression.properties, (node) => |
| 107 | + ts.isObjectLiteralElementLike(node) |
| 108 | + ? visitComponentMetadata(nodeFactory, node, styleReplacements, resourceImportDeclarations) |
| 109 | + : node, |
| 110 | + ); |
| 111 | + |
| 112 | + // replace properties with updated properties |
| 113 | + if (styleReplacements.length > 0) { |
| 114 | + const styleProperty = nodeFactory.createPropertyAssignment( |
| 115 | + nodeFactory.createIdentifier('styles'), |
| 116 | + nodeFactory.createArrayLiteralExpression(styleReplacements), |
| 117 | + ); |
| 118 | + |
| 119 | + properties = nodeFactory.createNodeArray([...properties, styleProperty]); |
| 120 | + } |
| 121 | + |
| 122 | + return nodeFactory.updateDecorator( |
| 123 | + node, |
| 124 | + nodeFactory.updateCallExpression( |
| 125 | + decoratorFactory, |
| 126 | + decoratorFactory.expression, |
| 127 | + decoratorFactory.typeArguments, |
| 128 | + [nodeFactory.updateObjectLiteralExpression(objectExpression, properties)], |
| 129 | + ), |
| 130 | + ); |
| 131 | +} |
| 132 | + |
| 133 | +function visitComponentMetadata( |
| 134 | + nodeFactory: ts.NodeFactory, |
| 135 | + node: ts.ObjectLiteralElementLike, |
| 136 | + styleReplacements: ts.Expression[], |
| 137 | + resourceImportDeclarations: ts.ImportDeclaration[], |
| 138 | +): ts.ObjectLiteralElementLike | undefined { |
| 139 | + if (!ts.isPropertyAssignment(node) || ts.isComputedPropertyName(node.name)) { |
| 140 | + return node; |
| 141 | + } |
| 142 | + |
| 143 | + switch (node.name.text) { |
| 144 | + case 'templateUrl': |
| 145 | + // Only analyze string literals |
| 146 | + if ( |
| 147 | + !ts.isStringLiteral(node.initializer) && |
| 148 | + !ts.isNoSubstitutionTemplateLiteral(node.initializer) |
| 149 | + ) { |
| 150 | + return node; |
| 151 | + } |
| 152 | + |
| 153 | + const url = node.initializer.text; |
| 154 | + if (!url) { |
| 155 | + return node; |
| 156 | + } |
| 157 | + |
| 158 | + return nodeFactory.updatePropertyAssignment( |
| 159 | + node, |
| 160 | + nodeFactory.createIdentifier('template'), |
| 161 | + createResourceImport( |
| 162 | + nodeFactory, |
| 163 | + generateJitFileUri(url, 'template'), |
| 164 | + resourceImportDeclarations, |
| 165 | + ), |
| 166 | + ); |
| 167 | + case 'styles': |
| 168 | + if (!ts.isArrayLiteralExpression(node.initializer)) { |
| 169 | + return node; |
| 170 | + } |
| 171 | + |
| 172 | + const inlineStyles = ts.visitNodes(node.initializer.elements, (node) => { |
| 173 | + if (!ts.isStringLiteral(node) && !ts.isNoSubstitutionTemplateLiteral(node)) { |
| 174 | + return node; |
| 175 | + } |
| 176 | + |
| 177 | + const contents = node.text; |
| 178 | + if (!contents) { |
| 179 | + // An empty inline style is equivalent to not having a style element |
| 180 | + return undefined; |
| 181 | + } |
| 182 | + |
| 183 | + return createResourceImport( |
| 184 | + nodeFactory, |
| 185 | + generateJitInlineUri(contents, 'style'), |
| 186 | + resourceImportDeclarations, |
| 187 | + ); |
| 188 | + }); |
| 189 | + |
| 190 | + // Inline styles should be placed first |
| 191 | + styleReplacements.unshift(...inlineStyles); |
| 192 | + |
| 193 | + // The inline styles will be added afterwards in combination with any external styles |
| 194 | + return undefined; |
| 195 | + case 'styleUrls': |
| 196 | + if (!ts.isArrayLiteralExpression(node.initializer)) { |
| 197 | + return node; |
| 198 | + } |
| 199 | + |
| 200 | + const externalStyles = ts.visitNodes(node.initializer.elements, (node) => { |
| 201 | + if (!ts.isStringLiteral(node) && !ts.isNoSubstitutionTemplateLiteral(node)) { |
| 202 | + return node; |
| 203 | + } |
| 204 | + |
| 205 | + const url = node.text; |
| 206 | + if (!url) { |
| 207 | + return node; |
| 208 | + } |
| 209 | + |
| 210 | + return createResourceImport( |
| 211 | + nodeFactory, |
| 212 | + generateJitFileUri(url, 'style'), |
| 213 | + resourceImportDeclarations, |
| 214 | + ); |
| 215 | + }); |
| 216 | + |
| 217 | + // External styles are applied after any inline styles |
| 218 | + styleReplacements.push(...externalStyles); |
| 219 | + |
| 220 | + // The external styles will be added afterwards in combination with any inline styles |
| 221 | + return undefined; |
| 222 | + default: |
| 223 | + // All other elements are passed through |
| 224 | + return node; |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +function createResourceImport( |
| 229 | + nodeFactory: ts.NodeFactory, |
| 230 | + url: string, |
| 231 | + resourceImportDeclarations: ts.ImportDeclaration[], |
| 232 | +): ts.Identifier { |
| 233 | + const urlLiteral = nodeFactory.createStringLiteral(url); |
| 234 | + |
| 235 | + const importName = nodeFactory.createIdentifier( |
| 236 | + `__NG_CLI_RESOURCE__${resourceImportDeclarations.length}`, |
| 237 | + ); |
| 238 | + resourceImportDeclarations.push( |
| 239 | + nodeFactory.createImportDeclaration( |
| 240 | + undefined, |
| 241 | + nodeFactory.createImportClause(false, importName, undefined), |
| 242 | + urlLiteral, |
| 243 | + ), |
| 244 | + ); |
| 245 | + |
| 246 | + return importName; |
| 247 | +} |
| 248 | + |
| 249 | +function getDecoratorOrigin( |
| 250 | + decorator: ts.Decorator, |
| 251 | + typeChecker: ts.TypeChecker, |
| 252 | +): { name: string; module: string } | null { |
| 253 | + if (!ts.isCallExpression(decorator.expression)) { |
| 254 | + return null; |
| 255 | + } |
| 256 | + |
| 257 | + let identifier: ts.Node; |
| 258 | + let name = ''; |
| 259 | + |
| 260 | + if (ts.isPropertyAccessExpression(decorator.expression.expression)) { |
| 261 | + identifier = decorator.expression.expression.expression; |
| 262 | + name = decorator.expression.expression.name.text; |
| 263 | + } else if (ts.isIdentifier(decorator.expression.expression)) { |
| 264 | + identifier = decorator.expression.expression; |
| 265 | + } else { |
| 266 | + return null; |
| 267 | + } |
| 268 | + |
| 269 | + // NOTE: resolver.getReferencedImportDeclaration would work as well but is internal |
| 270 | + const symbol = typeChecker.getSymbolAtLocation(identifier); |
| 271 | + if (symbol && symbol.declarations && symbol.declarations.length > 0) { |
| 272 | + const declaration = symbol.declarations[0]; |
| 273 | + let module: string; |
| 274 | + |
| 275 | + if (ts.isImportSpecifier(declaration)) { |
| 276 | + name = (declaration.propertyName || declaration.name).text; |
| 277 | + module = (declaration.parent.parent.parent.moduleSpecifier as ts.StringLiteral).text; |
| 278 | + } else if (ts.isNamespaceImport(declaration)) { |
| 279 | + // Use the name from the decorator namespace property access |
| 280 | + module = (declaration.parent.parent.moduleSpecifier as ts.StringLiteral).text; |
| 281 | + } else if (ts.isImportClause(declaration)) { |
| 282 | + name = (declaration.name as ts.Identifier).text; |
| 283 | + module = (declaration.parent.moduleSpecifier as ts.StringLiteral).text; |
| 284 | + } else { |
| 285 | + return null; |
| 286 | + } |
| 287 | + |
| 288 | + return { name, module }; |
| 289 | + } |
| 290 | + |
| 291 | + return null; |
| 292 | +} |
0 commit comments