mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-05-04 01:52:16 +00:00
This commit is an implementation of [RFC 6] which enables crates to inline local JS snippets into the final output artifact of `wasm-bindgen`. This is accompanied with a few minor breaking changes which are intended to be relatively minor in practice: * The `module` attribute disallows paths starting with `./` and `../`. It requires paths starting with `/` to actually exist on the filesystem. * The `--browser` flag no longer emits bundler-compatible code, but rather emits an ES module that can be natively loaded into a browser. Otherwise be sure to check out [the RFC][RFC 6] for more details, and otherwise this should implement at least the MVP version of the RFC! Notably at this time JS snippets with `--nodejs` or `--no-modules` are not supported and will unconditionally generate an error. [RFC 6]: https://github.com/rustwasm/rfcs/pull/6 Closes #1311
122 lines
4.1 KiB
Rust
122 lines
4.1 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
|
|
--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 (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_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)?
|
|
.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)
|
|
}
|