| 8 | const moduleDirectory = dirname(fileURLToPath(import.meta.url)) |
| 9 | |
| 10 | export async function withGeneratedModule<Module>( |
| 11 | source: string, |
| 12 | use: (module: Module) => void | Promise<void> |
| 13 | ): Promise<void> { |
| 14 | const directory = mkdtempSync(join(moduleDirectory, ".generated-module-")) |
| 15 | try { |
| 16 | const file = join(directory, "module.mjs") |
| 17 | // Transpile only: enum artifacts need more than Node's type stripping. |
| 18 | const { code } = await transformWithOxc(source, "module.ts", { |
| 19 | typescript: { onlyRemoveTypeImports: true }, |
| 20 | sourcemap: false |
| 21 | }) |
| 22 | writeFileSync(file, code) |
| 23 | const module: Module = await import(pathToFileURL(file).href) |
| 24 | await use(module) |
| 25 | } finally { |
| 26 | rmSync(directory, { recursive: true, force: true }) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** Check all generated modules in one compiler invocation. */ |
| 31 | export async function checkGeneratedModules(sources: ReadonlyMap<string, string>): Promise<void> { |