Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit a11734e

Browse files
committed
Add applyImportMap help function
1 parent 8bab80a commit a11734e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

server/helpers.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { basename, dirname, globToRegExp, join } from "https://deno.land/[email protected]/path/mod.ts";
22
import { JSONC } from "https://deno.land/x/[email protected]/src/jsonc.ts";
3-
import { findFile } from "../lib/fs.ts";
43
import { createGenerator } from "https://esm.sh/@unocss/[email protected]";
4+
import { findFile } from "../lib/fs.ts";
55
import log from "../lib/log.ts";
66
import util from "../lib/util.ts";
77
import { isCanary, VERSION } from "../version.ts";
@@ -28,7 +28,7 @@ export function getAlephPkgUri() {
2828
export function getUnoGenerator() {
2929
return globalIt("__UNO_GENERATOR", () => {
3030
const config: AlephConfig | undefined = Reflect.get(globalThis, "__ALEPH_CONFIG");
31-
if (config?.unocss?.presets?.length) {
31+
if (config?.unocss?.presets) {
3232
return createGenerator(config.unocss);
3333
}
3434
return null;
@@ -271,6 +271,19 @@ export async function parseImportMap(importMapFile: string): Promise<ImportMap>
271271
return importMap;
272272
}
273273

274+
export function applyImportMap(specifier: string, importMap: ImportMap): string {
275+
if (specifier in importMap.imports) {
276+
return importMap.imports[specifier];
277+
}
278+
for (const key in importMap.imports) {
279+
if (key.endsWith("/") && specifier.startsWith(key)) {
280+
return importMap.imports[key] + specifier.slice(key.length);
281+
}
282+
}
283+
// todo: support scopes
284+
return specifier;
285+
}
286+
274287
function toStringMap(v: unknown): Record<string, string> {
275288
const m: Record<string, string> = {};
276289
if (util.isPlainObject(v)) {

tests/server_helpers_test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertEquals } from "std/testing/asserts.ts";
2-
import { restoreUrl, toLocalPath } from "../server/helpers.ts";
2+
import { applyImportMap, restoreUrl, toLocalPath } from "../server/helpers.ts";
33

44
Deno.test("lib/helpers.ts: toLocalPath", () => {
55
assertEquals(toLocalPath("https://foo.com/[email protected]?action"), "/-/foo.com/[email protected]?action");
@@ -16,3 +16,19 @@ Deno.test("lib/helpers.ts: restoreUrl", () => {
1616
assertEquals(restoreUrl("/-/http_foo.com/bar?lang=us-en"), "http://foo.com/bar?lang=us-en");
1717
assertEquals(restoreUrl("/-/http_foo.com_8080/bar"), "http://foo.com:8080/bar");
1818
});
19+
20+
Deno.test("lib/helpers.ts: applyImportMap", () => {
21+
const importMap = {
22+
__filename: "",
23+
imports: {
24+
"aleph": "https://deno.land/x/aleph/mod.ts",
25+
"aleph/": "https://deno.land/x/aleph/",
26+
},
27+
scopes: {},
28+
};
29+
assertEquals(applyImportMap("aleph", importMap), "https://deno.land/x/aleph/mod.ts");
30+
assertEquals(
31+
applyImportMap("aleph/framework/react/mod.ts", importMap),
32+
"https://deno.land/x/aleph/framework/react/mod.ts",
33+
);
34+
});

0 commit comments

Comments
 (0)