'use strict'; // src/parse.ts function parsePackage(pkg) { const matched = /^(@(?[^/]+)\/(?[^@/]+))(?:@(?[^/]+))?(?\/.*)?$/.exec( pkg ) || /^(?[^@/]+)(?:@(?[^/]+))?(?\/.*)?$/.exec(pkg); if (!matched || !matched.groups?.name) { throw new Error(`Invalid package name: ${pkg}`); } const version = matched.groups?.version || "latest"; const path = matched.groups?.path; const scope = matched.groups?.scope; const name = scope ? `@${scope}/${matched.groups.name}` : matched.groups.name; return { name, version, path, scope, full: `${name}@${version}${path || ""}` }; } // src/esm.ts function buildESMUrl(module, options) { try { const pkg = parsePackage(module); const url = new URL( `${options?.external === true ? "*" : ""}${pkg.full}`, "https://esm.sh/" ); if (options?.deps) { url.searchParams.append("deps", options.deps.join(",")); } if (options?.alias) { url.searchParams.append( "alias", Object.entries(options.alias).map(([key, value]) => `${key}:${value}`).join(",") ); } if (options?.treeShake) { url.searchParams.append("exports", options.treeShake.join(",")); } if (options?.bundle) { url.searchParams.append("bundle", options.bundle); } if (options?.dev) { url.searchParams.append("dev", "true"); } if (options?.esbuild) { if (options.esbuild.target) { url.searchParams.append("target", options.esbuild.target); } if (options.esbuild.conditions) { url.searchParams.append( "conditions", options.esbuild.conditions.join(",") ); } if (options.esbuild.keepNames) { url.searchParams.append("keep-names", "true"); } if (options.esbuild.ignoreAnnotations) { url.searchParams.append("ignore-annotations", "true"); } } if (options?.worker) { url.searchParams.append("worker", "true"); } if (options?.css) { url.searchParams.append("css", "true"); } if (options?.wasm) { url.searchParams.append("module", "true"); } if (options?.external && Array.isArray(options.external)) { url.searchParams.append("external", options.external.join(",")); } if (options?.pin) { url.searchParams.append("pin", options.pin); } if (options?.noDts) { url.searchParams.append("no-dts", "true"); } if (options?.raw) { url.searchParams.append("raw", "true"); } return url; } catch (err) { return void 0; } } async function resolveESMTypes(url) { if (url instanceof URL) { url = url.toString(); } const headers = await fetch(url).then((res) => res.headers); return headers.get("x-typescript-types"); } // src/jsdelivr.ts function buildJSDelivrUrl(module) { try { const pkg = parsePackage(module); return new URL(pkg.full, "https://cdn.jsdelivr.net/npm/"); } catch (err) { return void 0; } } // src/skypack.ts function buildSkypackUrl(module, options) { try { const pkg = parsePackage(module); const url = new URL(pkg.full, "https://cdn.skypack.dev/"); if (options?.dts) { url.searchParams.set("dts", "true"); } if (options?.min) { url.searchParams.set("min", "true"); } return url; } catch (err) { return void 0; } } async function resolveSkypackHeaders(url) { if (url instanceof URL) { url = url.toString(); } const headers = await fetch(url).then((res) => res.headers); return { typesUrl: headers.has("x-typescript-types") ? `https://cdn.skypack.dev${headers.get("x-typescript-types")}` : void 0, pinnedUrl: headers.has("x-pinned-url") ? `https://cdn.skypack.dev${headers.get("x-pinned-url")}` : void 0, importUrl: headers.has("x-import-url") ? `https://cdn.skypack.dev${headers.get("x-import-url")}` : void 0 }; } // src/unpkg.ts function buildUnpkgUrl(module, options) { try { const pkg = parsePackage(module); const url = new URL(pkg.full, "https://unpkg.com/"); if (options?.meta) { url.searchParams.set("meta", "true"); } if (options?.module) { url.searchParams.set("module", "true"); } return url; } catch (err) { return void 0; } } // src/index.ts var CDN_URLS = { skypack: "https://cdn.skypack.dev", esm: "https://esm.sh", unpkg: "https://unpkg.com", jsdelivr: "https://cdn.jsdelivr.net/npm" }; function normalizeCdnUrl(cdn, module) { return `${CDN_URLS[cdn]}/${module.replace(/^\//, "")}`; } function buildCDNUrl(cdn) { switch (cdn) { case "skypack": return buildSkypackUrl; case "esm": return buildESMUrl; case "unpkg": return buildUnpkgUrl; case "jsdelivr": return buildJSDelivrUrl; default: throw new Error(`Unknown CDN: ${cdn}`); } } exports.CDN_URLS = CDN_URLS; exports.buildCDNUrl = buildCDNUrl; exports.buildESMUrl = buildESMUrl; exports.buildJSDelivrUrl = buildJSDelivrUrl; exports.buildSkypackUrl = buildSkypackUrl; exports.buildUnpkgUrl = buildUnpkgUrl; exports.normalizeCdnUrl = normalizeCdnUrl; exports.parsePackage = parsePackage; exports.resolveESMTypes = resolveESMTypes; exports.resolveSkypackHeaders = resolveSkypackHeaders;