// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
//
// SPDX-License-Identifier: GPL-2.0-or-later

import { build } from "esbuild";

export async function bundleEntry(entryUrl, globalName) {
  const result = await build({
    entryPoints: [entryUrl.pathname],
    bundle: true,
    format: "iife",
    platform: "browser",
    target: "es2022",
    write: false,
    ...(globalName ? { globalName } : {}),
  });
  const output = result.outputFiles[0];
  if (!output) throw new Error(`No bundle output for ${entryUrl.pathname}`);
  return output.text;
}
