|
| 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 } from '@angular-devkit/schematics'; |
| 10 | +import * as ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; |
| 11 | +import { getPackageJsonDependency } from '../../utility/dependencies'; |
| 12 | + |
| 13 | +function* visit(directory: DirEntry): IterableIterator<[fileName: string, contents: string]> { |
| 14 | + for (const path of directory.subfiles) { |
| 15 | + if (path.endsWith('.ts') && !path.endsWith('.d.ts')) { |
| 16 | + const entry = directory.file(path); |
| 17 | + if (entry) { |
| 18 | + const content = entry.content; |
| 19 | + if (content.includes('provideServerRouting') && content.includes('@angular/ssr')) { |
| 20 | + // Only need to rename the import so we can just string replacements. |
| 21 | + yield [entry.path, content.toString()]; |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + for (const path of directory.subdirs) { |
| 28 | + if (path === 'node_modules' || path.startsWith('.')) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + yield* visit(directory.dir(path)); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export default function (): Rule { |
| 37 | + return async (tree) => { |
| 38 | + if (!getPackageJsonDependency(tree, '@angular/ssr')) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + for (const [filePath, content] of visit(tree.root)) { |
| 43 | + const recorder = tree.beginUpdate(filePath); |
| 44 | + const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true); |
| 45 | + |
| 46 | + function visit(node: ts.Node) { |
| 47 | + if ( |
| 48 | + ts.isPropertyAssignment(node) && |
| 49 | + ts.isIdentifier(node.name) && |
| 50 | + node.name.text === 'providers' && |
| 51 | + ts.isArrayLiteralExpression(node.initializer) |
| 52 | + ) { |
| 53 | + const providersArray = node.initializer; |
| 54 | + const newProviders = providersArray.elements |
| 55 | + .filter((el) => { |
| 56 | + return !( |
| 57 | + ts.isCallExpression(el) && |
| 58 | + ts.isIdentifier(el.expression) && |
| 59 | + el.expression.text === 'provideServerRendering' |
| 60 | + ); |
| 61 | + }) |
| 62 | + .map((el) => { |
| 63 | + if ( |
| 64 | + ts.isCallExpression(el) && |
| 65 | + ts.isIdentifier(el.expression) && |
| 66 | + el.expression.text === 'provideServerRouting' |
| 67 | + ) { |
| 68 | + const [withRouteVal, ...others] = el.arguments.map((arg) => arg.getText()); |
| 69 | + |
| 70 | + return `provideServerRendering(withRoutes(${withRouteVal})${others.length ? ', ' + others.join(', ') : ''})`; |
| 71 | + } |
| 72 | + |
| 73 | + return el.getText(); |
| 74 | + }); |
| 75 | + |
| 76 | + // Update the 'providers' array in the source file |
| 77 | + recorder.remove(providersArray.getStart(), providersArray.getWidth()); |
| 78 | + recorder.insertRight(providersArray.getStart(), `[${newProviders.join(', ')}]`); |
| 79 | + } |
| 80 | + |
| 81 | + ts.forEachChild(node, visit); |
| 82 | + } |
| 83 | + |
| 84 | + // Visit all nodes to update 'providers' |
| 85 | + visit(sourceFile); |
| 86 | + |
| 87 | + // Update imports by removing 'provideServerRouting' |
| 88 | + const importDecl = sourceFile.statements.find( |
| 89 | + (stmt) => |
| 90 | + ts.isImportDeclaration(stmt) && |
| 91 | + ts.isStringLiteral(stmt.moduleSpecifier) && |
| 92 | + stmt.moduleSpecifier.text === '@angular/ssr', |
| 93 | + ) as ts.ImportDeclaration | undefined; |
| 94 | + |
| 95 | + if (importDecl?.importClause?.namedBindings) { |
| 96 | + const namedBindings = importDecl?.importClause.namedBindings; |
| 97 | + |
| 98 | + if (ts.isNamedImports(namedBindings)) { |
| 99 | + const elements = namedBindings.elements; |
| 100 | + const updatedElements = elements |
| 101 | + .map((el) => el.getText()) |
| 102 | + .filter((x) => x !== 'provideServerRouting'); |
| 103 | + |
| 104 | + updatedElements.push('withRoutes'); |
| 105 | + |
| 106 | + recorder.remove(namedBindings.getStart(), namedBindings.getWidth()); |
| 107 | + recorder.insertLeft(namedBindings.getStart(), `{ ${updatedElements.sort().join(', ')} }`); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + tree.commitUpdate(recorder); |
| 112 | + } |
| 113 | + }; |
| 114 | +} |
0 commit comments