wasm-bindgen/crates/cli/src/bin/wasm-bindgen.rs
Alex Crichton cd3781cb73 Switch the --browser argument to --web
This commit reverts part of the implementation of [RFC 6]. That RFC
specified that the `--browser` flag was going to be repurposed for the
new "natively loadable as ES module output", but unfortunately the
breakage is far broader than initially expected. It turns out that
`wasm-pack` passes `--browser` by default which means that a change to
break `--browser` would break all historical versions of `wasm-pack`
which is a bit much for now.

To solve this the `--browser` flag is going back to what it represents
on the current released version of `wasm-bindgen` (optimize away some
node.js checks in a few places for bundler-style output) and a new
`--web` flag is being introduced as the new deployment strategy.

[RFC 6]: https://github.com/rustwasm/rfcs/pull/6

Closes #1318
2019-03-07 08:26:36 -08:00

125 lines
4.2 KiB
Rust

use docopt::Docopt;
use failure::{bail, Error};
use serde::Deserialize;
use std::path::PathBuf;
use std::process;
use wasm_bindgen_cli_support::{Bindgen, EncodeInto};
// no need for jemalloc bloat in this binary (and we don't need speed)
#[global_allocator]
static ALLOC: std::alloc::System = std::alloc::System;
const USAGE: &'static str = "
Generating JS bindings for a wasm file
Usage:
wasm-bindgen [options] <input>
wasm-bindgen -h | --help
wasm-bindgen -V | --version
Options:
-h --help Show this screen.
--out-dir DIR Output directory
--out-name VAR Set a custom output filename (Without extension. Defaults to crate name)
--nodejs Generate output that only works in node.js
--web Generate output that only works in a browser
--browser Hint that JS should only be compatible with 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 (on by default)
--no-typescript Don't emit a *.d.ts file
--debug Include otherwise-extraneous debug checks in output
--no-demangle Don't demangle Rust symbol names
--keep-debug Keep debug sections in wasm files
--remove-name-section Remove the debugging `name` section of the file
--remove-producers-section Remove the telemetry `producers` section
--encode-into MODE Whether or not to use TextEncoder#encodeInto,
valid values are [test, always, never]
-V --version Print the version number of wasm-bindgen
";
#[derive(Debug, Deserialize)]
struct Args {
flag_nodejs: bool,
flag_browser: bool,
flag_web: bool,
flag_no_modules: bool,
flag_typescript: bool,
flag_no_typescript: bool,
flag_out_dir: Option<PathBuf>,
flag_out_name: Option<String>,
flag_debug: bool,
flag_version: bool,
flag_no_demangle: bool,
flag_no_modules_global: Option<String>,
flag_remove_name_section: bool,
flag_remove_producers_section: bool,
flag_keep_debug: bool,
flag_encode_into: Option<String>,
arg_input: Option<PathBuf>,
}
fn main() {
env_logger::init();
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());
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"),
};
let typescript = args.flag_typescript || !args.flag_no_typescript;
let mut b = Bindgen::new();
b.input_path(input)
.nodejs(args.flag_nodejs)?
.web(args.flag_web)?
.browser(args.flag_browser)?
.no_modules(args.flag_no_modules)?
.debug(args.flag_debug)
.demangle(!args.flag_no_demangle)
.keep_debug(args.flag_keep_debug)
.remove_name_section(args.flag_remove_name_section)
.remove_producers_section(args.flag_remove_producers_section)
.typescript(typescript);
if let Some(ref name) = args.flag_no_modules_global {
b.no_modules_global(name)?;
}
if let Some(ref name) = args.flag_out_name {
b.out_name(name);
}
if let Some(mode) = &args.flag_encode_into {
match mode.as_str() {
"test" => b.encode_into(EncodeInto::Test),
"always" => b.encode_into(EncodeInto::Always),
"never" => b.encode_into(EncodeInto::Never),
s => bail!("invalid encode-into mode: `{}`", s),
};
}
let out_dir = match args.flag_out_dir {
Some(ref p) => p,
None => bail!("the `--out-dir` argument is now required"),
};
b.generate(out_dir)
}