Skip to content

Commit 673e9b7

Browse files
authored
Add electron support via --omit-imports (#1958)
1 parent ca742a8 commit 673e9b7

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

crates/cli-support/src/js/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ impl<'a> Context<'a> {
316316

317317
fn js_import_header(&self) -> Result<String, Error> {
318318
let mut imports = String::new();
319+
320+
if self.config.omit_imports {
321+
return Ok(imports)
322+
}
323+
319324
match &self.config.mode {
320325
OutputMode::NoModules { .. } => {
321326
for (module, _items) in self.js_imports.iter() {

crates/cli-support/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct Bindgen {
2626
mode: OutputMode,
2727
debug: bool,
2828
typescript: bool,
29+
omit_imports: bool,
2930
demangle: bool,
3031
keep_debug: bool,
3132
remove_name_section: bool,
@@ -97,6 +98,7 @@ impl Bindgen {
9798
},
9899
debug: false,
99100
typescript: false,
101+
omit_imports: false,
100102
demangle: true,
101103
keep_debug: false,
102104
remove_name_section: false,
@@ -222,6 +224,11 @@ impl Bindgen {
222224
self
223225
}
224226

227+
pub fn omit_imports(&mut self, omit_imports: bool) -> &mut Bindgen {
228+
self.omit_imports = omit_imports;
229+
self
230+
}
231+
225232
pub fn demangle(&mut self, demangle: bool) -> &mut Bindgen {
226233
self.demangle = demangle;
227234
self

crates/cli/src/bin/wasm-bindgen.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Options:
2828
--browser Hint that JS should only be compatible with a browser
2929
--typescript Output a TypeScript definition file (on by default)
3030
--no-typescript Don't emit a *.d.ts file
31+
--omit-imports Don't emit imports in generated JavaScript
3132
--debug Include otherwise-extraneous debug checks in output
3233
--no-demangle Don't demangle Rust symbol names
3334
--keep-debug Keep debug sections in wasm files
@@ -49,6 +50,7 @@ struct Args {
4950
flag_no_modules: bool,
5051
flag_typescript: bool,
5152
flag_no_typescript: bool,
53+
flag_omit_imports: bool,
5254
flag_out_dir: Option<PathBuf>,
5355
flag_out_name: Option<String>,
5456
flag_debug: bool,
@@ -109,7 +111,8 @@ fn rmain(args: &Args) -> Result<(), Error> {
109111
.keep_debug(args.flag_keep_debug)
110112
.remove_name_section(args.flag_remove_name_section)
111113
.remove_producers_section(args.flag_remove_producers_section)
112-
.typescript(typescript);
114+
.typescript(typescript)
115+
.omit_imports(args.flag_omit_imports);
113116
if let Some(ref name) = args.flag_no_modules_global {
114117
b.no_modules_global(name)?;
115118
}

guide/src/reference/cli.md

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ is on by default.
4848
By default, a `*.d.ts` TypeScript declaration file is generated for the
4949
generated JavaScript bindings, but this flag will disable that.
5050

51+
### `--omit-imports`
52+
53+
When the `module` attribute is used with the `wasm-bindgen` macro, the code
54+
generator will emit corresponding `import` or `require` statements in the header
55+
section of the generated javascript. This flag causes those import statements to
56+
be omitted. This is necessary for some use cases, such as generating javascript
57+
which is intended to be used with Electron (with node integration disabled),
58+
where the imports are instead handled through a separate preload script.
59+
5160
### `--debug`
5261

5362
Generates a bit more JS and wasm in "debug mode" to help catch programmer

0 commit comments

Comments
 (0)