Skip to content

Commit cd3781c

Browse files
committed
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]: rustwasm/rfcs#6 Closes wasm-bindgen#1318
1 parent 79a8c5d commit cd3781c

File tree

12 files changed

+70
-45
lines changed

12 files changed

+70
-45
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'a> Context<'a> {
166166
format!("__exports.{} = {};\n", name, contents)
167167
}
168168
}
169-
OutputMode::Bundler
169+
OutputMode::Bundler { .. }
170170
| OutputMode::Node {
171171
experimental_modules: true,
172172
} => {
@@ -178,7 +178,7 @@ impl<'a> Context<'a> {
178178
format!("export const {} = {};\n", name, contents)
179179
}
180180
}
181-
OutputMode::Browser => {
181+
OutputMode::Web => {
182182
// In browser mode there's no need to export the internals of
183183
// wasm-bindgen as we're not using the module itself as the
184184
// import object but rather the `__exports` map we'll be
@@ -202,7 +202,7 @@ impl<'a> Context<'a> {
202202
};
203203
self.global(&global);
204204

205-
if self.config.mode.browser() {
205+
if self.config.mode.web() {
206206
self.global(&format!("__exports.{} = {0};", name));
207207
}
208208
}
@@ -300,7 +300,7 @@ impl<'a> Context<'a> {
300300
}
301301

302302
/// Performs the task of actually generating the final JS module, be it
303-
/// `--no-modules`, `--browser`, or for bundlers. This is the very last step
303+
/// `--no-modules`, `--web`, or for bundlers. This is the very last step
304304
/// performed in `finalize`.
305305
fn finalize_js(&mut self, module_name: &str, needs_manual_start: bool) -> (String, String) {
306306
let mut js = String::new();
@@ -340,7 +340,7 @@ impl<'a> Context<'a> {
340340
// With Bundlers and modern ES6 support in Node we can simply import
341341
// the wasm file as if it were an ES module and let the
342342
// bundler/runtime take care of it.
343-
OutputMode::Bundler
343+
OutputMode::Bundler { .. }
344344
| OutputMode::Node {
345345
experimental_modules: true,
346346
} => {
@@ -354,7 +354,7 @@ impl<'a> Context<'a> {
354354
// browsers don't support natively importing wasm right now so we
355355
// expose the same initialization function as `--no-modules` as the
356356
// default export of the module.
357-
OutputMode::Browser => {
357+
OutputMode::Web => {
358358
js.push_str("const __exports = {};\n");
359359
self.imports_post.push_str("let wasm;\n");
360360
init = self.gen_init(&module_name, needs_manual_start);
@@ -752,10 +752,10 @@ impl<'a> Context<'a> {
752752
})?;
753753

754754
self.bind("__wbindgen_module", &|me| {
755-
if !me.config.mode.no_modules() && !me.config.mode.browser() {
755+
if !me.config.mode.no_modules() && !me.config.mode.web() {
756756
bail!(
757757
"`wasm_bindgen::module` is currently only supported with \
758-
--no-modules"
758+
--no-modules and --web"
759759
);
760760
}
761761
Ok(format!(
@@ -2843,13 +2843,13 @@ impl<'a, 'b> SubContext<'a, 'b> {
28432843
if is_local_snippet {
28442844
bail!(
28452845
"local JS snippets are not supported with `--no-modules`; \
2846-
use `--browser` or no flag instead",
2846+
use `--web` or no flag instead",
28472847
);
28482848
}
28492849
if let decode::ImportModule::Named(module) = &import.module {
28502850
bail!(
28512851
"import from `{}` module not allowed with `--no-modules`; \
2852-
use `--nodejs`, `--browser`, or no flag instead",
2852+
use `--nodejs`, `--web`, or no flag instead",
28532853
module
28542854
);
28552855
}

crates/cli-support/src/lib.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub struct Bindgen {
3636
}
3737

3838
enum OutputMode {
39-
Bundler,
40-
Browser,
39+
Bundler { browser_only: bool },
40+
Web,
4141
NoModules { global: String },
4242
Node { experimental_modules: bool },
4343
}
@@ -59,7 +59,7 @@ impl Bindgen {
5959
Bindgen {
6060
input: Input::None,
6161
out_name: None,
62-
mode: OutputMode::Bundler,
62+
mode: OutputMode::Bundler { browser_only: false },
6363
debug: false,
6464
typescript: false,
6565
demangle: true,
@@ -93,7 +93,7 @@ impl Bindgen {
9393

9494
fn switch_mode(&mut self, mode: OutputMode, flag: &str) -> Result<(), Error> {
9595
match self.mode {
96-
OutputMode::Bundler => self.mode = mode,
96+
OutputMode::Bundler { .. } => self.mode = mode,
9797
_ => bail!(
9898
"cannot specify `{}` with another output mode already specified",
9999
flag
@@ -126,9 +126,9 @@ impl Bindgen {
126126
Ok(self)
127127
}
128128

129-
pub fn browser(&mut self, browser: bool) -> Result<&mut Bindgen, Error> {
130-
if browser {
131-
self.switch_mode(OutputMode::Browser, "--browser")?;
129+
pub fn web(&mut self, web: bool) -> Result<&mut Bindgen, Error> {
130+
if web {
131+
self.switch_mode(OutputMode::Web, "--web")?;
132132
}
133133
Ok(self)
134134
}
@@ -145,6 +145,16 @@ impl Bindgen {
145145
Ok(self)
146146
}
147147

148+
pub fn browser(&mut self, browser: bool) -> Result<&mut Bindgen, Error> {
149+
if browser {
150+
match &mut self.mode {
151+
OutputMode::Bundler { browser_only } => *browser_only = true,
152+
_ => bail!("cannot specify `--browser` with other output types"),
153+
}
154+
}
155+
Ok(self)
156+
}
157+
148158
pub fn no_modules_global(&mut self, name: &str) -> Result<&mut Bindgen, Error> {
149159
match &mut self.mode {
150160
OutputMode::NoModules { global } => *global = name.to_string(),
@@ -660,15 +670,16 @@ impl OutputMode {
660670

661671
fn always_run_in_browser(&self) -> bool {
662672
match self {
663-
OutputMode::Browser => true,
673+
OutputMode::Web => true,
664674
OutputMode::NoModules { .. } => true,
675+
OutputMode::Bundler { browser_only } => *browser_only,
665676
_ => false,
666677
}
667678
}
668679

669-
fn browser(&self) -> bool {
680+
fn web(&self) -> bool {
670681
match self {
671-
OutputMode::Browser => true,
682+
OutputMode::Web => true,
672683
_ => false,
673684
}
674685
}

crates/cli/src/bin/wasm-bindgen-test-runner/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn rmain() -> Result<(), Error> {
108108
let mut b = Bindgen::new();
109109
b.debug(debug)
110110
.nodejs(node)?
111-
.browser(!node)?
111+
.web(!node)?
112112
.input_module(module, wasm)
113113
.keep_debug(false)
114114
.emit_start(false)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Options:
2222
--out-dir DIR Output directory
2323
--out-name VAR Set a custom output filename (Without extension. Defaults to crate name)
2424
--nodejs Generate output that only works in node.js
25-
--browser Generate output that only works in a browser
25+
--web Generate output that only works in a browser
26+
--browser Hint that JS should only be compatible with a browser
2627
--no-modules Generate output that only works in a browser (without modules)
2728
--no-modules-global VAR Name of the global variable to initialize
2829
--typescript Output a TypeScript definition file (on by default)
@@ -41,6 +42,7 @@ Options:
4142
struct Args {
4243
flag_nodejs: bool,
4344
flag_browser: bool,
45+
flag_web: bool,
4446
flag_no_modules: bool,
4547
flag_typescript: bool,
4648
flag_no_typescript: bool,
@@ -89,6 +91,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
8991
let mut b = Bindgen::new();
9092
b.input_path(input)
9193
.nodejs(args.flag_nodejs)?
94+
.web(args.flag_web)?
9295
.browser(args.flag_browser)?
9396
.no_modules(args.flag_no_modules)?
9497
.debug(args.flag_debug)

examples/without-a-bundler-no-modules/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ and then opening `index.html` in a browser should run the example!
1414

1515
Note that this example is in contrast to the [without a bundler][wab] example
1616
which performs a similar purpose except it uses `--no-modules` instead of
17-
`--browser`. The main difference here is how the shim JS and module are loaded,
18-
where this example uses old-school `script` tags while `--browser` uses ES
17+
`--web`. The main difference here is how the shim JS and module are loaded,
18+
where this example uses old-school `script` tags while `--web` uses ES
1919
modules.
2020

2121
[wab]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/without-a-bundler

examples/without-a-bundler-no-modules/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<script src='pkg/without_a_bundler_no_modules.js'></script>
88

99
<script type=module>
10-
// Like with the `--browser` output the exports are immediately available
11-
// but they won't work until we initialize the module. Unlike `--browser`,
10+
// Like with the `--web` output the exports are immediately available
11+
// but they won't work until we initialize the module. Unlike `--web`,
1212
// however, the globals are all stored on a `wasm_bindgen` global. The
1313
// global itself is the initialization function and then the properties of
1414
// the global are all the exported functions.

examples/without-a-bundler/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $ cargo build --target wasm32-unknown-unknown --release
1111
$ cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \
1212
../../target/wasm32-unknown-unknown/release/without_a_bundler.wasm \
1313
--out-dir pkg \
14-
--browser
14+
--web
1515
```
1616

1717
and then opening `index.html` in a browser should run the example!

examples/without-a-bundler/build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
set -ex
44

55
# Note that typically we'd use `wasm-pack` to build the crate, but the
6-
# `--browser` flag is very new to `wasm-bindgen` and as such doesn't have
6+
# `--web` flag is very new to `wasm-bindgen` and as such doesn't have
77
# support in `wasm-pack` yet. Support will be added soon though!
88

99
cargo build --target wasm32-unknown-unknown --release
1010
cargo run --manifest-path ../../crates/cli/Cargo.toml \
1111
--bin wasm-bindgen -- \
1212
../../target/wasm32-unknown-unknown/release/without_a_bundler.wasm --out-dir pkg \
13-
--browser
13+
--web
1414

1515
python3 -m http.server

guide/src/examples/without-a-bundler.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
[code]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/without-a-bundler
66

7-
This example shows how the `--browser` flag can be used load code in a
7+
This example shows how the `--web` flag can be used load code in a
88
browser directly. For this deployment strategy bundlers like Webpack are not
99
required. For more information on deployment see the [dedicated
1010
documentation][deployment].
1111

12-
> **Note**: the `--browser` flag is quite new to `wasm-bindgen`, and does not
12+
> **Note**: the `--web` flag is quite new to `wasm-bindgen`, and does not
1313
> currently have support in `wasm-pack` yet. Support will be added soon though!
1414
15-
First let's take a look at the code and see how when we're using `--browser`
15+
First let's take a look at the code and see how when we're using `--web`
1616
we're not actually losing any functionality!
1717

1818
```rust
@@ -40,7 +40,7 @@ The older version of using `wasm-bindgen` without a bundler is to use the
4040
`--no-modules` flag to the `wasm-bindgen` CLI. This corresponds to `--target
4141
no-modules` in `wasm-pack`.
4242

43-
While similar to the newer `--browser`, the `--no-modules` flag has a few
43+
While similar to the newer `--web`, the `--no-modules` flag has a few
4444
caveats:
4545

4646
* It does not support [local JS snippets][snippets]

guide/src/reference/cli.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ usage of `require` of the generated JS and internally using `require` instead of
2929
ECMAScript modules. When using this flag no further postprocessing (aka a
3030
bundler) should be necessary to work with the wasm.
3131

32-
### `--browser`
32+
For more information about this see the section on [deployment]
33+
34+
[deployment]: deployment.html
35+
36+
### `--web`
37+
38+
This flag will generate output suitable for loading natively in browsers today.
39+
The generated JS shims are an ES module which export a `default` instantiation
40+
function, like `--no-modules` below.
3341

34-
This flag will tailor the output specifically for browsers, making it
35-
incompatible with Node. This will basically make the generated JS a tiny bit
36-
smaller as runtime checks for Node won't be necessary.
42+
For more information about this see the section on [deployment]
3743

3844
### `--no-modules` and `--no-modules-global VAR`
3945

@@ -44,8 +50,7 @@ tailored for a properties on the JavaScript global object (e.g. `window`).
4450
The `--no-modules-global VAR` option makes `VAR` the global property that the
4551
JavaScript bindings are attached to.
4652

47-
More information can be found in the [documentation for building without
48-
ECMAScript modules](./no-esm.html).
53+
For more information about this see the section on [deployment]
4954

5055
### `--typescript`
5156

@@ -71,3 +76,9 @@ When post-processing the `.wasm` binary, do not demangle Rust symbols in the
7176

7277
When post-processing the `.wasm` binary, do not strip DWARF debug info custom
7378
sections.
79+
80+
### `--browser`
81+
82+
When generating bundler-compatible code (see the section on [deployment]) this
83+
indicates that the bundled code is always intended to go into a browser so a few
84+
checks for Node.js can be elided.

0 commit comments

Comments
 (0)