From c19e28caaa3fb8d6dd050aa4df7ea13717ab9215 Mon Sep 17 00:00:00 2001 From: Nick Giampietro Date: Fri, 1 Mar 2024 09:33:06 -0800 Subject: [PATCH] Improve ES6 support in TypeScript definitions generation Users building with --emit-tsd and the EXPORT_ES6 setting should get TypeScript definitions accounting for the fact that ES6 modules include a default export that is a functor to instantiate and access the wasm module in question When generating TS definitions, query the EXPORT_ES6 setting and, if present, add the appropriate exports to the output Include options param in default functor export for `--emit-tsd`. This serves as a starting point for incluing all options such as `locateFile` (and the slew of others), without requiring manually iterating through them Update tests to include factory function --- test/other/embind_tsgen_ignore_2.d.ts | 1 + tools/emscripten.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/test/other/embind_tsgen_ignore_2.d.ts b/test/other/embind_tsgen_ignore_2.d.ts index 81ceadf3278a3..44e4a545a5e44 100644 --- a/test/other/embind_tsgen_ignore_2.d.ts +++ b/test/other/embind_tsgen_ignore_2.d.ts @@ -109,3 +109,4 @@ interface EmbindModule { wstring_test(_0: string): string; } export type MainModule = WasmModule & EmbindModule; +export default function MainModuleFactory (options?: unknown): Promise; diff --git a/tools/emscripten.py b/tools/emscripten.py index f89ac03cc5d69..ec2f71062f4c2 100644 --- a/tools/emscripten.py +++ b/tools/emscripten.py @@ -631,6 +631,8 @@ def create_tsd(metadata, embind_tsd): if embind_tsd: export_interfaces += ' & EmbindModule' out += f'export type MainModule = {export_interfaces};\n' + if settings.EXPORT_ES6 and settings.MODULARIZE: + out += 'export default function MainModuleFactory (options?: unknown): Promise;\n' return out