From 4c1cd0e2ef91235ec68bddc0c75f39a337964482 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 19 Sep 2023 10:13:51 -0300 Subject: [PATCH] Add `WebAssembly` namespace type generation --- packages/runtime/build.mjs | 40 +++++++++++++++++++++++++++--------- packages/runtime/src/wasm.ts | 1 - 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/packages/runtime/build.mjs b/packages/runtime/build.mjs index dc0c2aa1..eeb81d5a 100644 --- a/packages/runtime/build.mjs +++ b/packages/runtime/build.mjs @@ -58,16 +58,36 @@ output = output.replace(/\bHTMLCanvasElement\b/g, 'Canvas'); // hurt, but also isn't necessary. Clean that up. output = output.trim().split('\n').slice(0, -1).join('\n'); -let [output2] = generateDtsBundle( - [ +function generateNamespace(name, filePath) { + let [output] = generateDtsBundle( + [ + { + filePath, + }, + ], { - filePath: './src/wasm.ts', - }, - ], - { - noBanner: true, - } -); -console.log({ output2 }); + noBanner: true, + } + ); + output = output.replace(/^export (declare )?/gm, ''); + output = output.replace(/\bimplements (.*){/g, (_, matches) => { + const filtered = matches + .split(',') + .map((i) => i.trim()) + .filter((i) => !i.startsWith(`${name}.`)); + if (filtered.length > 0) { + return `implements ${filtered.join(', ')} {`; + } + return '{'; + }); + output = output.trim().split('\n').slice(2, -2).map(l => ` ${l}`).join('\n'); + //console.log({ output }); + return `declare namespace ${name} {\n${output}\n}\n`; +} + +// `dts-bundle-generator` does not currently support namespaces, +// so we need to add those in manually: +// See: https://github.com/timocov/dts-bundle-generator/issues/134 +output += generateNamespace('WebAssembly', './src/wasm.ts'); fs.writeFileSync(new URL('index.d.ts', distDir), output); diff --git a/packages/runtime/src/wasm.ts b/packages/runtime/src/wasm.ts index bdf431e0..3de36462 100644 --- a/packages/runtime/src/wasm.ts +++ b/packages/runtime/src/wasm.ts @@ -1,5 +1,4 @@ import type { SwitchClass } from './switch'; -import type { BufferSource } from './types'; declare const Switch: SwitchClass;