aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/viewer/tests/importmap-hooks.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'viewer/tests/importmap-hooks.mjs')
-rw-r--r--viewer/tests/importmap-hooks.mjs11
1 files changed, 11 insertions, 0 deletions
diff --git a/viewer/tests/importmap-hooks.mjs b/viewer/tests/importmap-hooks.mjs
new file mode 100644
index 0000000..3769c21
--- /dev/null
+++ b/viewer/tests/importmap-hooks.mjs
@@ -0,0 +1,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);
+}