|
| 1 | +import { type ImportMapJson, parseFromJson, toFileUrl } from "./deps.ts"; |
| 2 | + |
| 3 | +export type ImportMap = ImportMapJson; |
| 4 | + |
| 5 | +export async function createImportMapProxy( |
| 6 | + target: ImportMapJson, |
| 7 | + root: string | URL, |
| 8 | +) { |
| 9 | + const base = root instanceof URL ? root : toFileUrl(root); |
| 10 | + const importMap = await parseFromJson(base, target); |
| 11 | + |
| 12 | + const importsProxy = new Proxy(target.imports, { |
| 13 | + get: (target, prop) => { |
| 14 | + if (typeof prop === "symbol") { |
| 15 | + throw new TypeError("Symbol properties are not supported."); |
| 16 | + } |
| 17 | + |
| 18 | + const value = target[prop]; |
| 19 | + const resolved = !value ? importMap.resolve(prop, base) : value; |
| 20 | + |
| 21 | + return resolved; |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + return new Proxy(target, { |
| 26 | + get: (target, prop) => { |
| 27 | + if (typeof prop === "symbol") { |
| 28 | + throw new TypeError("Symbol properties are not supported."); |
| 29 | + } |
| 30 | + |
| 31 | + if (prop === "toJSON") { |
| 32 | + return () => target; |
| 33 | + } |
| 34 | + |
| 35 | + if (prop === "imports") { |
| 36 | + return importsProxy; |
| 37 | + } |
| 38 | + |
| 39 | + return target[prop as keyof typeof target]; |
| 40 | + }, |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +type ImportMapProxyOptions = { |
| 45 | + root: string | URL; |
| 46 | +}; |
| 47 | + |
| 48 | +export class ImportMapProxy { |
| 49 | + imports: object; |
| 50 | + scopes: object; |
| 51 | + |
| 52 | + constructor(target: ImportMapJson, options: ImportMapProxyOptions) { |
| 53 | + const root = options.root instanceof URL |
| 54 | + ? options.root |
| 55 | + : toFileUrl(options.root); |
| 56 | + |
| 57 | + this.imports = new Proxy(target.imports ?? {}, { |
| 58 | + get: (target, prop) => { |
| 59 | + if (typeof prop === "symbol") { |
| 60 | + throw new TypeError("Symbol properties are not supported."); |
| 61 | + } |
| 62 | + |
| 63 | + const specifier = target[prop]; |
| 64 | + |
| 65 | + if (specifier) { |
| 66 | + return new URL(specifier, root).href; |
| 67 | + } |
| 68 | + |
| 69 | + return undefined; |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + this.scopes = new Proxy(target.scopes ?? {}, { |
| 74 | + get: (target, prop) => { |
| 75 | + if (typeof prop === "symbol") { |
| 76 | + throw new TypeError("Symbol properties are not supported."); |
| 77 | + } |
| 78 | + |
| 79 | + const scope = target[prop]; |
| 80 | + if (scope) { |
| 81 | + return new Proxy(scope, { |
| 82 | + get: (target, prop) => { |
| 83 | + if (typeof prop === "symbol") { |
| 84 | + throw new TypeError("Symbol properties are not supported."); |
| 85 | + } |
| 86 | + const specifier = target[prop]; |
| 87 | + if (specifier) { |
| 88 | + return new URL(specifier, root).href; |
| 89 | + } |
| 90 | + return undefined; |
| 91 | + }, |
| 92 | + }); |
| 93 | + } |
| 94 | + return undefined; |
| 95 | + }, |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
0 commit comments