diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index cdd2ea57af4..83c667de4df 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -179,7 +179,7 @@ impl<'a> Context<'a> { } } OutputMode::Web => { - // In web mode there's no need to export the internals of + // In web mode there's no need to export the internals of // wasm-bindgen as we're not using the module itself as the // import object but rather the `__exports` map we'll be // initializing below. @@ -300,8 +300,8 @@ impl<'a> Context<'a> { } /// Performs the task of actually generating the final JS module, be it - /// `--no-modules`, `--web`, or for bundlers. This is the very last step - /// performed in `finalize`. + /// `--target no-modules`, `--target web`, or for bundlers. This is the very + /// last step performed in `finalize`. fn finalize_js(&mut self, module_name: &str, needs_manual_start: bool) -> (String, String) { let mut js = String::new(); if self.config.mode.no_modules() { @@ -312,8 +312,9 @@ impl<'a> Context<'a> { // import the wasm file in one way or another. let mut init = String::new(); match &self.config.mode { - // In `--no-modules` mode we need to both expose a name on the - // global object as well as generate our own custom start function. + // In `--target no-modules` mode we need to both expose a name on + // the global object as well as generate our own custom start + // function. OutputMode::NoModules { global } => { js.push_str("const __exports = {};\n"); js.push_str("let wasm;\n"); @@ -352,8 +353,8 @@ impl<'a> Context<'a> { // With a browser-native output we're generating an ES module, but // browsers don't support natively importing wasm right now so we - // expose the same initialization function as `--no-modules` as the - // default export of the module. + // expose the same initialization function as `--target no-modules` + // as the default export of the module. OutputMode::Web => { js.push_str("const __exports = {};\n"); self.imports_post.push_str("let wasm;\n"); @@ -755,7 +756,7 @@ impl<'a> Context<'a> { if !me.config.mode.no_modules() && !me.config.mode.web() { bail!( "`wasm_bindgen::module` is currently only supported with \ - --no-modules and --web" + `--target no-modules` and `--target web`" ); } Ok(format!( @@ -2832,8 +2833,8 @@ impl<'a, 'b> SubContext<'a, 'b> { import: &decode::Import<'b>, item: &'b str, ) -> Result, Error> { - // First up, imports don't work at all in `--no-modules` mode as we're - // not sure how to import them. + // First up, imports don't work at all in `--target no-modules` mode as + // we're not sure how to import them. let is_local_snippet = match import.module { decode::ImportModule::Named(s) => self.cx.local_modules.contains_key(s), decode::ImportModule::RawNamed(_) => false, @@ -2843,14 +2844,14 @@ impl<'a, 'b> SubContext<'a, 'b> { if self.cx.config.mode.no_modules() { if is_local_snippet { bail!( - "local JS snippets are not supported with `--no-modules`; \ - use `--web` or no flag instead", + "local JS snippets are not supported with `--target no-modules`; \ + use `--target web` or no flag instead", ); } if let decode::ImportModule::Named(module) = &import.module { bail!( - "import from `{}` module not allowed with `--no-modules`; \ - use `--nodejs`, `--web`, or no flag instead", + "import from `{}` module not allowed with `--target no-modules`; \ + use `nodejs`, `web`, or `bundler` target instead", module ); } @@ -2866,16 +2867,16 @@ impl<'a, 'b> SubContext<'a, 'b> { // have a small unergonomic escape hatch for our webidl-tests tests if env::var("WBINDGEN_I_PROMISE_JS_SYNTAX_WORKS_IN_NODE").is_err() { bail!( - "local JS snippets are not supported with `--nodejs`; \ + "local JS snippets are not supported with `--target nodejs`; \ see rustwasm/rfcs#6 for more details, but this restriction \ will be lifted in the future" ); } } - // Similar to `--no-modules`, only allow vendor prefixes basically for web - // apis, shouldn't be necessary for things like npm packages or other - // imported items. + // Similar to `--target no-modules`, only allow vendor prefixes + // basically for web apis, shouldn't be necessary for things like npm + // packages or other imported items. let vendor_prefixes = self.vendor_prefixes.get(item); if let Some(vendor_prefixes) = vendor_prefixes { assert!(vendor_prefixes.len() > 0); diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index d825445ac53..205d502d7d8 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -59,7 +59,9 @@ impl Bindgen { Bindgen { input: Input::None, out_name: None, - mode: OutputMode::Bundler { browser_only: false }, + mode: OutputMode::Bundler { + browser_only: false, + }, debug: false, typescript: false, demangle: true, @@ -108,7 +110,7 @@ impl Bindgen { OutputMode::Node { experimental_modules: false, }, - "--nodejs", + "--target nodejs", )?; } Ok(self) @@ -126,9 +128,21 @@ impl Bindgen { Ok(self) } + pub fn bundler(&mut self, bundler: bool) -> Result<&mut Bindgen, Error> { + if bundler { + self.switch_mode( + OutputMode::Bundler { + browser_only: false, + }, + "--target bundler", + )?; + } + Ok(self) + } + pub fn web(&mut self, web: bool) -> Result<&mut Bindgen, Error> { if web { - self.switch_mode(OutputMode::Web, "--web")?; + self.switch_mode(OutputMode::Web, "--target web")?; } Ok(self) } @@ -139,7 +153,7 @@ impl Bindgen { OutputMode::NoModules { global: "wasm_bindgen".to_string(), }, - "--no-modules", + "--target no-modules", )?; } Ok(self) @@ -158,7 +172,7 @@ impl Bindgen { pub fn no_modules_global(&mut self, name: &str) -> Result<&mut Bindgen, Error> { match &mut self.mode { OutputMode::NoModules { global } => *global = name.to_string(), - _ => bail!("can only specify `--no-modules-global` with `--no-modules`"), + _ => bail!("can only specify `--no-modules-global` with `--target no-modules`"), } Ok(self) } diff --git a/crates/cli/src/bin/wasm-bindgen.rs b/crates/cli/src/bin/wasm-bindgen.rs index 03f025c9969..6aa676a87f9 100644 --- a/crates/cli/src/bin/wasm-bindgen.rs +++ b/crates/cli/src/bin/wasm-bindgen.rs @@ -21,11 +21,11 @@ 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) + --target TARGET What type of output to generate, valid + values are [web, bundler, nodejs, no-modules], + and the default is [bundler] --no-modules-global VAR Name of the global variable to initialize + --browser Hint that JS should only be compatible with a browser --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 @@ -35,6 +35,9 @@ Options: --remove-producers-section Remove the telemetry `producers` section --encode-into MODE Whether or not to use TextEncoder#encodeInto, valid values are [test, always, never] + --nodejs Deprecated, use `--target nodejs` + --web Deprecated, use `--target web` + --no-modules Deprecated, use `--target no-modules` -V --version Print the version number of wasm-bindgen "; @@ -56,6 +59,7 @@ struct Args { flag_remove_producers_section: bool, flag_keep_debug: bool, flag_encode_into: Option, + flag_target: Option, arg_input: Option, } @@ -89,6 +93,15 @@ fn rmain(args: &Args) -> Result<(), Error> { let typescript = args.flag_typescript || !args.flag_no_typescript; let mut b = Bindgen::new(); + if let Some(name) = &args.flag_target { + match name.as_str() { + "bundler" => b.bundler(true)?, + "web" => b.web(true)?, + "no-modules" => b.no_modules(true)?, + "nodejs" => b.nodejs(true)?, + s => bail!("invalid encode-into mode: `{}`", s), + }; + } b.input_path(input) .nodejs(args.flag_nodejs)? .web(args.flag_web)? diff --git a/examples/raytrace-parallel/build.sh b/examples/raytrace-parallel/build.sh deleted file mode 100755 index c113f09f1d1..00000000000 --- a/examples/raytrace-parallel/build.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh - -set -ex - -# Two critical steps are required here to get this working: -# -# * First, the Rust standard library needs to be compiled. The default version -# is not compatible with atomics so we need to compile a version, with xargo, -# that is compatible. -# -# * Next we need to compile everything with the `atomics` feature enabled, -# ensuring that LLVM will generate atomic instructions and such. -RUSTFLAGS='-C target-feature=+atomics' \ - rustup run nightly xargo build --target wasm32-unknown-unknown --release - -# Threading support is disabled by default in wasm-bindgen, so use an env var -# here to turn it on for our bindings generation. Also note that webpack isn't -# currently compatible with atomics, so we go with the --no-modules output. -WASM_BINDGEN_THREADS=1 \ - cargo run --manifest-path ../../crates/cli/Cargo.toml \ - --bin wasm-bindgen -- \ - ../../target/wasm32-unknown-unknown/release/raytrace_parallel.wasm --out-dir . \ - --no-modules - -python3 -m http.server diff --git a/examples/without-a-bundler-no-modules/README.md b/examples/without-a-bundler-no-modules/README.md index 891d84795fb..801d8abe0af 100644 --- a/examples/without-a-bundler-no-modules/README.md +++ b/examples/without-a-bundler-no-modules/README.md @@ -1,4 +1,4 @@ -# Without a Bundler Using `--no-modules` +# Without a Bundler Using `--target no-modules` [View documentation for this example online][dox] @@ -13,9 +13,9 @@ $ wasm-pack build --target no-modules and then opening `index.html` in a browser should run the example! Note that this example is in contrast to the [without a bundler][wab] example -which performs a similar purpose except it uses `--no-modules` instead of -`--web`. The main difference here is how the shim JS and module are loaded, -where this example uses old-school `script` tags while `--web` uses ES -modules. +which performs a similar purpose except it uses `--target no-modules` instead of +`--target web`. The main difference here is how the shim JS and module are +loaded, where this example uses old-school `script` tags while `--target web` +uses ES modules. [wab]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/without-a-bundler diff --git a/examples/without-a-bundler-no-modules/index.html b/examples/without-a-bundler-no-modules/index.html index 63d3e4df3b1..db5be832467 100644 --- a/examples/without-a-bundler-no-modules/index.html +++ b/examples/without-a-bundler-no-modules/index.html @@ -7,11 +7,12 @@