// Load a TypeScript module from src/ for node:test without any extra
// toolchain: esbuild (already a devDependency) bundles the entry — resolving
// the extensionless intra-lib imports that plain type-stripping cannot — and
// the result is imported as a data: URL. Same trick build-dataset.mjs uses for
// the WADComs additions.
import esbuild from 'esbuild';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const cache = new Map();
export async function loadTs(relPath, { base = '/' } = {}) {
const key = `${relPath}|${base}`;
if (cache.has(key)) return cache.get(key);
const { outputFiles } = await esbuild.build({
entryPoints: [resolve(ROOT, relPath)],
bundle: true,
format: 'esm',
platform: 'neutral',
write: false,
logLevel: 'silent',
define: { 'import.meta.env.BASE_URL': JSON.stringify(base) },
});
const code = outputFiles[0].text;
const mod = await import('data:text/javascript;base64,' + Buffer.from(code).toString('base64'));
cache.set(key, mod);
return mod;
}