Skip to content

Commit

Permalink
feat: reftypes argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarnadas committed Jul 31, 2020
1 parent 9f9634c commit dbb3a2c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 26 deletions.
50 changes: 35 additions & 15 deletions docs/src/commands/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ wasm-pack build --out-name index
# index.d.ts index.js index_bg.d.ts index_bg.wasm package.json README.md
```


## Profile

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

| Profile | Debug Assertions | Debug Info | Optimizations | Notes |
|---------------|------------------|------------|---------------|---------------------------------------|
| `--dev` | Yes | Yes | No | Useful for development and debugging. |
| Profile | Debug Assertions | Debug Info | Optimizations | Notes |
| ------------- | ---------------- | ---------- | ------------- | ----------------------------------------------------------- |
| `--dev` | Yes | Yes | No | Useful for development and debugging. |
| `--profiling` | No | Yes | Yes | Useful when profiling and investigating performance issues. |
| `--release` | No | No | Yes | Useful for shipping to production. |
| `--release` | No | No | Yes | Useful for shipping to production. |

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

| Option | Usage | Description |
|-----------|------------|-----------------------------------------------------------------------------------------------------|
| *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. |
| `nodejs` | [Node.js][deploy-nodejs] | Outputs JS that uses CommonJS modules, for use with a `require` statement. `main` key in `package.json`. |
| `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. |
| `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` |
| Option | Usage | Description |
| ---------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| _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. |
| `nodejs` | [Node.js][deploy-nodejs] | Outputs JS that uses CommonJS modules, for use with a `require` statement. `main` key in `package.json`. |
| `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. |
| `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` |

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

The `build` command accepts an optional `--mode` argument.

```
wasm-pack build examples/js-hello-world --mode no-install
```

| Option | Description |
|---------------|------------------------------------------------------------------------------------------|
| `no-install` | `wasm-pack init` implicitly and create wasm binding without installing `wasm-bindgen`. |
| `normal` | do all the stuffs of `no-install` with installed `wasm-bindgen`. |
| Option | Description |
| ------------ | -------------------------------------------------------------------------------------- |
| `no-install` | `wasm-pack init` implicitly and create wasm binding without installing `wasm-bindgen`. |
| `normal` | do all the stuffs of `no-install` with installed `wasm-bindgen`. |

## Support for Reference Types

You can enable support for reference types with the optional `--reftypes` argument.

```
wasm-pack build examples/js-hello-world --reftypes
```

This feature is hoped to enable more efficient communication between the host (JS) and the wasm module, but not yet all browsers support it.

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).

This argument will automatically disable `wasm-opt`.
Otherwise compilation would crash with the following error:

```
[parse exception: Only 1 table definition allowed in MVP (at 0:4234)]
Fatal: error in parsing input
```

## Extra options

Expand Down
5 changes: 5 additions & 0 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn wasm_bindgen_build(
out_dir: &Path,
out_name: &Option<String>,
disable_dts: bool,
enable_reftypes: bool,
target: Target,
profile: BuildProfile,
) -> Result<(), failure::Error> {
Expand Down Expand Up @@ -48,6 +49,10 @@ pub fn wasm_bindgen_build(
.arg(out_dir)
.arg(dts_arg);

if enable_reftypes {
cmd.arg("--reference-types");
}

let target_arg = build_target_arg(target, &bindgen_path)?;
if supports_dash_dash_target(bindgen_path.to_path_buf())? {
cmd.arg("--target").arg(target_arg);
Expand Down
12 changes: 12 additions & 0 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Build {
pub crate_data: manifest::CrateData,
pub scope: Option<String>,
pub disable_dts: bool,
pub enable_reftypes: bool,
pub target: Target,
pub profile: BuildProfile,
pub mode: InstallMode,
Expand Down Expand Up @@ -119,6 +120,11 @@ pub struct BuildOptions {
/// this flag will disable generating this TypeScript file.
pub disable_dts: bool,

#[structopt(long = "reftypes")]
/// Enable support for reference types. This feature is hoped to enable more efficient
/// communication between the host (JS) and the wasm module, but not yet all browsers support it.
pub enable_reftypes: bool,

#[structopt(long = "target", short = "t", default_value = "bundler")]
/// Sets the target environment. [possible values: bundler, nodejs, web, no-modules]
pub target: Target,
Expand Down Expand Up @@ -160,6 +166,7 @@ impl Default for BuildOptions {
scope: None,
mode: InstallMode::default(),
disable_dts: false,
enable_reftypes: false,
target: Target::default(),
debug: false,
dev: false,
Expand Down Expand Up @@ -196,6 +203,7 @@ impl Build {
crate_data,
scope: build_opts.scope,
disable_dts: build_opts.disable_dts,
enable_reftypes: build_opts.enable_reftypes,
target: build_opts.target,
profile,
mode: build_opts.mode,
Expand Down Expand Up @@ -370,6 +378,7 @@ impl Build {
&self.out_dir,
&self.out_name,
self.disable_dts,
self.enable_reftypes,
self.target,
self.profile,
)?;
Expand All @@ -378,6 +387,9 @@ impl Build {
}

fn step_run_wasm_opt(&mut self) -> Result<(), Error> {
if self.enable_reftypes {
return Ok(());
}
let args = match self
.crate_data
.configured_profile(self.profile)
Expand Down
14 changes: 13 additions & 1 deletion tests/all/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn dash_dash_web_target_has_error_on_old_bindgen() {
name = 'bar'
[dependencies]
wasm-bindgen = "=0.2.37"
wasm-bindgen = "=0.2.67"
"#,
)
.file(
Expand Down Expand Up @@ -376,3 +376,15 @@ fn build_crates_with_same_names() {
.assert()
.success();
}

#[test]
fn build_with_reftypes() {
let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen();
fixture
.wasm_pack()
.arg("build")
.arg("--reftypes")
.assert()
.success();
}
4 changes: 2 additions & 2 deletions tests/all/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn can_download_prebuilt_wasm_bindgen() {
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
if let install::Status::Found(dl) =
install::download_prebuilt(&Tool::WasmBindgen, &cache, "0.2.37", true).unwrap()
install::download_prebuilt(&Tool::WasmBindgen, &cache, "0.2.67", true).unwrap()
{
assert!(dl.binary("wasm-bindgen").unwrap().is_file());
assert!(dl.binary("wasm-bindgen-test-runner").unwrap().is_file())
Expand All @@ -29,7 +29,7 @@ fn can_download_prebuilt_wasm_bindgen() {
))]
fn downloading_prebuilt_wasm_bindgen_handles_http_errors() {
let dir = tempfile::TempDir::new().unwrap();
let bad_version = "0.2.37-some-trailing-version-stuff-that-does-not-exist";
let bad_version = "0.2.67-some-trailing-version-stuff-that-does-not-exist";
let cache = Cache::at(dir.path());
let result = install::download_prebuilt(&Tool::WasmBindgen, &cache, bad_version, true);
assert!(result.is_err());
Expand Down
12 changes: 6 additions & 6 deletions tests/all/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn it_gets_wasm_bindgen_version() {
fixture.cargo_check();
let data = CrateData::new(&fixture.path, None).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.67"),);
}

#[test]
Expand All @@ -17,7 +17,7 @@ fn it_gets_wasm_bindgen_test_version() {
fixture.cargo_check();
let data = CrateData::new(&fixture.path, None).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_test_version(), Some("0.2.37"),);
assert_eq!(lock.wasm_bindgen_test_version(), Some("0.2.67"),);
}

#[test]
Expand Down Expand Up @@ -46,7 +46,7 @@ fn it_gets_wasm_bindgen_version_in_crate_inside_workspace() {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.37"
wasm-bindgen = "=0.2.67"
"#,
)
.file(
Expand All @@ -62,7 +62,7 @@ fn it_gets_wasm_bindgen_version_in_crate_inside_workspace() {
fixture.cargo_check();
let data = CrateData::new(&fixture.path.join("blah"), None).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.67"),);
}

#[test]
Expand Down Expand Up @@ -91,7 +91,7 @@ fn it_gets_wasm_bindgen_version_from_dependencies() {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.37"
wasm-bindgen = "=0.2.67"
"#,
)
.file(
Expand Down Expand Up @@ -130,5 +130,5 @@ fn it_gets_wasm_bindgen_version_from_dependencies() {
fixture.cargo_check();
let data = CrateData::new(&fixture.path.join("parent"), None).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.67"),);
}
4 changes: 2 additions & 2 deletions tests/all/utils/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Fixture {
# bindgen downloaded is what we expect, and if `=` is
# removed then it will download whatever the newest version
# of wasm-bindgen is which may not be what's listed here.
wasm-bindgen = "=0.2.37"
wasm-bindgen = "=0.2.67"
[dev-dependencies]
wasm-bindgen-test = "0.2"
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Fixture {

static INSTALL_WASM_BINDGEN: Once = Once::new();
let cache = self.cache();
let version = "0.2.37";
let version = "0.2.67";

let download = || {
if let Ok(download) =
Expand Down

0 comments on commit dbb3a2c

Please sign in to comment.