Add electron support via --omit-imports (#1958)

This commit is contained in:
Darin Morrison
2020-02-12 10:52:59 -07:00
committed by GitHub
parent ca742a84c4
commit 673e9b7830
4 changed files with 25 additions and 1 deletions

View File

@ -316,6 +316,11 @@ impl<'a> Context<'a> {
fn js_import_header(&self) -> Result<String, Error> { fn js_import_header(&self) -> Result<String, Error> {
let mut imports = String::new(); let mut imports = String::new();
if self.config.omit_imports {
return Ok(imports)
}
match &self.config.mode { match &self.config.mode {
OutputMode::NoModules { .. } => { OutputMode::NoModules { .. } => {
for (module, _items) in self.js_imports.iter() { for (module, _items) in self.js_imports.iter() {

View File

@ -26,6 +26,7 @@ pub struct Bindgen {
mode: OutputMode, mode: OutputMode,
debug: bool, debug: bool,
typescript: bool, typescript: bool,
omit_imports: bool,
demangle: bool, demangle: bool,
keep_debug: bool, keep_debug: bool,
remove_name_section: bool, remove_name_section: bool,
@ -97,6 +98,7 @@ impl Bindgen {
}, },
debug: false, debug: false,
typescript: false, typescript: false,
omit_imports: false,
demangle: true, demangle: true,
keep_debug: false, keep_debug: false,
remove_name_section: false, remove_name_section: false,
@ -222,6 +224,11 @@ impl Bindgen {
self 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 { pub fn demangle(&mut self, demangle: bool) -> &mut Bindgen {
self.demangle = demangle; self.demangle = demangle;
self self

View File

@ -28,6 +28,7 @@ Options:
--browser Hint that JS should only be compatible with a browser --browser Hint that JS should only be compatible with a browser
--typescript Output a TypeScript definition file (on by default) --typescript Output a TypeScript definition file (on by default)
--no-typescript Don't emit a *.d.ts file --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 --debug Include otherwise-extraneous debug checks in output
--no-demangle Don't demangle Rust symbol names --no-demangle Don't demangle Rust symbol names
--keep-debug Keep debug sections in wasm files --keep-debug Keep debug sections in wasm files
@ -49,6 +50,7 @@ struct Args {
flag_no_modules: bool, flag_no_modules: bool,
flag_typescript: bool, flag_typescript: bool,
flag_no_typescript: bool, flag_no_typescript: bool,
flag_omit_imports: bool,
flag_out_dir: Option<PathBuf>, flag_out_dir: Option<PathBuf>,
flag_out_name: Option<String>, flag_out_name: Option<String>,
flag_debug: bool, flag_debug: bool,
@ -109,7 +111,8 @@ fn rmain(args: &Args) -> Result<(), Error> {
.keep_debug(args.flag_keep_debug) .keep_debug(args.flag_keep_debug)
.remove_name_section(args.flag_remove_name_section) .remove_name_section(args.flag_remove_name_section)
.remove_producers_section(args.flag_remove_producers_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 { if let Some(ref name) = args.flag_no_modules_global {
b.no_modules_global(name)?; b.no_modules_global(name)?;
} }

View File

@ -48,6 +48,15 @@ is on by default.
By default, a `*.d.ts` TypeScript declaration file is generated for the By default, a `*.d.ts` TypeScript declaration file is generated for the
generated JavaScript bindings, but this flag will disable that. 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` ### `--debug`
Generates a bit more JS and wasm in "debug mode" to help catch programmer Generates a bit more JS and wasm in "debug mode" to help catch programmer