cdn-resolve
Version:
Resolves import paths to their CDN equivalents
35 lines (31 loc) • 910 B
JavaScript
;
// src/parse.ts
function parsePackage(pkg) {
const matched = /^(@(?<scope>[^/]+)\/(?<name>[^@/]+))(?:@(?<version>[^/]+))?(?<path>\/.*)?$/.exec(
pkg
) || /^(?<name>[^@/]+)(?:@(?<version>[^/]+))?(?<path>\/.*)?$/.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/jsdelivr.ts
function buildJSDelivrUrl(module) {
try {
const pkg = parsePackage(module);
return new URL(pkg.full, "https://cdn.jsdelivr.net/npm/");
} catch {
return void 0;
}
}
exports.buildJSDelivrUrl = buildJSDelivrUrl;