UNPKG

1.63 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.isComponentAlreadyDeclared = void 0;
4const core_1 = require("@angular/core");
5const reflectionCapabilities = new core_1.ɵReflectionCapabilities();
6/**
7 * Avoid component redeclaration
8 *
9 * Checks recursively if the component has already been declared in all import Module
10 */
11const isComponentAlreadyDeclared = (componentToFind, moduleDeclarations, moduleImports) => {
12 if (moduleDeclarations &&
13 moduleDeclarations.some((declaration) => declaration === componentToFind)) {
14 // Found component in declarations array
15 return true;
16 }
17 if (!moduleImports) {
18 return false;
19 }
20 return moduleImports.some((importItem) => {
21 const extractedNgModuleMetadata = extractNgModuleMetadata(importItem);
22 if (!extractedNgModuleMetadata) {
23 // Not an NgModule
24 return false;
25 }
26 return (0, exports.isComponentAlreadyDeclared)(componentToFind, extractedNgModuleMetadata.declarations, extractedNgModuleMetadata.imports);
27 });
28};
29exports.isComponentAlreadyDeclared = isComponentAlreadyDeclared;
30const extractNgModuleMetadata = (importItem) => {
31 const target = importItem && importItem.ngModule ? importItem.ngModule : importItem;
32 const decorators = reflectionCapabilities.annotations(target);
33 if (!decorators || decorators.length === 0) {
34 return null;
35 }
36 const ngModuleDecorator = decorators.find((decorator) => decorator instanceof core_1.NgModule);
37 if (!ngModuleDecorator) {
38 return null;
39 }
40 return ngModuleDecorator;
41};