add-declaration-to-ng-module.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.addDeclarationToNgModule = addDeclarationToNgModule;
  44. const schematics_1 = require("@angular-devkit/schematics");
  45. const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
  46. const ast_utils_1 = require("./ast-utils");
  47. const change_1 = require("./change");
  48. const find_module_1 = require("./find-module");
  49. function addDeclarationToNgModule(options) {
  50. return (host) => {
  51. const modulePath = options.module;
  52. if (options.skipImport || options.standalone || !modulePath) {
  53. return host;
  54. }
  55. const sourceText = host.readText(modulePath);
  56. const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
  57. const filePath = `/${options.path}/` +
  58. (options.flat ? '' : schematics_1.strings.dasherize(options.name) + '/') +
  59. schematics_1.strings.dasherize(options.name) +
  60. (options.type ? '.' : '') +
  61. schematics_1.strings.dasherize(options.type);
  62. const importPath = (0, find_module_1.buildRelativePath)(modulePath, filePath);
  63. const classifiedName = schematics_1.strings.classify(options.name) + schematics_1.strings.classify(options.type);
  64. const changes = (0, ast_utils_1.addDeclarationToModule)(source, modulePath, classifiedName, importPath);
  65. if (options.export) {
  66. changes.push(...(0, ast_utils_1.addSymbolToNgModuleMetadata)(source, modulePath, 'exports', classifiedName));
  67. }
  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. }