index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 schematics_1 = require("@angular-devkit/schematics");
  45. const posix_1 = require("node:path/posix");
  46. const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
  47. const ast_utils_1 = require("../utility/ast-utils");
  48. const change_1 = require("../utility/change");
  49. const find_module_1 = require("../utility/find-module");
  50. const parse_name_1 = require("../utility/parse-name");
  51. const validation_1 = require("../utility/validation");
  52. const workspace_1 = require("../utility/workspace");
  53. const schema_1 = require("./schema");
  54. function buildRelativeModulePath(options, modulePath) {
  55. const importModulePath = (0, posix_1.join)(options.path ?? '', options.flat ? '' : schematics_1.strings.dasherize(options.name), schematics_1.strings.dasherize(options.name) + '.module');
  56. return (0, find_module_1.buildRelativePath)(modulePath, importModulePath);
  57. }
  58. function addImportToNgModule(options) {
  59. return (host) => {
  60. if (!options.module) {
  61. return host;
  62. }
  63. const modulePath = options.module;
  64. const sourceText = host.readText(modulePath);
  65. const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
  66. const relativePath = buildRelativeModulePath(options, modulePath);
  67. const changes = (0, ast_utils_1.addImportToModule)(source, modulePath, schematics_1.strings.classify(`${options.name}Module`), relativePath);
  68. const recorder = host.beginUpdate(modulePath);
  69. for (const change of changes) {
  70. if (change instanceof change_1.InsertChange) {
  71. recorder.insertLeft(change.pos, change.toAdd);
  72. }
  73. }
  74. host.commitUpdate(recorder);
  75. return host;
  76. };
  77. }
  78. function addRouteDeclarationToNgModule(options, routingModulePath) {
  79. return (host) => {
  80. if (!options.route) {
  81. return host;
  82. }
  83. if (!options.module) {
  84. throw new Error('Module option required when creating a lazy loaded routing module.');
  85. }
  86. let path;
  87. if (routingModulePath) {
  88. path = routingModulePath;
  89. }
  90. else {
  91. path = options.module;
  92. }
  93. const sourceText = host.readText(path);
  94. const addDeclaration = (0, ast_utils_1.addRouteDeclarationToModule)(ts.createSourceFile(path, sourceText, ts.ScriptTarget.Latest, true), path, buildRoute(options, options.module));
  95. const recorder = host.beginUpdate(path);
  96. recorder.insertLeft(addDeclaration.pos, addDeclaration.toAdd);
  97. host.commitUpdate(recorder);
  98. return host;
  99. };
  100. }
  101. function getRoutingModulePath(host, modulePath) {
  102. const routingModulePath = modulePath.endsWith(find_module_1.ROUTING_MODULE_EXT)
  103. ? modulePath
  104. : modulePath.replace(find_module_1.MODULE_EXT, find_module_1.ROUTING_MODULE_EXT);
  105. return host.exists(routingModulePath) ? routingModulePath : undefined;
  106. }
  107. function buildRoute(options, modulePath) {
  108. const relativeModulePath = buildRelativeModulePath(options, modulePath);
  109. const moduleName = `${schematics_1.strings.classify(options.name)}Module`;
  110. const loadChildren = `() => import('${relativeModulePath}').then(m => m.${moduleName})`;
  111. return `{ path: '${options.route}', loadChildren: ${loadChildren} }`;
  112. }
  113. function default_1(options) {
  114. return async (host) => {
  115. if (options.path === undefined) {
  116. options.path = await (0, workspace_1.createDefaultPath)(host, options.project);
  117. }
  118. if (options.module) {
  119. options.module = (0, find_module_1.findModuleFromOptions)(host, options);
  120. }
  121. let routingModulePath;
  122. const isLazyLoadedModuleGen = !!(options.route && options.module);
  123. if (isLazyLoadedModuleGen) {
  124. options.routingScope = schema_1.RoutingScope.Child;
  125. routingModulePath = getRoutingModulePath(host, options.module);
  126. }
  127. const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
  128. options.name = parsedPath.name;
  129. options.path = parsedPath.path;
  130. (0, validation_1.validateClassName)(schematics_1.strings.classify(options.name));
  131. const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
  132. options.routing || (isLazyLoadedModuleGen && routingModulePath)
  133. ? (0, schematics_1.noop)()
  134. : (0, schematics_1.filter)((path) => !path.endsWith('-routing.module.ts.template')),
  135. (0, schematics_1.applyTemplates)({
  136. ...schematics_1.strings,
  137. 'if-flat': (s) => (options.flat ? '' : s),
  138. lazyRoute: isLazyLoadedModuleGen,
  139. lazyRouteWithoutRouteModule: isLazyLoadedModuleGen && !routingModulePath,
  140. lazyRouteWithRouteModule: isLazyLoadedModuleGen && !!routingModulePath,
  141. ...options,
  142. }),
  143. (0, schematics_1.move)(parsedPath.path),
  144. ]);
  145. const moduleDasherized = schematics_1.strings.dasherize(options.name);
  146. const modulePath = `${!options.flat ? moduleDasherized + '/' : ''}${moduleDasherized}.module.ts`;
  147. const componentOptions = {
  148. module: modulePath,
  149. flat: options.flat,
  150. name: options.name,
  151. path: options.path,
  152. project: options.project,
  153. standalone: false,
  154. };
  155. return (0, schematics_1.chain)([
  156. !isLazyLoadedModuleGen ? addImportToNgModule(options) : (0, schematics_1.noop)(),
  157. addRouteDeclarationToNgModule(options, routingModulePath),
  158. (0, schematics_1.mergeWith)(templateSource),
  159. isLazyLoadedModuleGen ? (0, schematics_1.schematic)('component', componentOptions) : (0, schematics_1.noop)(),
  160. ]);
  161. };
  162. }