migration.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. const latest_versions_1 = require("../../utility/latest-versions");
  47. function* visit(directory) {
  48. for (const path of directory.subfiles) {
  49. if (path.endsWith('.ts') && !path.endsWith('.d.ts')) {
  50. const entry = directory.file(path);
  51. if (entry) {
  52. const content = entry.content;
  53. if (content.includes('provideServerRendering') &&
  54. content.includes('@angular/platform-server')) {
  55. // Only need to rename the import so we can just string replacements.
  56. yield [entry.path, content.toString()];
  57. }
  58. }
  59. }
  60. }
  61. for (const path of directory.subdirs) {
  62. if (path === 'node_modules' || path.startsWith('.')) {
  63. continue;
  64. }
  65. yield* visit(directory.dir(path));
  66. }
  67. }
  68. function default_1() {
  69. return async (tree) => {
  70. let angularSSRAdded = false;
  71. for (const [filePath, content] of visit(tree.root)) {
  72. let updatedContent = content;
  73. const ssrImports = new Set();
  74. const platformServerImports = new Set();
  75. const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
  76. sourceFile.forEachChild((node) => {
  77. if (ts.isImportDeclaration(node)) {
  78. const moduleSpecifier = node.moduleSpecifier.getText(sourceFile);
  79. if (moduleSpecifier.includes('@angular/platform-server')) {
  80. const importClause = node.importClause;
  81. if (importClause &&
  82. importClause.namedBindings &&
  83. ts.isNamedImports(importClause.namedBindings)) {
  84. const namedImports = importClause.namedBindings.elements.map((e) => e.getText(sourceFile));
  85. namedImports.forEach((importName) => {
  86. if (importName === 'provideServerRendering') {
  87. ssrImports.add(importName);
  88. }
  89. else {
  90. platformServerImports.add(importName);
  91. }
  92. });
  93. }
  94. updatedContent = updatedContent.replace(node.getFullText(sourceFile), '');
  95. }
  96. else if (moduleSpecifier.includes('@angular/ssr')) {
  97. const importClause = node.importClause;
  98. if (importClause &&
  99. importClause.namedBindings &&
  100. ts.isNamedImports(importClause.namedBindings)) {
  101. importClause.namedBindings.elements.forEach((e) => {
  102. ssrImports.add(e.getText(sourceFile));
  103. });
  104. }
  105. updatedContent = updatedContent.replace(node.getFullText(sourceFile), '');
  106. }
  107. }
  108. });
  109. if (platformServerImports.size > 0) {
  110. updatedContent =
  111. `import { ${Array.from(platformServerImports).sort().join(', ')} } from '@angular/platform-server';\n` +
  112. updatedContent;
  113. }
  114. if (ssrImports.size > 0) {
  115. updatedContent =
  116. `import { ${Array.from(ssrImports).sort().join(', ')} } from '@angular/ssr';\n` +
  117. updatedContent;
  118. }
  119. if (content !== updatedContent) {
  120. tree.overwrite(filePath, updatedContent);
  121. if (!angularSSRAdded) {
  122. (0, dependencies_1.addPackageJsonDependency)(tree, {
  123. name: '@angular/ssr',
  124. version: latest_versions_1.latestVersions.AngularSSR,
  125. type: dependencies_1.NodeDependencyType.Default,
  126. overwrite: false,
  127. });
  128. angularSSRAdded = true;
  129. }
  130. }
  131. }
  132. };
  133. }