Skip to content

Commit

Permalink
Add WebAssembly namespace type generation
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Sep 19, 2023
1 parent 6915756 commit 4c1cd0e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
40 changes: 30 additions & 10 deletions packages/runtime/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 0 additions & 1 deletion packages/runtime/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { SwitchClass } from './switch';
import type { BufferSource } from './types';

declare const Switch: SwitchClass;

Expand Down

0 comments on commit 4c1cd0e

Please sign in to comment.