diff --git a/README.md b/README.md index 1f9fe1b8..a1250668 100644 --- a/README.md +++ b/README.md @@ -544,7 +544,8 @@ some notable options are: tailored for a web browser. In this mode `window.wasm_bindgen` will be a function that takes a path to the wasm file to fetch and instantiate. Afterwards exported functions from the wasm are available through - `window.wasm_bindgen.foo`. + `window.wasm_bindgen.foo`. Note that the name `wasm_bindgen` can be configured + with the `--no-modules-global FOO` flag. * `--typescript` - when passed a `*.d.ts` file will be generated for the generated JS file. This should allow hooking into TypeScript projects to diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 0803387c..d138c5c4 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -295,11 +295,15 @@ impl<'a> Context<'a> { return; }}); }}; - self.wasm_bindgen = Object.assign(init, __exports); + self.{global_name} = Object.assign(init, __exports); }})(); ", globals = self.globals, module = module_name, + global_name = self.config.no_modules_global + .as_ref() + .map(|s| &**s) + .unwrap_or("wasm_bindgen"), ) } else { let import_wasm = if self.config.nodejs { diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index ab1ec572..3708ad53 100644 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -21,6 +21,7 @@ pub struct Bindgen { nodejs: bool, browser: bool, no_modules: bool, + no_modules_global: Option, debug: bool, typescript: bool, demangle: bool, @@ -42,6 +43,7 @@ impl Bindgen { nodejs: false, browser: false, no_modules: false, + no_modules_global: None, debug: false, typescript: false, demangle: true, @@ -68,6 +70,11 @@ impl Bindgen { self } + pub fn no_modules_global(&mut self, name: &str) -> &mut Bindgen { + self.no_modules_global = Some(name.to_string()); + self + } + pub fn debug(&mut self, debug: bool) -> &mut Bindgen { self.debug = debug; self diff --git a/crates/cli/src/bin/wasm-bindgen.rs b/crates/cli/src/bin/wasm-bindgen.rs index 40e9fa69..d810b7e7 100644 --- a/crates/cli/src/bin/wasm-bindgen.rs +++ b/crates/cli/src/bin/wasm-bindgen.rs @@ -23,6 +23,7 @@ Options: --nodejs Generate output that only works in node.js --browser Generate output that only works in a browser --no-modules Generate output that only works in a browser (without modules) + --no-modules-global VAR Name of the global variable to initialize --typescript Output a TypeScript definition file --debug Include otherwise-extraneous debug checks in output --no-demangle Don't demangle Rust symbol names @@ -39,6 +40,7 @@ struct Args { flag_debug: bool, flag_version: bool, flag_no_demangle: bool, + flag_no_modules_global: Option, arg_input: Option, } @@ -65,6 +67,9 @@ fn main() { .debug(args.flag_debug) .demangle(!args.flag_no_demangle) .typescript(args.flag_typescript); + if let Some(name) = &args.flag_no_modules_global { + b.no_modules_global(name); + } let out_dir = match args.flag_out_dir { Some(ref p) => p,