|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +import type { |
| 8 | + CustomElementDeclaration, |
| 9 | + CustomElementExport, |
| 10 | + Module, |
| 11 | + Package, |
| 12 | + Reference, |
| 13 | +} from 'custom-elements-manifest/schema.js'; |
| 14 | +import {isCustomElementDeclaration} from './predicates.js'; |
| 15 | +import {resolveReference} from './resolve-reference.js'; |
| 16 | + |
| 17 | +export type CustomElementInfo = { |
| 18 | + package: Package; |
| 19 | + module: Module; |
| 20 | + export: CustomElementExport; |
| 21 | + declaration: CustomElementDeclaration; |
| 22 | + declarationReference: Reference; |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Gets all the custom element exports of a package |
| 27 | + * |
| 28 | + * @param pkg The package to search |
| 29 | + * @param packageName The name of the package |
| 30 | + * @param packageVersion The version of the package |
| 31 | + */ |
| 32 | +export const getCustomElements = ( |
| 33 | + pkg: Package, |
| 34 | + packageName: string, |
| 35 | + packageVersion: string |
| 36 | +): Array<CustomElementInfo> => { |
| 37 | + const customElements: Array<CustomElementInfo> = []; |
| 38 | + for (const mod of pkg.modules) { |
| 39 | + if (mod.exports) { |
| 40 | + for (const e of mod.exports) { |
| 41 | + if (e.kind === 'custom-element-definition') { |
| 42 | + // TODO (justinfagnani): for large manifests we want to index ahead |
| 43 | + // of time to avoid polynomial lookups |
| 44 | + const decl = resolveReference( |
| 45 | + pkg, |
| 46 | + mod, |
| 47 | + e.declaration, |
| 48 | + packageName, |
| 49 | + packageVersion |
| 50 | + ); |
| 51 | + if (decl !== undefined && isCustomElementDeclaration(decl)) { |
| 52 | + customElements.push({ |
| 53 | + package: pkg, |
| 54 | + module: mod, |
| 55 | + export: e, |
| 56 | + declaration: decl, |
| 57 | + declarationReference: e.declaration, |
| 58 | + }); |
| 59 | + } else { |
| 60 | + // This is some kind of manifest error, should we warn? |
| 61 | + // Or assume it was handled in a validation pass? |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return customElements; |
| 68 | +}; |
0 commit comments