Skip to content

Commit 2ec80a0

Browse files
committed
feat: reftypes argument
1 parent 9f9634c commit 2ec80a0

File tree

9 files changed

+78
-29
lines changed

9 files changed

+78
-29
lines changed

docs/src/commands/build.md

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ wasm-pack build --out-name index
4848
# index.d.ts index.js index_bg.d.ts index_bg.wasm package.json README.md
4949
```
5050

51-
5251
## Profile
5352

5453
The `build` command accepts an optional profile argument: one of `--dev`,
@@ -57,11 +56,11 @@ The `build` command accepts an optional profile argument: one of `--dev`,
5756
This controls whether debug assertions are enabled, debug info is generated, and
5857
which (if any) optimizations are enabled.
5958

60-
| Profile | Debug Assertions | Debug Info | Optimizations | Notes |
61-
|---------------|------------------|------------|---------------|---------------------------------------|
62-
| `--dev` | Yes | Yes | No | Useful for development and debugging. |
59+
| Profile | Debug Assertions | Debug Info | Optimizations | Notes |
60+
| ------------- | ---------------- | ---------- | ------------- | ----------------------------------------------------------- |
61+
| `--dev` | Yes | Yes | No | Useful for development and debugging. |
6362
| `--profiling` | No | Yes | Yes | Useful when profiling and investigating performance issues. |
64-
| `--release` | No | No | Yes | Useful for shipping to production. |
63+
| `--release` | No | No | Yes | Useful for shipping to production. |
6564

6665
The `--dev` profile will build the output package using cargo's [default
6766
non-release profile][cargo-profile-sections-documentation]. Building this way is
@@ -85,12 +84,12 @@ using the compiled output][deploy].
8584
wasm-pack build --target nodejs
8685
```
8786

88-
| Option | Usage | Description |
89-
|-----------|------------|-----------------------------------------------------------------------------------------------------|
90-
| *not specified* or `bundler` | [Bundler][bundlers] | Outputs JS that is suitable for interoperation with a Bundler like Webpack. You'll `import` the JS and the `module` key is specified in `package.json`. `sideEffects: false` is by default. |
91-
| `nodejs` | [Node.js][deploy-nodejs] | Outputs JS that uses CommonJS modules, for use with a `require` statement. `main` key in `package.json`. |
92-
| `web` | [Native in browser][deploy-web] | Outputs JS that can be natively imported as an ES module in a browser, but the WebAssembly must be manually instantiated and loaded. |
93-
| `no-modules` | [Native in browser][deploy-web] | Same as `web`, except the JS is included on a page and modifies global state, and doesn't support as many `wasm-bindgen` features as `web` |
87+
| Option | Usage | Description |
88+
| ---------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
89+
| _not specified_ or `bundler` | [Bundler][bundlers] | Outputs JS that is suitable for interoperation with a Bundler like Webpack. You'll `import` the JS and the `module` key is specified in `package.json`. `sideEffects: false` is by default. |
90+
| `nodejs` | [Node.js][deploy-nodejs] | Outputs JS that uses CommonJS modules, for use with a `require` statement. `main` key in `package.json`. |
91+
| `web` | [Native in browser][deploy-web] | Outputs JS that can be natively imported as an ES module in a browser, but the WebAssembly must be manually instantiated and loaded. |
92+
| `no-modules` | [Native in browser][deploy-web] | Same as `web`, except the JS is included on a page and modifies global state, and doesn't support as many `wasm-bindgen` features as `web` |
9493

9594
[deploy]: https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html
9695
[bundlers]: https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#bundlers
@@ -116,14 +115,35 @@ the npm documentation [here][npm-scope-documentation].
116115
## Mode
117116

118117
The `build` command accepts an optional `--mode` argument.
118+
119119
```
120120
wasm-pack build examples/js-hello-world --mode no-install
121121
```
122122

123-
| Option | Description |
124-
|---------------|------------------------------------------------------------------------------------------|
125-
| `no-install` | `wasm-pack init` implicitly and create wasm binding without installing `wasm-bindgen`. |
126-
| `normal` | do all the stuffs of `no-install` with installed `wasm-bindgen`. |
123+
| Option | Description |
124+
| ------------ | -------------------------------------------------------------------------------------- |
125+
| `no-install` | `wasm-pack init` implicitly and create wasm binding without installing `wasm-bindgen`. |
126+
| `normal` | do all the stuffs of `no-install` with installed `wasm-bindgen`. |
127+
128+
## Support for Reference Types
129+
130+
You can enable support for reference types with the optional `--reftypes` argument.
131+
132+
```
133+
wasm-pack build examples/js-hello-world --reftypes
134+
```
135+
136+
This feature is hoped to enable more efficient communication between the host (JS) and the wasm module, but not yet all browsers support it.
137+
138+
For more information about reference types, you should read the [wasm-bindgen Guide](https://rustwasm.github.io/docs/wasm-bindgen/reference/reference-types.html#support-for-reference-types).
139+
140+
This argument will automatically disable `wasm-opt`.
141+
Otherwise compilation would crash with the following error:
142+
143+
```
144+
[parse exception: Only 1 table definition allowed in MVP (at 0:4234)]
145+
Fatal: error in parsing input
146+
```
127147

128148
## Extra options
129149

src/bindgen.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub fn wasm_bindgen_build(
1717
out_dir: &Path,
1818
out_name: &Option<String>,
1919
disable_dts: bool,
20+
enable_reftypes: bool,
2021
target: Target,
2122
profile: BuildProfile,
2223
) -> Result<(), failure::Error> {
@@ -48,6 +49,10 @@ pub fn wasm_bindgen_build(
4849
.arg(out_dir)
4950
.arg(dts_arg);
5051

52+
if enable_reftypes {
53+
cmd.arg("--reference-types");
54+
}
55+
5156
let target_arg = build_target_arg(target, &bindgen_path)?;
5257
if supports_dash_dash_target(bindgen_path.to_path_buf())? {
5358
cmd.arg("--target").arg(target_arg);

src/command/build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct Build {
2727
pub crate_data: manifest::CrateData,
2828
pub scope: Option<String>,
2929
pub disable_dts: bool,
30+
pub enable_reftypes: bool,
3031
pub target: Target,
3132
pub profile: BuildProfile,
3233
pub mode: InstallMode,
@@ -119,6 +120,11 @@ pub struct BuildOptions {
119120
/// this flag will disable generating this TypeScript file.
120121
pub disable_dts: bool,
121122

123+
#[structopt(long = "reftypes")]
124+
/// Enable support for reference types. This feature is hoped to enable more efficient
125+
/// communication between the host (JS) and the wasm module, but not yet all browsers support it.
126+
pub enable_reftypes: bool,
127+
122128
#[structopt(long = "target", short = "t", default_value = "bundler")]
123129
/// Sets the target environment. [possible values: bundler, nodejs, web, no-modules]
124130
pub target: Target,
@@ -160,6 +166,7 @@ impl Default for BuildOptions {
160166
scope: None,
161167
mode: InstallMode::default(),
162168
disable_dts: false,
169+
enable_reftypes: false,
163170
target: Target::default(),
164171
debug: false,
165172
dev: false,
@@ -196,6 +203,7 @@ impl Build {
196203
crate_data,
197204
scope: build_opts.scope,
198205
disable_dts: build_opts.disable_dts,
206+
enable_reftypes: build_opts.enable_reftypes,
199207
target: build_opts.target,
200208
profile,
201209
mode: build_opts.mode,
@@ -370,6 +378,7 @@ impl Build {
370378
&self.out_dir,
371379
&self.out_name,
372380
self.disable_dts,
381+
self.enable_reftypes,
373382
self.target,
374383
self.profile,
375384
)?;
@@ -378,6 +387,9 @@ impl Build {
378387
}
379388

380389
fn step_run_wasm_opt(&mut self) -> Result<(), Error> {
390+
if self.enable_reftypes {
391+
return Ok(());
392+
}
381393
let args = match self
382394
.crate_data
383395
.configured_profile(self.profile)

src/command/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl Test {
263263
bail!(
264264
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
265265
[dev-dependencies]\n\
266-
wasm-bindgen-test = \"0.2\"",
266+
wasm-bindgen-test = \"0.3\"",
267267
style("wasm-bindgen-test").bold().dim(),
268268
)
269269
}

tests/all/build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,15 @@ fn build_crates_with_same_names() {
376376
.assert()
377377
.success();
378378
}
379+
380+
#[test]
381+
fn build_with_reftypes() {
382+
let fixture = utils::fixture::js_hello_world();
383+
fixture.install_local_wasm_bindgen();
384+
fixture
385+
.wasm_pack()
386+
.arg("build")
387+
.arg("--reftypes")
388+
.assert()
389+
.success();
390+
}

tests/all/download.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn can_download_prebuilt_wasm_bindgen() {
1212
let dir = tempfile::TempDir::new().unwrap();
1313
let cache = Cache::at(dir.path());
1414
if let install::Status::Found(dl) =
15-
install::download_prebuilt(&Tool::WasmBindgen, &cache, "0.2.37", true).unwrap()
15+
install::download_prebuilt(&Tool::WasmBindgen, &cache, "0.2.67", true).unwrap()
1616
{
1717
assert!(dl.binary("wasm-bindgen").unwrap().is_file());
1818
assert!(dl.binary("wasm-bindgen-test-runner").unwrap().is_file())
@@ -29,7 +29,7 @@ fn can_download_prebuilt_wasm_bindgen() {
2929
))]
3030
fn downloading_prebuilt_wasm_bindgen_handles_http_errors() {
3131
let dir = tempfile::TempDir::new().unwrap();
32-
let bad_version = "0.2.37-some-trailing-version-stuff-that-does-not-exist";
32+
let bad_version = "0.2.67-some-trailing-version-stuff-that-does-not-exist";
3333
let cache = Cache::at(dir.path());
3434
let result = install::download_prebuilt(&Tool::WasmBindgen, &cache, bad_version, true);
3535
assert!(result.is_err());

tests/all/lockfile.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn it_gets_wasm_bindgen_version() {
88
fixture.cargo_check();
99
let data = CrateData::new(&fixture.path, None).unwrap();
1010
let lock = Lockfile::new(&data).unwrap();
11-
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
11+
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.67"),);
1212
}
1313

1414
#[test]
@@ -17,7 +17,7 @@ fn it_gets_wasm_bindgen_test_version() {
1717
fixture.cargo_check();
1818
let data = CrateData::new(&fixture.path, None).unwrap();
1919
let lock = Lockfile::new(&data).unwrap();
20-
assert_eq!(lock.wasm_bindgen_test_version(), Some("0.2.37"),);
20+
assert_eq!(lock.wasm_bindgen_test_version(), Some("0.3.17"),);
2121
}
2222

2323
#[test]
@@ -46,7 +46,7 @@ fn it_gets_wasm_bindgen_version_in_crate_inside_workspace() {
4646
crate-type = ["cdylib"]
4747
4848
[dependencies]
49-
wasm-bindgen = "=0.2.37"
49+
wasm-bindgen = "=0.2.67"
5050
"#,
5151
)
5252
.file(
@@ -62,7 +62,7 @@ fn it_gets_wasm_bindgen_version_in_crate_inside_workspace() {
6262
fixture.cargo_check();
6363
let data = CrateData::new(&fixture.path.join("blah"), None).unwrap();
6464
let lock = Lockfile::new(&data).unwrap();
65-
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
65+
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.67"),);
6666
}
6767

6868
#[test]
@@ -91,7 +91,7 @@ fn it_gets_wasm_bindgen_version_from_dependencies() {
9191
crate-type = ["cdylib"]
9292
9393
[dependencies]
94-
wasm-bindgen = "=0.2.37"
94+
wasm-bindgen = "=0.2.67"
9595
"#,
9696
)
9797
.file(
@@ -130,5 +130,5 @@ fn it_gets_wasm_bindgen_version_from_dependencies() {
130130
fixture.cargo_check();
131131
let data = CrateData::new(&fixture.path.join("parent"), None).unwrap();
132132
let lock = Lockfile::new(&data).unwrap();
133-
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
133+
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.67"),);
134134
}

tests/all/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn complains_about_missing_wasm_bindgen_test_dependency() {
197197
"Ensure that you have \"wasm-bindgen-test\" as a dependency in your Cargo.toml file",
198198
))
199199
.stderr(predicates::str::contains("[dev-dependencies]"))
200-
.stderr(predicates::str::contains("wasm-bindgen-test = \"0.2\""));
200+
.stderr(predicates::str::contains("wasm-bindgen-test = \"0.3\""));
201201
}
202202

203203
#[test]

tests/all/utils/fixture.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ impl Fixture {
143143
# bindgen downloaded is what we expect, and if `=` is
144144
# removed then it will download whatever the newest version
145145
# of wasm-bindgen is which may not be what's listed here.
146-
wasm-bindgen = "=0.2.37"
146+
wasm-bindgen = "=0.2.67"
147147
148148
[dev-dependencies]
149-
wasm-bindgen-test = "0.2"
149+
wasm-bindgen-test = "0.3"
150150
"#,
151151
name
152152
),
@@ -221,7 +221,7 @@ impl Fixture {
221221

222222
static INSTALL_WASM_BINDGEN: Once = Once::new();
223223
let cache = self.cache();
224-
let version = "0.2.37";
224+
let version = "0.2.67";
225225

226226
let download = || {
227227
if let Ok(download) =
@@ -417,7 +417,7 @@ pub fn no_cdylib() -> Fixture {
417417
wasm-bindgen = "0.2"
418418
419419
[dev-dependencies]
420-
wasm-bindgen-test = "0.2"
420+
wasm-bindgen-test = "0.3"
421421
"#,
422422
);
423423
fixture

0 commit comments

Comments
 (0)