_loadts.mjs (1134B)
1 // Load a TypeScript module from src/ for node:test without any extra 2 // toolchain: esbuild (already a devDependency) bundles the entry — resolving 3 // the extensionless intra-lib imports that plain type-stripping cannot — and 4 // the result is imported as a data: URL. Same trick build-dataset.mjs uses for 5 // the WADComs additions. 6 import esbuild from 'esbuild'; 7 import { fileURLToPath } from 'node:url'; 8 import { dirname, resolve } from 'node:path'; 9 10 const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..'); 11 const cache = new Map(); 12 13 export async function loadTs(relPath, { base = '/' } = {}) { 14 const key = `${relPath}|${base}`; 15 if (cache.has(key)) return cache.get(key); 16 const { outputFiles } = await esbuild.build({ 17 entryPoints: [resolve(ROOT, relPath)], 18 bundle: true, 19 format: 'esm', 20 platform: 'neutral', 21 write: false, 22 logLevel: 'silent', 23 define: { 'import.meta.env.BASE_URL': JSON.stringify(base) }, 24 }); 25 const code = outputFiles[0].text; 26 const mod = await import('data:text/javascript;base64,' + Buffer.from(code).toString('base64')); 27 cache.set(key, mod); 28 return mod; 29 }