From b22a1556fb46e1c5e28a20ca34873c0b70d63cfa Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 18 Oct 2020 00:14:20 -0600 Subject: [PATCH 01/11] In NoModule builds, switched "export" keyword to "declare" in d.ts file. This aims to be the first step at getting the d.ts file working. The reason for this is that the TypeScript (3.8.3) compiler ignores "declare" statements if it sees "export" statements. Not sure if that's a bug in TypeScript. Either way, all I really did here was scan through all changes to `self.typescript` and see if it could add an "export". The only big wild-card I found was the `typescript_custom_sections`, which I think exists just to let `wasm_buildgen` annotations to add extra typescript to the document? --- crates/cli-support/src/js/mod.rs | 34 ++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 9fe2e99e25a..c60ae6c2b4e 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -536,6 +536,16 @@ impl<'a> Context<'a> { Ok(imports) } + fn declare_or_export(&self) -> String { + // With TypeScript 3.8.3, I'm seeing that any "export"s at the root level, cause TypeScript to ignore all "declare" statements. + // So using "declare" everywhere for at least the NoModules option. + if let OutputMode::NoModules { global : _ } = &self.config.mode { + String::from("declare") + } else { + String::from("export") + } + } + fn ts_for_init_fn( &self, has_memory: bool, @@ -552,11 +562,19 @@ impl<'a> Context<'a> { ("", "") }; let arg_optional = if has_module_or_path_optional { "?" } else { "" }; + // With TypeScript 3.8.3, I'm seeing that any "export"s at the root level, cause TypeScript to ignore all "declare" statements. + // So using "declare" everywhere for at least the NoModules option. + let declare_export_default; + if let OutputMode::NoModules { global : _ } = &self.config.mode { + declare_export_default = "declare"; + } else { + declare_export_default = "export default"; + } Ok(format!( "\n\ - export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;\n\ + {declare_export} type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;\n\ \n\ - export interface InitOutput {{\n\ + {declare_export} interface InitOutput {{\n\ {output}}}\n\ \n\ /**\n\ @@ -568,11 +586,13 @@ impl<'a> Context<'a> { *\n\ * @returns {{Promise}}\n\ */\n\ - export default function init \ + {declare_export_default} default function init \ (module_or_path{}: InitInput | Promise{}): Promise; ", memory_doc, arg_optional, memory_param, output = output, + declare_export = self.declare_or_export(), + declare_export_default = declare_export_default, )) } @@ -757,7 +777,7 @@ impl<'a> Context<'a> { fn write_class(&mut self, name: &str, class: &ExportedClass) -> Result<(), Error> { let mut dst = format!("class {} {{\n", name); - let mut ts_dst = format!("export {}", dst); + let mut ts_dst = format!("{} {}", self.declare_or_export(), dst); if self.config.debug && !class.has_constructor { dst.push_str( @@ -2327,7 +2347,8 @@ impl<'a> Context<'a> { AuxExportKind::Function(name) => { if let Some(ts_sig) = ts_sig { self.typescript.push_str(&docs); - self.typescript.push_str("export function "); + self.typescript.push_str(&self.declare_or_export()); + self.typescript.push_str(" function "); self.typescript.push_str(&name); self.typescript.push_str(ts_sig); self.typescript.push_str(";\n"); @@ -3056,8 +3077,9 @@ impl<'a> Context<'a> { if enum_.generate_typescript { self.typescript.push_str(&docs); + self.typescript.push_str(&self.declare_or_export()); self.typescript - .push_str(&format!("export enum {} {{", enum_.name)); + .push_str(&format!(" enum {} {{", enum_.name)); } for (name, value, comments) in enum_.variants.iter() { let variant_docs = if comments.is_empty() { From 4bbdfea71880975a5aca9d7fbd2039a41b1eb20e Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 18 Oct 2020 00:46:12 -0600 Subject: [PATCH 02/11] Trying to putting the generated methods into a `wasm_bindgen` namespace. This is a bit crude, basically anything before the "init" block is put in this namespace. Hopefully this works in general. I'm not 100% sure yet. --- crates/cli-support/src/js/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index c60ae6c2b4e..7ed175e38df 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -335,7 +335,7 @@ impl<'a> Context<'a> { module_name: &str, needs_manual_start: bool, ) -> Result<(String, String, Option), Error> { - let mut ts = self.typescript.clone(); + let mut ts; let mut js = String::new(); let mut start = None; @@ -437,6 +437,16 @@ impl<'a> Context<'a> { } } + // Before putting the static init code declaration info, put all existing typescript into a `wasm_bindgen` namespace declaration. + // Not sure if this should happen in all cases, so just adding it to NoModules for now... + if let OutputMode::NoModules { global : _ } = &self.config.mode { + ts = String::from("declare namespace wasm_bindgen {\n\t"); + ts.push_str(&self.typescript.replace("\n", "\n\t")); + ts.push_str("\n}\n"); + } else { + ts = self.typescript.clone(); + } + let (init_js, init_ts) = init; ts.push_str(&init_ts); From 0c292c49ef8e356e517d2148633870864f0b7740 Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 18 Oct 2020 01:51:59 -0600 Subject: [PATCH 03/11] Fixed a typo in the comments. --- crates/cli-support/src/js/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 7ed175e38df..6e85c91cd69 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -547,7 +547,7 @@ impl<'a> Context<'a> { } fn declare_or_export(&self) -> String { - // With TypeScript 3.8.3, I'm seeing that any "export"s at the root level, cause TypeScript to ignore all "declare" statements. + // With TypeScript 3.8.3, I'm seeing that any "export"s at the root level cause TypeScript to ignore all "declare" statements. // So using "declare" everywhere for at least the NoModules option. if let OutputMode::NoModules { global : _ } = &self.config.mode { String::from("declare") @@ -572,7 +572,7 @@ impl<'a> Context<'a> { ("", "") }; let arg_optional = if has_module_or_path_optional { "?" } else { "" }; - // With TypeScript 3.8.3, I'm seeing that any "export"s at the root level, cause TypeScript to ignore all "declare" statements. + // With TypeScript 3.8.3, I'm seeing that any "export"s at the root level cause TypeScript to ignore all "declare" statements. // So using "declare" everywhere for at least the NoModules option. let declare_export_default; if let OutputMode::NoModules { global : _ } = &self.config.mode { From 0a3d120d6dbef7e93d069dd158aa9bb9b47e6f17 Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 18 Oct 2020 10:58:46 -0600 Subject: [PATCH 04/11] In NoModules d.ts files `init()` is renamed to `wasm_bindgen()`. Based on the output JavaScript this seems to be how it works. --- crates/cli-support/src/js/mod.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 6e85c91cd69..a110cbdd804 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -574,11 +574,12 @@ impl<'a> Context<'a> { let arg_optional = if has_module_or_path_optional { "?" } else { "" }; // With TypeScript 3.8.3, I'm seeing that any "export"s at the root level cause TypeScript to ignore all "declare" statements. // So using "declare" everywhere for at least the NoModules option. - let declare_export_default; + // Also in (at least) the NoModules, the `init()` method is renamed to `wasm_bindgen()`. + let setup_function_declaration; if let OutputMode::NoModules { global : _ } = &self.config.mode { - declare_export_default = "declare"; + setup_function_declaration = "declare function wasm_bindgen"; } else { - declare_export_default = "export default"; + setup_function_declaration = "export default function init"; } Ok(format!( "\n\ @@ -596,13 +597,13 @@ impl<'a> Context<'a> { *\n\ * @returns {{Promise}}\n\ */\n\ - {declare_export_default} default function init \ + {setup_function_declaration} \ (module_or_path{}: InitInput | Promise{}): Promise; ", memory_doc, arg_optional, memory_param, output = output, declare_export = self.declare_or_export(), - declare_export_default = declare_export_default, + setup_function_declaration = setup_function_declaration, )) } From 6e04e847b552e3f1742de6548826a9533b5076ae Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 13 Dec 2020 01:18:52 -0700 Subject: [PATCH 05/11] Realized that the function declarations couldn't also have `declare`s. Then traced through the other instances where I'd switched `export` to `declare` and switched them over if they were going to end up in the same `wasm_bindgen` namespace. --- crates/cli-support/src/js/mod.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index a110cbdd804..04a40ff0249 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -546,13 +546,14 @@ impl<'a> Context<'a> { Ok(imports) } - fn declare_or_export(&self) -> String { - // With TypeScript 3.8.3, I'm seeing that any "export"s at the root level cause TypeScript to ignore all "declare" statements. - // So using "declare" everywhere for at least the NoModules option. + /// For a few instances where should (sometimes) use the "export" keyword in the TypeScript declaration file. + /// In particular, in no-modules mode, all functions/classes/enums are placed in a `declare`d namespace. + /// So they don't need to be `export`ed too (as of TypeScript 3.8.3 at least). + fn maybe_export(&self) -> String { if let OutputMode::NoModules { global : _ } = &self.config.mode { - String::from("declare") + String::from("") } else { - String::from("export") + String::from("export ") } } @@ -576,16 +577,19 @@ impl<'a> Context<'a> { // So using "declare" everywhere for at least the NoModules option. // Also in (at least) the NoModules, the `init()` method is renamed to `wasm_bindgen()`. let setup_function_declaration; + let declare_or_export; if let OutputMode::NoModules { global : _ } = &self.config.mode { + declare_or_export = "declare"; setup_function_declaration = "declare function wasm_bindgen"; } else { + declare_or_export = "export"; setup_function_declaration = "export default function init"; } Ok(format!( "\n\ - {declare_export} type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;\n\ + {declare_or_export} type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;\n\ \n\ - {declare_export} interface InitOutput {{\n\ + {declare_or_export} interface InitOutput {{\n\ {output}}}\n\ \n\ /**\n\ @@ -602,7 +606,7 @@ impl<'a> Context<'a> { ", memory_doc, arg_optional, memory_param, output = output, - declare_export = self.declare_or_export(), + declare_or_export = declare_or_export, setup_function_declaration = setup_function_declaration, )) } @@ -788,7 +792,7 @@ impl<'a> Context<'a> { fn write_class(&mut self, name: &str, class: &ExportedClass) -> Result<(), Error> { let mut dst = format!("class {} {{\n", name); - let mut ts_dst = format!("{} {}", self.declare_or_export(), dst); + let mut ts_dst = format!("{}{}", self.maybe_export(), dst); if self.config.debug && !class.has_constructor { dst.push_str( @@ -2358,8 +2362,8 @@ impl<'a> Context<'a> { AuxExportKind::Function(name) => { if let Some(ts_sig) = ts_sig { self.typescript.push_str(&docs); - self.typescript.push_str(&self.declare_or_export()); - self.typescript.push_str(" function "); + self.typescript.push_str(&self.maybe_export()); + self.typescript.push_str("function "); self.typescript.push_str(&name); self.typescript.push_str(ts_sig); self.typescript.push_str(";\n"); @@ -3088,9 +3092,9 @@ impl<'a> Context<'a> { if enum_.generate_typescript { self.typescript.push_str(&docs); - self.typescript.push_str(&self.declare_or_export()); + self.typescript.push_str(&self.maybe_export()); self.typescript - .push_str(&format!(" enum {} {{", enum_.name)); + .push_str(&format!("enum {} {{", enum_.name)); } for (name, value, comments) in enum_.variants.iter() { let variant_docs = if comments.is_empty() { From c7c90ed4930b9b6787859fe8a0ae81adcb391d70 Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 13 Dec 2020 01:31:23 -0700 Subject: [PATCH 06/11] Realized there was a nice no_modules() method. --- crates/cli-support/src/js/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 04a40ff0249..65fddb5bf52 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -439,7 +439,7 @@ impl<'a> Context<'a> { // Before putting the static init code declaration info, put all existing typescript into a `wasm_bindgen` namespace declaration. // Not sure if this should happen in all cases, so just adding it to NoModules for now... - if let OutputMode::NoModules { global : _ } = &self.config.mode { + if self.config.mode.no_modules() { ts = String::from("declare namespace wasm_bindgen {\n\t"); ts.push_str(&self.typescript.replace("\n", "\n\t")); ts.push_str("\n}\n"); @@ -550,7 +550,7 @@ impl<'a> Context<'a> { /// In particular, in no-modules mode, all functions/classes/enums are placed in a `declare`d namespace. /// So they don't need to be `export`ed too (as of TypeScript 3.8.3 at least). fn maybe_export(&self) -> String { - if let OutputMode::NoModules { global : _ } = &self.config.mode { + if self.config.mode.no_modules() { String::from("") } else { String::from("export ") @@ -578,7 +578,7 @@ impl<'a> Context<'a> { // Also in (at least) the NoModules, the `init()` method is renamed to `wasm_bindgen()`. let setup_function_declaration; let declare_or_export; - if let OutputMode::NoModules { global : _ } = &self.config.mode { + if self.config.mode.no_modules() { declare_or_export = "declare"; setup_function_declaration = "declare function wasm_bindgen"; } else { From 38af3766aa7ec6e84c63d955e778f7c3ca0ee816 Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 13 Dec 2020 01:31:55 -0700 Subject: [PATCH 07/11] Removing the extra spaces at the end of the .d.ts files. --- crates/cli-support/src/js/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 65fddb5bf52..5526ae84be5 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -602,8 +602,7 @@ impl<'a> Context<'a> { * @returns {{Promise}}\n\ */\n\ {setup_function_declaration} \ - (module_or_path{}: InitInput | Promise{}): Promise; - ", + (module_or_path{}: InitInput | Promise{}): Promise;\n", memory_doc, arg_optional, memory_param, output = output, declare_or_export = declare_or_export, From 0e731a54bec8f91f26e201768e1083db0bdbc0b0 Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 13 Dec 2020 01:49:16 -0700 Subject: [PATCH 08/11] Realized that can leave the `export` in the namespace declaration. --- crates/cli-support/src/js/mod.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 5526ae84be5..3022f5742f5 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -546,17 +546,6 @@ impl<'a> Context<'a> { Ok(imports) } - /// For a few instances where should (sometimes) use the "export" keyword in the TypeScript declaration file. - /// In particular, in no-modules mode, all functions/classes/enums are placed in a `declare`d namespace. - /// So they don't need to be `export`ed too (as of TypeScript 3.8.3 at least). - fn maybe_export(&self) -> String { - if self.config.mode.no_modules() { - String::from("") - } else { - String::from("export ") - } - } - fn ts_for_init_fn( &self, has_memory: bool, @@ -791,7 +780,7 @@ impl<'a> Context<'a> { fn write_class(&mut self, name: &str, class: &ExportedClass) -> Result<(), Error> { let mut dst = format!("class {} {{\n", name); - let mut ts_dst = format!("{}{}", self.maybe_export(), dst); + let mut ts_dst = format!("export {}", dst); if self.config.debug && !class.has_constructor { dst.push_str( @@ -2361,8 +2350,7 @@ impl<'a> Context<'a> { AuxExportKind::Function(name) => { if let Some(ts_sig) = ts_sig { self.typescript.push_str(&docs); - self.typescript.push_str(&self.maybe_export()); - self.typescript.push_str("function "); + self.typescript.push_str("export function "); self.typescript.push_str(&name); self.typescript.push_str(ts_sig); self.typescript.push_str(";\n"); @@ -3091,9 +3079,8 @@ impl<'a> Context<'a> { if enum_.generate_typescript { self.typescript.push_str(&docs); - self.typescript.push_str(&self.maybe_export()); self.typescript - .push_str(&format!("enum {} {{", enum_.name)); + .push_str(&format!("export enum {} {{", enum_.name)); } for (name, value, comments) in enum_.variants.iter() { let variant_docs = if comments.is_empty() { From 4a7cf945441da15f975b18a2c9dc6559debc6920 Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sun, 27 Dec 2020 22:06:49 -0700 Subject: [PATCH 09/11] Adding tests for the no-modules target. I'm hoping that `run.sh` is what the CI runs. --- crates/typescript-tests/no_modules.tsconfig.json | 11 +++++++++++ crates/typescript-tests/run.sh | 10 ++++++++++ 2 files changed, 21 insertions(+) create mode 100644 crates/typescript-tests/no_modules.tsconfig.json diff --git a/crates/typescript-tests/no_modules.tsconfig.json b/crates/typescript-tests/no_modules.tsconfig.json new file mode 100644 index 00000000000..1f97e261984 --- /dev/null +++ b/crates/typescript-tests/no_modules.tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es6", + "noImplicitAny": true, + "sourceMap": true, + "outDir": "nowhere", + }, + "include": [ + "pkg/no_modules/typescript_tests.d.ts" + ] +} diff --git a/crates/typescript-tests/run.sh b/crates/typescript-tests/run.sh index 95a868a58e6..8c3bed1efe2 100755 --- a/crates/typescript-tests/run.sh +++ b/crates/typescript-tests/run.sh @@ -23,3 +23,13 @@ if [ ! -d node_modules ]; then fi npm run tsc + +# Build using no-modules, and make sure can minimally be read by the TypeScript compiler. +mkdir pkg/no_modules +cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \ + ../../target/wasm32-unknown-unknown/debug/typescript_tests.wasm \ + --out-dir pkg/no_modules \ + --target no-modules \ + --typescript + +npm run tsc -- -p no_modules.tsconfig.json From 2b0e83ec8ee5cf68d283b22ca598cb6b5eebbf69 Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Sat, 9 Jan 2021 20:03:55 -0700 Subject: [PATCH 10/11] Tried to get the no-modules TypeScript tests to build against actual TypeScript. So now the `*.d.ts` files will actually be build against (modified) `*.ts` files. This should be a better check than only verifying TypeScript doesn't fail when given just the `*.d.ts` files (what it did before). Note that the `*.ts` files are generated from the `*.ts` files in `src` using some fairly simple Regular Expressions. This skips checking the `typescript_tests_bg.wasm.d.ts` file, as I have no experience using that. --- crates/typescript-tests/.gitignore | 2 ++ .../typescript-tests/no_modules.tsconfig.json | 5 +++-- crates/typescript-tests/run.sh | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/typescript-tests/.gitignore b/crates/typescript-tests/.gitignore index 2dfbe6f5bc4..834c04dd11a 100644 --- a/crates/typescript-tests/.gitignore +++ b/crates/typescript-tests/.gitignore @@ -1,2 +1,4 @@ pkg dist +src_no_modules +dist_no_modules diff --git a/crates/typescript-tests/no_modules.tsconfig.json b/crates/typescript-tests/no_modules.tsconfig.json index 1f97e261984..e3688b7d156 100644 --- a/crates/typescript-tests/no_modules.tsconfig.json +++ b/crates/typescript-tests/no_modules.tsconfig.json @@ -3,9 +3,10 @@ "target": "es6", "noImplicitAny": true, "sourceMap": true, - "outDir": "nowhere", + "outDir": "dist_no_modules", }, "include": [ - "pkg/no_modules/typescript_tests.d.ts" + "pkg/no_modules/*.d.ts", + "src_no_modules/*.ts", ] } diff --git a/crates/typescript-tests/run.sh b/crates/typescript-tests/run.sh index 8c3bed1efe2..78f261021dd 100755 --- a/crates/typescript-tests/run.sh +++ b/crates/typescript-tests/run.sh @@ -32,4 +32,24 @@ cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \ --target no-modules \ --typescript +rm -rf src_no_modules +mkdir src_no_modules +cd src +for filename in *.ts; do + # Create TypeScript file copies that don't use the module system. + if grep -q "as wasm" "$filename" + then + echo "Ignoring $filename as it uses the *.wasm.d.ts file." + else + path="../src_no_modules/$filename" + # Copy over everything but the import statements. + grep -v -w "import" "$filename" > "$path" + # Then replace "wbg."/"wbg "/"wbg[" with "wasm_bindgen" (the namespace the .d.ts file uses). + sed -i "s/wbg\./wasm_bindgen./g" "$path" + sed -i "s/wbg /wasm_bindgen /g" "$path" + sed -i "s/wbg\[/wasm_bindgen[/g" "$path" + fi +done +cd .. + npm run tsc -- -p no_modules.tsconfig.json From 564a517d3ffe06283261aba2b172dbb250c93464 Mon Sep 17 00:00:00 2001 From: aaron-human <59424745+aaron-human@users.noreply.github.com> Date: Thu, 4 Feb 2021 19:33:56 -0700 Subject: [PATCH 11/11] Adding more info about the testing changes in run.sh. --- crates/typescript-tests/run.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/typescript-tests/run.sh b/crates/typescript-tests/run.sh index 78f261021dd..f971f251bf2 100755 --- a/crates/typescript-tests/run.sh +++ b/crates/typescript-tests/run.sh @@ -24,7 +24,7 @@ fi npm run tsc -# Build using no-modules, and make sure can minimally be read by the TypeScript compiler. +# Build using no-modules. mkdir pkg/no_modules cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \ ../../target/wasm32-unknown-unknown/debug/typescript_tests.wasm \ @@ -32,19 +32,22 @@ cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \ --target no-modules \ --typescript +# Then create copies of the TypeScript tests used on "--target web" (i.e. those in ./src) but adjust them to work with "--target no-modules". +# Store the new generated test files under "src_no_modules". rm -rf src_no_modules mkdir src_no_modules cd src for filename in *.ts; do - # Create TypeScript file copies that don't use the module system. - if grep -q "as wasm" "$filename" + if grep -q "_bg.wasm" "$filename" then + # I am unsure how to import or test the "*_bg.wasm" file. + # So any test file that includes those is currently being skipped. echo "Ignoring $filename as it uses the *.wasm.d.ts file." else path="../src_no_modules/$filename" - # Copy over everything but the import statements. + # Copy over every line EXCEPT for the import statements (since "no-modules" has no modules to import). grep -v -w "import" "$filename" > "$path" - # Then replace "wbg."/"wbg "/"wbg[" with "wasm_bindgen" (the namespace the .d.ts file uses). + # Then replace all of the instances of "wbg" (the namespace all the tests use to import the *.d.ts files from "--target web") with "wasm_bindgen" (the namespace the `no-modules` *.d.ts files use). sed -i "s/wbg\./wasm_bindgen./g" "$path" sed -i "s/wbg /wasm_bindgen /g" "$path" sed -i "s/wbg\[/wasm_bindgen[/g" "$path" @@ -52,4 +55,5 @@ for filename in *.ts; do done cd .. +# Then try to build the typescript in the src_no_modules folder against the pkg/no_modules build. npm run tsc -- -p no_modules.tsconfig.json