Skip to content

Commit 0e864a4

Browse files
authored
Merge pull request #1369 from alexcrichton/targets
Replace target flags with `--target`
2 parents 93cab3d + 995be7c commit 0e864a4

File tree

12 files changed

+125
-118
lines changed

12 files changed

+125
-118
lines changed

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'a> Context<'a> {
179179
}
180180
}
181181
OutputMode::Web => {
182-
// In web mode there's no need to export the internals of
182+
// In web 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
185185
// initializing below.
@@ -300,8 +300,8 @@ impl<'a> Context<'a> {
300300
}
301301

302302
/// Performs the task of actually generating the final JS module, be it
303-
/// `--no-modules`, `--web`, or for bundlers. This is the very last step
304-
/// performed in `finalize`.
303+
/// `--target no-modules`, `--target web`, or for bundlers. This is the very
304+
/// last step performed in `finalize`.
305305
fn finalize_js(&mut self, module_name: &str, needs_manual_start: bool) -> (String, String) {
306306
let mut js = String::new();
307307
if self.config.mode.no_modules() {
@@ -312,8 +312,9 @@ impl<'a> Context<'a> {
312312
// import the wasm file in one way or another.
313313
let mut init = String::new();
314314
match &self.config.mode {
315-
// In `--no-modules` mode we need to both expose a name on the
316-
// global object as well as generate our own custom start function.
315+
// In `--target no-modules` mode we need to both expose a name on
316+
// the global object as well as generate our own custom start
317+
// function.
317318
OutputMode::NoModules { global } => {
318319
js.push_str("const __exports = {};\n");
319320
js.push_str("let wasm;\n");
@@ -352,8 +353,8 @@ impl<'a> Context<'a> {
352353

353354
// With a browser-native output we're generating an ES module, but
354355
// browsers don't support natively importing wasm right now so we
355-
// expose the same initialization function as `--no-modules` as the
356-
// default export of the module.
356+
// expose the same initialization function as `--target no-modules`
357+
// as the default export of the module.
357358
OutputMode::Web => {
358359
js.push_str("const __exports = {};\n");
359360
self.imports_post.push_str("let wasm;\n");
@@ -755,7 +756,7 @@ impl<'a> Context<'a> {
755756
if !me.config.mode.no_modules() && !me.config.mode.web() {
756757
bail!(
757758
"`wasm_bindgen::module` is currently only supported with \
758-
--no-modules and --web"
759+
`--target no-modules` and `--target web`"
759760
);
760761
}
761762
Ok(format!(
@@ -2832,8 +2833,8 @@ impl<'a, 'b> SubContext<'a, 'b> {
28322833
import: &decode::Import<'b>,
28332834
item: &'b str,
28342835
) -> Result<Import<'b>, Error> {
2835-
// First up, imports don't work at all in `--no-modules` mode as we're
2836-
// not sure how to import them.
2836+
// First up, imports don't work at all in `--target no-modules` mode as
2837+
// we're not sure how to import them.
28372838
let is_local_snippet = match import.module {
28382839
decode::ImportModule::Named(s) => self.cx.local_modules.contains_key(s),
28392840
decode::ImportModule::RawNamed(_) => false,
@@ -2843,14 +2844,14 @@ impl<'a, 'b> SubContext<'a, 'b> {
28432844
if self.cx.config.mode.no_modules() {
28442845
if is_local_snippet {
28452846
bail!(
2846-
"local JS snippets are not supported with `--no-modules`; \
2847-
use `--web` or no flag instead",
2847+
"local JS snippets are not supported with `--target no-modules`; \
2848+
use `--target web` or no flag instead",
28482849
);
28492850
}
28502851
if let decode::ImportModule::Named(module) = &import.module {
28512852
bail!(
2852-
"import from `{}` module not allowed with `--no-modules`; \
2853-
use `--nodejs`, `--web`, or no flag instead",
2853+
"import from `{}` module not allowed with `--target no-modules`; \
2854+
use `nodejs`, `web`, or `bundler` target instead",
28542855
module
28552856
);
28562857
}
@@ -2866,16 +2867,16 @@ impl<'a, 'b> SubContext<'a, 'b> {
28662867
// have a small unergonomic escape hatch for our webidl-tests tests
28672868
if env::var("WBINDGEN_I_PROMISE_JS_SYNTAX_WORKS_IN_NODE").is_err() {
28682869
bail!(
2869-
"local JS snippets are not supported with `--nodejs`; \
2870+
"local JS snippets are not supported with `--target nodejs`; \
28702871
see rustwasm/rfcs#6 for more details, but this restriction \
28712872
will be lifted in the future"
28722873
);
28732874
}
28742875
}
28752876

2876-
// Similar to `--no-modules`, only allow vendor prefixes basically for web
2877-
// apis, shouldn't be necessary for things like npm packages or other
2878-
// imported items.
2877+
// Similar to `--target no-modules`, only allow vendor prefixes
2878+
// basically for web apis, shouldn't be necessary for things like npm
2879+
// packages or other imported items.
28792880
let vendor_prefixes = self.vendor_prefixes.get(item);
28802881
if let Some(vendor_prefixes) = vendor_prefixes {
28812882
assert!(vendor_prefixes.len() > 0);

crates/cli-support/src/lib.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ impl Bindgen {
5959
Bindgen {
6060
input: Input::None,
6161
out_name: None,
62-
mode: OutputMode::Bundler { browser_only: false },
62+
mode: OutputMode::Bundler {
63+
browser_only: false,
64+
},
6365
debug: false,
6466
typescript: false,
6567
demangle: true,
@@ -108,7 +110,7 @@ impl Bindgen {
108110
OutputMode::Node {
109111
experimental_modules: false,
110112
},
111-
"--nodejs",
113+
"--target nodejs",
112114
)?;
113115
}
114116
Ok(self)
@@ -126,9 +128,21 @@ impl Bindgen {
126128
Ok(self)
127129
}
128130

131+
pub fn bundler(&mut self, bundler: bool) -> Result<&mut Bindgen, Error> {
132+
if bundler {
133+
self.switch_mode(
134+
OutputMode::Bundler {
135+
browser_only: false,
136+
},
137+
"--target bundler",
138+
)?;
139+
}
140+
Ok(self)
141+
}
142+
129143
pub fn web(&mut self, web: bool) -> Result<&mut Bindgen, Error> {
130144
if web {
131-
self.switch_mode(OutputMode::Web, "--web")?;
145+
self.switch_mode(OutputMode::Web, "--target web")?;
132146
}
133147
Ok(self)
134148
}
@@ -139,7 +153,7 @@ impl Bindgen {
139153
OutputMode::NoModules {
140154
global: "wasm_bindgen".to_string(),
141155
},
142-
"--no-modules",
156+
"--target no-modules",
143157
)?;
144158
}
145159
Ok(self)
@@ -158,7 +172,7 @@ impl Bindgen {
158172
pub fn no_modules_global(&mut self, name: &str) -> Result<&mut Bindgen, Error> {
159173
match &mut self.mode {
160174
OutputMode::NoModules { global } => *global = name.to_string(),
161-
_ => bail!("can only specify `--no-modules-global` with `--no-modules`"),
175+
_ => bail!("can only specify `--no-modules-global` with `--target no-modules`"),
162176
}
163177
Ok(self)
164178
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ Options:
2121
-h --help Show this screen.
2222
--out-dir DIR Output directory
2323
--out-name VAR Set a custom output filename (Without extension. Defaults to crate name)
24-
--nodejs Generate output that only works in node.js
25-
--web Generate output that only works in a browser
26-
--browser Hint that JS should only be compatible with a browser
27-
--no-modules Generate output that only works in a browser (without modules)
24+
--target TARGET What type of output to generate, valid
25+
values are [web, bundler, nodejs, no-modules],
26+
and the default is [bundler]
2827
--no-modules-global VAR Name of the global variable to initialize
28+
--browser Hint that JS should only be compatible with a browser
2929
--typescript Output a TypeScript definition file (on by default)
3030
--no-typescript Don't emit a *.d.ts file
3131
--debug Include otherwise-extraneous debug checks in output
@@ -35,6 +35,9 @@ Options:
3535
--remove-producers-section Remove the telemetry `producers` section
3636
--encode-into MODE Whether or not to use TextEncoder#encodeInto,
3737
valid values are [test, always, never]
38+
--nodejs Deprecated, use `--target nodejs`
39+
--web Deprecated, use `--target web`
40+
--no-modules Deprecated, use `--target no-modules`
3841
-V --version Print the version number of wasm-bindgen
3942
";
4043

@@ -56,6 +59,7 @@ struct Args {
5659
flag_remove_producers_section: bool,
5760
flag_keep_debug: bool,
5861
flag_encode_into: Option<String>,
62+
flag_target: Option<String>,
5963
arg_input: Option<PathBuf>,
6064
}
6165

@@ -89,6 +93,15 @@ fn rmain(args: &Args) -> Result<(), Error> {
8993
let typescript = args.flag_typescript || !args.flag_no_typescript;
9094

9195
let mut b = Bindgen::new();
96+
if let Some(name) = &args.flag_target {
97+
match name.as_str() {
98+
"bundler" => b.bundler(true)?,
99+
"web" => b.web(true)?,
100+
"no-modules" => b.no_modules(true)?,
101+
"nodejs" => b.nodejs(true)?,
102+
s => bail!("invalid encode-into mode: `{}`", s),
103+
};
104+
}
92105
b.input_path(input)
93106
.nodejs(args.flag_nodejs)?
94107
.web(args.flag_web)?

examples/raytrace-parallel/build.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Without a Bundler Using `--no-modules`
1+
# Without a Bundler Using `--target no-modules`
22

33
[View documentation for this example online][dox]
44

@@ -13,9 +13,9 @@ $ wasm-pack build --target no-modules
1313
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
16-
which performs a similar purpose except it uses `--no-modules` instead of
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
19-
modules.
16+
which performs a similar purpose except it uses `--target no-modules` instead of
17+
`--target web`. The main difference here is how the shim JS and module are
18+
loaded, where this example uses old-school `script` tags while `--target web`
19+
uses ES 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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
<script src='pkg/without_a_bundler_no_modules.js'></script>
88

99
<script type=module>
10-
// Like with the `--web` output the exports are immediately available
11-
// but they won't work until we initialize the module. Unlike `--web`,
12-
// however, the globals are all stored on a `wasm_bindgen` global. The
13-
// global itself is the initialization function and then the properties of
14-
// the global are all the exported functions.
10+
// Like with the `--target web` output the exports are immediately
11+
// available but they won't work until we initialize the module. Unlike
12+
// `--target web`, however, the globals are all stored on a
13+
// `wasm_bindgen` global. The global itself is the initialization
14+
// function and then the properties of the global are all the exported
15+
// functions.
1516
//
1617
// Note that the name `wasm_bindgen` can be configured with the
1718
// `--no-modules-global` CLI flag

examples/without-a-bundler/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
You can build the example locally with:
88

99
```
10-
$ cargo build --target wasm32-unknown-unknown --release
11-
$ cargo run -p wasm-bindgen-cli --bin wasm-bindgen -- \
12-
../../target/wasm32-unknown-unknown/release/without_a_bundler.wasm \
13-
--out-dir pkg \
14-
--web
10+
$ wasm-pack build --target web
1511
```
1612

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

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

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

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

7-
This example shows how the `--web` flag can be used load code in a
7+
This example shows how the `--target 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-
First let's take a look at the code and see how when we're using `--web`
12+
First let's take a look at the code and see how when we're using `--target web`
1313
we're not actually losing any functionality!
1414

1515
```rust
@@ -27,18 +27,17 @@ what it means to deploy without a bundler.
2727

2828
[deployment]: ../reference/deployment.html
2929

30-
## Using the older `--no-modules`
30+
## Using the older `--target no-modules`
3131

3232
[View full source code][code]
3333

3434
[code]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/without-a-bundler-no-modules
3535

3636
The older version of using `wasm-bindgen` without a bundler is to use the
37-
`--no-modules` flag to the `wasm-bindgen` CLI. This corresponds to `--target
38-
no-modules` in `wasm-pack`.
37+
`--target no-modules` flag to the `wasm-bindgen` CLI.
3938

40-
While similar to the newer `--web`, the `--no-modules` flag has a few
41-
caveats:
39+
While similar to the newer `--target web`, the `--target no-modules` flag has a
40+
few caveats:
4241

4342
* It does not support [local JS snippets][snippets]
4443
* It does not generate an ES module

guide/src/reference/cli.md

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,19 @@ wasm-bindgen [options] ./target/wasm32-unknown-unknown/release/crate.wasm
2222
The target directory to emit the JavaScript bindings, TypeScript definitions,
2323
processed `.wasm` binary, etc...
2424

25-
### `--nodejs`
25+
### `--target`
2626

27-
This flag will tailor output for Node instead of browsers, allowing for native
28-
usage of `require` of the generated JS and internally using `require` instead of
29-
ECMAScript modules. When using this flag no further postprocessing (aka a
30-
bundler) should be necessary to work with the wasm.
31-
32-
For more information about this see the section on [deployment]
27+
This flag indicates what flavor of output what `wasm-bindgen` should generate.
28+
For example it could generate code to be loaded in a bundler like Webpack, a
29+
native web page, or Node.js. For a full list of options to pass this flag, see
30+
the section on [deployment]
3331

3432
[deployment]: deployment.html
3533

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.
41-
42-
For more information about this see the section on [deployment]
43-
44-
### `--no-modules` and `--no-modules-global VAR`
45-
46-
The default output of `wasm-bindgen` uses ECMAScript modules. These options
47-
indicate that ECMAScript modules should *not* be used, and that output should be
48-
tailored for a properties on the JavaScript global object (e.g. `window`).
34+
### `--no-modules-global VAR`
4935

50-
The `--no-modules-global VAR` option makes `VAR` the global property that the
51-
JavaScript bindings are attached to.
36+
When `--target no-modules` is used this flag can indicate what the name of the
37+
global to assign generated bindings to.
5238

5339
For more information about this see the section on [deployment]
5440

0 commit comments

Comments
 (0)