Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hook into deno.json for deno dependencies #290

Merged
merged 3 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/providers/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SemverRange } from "sver";
// @ts-ignore
import { fetch } from "#fetch";
import { Install } from "../generator.js";
import { IImportMap, ImportMap } from "@jspm/import-map";

const cdnUrl = "https://deno.land/x/";
const stdlibUrl = "https://deno.land/std";
Expand Down Expand Up @@ -148,6 +149,41 @@ export async function getPackageConfig(
},
};
}

// If there's a package.json, return that:
const pkgJsonUrl = new URL("package.json", pkgUrl);
guybedford marked this conversation as resolved.
Show resolved Hide resolved
const pkgRes = await fetch(pkgJsonUrl.href, this.fetchOpts);
switch (pkgRes.status) {
case 200:
case 304:
return (await pkgRes.json()) as PackageConfig;
}

// If there's a deno.json, read off the top-level imports as deps:
const denoJsonUrl = new URL("deno.json", pkgUrl);
guybedford marked this conversation as resolved.
Show resolved Hide resolved
const denoRes = await fetch(denoJsonUrl.href, this.fetchOpts);
switch (denoRes.status) {
case 200:
case 304:
const json = await denoRes.json();

// If there's an "importMap" field, translate that:
if (json.importMap) {
const imRes = await fetch(
new URL(json.importMap, denoJsonUrl).href,
this.fetchOpts
);
switch (imRes.status) {
case 200:
case 304:
return translateMap(await imRes.json());
}
}

// Fallback to interpreting deno.json as an import map:
return translateMap(json);
}

return null;
}

Expand Down Expand Up @@ -233,3 +269,25 @@ export async function resolveLatestTarget(
if (registry === "deno") denoStdVersion = version;
return { registry, name, version };
}

/**
* Translates a Deno import map into a package config with corresponding
* dependencies. This is a small hack to let us handle Deno dependencies that
* are managing their secondary dependencies with deno.json import maps.
*
* TODO:
* In theory we should be doing some kind of scoped merge of the dependency's
* import map, but for now we just translate the top-level imports:
*/
function translateMap(importMap: IImportMap): PackageConfig {
let res: PackageConfig = {
dependencies: {},
};

for (const [name, url] of Object.entries(importMap?.imports ?? {})) {
if (name.startsWith("$") || name.endsWith("/")) continue;
res.dependencies[name] = url;
}

return res;
}
54 changes: 14 additions & 40 deletions test/providers/deno.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,6 @@ const oakVersion = (await lookup("denoland:oak")).resolved.version;
}
}

// {
// const generator = new Generator();

// await generator.link(new URL('./coremods/deno.js', import.meta.url).href);

// const json = generator.getMap();

// console.log(json);
// }

// {
// const generator = new Generator();

// await generator.install({ target: new URL('./coremods/', import.meta.url).href, subpath: './deno' });

// const json = generator.getMap();

// console.log(json);
// }

// {
// const generator = new Generator();

// try {
// await generator.link(new URL('./coremods/deno.notfound.js', import.meta.url).href);
// }
// catch (e) {
// console.log(e);
// }
// }

{
const generator = new Generator({
mapUrl: new URL("../../", import.meta.url),
Expand Down Expand Up @@ -222,16 +191,21 @@ const oakVersion = (await lookup("denoland:oak")).resolved.version;
json.imports["testing/asserts"],
`https://deno.land/std@0.148.0/testing/asserts.ts`
);
}

// await generator.update();

// {
// const json = generator.getMap();
{
const generator = new Generator({
defaultProvider: "deno",
});

// console.log(json);
await generator.install("denoland:fresh@1.1.5/runtime.ts");
const json = generator.getMap();

// assert.strictEqual(json.imports['fs'], `https://deno.land/std@${denoStdVersion}/fs/mod.ts`);
// assert.strictEqual(json.imports['async/abortable.ts'], `https://deno.land/std@${denoStdVersion}/async/abortable.ts`);
// assert.strictEqual(json.imports['testing/asserts'], `https://deno.land/std@${denoStdVersion}/testing/asserts.ts`);
// }
assert.strictEqual(
json.imports["fresh/runtime.ts"],
"https://deno.land/x/fresh@1.1.5/runtime.ts",
);
assert.ok(
json.scopes["https://deno.land/"]["preact"]?.includes("esm.sh"),
);
}