From 673e9b78300b38f7510ee6a4229a8ce04deca531 Mon Sep 17 00:00:00 2001 From: Darin Morrison Date: Wed, 12 Feb 2020 10:52:59 -0700 Subject: [PATCH] Add electron support via --omit-imports (#1958) --- crates/cli-support/src/js/mod.rs | 5 +++++ crates/cli-support/src/lib.rs | 7 +++++++ crates/cli/src/bin/wasm-bindgen.rs | 5 ++++- guide/src/reference/cli.md | 9 +++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index f916ab85feb..9d07120db8b 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -316,6 +316,11 @@ impl<'a> Context<'a> { fn js_import_header(&self) -> Result { let mut imports = String::new(); + + if self.config.omit_imports { + return Ok(imports) + } + match &self.config.mode { OutputMode::NoModules { .. } => { for (module, _items) in self.js_imports.iter() { diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index 1a25f5c6e57..49702c2d6dc 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -26,6 +26,7 @@ pub struct Bindgen { mode: OutputMode, debug: bool, typescript: bool, + omit_imports: bool, demangle: bool, keep_debug: bool, remove_name_section: bool, @@ -97,6 +98,7 @@ impl Bindgen { }, debug: false, typescript: false, + omit_imports: false, demangle: true, keep_debug: false, remove_name_section: false, @@ -222,6 +224,11 @@ impl Bindgen { self } + pub fn omit_imports(&mut self, omit_imports: bool) -> &mut Bindgen { + self.omit_imports = omit_imports; + self + } + pub fn demangle(&mut self, demangle: bool) -> &mut Bindgen { self.demangle = demangle; self diff --git a/crates/cli/src/bin/wasm-bindgen.rs b/crates/cli/src/bin/wasm-bindgen.rs index 57d8b4c2812..9e9969a63a3 100644 --- a/crates/cli/src/bin/wasm-bindgen.rs +++ b/crates/cli/src/bin/wasm-bindgen.rs @@ -28,6 +28,7 @@ Options: --browser Hint that JS should only be compatible with a browser --typescript Output a TypeScript definition file (on by default) --no-typescript Don't emit a *.d.ts file + --omit-imports Don't emit imports in generated JavaScript --debug Include otherwise-extraneous debug checks in output --no-demangle Don't demangle Rust symbol names --keep-debug Keep debug sections in wasm files @@ -49,6 +50,7 @@ struct Args { flag_no_modules: bool, flag_typescript: bool, flag_no_typescript: bool, + flag_omit_imports: bool, flag_out_dir: Option, flag_out_name: Option, flag_debug: bool, @@ -109,7 +111,8 @@ fn rmain(args: &Args) -> Result<(), Error> { .keep_debug(args.flag_keep_debug) .remove_name_section(args.flag_remove_name_section) .remove_producers_section(args.flag_remove_producers_section) - .typescript(typescript); + .typescript(typescript) + .omit_imports(args.flag_omit_imports); if let Some(ref name) = args.flag_no_modules_global { b.no_modules_global(name)?; } diff --git a/guide/src/reference/cli.md b/guide/src/reference/cli.md index 8afa336d634..da4f0fe6a12 100644 --- a/guide/src/reference/cli.md +++ b/guide/src/reference/cli.md @@ -48,6 +48,15 @@ is on by default. By default, a `*.d.ts` TypeScript declaration file is generated for the generated JavaScript bindings, but this flag will disable that. +### `--omit-imports` + +When the `module` attribute is used with the `wasm-bindgen` macro, the code +generator will emit corresponding `import` or `require` statements in the header +section of the generated javascript. This flag causes those import statements to +be omitted. This is necessary for some use cases, such as generating javascript +which is intended to be used with Electron (with node integration disabled), +where the imports are instead handled through a separate preload script. + ### `--debug` Generates a bit more JS and wasm in "debug mode" to help catch programmer