Add a --no-modules-global flag

This can be used to configure the name of the global that's initialized so it's
not unconditionally `wasm_bindgen`.

Closes #145
This commit is contained in:
Alex Crichton 2018-04-19 13:33:58 -07:00
parent 212703671a
commit 0ade4b8cac
4 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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 {

View File

@ -21,6 +21,7 @@ pub struct Bindgen {
nodejs: bool,
browser: bool,
no_modules: bool,
no_modules_global: Option<String>,
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

View File

@ -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<String>,
arg_input: Option<PathBuf>,
}
@ -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,