Skip to content

Commit

Permalink
fix(core): resolve mapped jsr imports with subpath
Browse files Browse the repository at this point in the history
Using a patched version of denoland/import_map
  • Loading branch information
hasundue committed May 9, 2024
1 parent fb45d48 commit fe66ed3
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ jobs:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
task: test:unit
submodules: true

integration:
name: Integration
uses: hasundue/actions/.github/workflows/integration-deno.yml@main
with:
task: test:integration
submodules: true
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "core/import_map"]
path = core/import_map
url = https://github.com/hasundue/import_map.git
8 changes: 1 addition & 7 deletions core/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
"@std/jsonc": "jsr:@std/jsonc@^0.222.1",
"@std/path": "jsr:@std/path@^0.222.1",
"@std/semver": "jsr:@std/semver@^0.222.1",
"@std/testing": "jsr:@std/testing@^0.222.1",
"x/import_map": "./vendor/deno.land/x/import_map@v0.19.1/mod.ts"
},
"scopes": {
"./vendor/": {
"https://deno.land/": "./vendor/deno.land/"
}
"@std/testing": "jsr:@std/testing@^0.222.1"
}
}
13 changes: 10 additions & 3 deletions core/dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,18 @@ function addSeparator(protocol: string): string {
* // -> "https://deno.land/std@1.0.0/fs/mod.ts"
* ```
*/
export function stringify(dependency: Dependency, includePath = true): string {
const header = addSeparator(dependency.protocol);
export function stringify(
dependency: Dependency,
include: { protocol?: boolean; path?: boolean } = {},
): string {
include.protocol ??= true;
const header = include.protocol ? addSeparator(dependency.protocol) : "";

const version = dependency.version ? "@" + dependency.version : "";
const path = dependency.path;
return `${header}${dependency.name}${version}` + (includePath ? path : "");

include.path ??= true;
return `${header}${dependency.name}${version}` + (include.path ? path : "");
}

export function hasVersionRange(
Expand Down
1 change: 1 addition & 0 deletions core/import_map
Submodule import_map added at 3c8d55
22 changes: 16 additions & 6 deletions core/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { maxBy } from "@std/collections";
import { parse as parseJsonc } from "@std/jsonc";
import { ensure, is } from "@core/unknownutil";
import { toPath } from "@molt/lib/path";
import { type ImportMapJson, parseFromJson } from "x/import_map";
import { type ImportMapJson, parseFromJson } from "./import_map/js/mod.ts";
import { parse, stringify } from "./dependency.ts";

export type { ImportMapJson };

Expand Down Expand Up @@ -98,13 +99,22 @@ export async function readFromJson(
return;
}
const url = new URL(resolved);
// Find which key is used for the resolution.
// This is ridiculously inefficient, but we prefer not to reimplement
// the whole import_map module. Maybe we should rather contribute to
// the original import_map module.
// Find which key is used for the resolution. This is ridiculously
// inefficient, but we prefer not to reimplement the whole
// import_map module.
// TODO: migrate to our own import_map wasm
const replacement = maxBy(
Object.entries(json.imports)
.filter(([, value]) => resolved.includes(value))
.filter(([, value]) =>
resolved.includes(
// We need this because import_map adds a heading slash to the
// dependency when a jsr import has a path.
// e.g. jsr:/@std/testing@0.210.0/
value.startsWith("jsr:")
? stringify(parse(value), { protocol: false, path: true })
: value,
)
)
.map(([key, value]) => ({ key, value })),
({ value }) => value.length,
);
Expand Down
23 changes: 19 additions & 4 deletions core/import_map_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe("resolve()", () => {
},
);
});

it("do not resolve an url", async () => {
const importMap = await readFromJson(
new URL(
Expand All @@ -97,6 +98,7 @@ describe("resolve()", () => {
undefined,
);
});

it("resolve specifiers in a referred import map", async () => {
const importMap = await readFromJson(
new URL(
Expand All @@ -118,6 +120,23 @@ describe("resolve()", () => {
},
);
});

it("resolve a jsr specifier with path", async () => {
const dir = "../test/cases/jsr_with_path_in_import_map";
const importMap = await readFromJson(
new URL(`${dir}/deno.json`, import.meta.url),
);
assertExists(importMap);
const referrer = new URL(`${dir}/mod.ts`, import.meta.url);
assertEquals(
importMap.resolve("@std/testing/bdd", referrer),
{
resolved: "jsr:/@std/testing@0.210.0/bdd",
key: "@std/testing",
value: "jsr:@std/testing@0.210.0",
},
);
});
});

Deno.test("resolveInner", async () => {
Expand All @@ -129,8 +148,4 @@ Deno.test("resolveInner", async () => {
resolveInner("/lib.ts", referrer),
new URL("../test/cases/import_map/lib.ts", import.meta.url).href,
);
assertEquals(
resolveInner("@std/testing/bdd", referrer),
"jsr:/@std/testing@0.200.0/bdd",
);
});
16 changes: 6 additions & 10 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"pre-commit": "deno fmt && deno lint && deno task -q check && deno task -q test",
"run": "deno run -A --unstable-kv --config ./deno.json ./cli/main.ts",
"update": "deno run --unstable-kv --config ./deno.json --allow-env --allow-read --allow-write --allow-net --allow-run=git,deno ./cli/main.ts ./deno.json ./*/deno.json --changelog",
"update:commit": "deno task -q update --commit --prefix 'build(deps):'",
"vendor": "deno vendor -qf --no-config --output ./core/vendor https://deno.land/x/import_map/mod.ts && curl -fsSL -o ./core/vendor/deno.land/x/import_map@v0.19.1/import_map_bg.wasm https://deno.land/x/import_map@v0.19.1/import_map_bg.wasm"
"update:commit": "deno task -q update --commit --prefix 'build(deps):'"
},
"imports": {
"@cliffy/ansi": "jsr:@cliffy/ansi@1.0.0-rc.4",
Expand All @@ -30,8 +29,7 @@
"@std/jsonc": "jsr:@std/jsonc@^0.222.1",
"@std/path": "jsr:@std/path@^0.222.1",
"@std/semver": "jsr:@std/semver@^0.222.1",
"@std/testing": "jsr:@std/testing@^0.222.1",
"x/import_map": "https://deno.land/x/import_map@v0.19.1/mod.ts"
"@std/testing": "jsr:@std/testing@^0.222.1"
},
"scopes": {
".": {
Expand All @@ -46,11 +44,6 @@
"@molt/integration/repository": "./integration/repository.ts"
}
},
"fmt": {
"exclude": [
"CHANGELOG.md"
]
},
"lint": {
"exclude": [
"test/cases",
Expand All @@ -61,5 +54,8 @@
"no-sync-fn-in-async-fn"
]
}
}
},
"exclude": [
"core/import_map"
]
}
108 changes: 108 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fe66ed3

Please sign in to comment.