|
| 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.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { DirEntry, Rule, UpdateRecorder } from '@angular-devkit/schematics'; |
| 10 | +import * as ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; |
| 11 | + |
| 12 | +function* visit(directory: DirEntry): IterableIterator<ts.SourceFile> { |
| 13 | + for (const path of directory.subfiles) { |
| 14 | + if (path.endsWith('.ts') && !path.endsWith('.d.ts')) { |
| 15 | + const entry = directory.file(path); |
| 16 | + if (entry) { |
| 17 | + const content = entry.content; |
| 18 | + if (content.includes('CommonEngine') && !content.includes('@angular/ssr/node')) { |
| 19 | + const source = ts.createSourceFile( |
| 20 | + entry.path, |
| 21 | + content.toString().replace(/^\uFEFF/, ''), |
| 22 | + ts.ScriptTarget.Latest, |
| 23 | + true, |
| 24 | + ); |
| 25 | + |
| 26 | + yield source; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + for (const path of directory.subdirs) { |
| 33 | + if (path === 'node_modules' || path.startsWith('.')) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + yield* visit(directory.dir(path)); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Schematics rule that identifies and updates import declarations in TypeScript files. |
| 43 | + * Specifically, it modifies imports of '@angular/ssr' by appending '/node' if the |
| 44 | + * `CommonEngine` is used from the old entry point. |
| 45 | + * |
| 46 | + */ |
| 47 | +export default function (): Rule { |
| 48 | + return (tree) => { |
| 49 | + for (const sourceFile of visit(tree.root)) { |
| 50 | + let recorder: UpdateRecorder | undefined; |
| 51 | + |
| 52 | + const allImportDeclarations = sourceFile.statements.filter((n) => ts.isImportDeclaration(n)); |
| 53 | + if (allImportDeclarations.length === 0) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + const ssrImports = allImportDeclarations.filter( |
| 58 | + (n) => ts.isStringLiteral(n.moduleSpecifier) && n.moduleSpecifier.text === '@angular/ssr', |
| 59 | + ); |
| 60 | + for (const ssrImport of ssrImports) { |
| 61 | + const ssrNamedBinding = getNamedImports(ssrImport); |
| 62 | + if (ssrNamedBinding) { |
| 63 | + const isUsingOldEntryPoint = ssrNamedBinding.elements.some((e) => |
| 64 | + e.name.text.startsWith('CommonEngine'), |
| 65 | + ); |
| 66 | + |
| 67 | + if (!isUsingOldEntryPoint) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + recorder ??= tree.beginUpdate(sourceFile.fileName); |
| 72 | + recorder.insertRight(ssrImport.moduleSpecifier.getEnd() - 1, '/node'); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (recorder) { |
| 77 | + tree.commitUpdate(recorder); |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +function getNamedImports( |
| 84 | + importDeclaration: ts.ImportDeclaration | undefined, |
| 85 | +): ts.NamedImports | undefined { |
| 86 | + const namedBindings = importDeclaration?.importClause?.namedBindings; |
| 87 | + if (namedBindings && ts.isNamedImports(namedBindings)) { |
| 88 | + return namedBindings; |
| 89 | + } |
| 90 | + |
| 91 | + return undefined; |
| 92 | +} |
0 commit comments