migration.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. var desc = Object.getOwnPropertyDescriptor(m, k);
  12. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  13. desc = { enumerable: true, get: function() { return m[k]; } };
  14. }
  15. Object.defineProperty(o, k2, desc);
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || (function () {
  26. var ownKeys = function(o) {
  27. ownKeys = Object.getOwnPropertyNames || function (o) {
  28. var ar = [];
  29. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  30. return ar;
  31. };
  32. return ownKeys(o);
  33. };
  34. return function (mod) {
  35. if (mod && mod.__esModule) return mod;
  36. var result = {};
  37. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  38. __setModuleDefault(result, mod);
  39. return result;
  40. };
  41. })();
  42. Object.defineProperty(exports, "__esModule", { value: true });
  43. exports.default = default_1;
  44. const ts = __importStar(require("../../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
  45. const dependencies_1 = require("../../utility/dependencies");
  46. function* visit(directory) {
  47. for (const path of directory.subfiles) {
  48. if (path.endsWith('.ts') && !path.endsWith('.d.ts')) {
  49. const entry = directory.file(path);
  50. if (entry) {
  51. const content = entry.content;
  52. if (content.includes('CommonEngine') && !content.includes('@angular/ssr/node')) {
  53. const source = ts.createSourceFile(entry.path, content.toString().replace(/^\uFEFF/, ''), ts.ScriptTarget.Latest, true);
  54. yield source;
  55. }
  56. }
  57. }
  58. }
  59. for (const path of directory.subdirs) {
  60. if (path === 'node_modules' || path.startsWith('.')) {
  61. continue;
  62. }
  63. yield* visit(directory.dir(path));
  64. }
  65. }
  66. /**
  67. * Schematics rule that identifies and updates import declarations in TypeScript files.
  68. * Specifically, it modifies imports of '@angular/ssr' by appending '/node' if the
  69. * `CommonEngine` is used from the old entry point.
  70. *
  71. */
  72. function default_1() {
  73. return (tree) => {
  74. if (!(0, dependencies_1.getPackageJsonDependency)(tree, '@angular/ssr')) {
  75. return;
  76. }
  77. for (const sourceFile of visit(tree.root)) {
  78. let recorder;
  79. const allImportDeclarations = sourceFile.statements.filter((n) => ts.isImportDeclaration(n));
  80. if (allImportDeclarations.length === 0) {
  81. continue;
  82. }
  83. const ssrImports = allImportDeclarations.filter((n) => ts.isStringLiteral(n.moduleSpecifier) && n.moduleSpecifier.text === '@angular/ssr');
  84. for (const ssrImport of ssrImports) {
  85. const ssrNamedBinding = getNamedImports(ssrImport);
  86. if (ssrNamedBinding) {
  87. const isUsingOldEntryPoint = ssrNamedBinding.elements.some((e) => e.name.text.startsWith('CommonEngine'));
  88. if (!isUsingOldEntryPoint) {
  89. continue;
  90. }
  91. recorder ??= tree.beginUpdate(sourceFile.fileName);
  92. recorder.insertRight(ssrImport.moduleSpecifier.getEnd() - 1, '/node');
  93. }
  94. }
  95. if (recorder) {
  96. tree.commitUpdate(recorder);
  97. }
  98. }
  99. };
  100. }
  101. function getNamedImports(importDeclaration) {
  102. const namedBindings = importDeclaration?.importClause?.namedBindings;
  103. if (namedBindings && ts.isNamedImports(namedBindings)) {
  104. return namedBindings;
  105. }
  106. return undefined;
  107. }