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

103 lines
2.9 KiB
Rust
Raw Normal View History

2017-12-18 12:39:14 -08:00
extern crate wasm_bindgen_cli_support;
2017-12-14 19:31:01 -08:00
#[macro_use]
extern crate serde_derive;
extern crate docopt;
extern crate wasm_bindgen_shared;
#[macro_use]
extern crate failure;
2017-12-14 19:31:01 -08:00
use std::path::PathBuf;
use std::process;
2017-12-14 19:31:01 -08:00
use docopt::Docopt;
use failure::Error;
2018-06-27 22:42:34 -07:00
use wasm_bindgen_cli_support::Bindgen;
2017-12-14 19:31:01 -08:00
const USAGE: &'static str = "
Generating JS bindings for a wasm file
Usage:
wasm-bindgen [options] <input>
2017-12-27 07:50:43 -08:00
wasm-bindgen -h | --help
wasm-bindgen -V | --version
2017-12-14 19:31:01 -08:00
Options:
-h --help Show this screen.
--out-dir DIR Output directory
--nodejs Generate output that only works in node.js
--browser Generate output that only works in a browser
2018-04-11 13:59:58 +05:45
--no-modules Generate output that only works in a browser (without modules)
--no-modules-global VAR Name of the global variable to initialize
2018-04-27 07:14:46 -07:00
--typescript Output a TypeScript definition file (on by default)
--no-typescript Don't emit a *.d.ts file
--debug Include otherwise-extraneous debug checks in output
2018-04-11 11:43:18 -07:00
--no-demangle Don't demangle Rust symbol names
--keep-debug Keep debug sections in wasm files
-V --version Print the version number of wasm-bindgen
2017-12-14 19:31:01 -08:00
";
#[derive(Debug, Deserialize)]
struct Args {
2017-12-14 21:55:21 -08:00
flag_nodejs: bool,
flag_browser: bool,
2018-04-11 13:59:58 +05:45
flag_no_modules: bool,
Rewrite wasm-bindgen with ES6 modules in mind This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
2018-01-29 21:20:38 -08:00
flag_typescript: bool,
2018-04-27 07:14:46 -07:00
flag_no_typescript: bool,
Rewrite wasm-bindgen with ES6 modules in mind This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
2018-01-29 21:20:38 -08:00
flag_out_dir: Option<PathBuf>,
2017-12-20 12:50:10 -08:00
flag_debug: bool,
flag_version: bool,
2018-04-11 11:43:18 -07:00
flag_no_demangle: bool,
flag_no_modules_global: Option<String>,
flag_keep_debug: bool,
arg_input: Option<PathBuf>,
2017-12-14 19:31:01 -08:00
}
fn main() {
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
if args.flag_version {
println!("wasm-bindgen {}", wasm_bindgen_shared::version());
2018-04-04 20:06:53 +05:45
return;
}
let err = match rmain(&args) {
Ok(()) => return,
Err(e) => e,
};
eprintln!("error: {}", err);
for cause in err.iter_causes() {
eprintln!("\tcaused by: {}", cause);
}
process::exit(1);
}
fn rmain(args: &Args) -> Result<(), Error> {
let input = match args.arg_input {
Some(ref s) => s,
None => bail!("input file expected"),
};
2018-04-27 07:14:46 -07:00
let typescript = args.flag_typescript || !args.flag_no_typescript;
2017-12-14 19:31:01 -08:00
let mut b = Bindgen::new();
b.input_path(input)
2018-04-04 20:06:53 +05:45
.nodejs(args.flag_nodejs)
.browser(args.flag_browser)
2018-04-11 13:59:58 +05:45
.no_modules(args.flag_no_modules)
2018-04-04 20:06:53 +05:45
.debug(args.flag_debug)
.demangle(!args.flag_no_demangle)
.keep_debug(args.flag_keep_debug)
2018-04-27 07:14:46 -07:00
.typescript(typescript);
2018-04-19 14:28:30 -07:00
if let Some(ref name) = args.flag_no_modules_global {
b.no_modules_global(name);
}
Rewrite wasm-bindgen with ES6 modules in mind This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
2018-01-29 21:20:38 -08:00
let out_dir = match args.flag_out_dir {
Some(ref p) => p,
None => bail!("the `--out-dir` argument is now required"),
Rewrite wasm-bindgen with ES6 modules in mind This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
2018-01-29 21:20:38 -08:00
};
b.generate(out_dir)
2017-12-14 19:31:01 -08:00
}