|
| 1 | +import type { ImportMaps, ModuleGraph } from '../../types/module-graph.js'; |
| 2 | +import { |
| 3 | + forEachAliasReExport, |
| 4 | + forEachNamespaceReExport, |
| 5 | + forEachPassThroughReExport, |
| 6 | + getStarReExportSources, |
| 7 | +} from '../visitors.js'; |
| 8 | + |
| 9 | +export const hasStrictlyNsReferences = ( |
| 10 | + graph: ModuleGraph, |
| 11 | + importsForExport: ImportMaps | undefined, |
| 12 | + identifier: string |
| 13 | +): [boolean, string?] => { |
| 14 | + if (!importsForExport) return [false]; |
| 15 | + |
| 16 | + let foundNamespace: string | undefined; |
| 17 | + |
| 18 | + const aliasByIdentifier = new Map<string, Array<{ alias: string; sources: Set<string> }>>(); |
| 19 | + const namespaceReExports = new Map<string, Array<Set<string>>>(); |
| 20 | + const namespaceEdges: Array<{ namespace: string; sources: Set<string> }> = []; |
| 21 | + const directById = new Map<string, Set<string>>(); |
| 22 | + |
| 23 | + forEachPassThroughReExport(importsForExport, (id, sources) => { |
| 24 | + directById.set(id, sources); |
| 25 | + }); |
| 26 | + |
| 27 | + forEachAliasReExport(importsForExport, (id, alias, sources) => { |
| 28 | + let arr = aliasByIdentifier.get(id); |
| 29 | + if (!arr) { |
| 30 | + arr = []; |
| 31 | + aliasByIdentifier.set(id, arr); |
| 32 | + } |
| 33 | + arr.push({ alias, sources }); |
| 34 | + }); |
| 35 | + |
| 36 | + forEachNamespaceReExport(importsForExport, (namespace, sources) => { |
| 37 | + namespaceEdges.push({ namespace, sources }); |
| 38 | + let arr = namespaceReExports.get(namespace); |
| 39 | + if (!arr) { |
| 40 | + arr = []; |
| 41 | + namespaceReExports.set(namespace, arr); |
| 42 | + } |
| 43 | + arr.push(sources); |
| 44 | + }); |
| 45 | + |
| 46 | + const starSources = getStarReExportSources(importsForExport); |
| 47 | + |
| 48 | + const followReExports = ( |
| 49 | + sources: Set<string>, |
| 50 | + nextId: string, |
| 51 | + propagateNamespace = true |
| 52 | + ): [boolean, string?] | undefined => { |
| 53 | + for (const filePath of sources) { |
| 54 | + const file = graph.get(filePath); |
| 55 | + if (!file?.imported) continue; |
| 56 | + const result = hasStrictlyNsReferences(graph, file.imported, nextId); |
| 57 | + if (result[0] === false) return result; |
| 58 | + if (propagateNamespace && result[1]) foundNamespace = result[1]; |
| 59 | + } |
| 60 | + return undefined; |
| 61 | + }; |
| 62 | + |
| 63 | + for (const ns of importsForExport.importedNs.keys()) { |
| 64 | + const hasNsRef = importsForExport.refs.has(ns); |
| 65 | + if (!hasNsRef) return [false, ns]; |
| 66 | + |
| 67 | + for (const ref of importsForExport.refs) { |
| 68 | + if (ref.startsWith(`${ns}.`)) return [false, ns]; |
| 69 | + } |
| 70 | + |
| 71 | + const nsReExports = namespaceReExports.get(ns); |
| 72 | + if (nsReExports) { |
| 73 | + for (const sources of nsReExports) { |
| 74 | + const result = followReExports(sources, identifier, false); |
| 75 | + if (result) return result; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const nsAliases = aliasByIdentifier.get(ns); |
| 80 | + if (nsAliases) { |
| 81 | + for (const { sources } of nsAliases) { |
| 82 | + const result = followReExports(sources, identifier, false); |
| 83 | + if (result) return result; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + foundNamespace = ns; |
| 88 | + } |
| 89 | + |
| 90 | + const directSources = directById.get(identifier); |
| 91 | + if (directSources) { |
| 92 | + const result = followReExports(directSources, identifier, true); |
| 93 | + if (result) return result; |
| 94 | + } |
| 95 | + |
| 96 | + if (starSources) { |
| 97 | + const result = followReExports(starSources, identifier, true); |
| 98 | + if (result) return result; |
| 99 | + } |
| 100 | + |
| 101 | + const [id, ...rest] = identifier.split('.'); |
| 102 | + const aliasEntries = aliasByIdentifier.get(id); |
| 103 | + if (aliasEntries) { |
| 104 | + for (const { alias, sources } of aliasEntries) { |
| 105 | + const result = followReExports(sources, [alias, ...rest].join('.'), true); |
| 106 | + if (result) return result; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for (const { namespace: ns, sources } of namespaceEdges) { |
| 111 | + const result = followReExports(sources, `${ns}.${identifier}`, true); |
| 112 | + if (result) return result; |
| 113 | + } |
| 114 | + |
| 115 | + if (foundNamespace) return [true, foundNamespace]; |
| 116 | + return [false]; |
| 117 | +}; |
0 commit comments