UNPKG

637 BJavaScriptView Raw
1// This is an esbuild (?) bug where default exports are rewritten with a number appended
2// so we'll just remove any trailing numbers
3const cleanComponentName = (cmp) => cmp.replace(/[0-9]+$/, "");
4export function generateHydrateScript(hydrateBindings, opts = {}) {
5 const { basePath = "/" } = opts;
6 const entries = Object.fromEntries(Object.entries(hydrateBindings)
7 .map(([file, exports]) => exports.map((cmp) => [
8 cleanComponentName(cmp),
9 [cmp, `${basePath}${file}`],
10 ]))
11 .flat(1));
12 return `import init from "${basePath}_static/vendor/microsite.js";\ninit(${JSON.stringify(entries)})`;
13}