aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/viewer/tests/importmap-hooks.mjs
blob: 3769c21b7ecb77940e16c498ebe6fbc71611e4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
// Node module-resolution hook: resolve bare specifiers with the page's import map.
let map;
export function initialize(data) { map = data; }
export async function resolve(spec, ctx, next) {
  for (const [k, v] of Object.entries(map.imports)) {
    if (spec === k || (k.endsWith('/') && spec.startsWith(k))) {
      return { url: new URL(v + spec.slice(k.length), map.base).href, shortCircuit: true };
    }
  }
  return next(spec, ctx);
}