Skip to content

Commit

Permalink
feat: force all deps to fetch their package.json even if not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
okikio committed Feb 26, 2022
1 parent cd670b5 commit 2cae3d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
20 changes: 9 additions & 11 deletions src/ts/plugins/cdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const CDN_RESOLVE = (host?: string): (args: OnResolveArgs) => OnResolveRe

if (typeof path === "string") {
subpath = path.replace(/^\.?\/?/, "/");
console.log("[CDN - Internal Import]", path, pkg);

if (subpath && subpath[0] !== "/")
subpath = `/${subpath}`;
Expand All @@ -46,22 +47,19 @@ export const CDN_RESOLVE = (host?: string): (args: OnResolveArgs) => OnResolveRe

if (keys.includes(args.path)) {
parsed.version = deps[args.path];
subpath = parsed.path;
}
}

if (!subpath) {
let { url } = getCDNHost(`${parsed.name}@${parsed.version}/package.json`, host);
let { url: pkgJSON_URL } = getCDNHost(`${parsed.name}@${parsed.version}/package.json`, host);

// Strongly cache package.json files
pkg = await getRequest(url, true).then((res) => res.json());
path = resolve(pkg, ".", {
require: args.kind === "require-call" || args.kind === "require-resolve",
}) || legacy(pkg);
// Strongly cache package.json files
pkg = await getRequest(pkgJSON_URL, true).then((res) => res.json());
path = resolve(pkg, subpath ? "." + subpath.replace(/^\.?\/?/, "/") : ".", {
require: args.kind === "require-call" || args.kind === "require-resolve",
}) || legacy(pkg);

if (typeof path === "string")
subpath = path.replace(/^\.?\/?/, "/");
}
if (typeof path === "string")
subpath = path.replace(/^\.?\/?/, "/");

if (subpath && subpath[0] !== "/")
subpath = `/${subpath}`;
Expand Down
20 changes: 13 additions & 7 deletions src/ts/plugins/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import { urlJoin } from "../util/path";
import { CDN_RESOLVE } from './cdn';

export async function fetchPkg(url: string) {
let response = await getRequest(url);
if (!response.ok)
throw new Error(`[getRequest] Failed to load ${response.url} (${response.status} code)`)
return {
url: response.url,
content: new Uint8Array(await response.arrayBuffer()),
};
try {
let response = await getRequest(url);
if (!response.ok)
throw new Error(`[getRequest] Failed to load ${response.url} (${response.status} code)`);

return {
url: response.url,
content: new Uint8Array(await response.arrayBuffer()),
};
} catch (err) {
throw new Error(`[getRequest] Failed at request (${url}) \n${err}`);

}
}

export const HTTP_NAMESPACE = 'http-url';
Expand Down

0 comments on commit 2cae3d9

Please sign in to comment.