fromSiblings
Imports of modules another package provides, rewritten to import from that package by name.
export function fromSiblings(text: string, kept: Set<string>, provided: Map<string, { module: string; name: string }>): string { const target = (hash: string) => { const p = provided.get(hash); if (!p) throw new Error(`internal: #${hash.slice(0, 8)} is neither packed nor provided`); return p; }; return text .replace(/import (type )?\{([^}]*)\} from "\.\/([a-z2-7]{52})\.js";/g, (m, type: string | undefined, specs: string, comp: string) => { if (kept.has(comp)) return m; const byModule = new Map<string, string[]>(); for (const spec of specs.split(",").map((x) => x.trim()).filter(Boolean)) { const s = /^(type )?h_([a-z2-7]{52})(?: as ([\w$]+))?$/.exec(spec); if (!s) throw new Error(`internal: can't read the import '${spec}'`); const p = target(s[2]!); byModule.set(p.module, [...(byModule.get(p.module) ?? []), `${s[1] ?? ""}${p.name} as ${s[3] ?? `h_${s[2]}`}`]); } return [...byModule].map(([mod, ss]) => `import ${type ?? ""}{ ${ss.join(", ")} } from "${mod}";`).join("\n"); }) .replace(/import\("\.\/([a-z2-7]{52})\.js"\)\.h_([a-z2-7]{52})/g, (m, comp: string, hash: string) => { if (kept.has(comp)) return m; const p = target(hash); return `import("${p.module}").${p.name}`; }); }