Update master

master
daubaris 6 years ago
commit 88f45fe963
  1. 3
      .appveyor.yml
  2. 6
      .github/PULL_REQUEST_TEMPLATE.md
  3. 1
      .gitignore
  4. 13
      .travis.yml
  5. 635
      CHANGELOG.md
  6. 1061
      Cargo.lock
  7. 25
      Cargo.toml
  8. 27
      README.md
  9. 23
      binary-install/Cargo.toml
  10. 2
      binary-install/README.md
  11. 141
      binary-install/src/lib.rs
  12. 142
      binary-install/tests/all/cache.rs
  13. 125
      binary-install/tests/all/download.rs
  14. 7
      binary-install/tests/all/main.rs
  15. 79
      binary-install/tests/all/utils/mod.rs
  16. 1
      clippy.toml
  17. 44
      docs/_theme/header.hbs
  18. 6
      docs/index.html
  19. 1
      docs/public/custom.css
  20. 32
      docs/src/SUMMARY.md
  21. 1
      docs/src/command/pack-and-publish.md
  22. 45
      docs/src/commands/build.md
  23. 12
      docs/src/commands/pack-and-publish.md
  24. 84
      docs/src/commands/test.md
  25. 21
      docs/src/prerequisites/index.md
  26. 35
      docs/src/prerequisites/rust.md
  27. 3
      docs/src/tutorial/template-deep-dive/src-lib-rs.md
  28. 3
      docs/src/tutorial/template-deep-dive/src-utils-rs.md
  29. 15
      docs/src/tutorials/hybrid-applications-with-webpack/getting-started.md
  30. 14
      docs/src/tutorials/hybrid-applications-with-webpack/index.md
  31. 85
      docs/src/tutorials/hybrid-applications-with-webpack/using-your-library.md
  32. 9
      docs/src/tutorials/index.md
  33. 24
      docs/src/tutorials/npm-browser-packages/building-your-package.md
  34. 2
      docs/src/tutorials/npm-browser-packages/getting-started.md
  35. 9
      docs/src/tutorials/npm-browser-packages/index.md
  36. 0
      docs/src/tutorials/npm-browser-packages/packaging-and-publishing.md
  37. 0
      docs/src/tutorials/npm-browser-packages/project-setup/index.md
  38. 0
      docs/src/tutorials/npm-browser-packages/project-setup/manual-setup.md
  39. 0
      docs/src/tutorials/npm-browser-packages/project-setup/using-a-template.md
  40. 0
      docs/src/tutorials/npm-browser-packages/template-deep-dive.md
  41. 1
      docs/src/tutorials/npm-browser-packages/template-deep-dive/building-your-project.md
  42. 65
      docs/src/tutorials/npm-browser-packages/template-deep-dive/cargo-toml.md
  43. 0
      docs/src/tutorials/npm-browser-packages/template-deep-dive/index.md
  44. 99
      docs/src/tutorials/npm-browser-packages/template-deep-dive/src-lib-rs.md
  45. 66
      docs/src/tutorials/npm-browser-packages/template-deep-dive/src-utils-rs.md
  46. 56
      docs/src/tutorials/npm-browser-packages/template-deep-dive/wee_alloc.md
  47. 0
      docs/src/tutorials/npm-browser-packages/using-your-library.md
  48. 1
      docs/src/tutorials/standalone-wasm-binaries/index.md
  49. 91
      src/bindgen.rs
  50. 131
      src/build.rs
  51. 14
      src/cache.rs
  52. 195
      src/child.rs
  53. 224
      src/command/build.rs
  54. 25
      src/command/login.rs
  55. 74
      src/command/mod.rs
  56. 16
      src/command/pack.rs
  57. 73
      src/command/publish/mod.rs
  58. 137
      src/command/test.rs
  59. 32
      src/command/utils.rs
  60. 2
      src/installer.rs
  61. 34
      src/lib.rs
  62. 89
      src/license.rs
  63. 2
      src/lockfile.rs
  64. 72
      src/logger.rs
  65. 14
      src/main.rs
  66. 315
      src/manifest/mod.rs
  67. 2
      src/manifest/npm/commonjs.rs
  68. 2
      src/manifest/npm/esmodules.rs
  69. 2
      src/manifest/npm/nomodules.rs
  70. 60
      src/npm.rs
  71. 111
      src/progressbar.rs
  72. 14
      src/readme.rs
  73. 28
      src/test/mod.rs
  74. 42
      src/test/webdriver.rs
  75. 10
      tests/all/bindgen.rs
  76. 124
      tests/all/build.rs
  77. 152
      tests/all/license.rs
  78. 12
      tests/all/lockfile.rs
  79. 6
      tests/all/main.rs
  80. 213
      tests/all/manifest.rs
  81. 6
      tests/all/readme.rs
  82. 283
      tests/all/test.rs
  83. 190
      tests/all/utils/fixture.rs
  84. 6
      tests/all/utils/logger.rs
  85. 1
      tests/all/utils/manifest.rs
  86. 1
      tests/all/utils/mod.rs
  87. 5
      tests/all/webdriver.rs

@ -17,6 +17,9 @@ build: false
test_script:
- cargo test --release --tests --locked
- cargo test --release --doc
- cd binary-install
- cargo test
- cd ..
before_deploy:
- ps: |

@ -1,12 +1,10 @@
Make sure these boxes are checked! 📦✅
- [ ] You have the latest version of `rustfmt` installed and have your
cloned directory set to nightly
- [ ] You have the latest version of `rustfmt` installed
```bash
$ rustup override set nightly
$ rustup component add rustfmt-preview --toolchain nightly
```
- [ ] You ran `rustfmt` on the code base before submitting
- [ ] You ran `cargo fmt` on the code base before submitting
- [ ] You reference which issue is being closed in the PR text
✨✨ 😄 Thanks so much for contributing to wasm-pack! 😄 ✨✨

1
.gitignore vendored

@ -2,7 +2,6 @@ target/
**/*.rs.bk
tests/.crates.toml
tests/bin
wasm-pack.log
/build-installer
docs/book
docs/installer

@ -40,17 +40,20 @@ matrix:
- name: Tests
env: RUST_BACKTRACE=1
rust: nightly
rust: stable
addons:
firefox: latest
chrome: stable
install:
- *INSTALL_NODE_VIA_NVM
script:
- cargo test --locked
- cargo test --all --locked
- rustup component add rustfmt-preview
- cargo fmt --version
- cargo fmt --all -- --check
- rustup component add clippy-preview
- cargo clippy --version
- cargo clippy
- name: Book
rust: stable
@ -59,7 +62,7 @@ matrix:
- (test -x $HOME/.cargo/bin/mdbook || cargo install --vers "^0.2" mdbook)
- cargo install-update -a
script:
- (cd docs && mdbook build)
- (cd docs && mv _theme theme && mdbook build)
- rustc ./docs/_installer/build-installer.rs
- ./build-installer
deploy:
@ -73,7 +76,7 @@ matrix:
- name: Linux Binary
env: TARGET=x86_64-unknown-linux-musl
rust: nightly
rust: stable
before_script: rustup target add $TARGET
script: cargo build --release --target $TARGET --locked --features vendored-openssl
addons:
@ -85,7 +88,7 @@ matrix:
- name: macOS Binary
env: MACOSX_DEPLOYMENT_TARGET=10.7 TARGET=x86_64-apple-darwin
os: osx
rust: nightly
rust: stable
script: cargo build --release --target $TARGET --locked
install: true
<<: *DEPLOY_TO_GITHUB

@ -1,5 +1,640 @@
# Changelog
## 🌤 0.7.0
- ### ✨ Features
- **Non `rustup` environment support - [drager], [pull/552]**
Before now, `wasm-pack` had a hard requirement that `rustup` had to be in the PATH. While most Rust users use
`rustup` there are variety reasons to have an environment that doesn't use `rustup`. With this PR, we'll now
support folks who are using a non-`rustup` environment!
[pull/552]: https://github.com/rustwasm/wasm-pack/pull/552
- **Improved CLI Output - [alexcrichton], [pull/547]**
It's hard to decide if this is a fix or a feature, but let's keep it positive! This PR moves `wasm-pack`'s CLI
output strategy closer to the desired standard we have for 1.0. This is important as it fixes many small bugs
that are distributed across a diveristy of terminals and difficult to test for locally.
This strategy was first introduced as a mini RFC in [issue/298], and then was discussed in a session at the Rust
All Hands ([notes](https://gist.github.com/fitzgen/23a62ebbd67574b9f6f72e5ac8eaeb67#file-road-to-wasm-pack-1-0-md)).
You'll notice that the spinner is gone- we eventually want to have one, but we'd like one that doesn't cause bugs!
If you have feedback about terminal support or an output bug, please [file an issue]! We want to hear from you!
Check out the new output in the `README` demo- or update your `wasm-pack` and take it for a spin!
[file an issue]: https://github.com/rustwasm/wasm-pack/issues/new/choose
[pull/547]: https://github.com/rustwasm/wasm-pack/pull/547
[issue/298]: https://github.com/rustwasm/wasm-pack/issues/298
- **Add support for `--target web` - [alexcrichton], [pull/567]**
Recently, `wasm-bindgen` add a new target- `web`. This new target is similar to the `no-modules` target, in that
it is designed to generate code that should be loaded directly in a browser, without the need of a bundler. As
opposed to the `no-modules` target, which produces an IIFE (Immediately Invoked Function Expression), this target
produces code that is an ES6 module.
You can use this target by running:
```
wasm-pack build --target web
```
Learn more about how to use this target by [checking out the docs!](https://rustwasm.github.io/wasm-pack/book/commands/build.html#target)
[pull/567]: https://github.com/rustwasm/wasm-pack/pull/567
- **Support passing arbitrary arguments to `cargo test` via `wasm-pack test` - [chinedufn], [issue/525] [pull/530]**
`wasm-pack test` is an awesome command that wraps `cargo test` in a way that helps provide you some nice out of the
box configuration and setup. However, you may find yourself wanting to leverage the full funcationality of `cargo test`
by passing arguments that haven't been re-exported by the `wasm-pack test` interface.
For example, if you have a large test suite, it can be nice to simply run one test, or a subset of your tests.
`cargo test` supports this, however up until now, the `wasm-pack test` interface did not!
`wasm-pack test` now accepts passing and arbitrary set of arguments that it will forward along to its `cargo test` call
by allowing users to use `--` after any `wasm-pack test` arguments, followed by the set of arguments you'd like to pass
to `cargo test`.
For example:
```
# Anything after `--` gets passed to the `cargo test`
wasm-pack test --firefox --headless -- --package my-workspace-crate my_test_name --color=always
```
This will just run the `my_test_name` test and will output using color!
[See the `test` docs here!](https://rustwasm.github.io/docs/wasm-pack/commands/test.html)
[chinedufn]: https://github.com/chinedufn
[issue/525]: https://github.com/rustwasm/wasm-pack/issues/525
[pull/530]: https://github.com/rustwasm/wasm-pack/pull/530
- **Support `homepage` field of `Cargo.toml` and `package.json` - [rhysd], [pull/531]**
Both `Cargo.toml` and `package.json` support a `homepage` field that allow you to specify a website for
your project. We didn't support it previously (purely an accidental omission) - but now we do!
[pull/531]: https://github.com/rustwasm/wasm-pack/pull/531
- **Support `license-file` field in `Cargo.toml` - [rhysd], [pull/527]**
Sometimes, you want to provide a custom license, or specific license file that doesn't map to SPDX standard
licenses. In Rust/Cargo, you accomplish this by omitting the `license` field and including a `license-file`
field instead. You can read more about this in the [`cargo` manifest documentation].
In an npm package, this translates to `"license": "SEE LICENSE IN <filename>"` in your `package.json`. You can
read more about this in the [npm `package.json` documentation].
We previously only supported using SPDX standard licenses, by only supporting the `"license"` key in your
`Cargo.toml`- but now we'll allow you to leverage the `license-file` key as well, and will translate it
correctly into your `package.json`!
[`cargo` manifest documentation]: https://doc.rust-lang.org/cargo/reference/manifest.html
[npm `package.json` documentation]: https://docs.npmjs.com/files/package.json#license
[rhysd]: https://github.com/rhysd
[pull/527]: https://github.com/rustwasm/wasm-pack/pull/527
- ### 🤕 Fixes
- **`wasm-pack-init (1).exe` should work - [ashleygwilliams], [issue/518] [pull/550]**
Several users noted that when downloading a new version of `wasm-pack` their browser named the executable
file `wasm-pack-init (1).exe`. When named this way, the file didn't show the init instructions on execution.
This happened because the installation logic was requiring an exact match on filename. We've loosened that
restriction so that the filename must *start* with `wasm-pack-init` and will still execute files with these
additional, extraneous charaters in the filename. Thanks so much to [Mblkolo] and [danwilhelm] for filing the
issue and the excellent discussion!
[issue/518]: https://github.com/rustwasm/wasm-pack/issues/518
[pull/550]: https://github.com/rustwasm/wasm-pack/pull/550
[Mblkolo]: https://github.com/Mblkolo
- **Fix chromedriver error and message on Windows for `wasm-pack test` - [jscheffner], [issue/535] [pull/537]**
When running `wasm-pack test` on a 64-bit Windows machine, users would receive an error:
`geckodriver binaries are unavailable for this target`. This error message had two issues- firstly, it accidentally
said "geckodriver" instead of "chromedriver", secondly, it threw an error instead of using the available 32-bit
chromedriver distribution. Chromedriver does not do a specific disribution for Windows 64-bit!
We've fixed the error message and have also ensured that 64-bit Windows users won't encounter an error, and will
appropriately fallback to the 32-bit Windows chromedriver.
[jscheffner]: https://github.com/jscheffner
[issue/535]: https://github.com/rustwasm/wasm-pack/issues/535
[pull/537]: https://github.com/rustwasm/wasm-pack/pull/537
- **Correct look up location for `wasm-bindgen` when it's installed via `cargo install` - [fitzgen], [pull/504]**
Sometimes, when a `wasm-bindgen` binary is not available, or if `wasm-pack` is being run on an architecture that
`wasm-bindgen` doesn't produce binaries for, instead of downloading a pre-built binary, `wasm-pack` will install
`wasm-bindgen` using `cargo install`. This is a great and flexible back up!
However, due to the last release's recent refactor to use a global cache, we overlooked the `cargo install` case
and did not look for `wasm-bindgen` in the appropriate location. As a result, this led to a bug where `wasm-pack`
would panic.
We've fixed the lookup for the `cargo install`'d `wasm-bindgen` by moving the `cargo-install`'d version to global
cache location for `wasm-pack` once it's successfully built. We also eliminated the panic in favor of
propagating an error. Thanks for your bug reports and sorry about the mistake!
[pull/504]: https://github.com/rustwasm/wasm-pack/pull/504
- **Only print `cargo test` output the once - [fitzgen], [issue/511] [pull/521]**
Due to some technical debt and churn in the part of the codebase that handles output, we were accidentally
printing the output of `cargo test` twice. Now we ensure that we print it only one time!
[issue/511]: https://github.com/rustwasm/wasm-pack/issues/511
[pull/521]: https://github.com/rustwasm/wasm-pack/pull/521
- ### 🛠 Maintenance
- **Fix `clippy` warnings - [mstallmo], [issue/477] [pull/478]**
[`clippy`] is an awesome utilty that helps lint your Rust code for common optimizations and idioms. at the
beginning of `wasm-pack` development, `clippy` had not yet stablized, but it has since 1.0'd and it was
high time we leveraged it in `wasm-pack`. We still aren't *completely* fixed, but we're working on it, and
we've already dervived a ton of value from the tool!
[`clippy`]: https://github.com/rust-lang/rust-clippy
[issue/477]: https://github.com/rustwasm/wasm-pack/issues/477
[pull/478]: https://github.com/rustwasm/wasm-pack/pull/478
- **Run `clippy` check on Travis - [drager], [pull/502]**
Now that `wasm-pack` has been clippified- we want to keep it that way! Now in addition to `cargo fmt` and
`cargo test`, we'll also run `cargo clippy` on all incoming PRs!
[pull/502]: https://github.com/rustwasm/wasm-pack/pull/502
- **Port tests to use `assert-cmd` - [fitzgen], [pull/522]**
[`assert_cmd`] is a great utility for testing CLI applications that is supported by the [CLI WG]. `wasm-pack`
development began before this library existed- so we were using a much less pleasant and efficient strategy
to test the CLI functionality of `wasm-pack`. Now we've ported over to using this great library!
[CLI WG]: https://www.rust-lang.org/what/cli
[`assert_cmd`]: https://crates.io/crates/assert_cmd
[pull/522]: https://github.com/rustwasm/wasm-pack/pull/522
- **Add initial tests for `binary-install` crate - [drager], [pull/517]**
In the last release, we separated some of our binary install logic into a new crate, `binary-install`.
However, that's about all we did... move the logic! In an effort to move the crate into true open source
status, [drager] has done some excellent work adding tests to the crate. This was trickier than it looked
and involved creating a test server! Thanks for all the efforts [drager], and the great review work [fitzgen]
and [lfairy]!
[pull/517]: https://github.com/rustwasm/wasm-pack/pull/517
[lfairy]: https://github.com/lfairy
- **Update tests `wasm-bindgen` version - [huangjj27], [issue/519] [issue/417] [pull/526]**
Our tests use fixtures that reference `wasm-bindgen` often, but the versions were not consistent or up to
date. As a result, the test suite leverage many version of `wasm-bindgen` which meant that they took a while
to run as they couldn't use the cached version of `wasm-bindgen` because the cached versions we slightly
different! Now they are up to date and consistent so the tests can perform better!
[pull/526]: https://github.com/rustwasm/wasm-pack/pull/526
[issue/519]: https://github.com/rustwasm/wasm-pack/issues/519
[issue/417]: https://github.com/rustwasm/wasm-pack/issues/417
- ### 📖 Documentation
- **Flag gh-pages docs as unpublished - [alexcrichton] [pull/565]**
Recently, [DebugSteven] made a PR to merge all the documentation for the rustwasm toolchain into a
[single location]. This is going to make discovering and using tools from the entire organization easier
for new and seasoned folks alike. This also has the feature of displaying documentation that is related
to the current published version of each tool- unlike before, where the only accessible documentation was
for the tools at current master (which may or may not be currently published!)
If you like reading the current master's documentation- fear not, each tool will still publish the
documentation generated from the master branch on their individual `gh-pages`
([See `wasm-pack's` master docs here]). To avoid confusion, we've added a flash message that let's you know
which documentation you are reading- and provides a link to documentation of the published version- just
in case that's what you're looking for!
[DebugSteve]: https://github.com/DebugSteven
[single location]: https://rustwasm.github.io/docs.html
[See `wasm-pack's` master docs here]: https://rustwasm.github.io/wasm-pack/book/
[pull/565]: https://github.com/rustwasm/wasm-pack/pull/565
- **Add new QuickStart guide for "Hybrid Applications with Webpack" - [DebugSteven] [pull/536]**
Since `wasm-pack` was first published, we've focused on a workflow where a user writes a library and then
publishes it to npm, where anyone can use it like any npm package in their JavaScript or Node.js application.
Shortly after `wasm-pack` appeared, some RustWASM teammates created a template for a similar workflow- building
a RustWASM package *alongside* an application. They did this by leveraging Webpack plugins, and it's a really
lovely user experience!
[This template] hasn't gotten as much attention because we've lacked a quickstart guide for folks to discover
and follow- now we've got one!
Check out the guide [here](https://rustwasm.github.io/wasm-pack/book/tutorials/hybrid-applications-with-webpack/index.html)!
[This temaplte]: https://github.com/rustwasm/rust-webpack-template
[DebugSteven]: https://github.com/DebugSteven
[pull/536]: https://github.com/rustwasm/wasm-pack/pull/536
- **Add `wee_alloc` deepdive - [surma], [pull/542]**
`wee_alloc` is a useful utility that deserved more attention and explanation than our previous docs addressed.
This was partially because the `wasm-pack` template has an explanatory comment that helps explain its use.
However, for folks who don't use the template, `wee_alloc` is something important to know about- so now we have
given it its own section!
Check out the deepdive [here](https://rustwasm.github.io/wasm-pack/book/tutorials/npm-browser-packages/template-deep-dive/wee_alloc.html)!
[surma]: https://github.com/surma
[pull/542]: https://github.com/rustwasm/wasm-pack/pull/542
- **Update prerequisite documentation - [alexcrichton], [pull/569]**
Many folks are using `wasm-pack` without publishing to npm- as a result, we've updated the documentation to
clearly indicate that npm is an optional requirement, only required for specific targets and workflows.
Additionally, since the 2018 Edition landed, `nightly` Rust is no longer a requirement. We've removed those
instructions and have consolidated the documentation so it is shorter and more efficient at getting you
started!
[pull/569]: https://github.com/rustwasm/wasm-pack/pull/569
- **Clarify what kind of account `login` adds - [killercup], [pull/539]**
Previously, when view `--help`, the command description for `login` showed:
`👤 Add a registry user account!` This could be confusing for folks, so now it's been updated to read:
`👤 Add an npm registry user account!`, which is much clearer!
[killercup]: https://github.com/killercup
[pull/539]: https://github.com/rustwasm/wasm-pack/pull/539
- **Wasm is a contraction, not an acronym - [fitzgen], [pull/555]**
Ever wonder how you're *actually* supposed to refer to WebAssembly in short-form? WASM? wasm? For the pedants
out there, the correct usage is "Wasm" because Wasm is a *contraction* of the words Web and Assembly. We've
updated our doucmentation to consistently refer to WebAssembly as Wasm in the shortform.
*The more you know!*
[pull/555]: https://github.com/rustwasm/wasm-pack/pull/555
- **Fix links and Rust highlightning - [drager], [issue/513] [pull/514] [pull/516]**
We had some broken links and missing Rust syntax highlighting in a few sections of the docs. This fixes that!
[issue/513]: https://github.com/rustwasm/wasm-pack/issues/513
[pull/514]: https://github.com/rustwasm/wasm-pack/pull/514
[pull/516]: https://github.com/rustwasm/wasm-pack/pull/516
## 🌅 0.6.0
- ### ✨ Features
- **Add three build profiles and infrastructure for their toml config - [fitzgen], [issue/153] [issue/160] [pull/440]**
When originally conceived, `wasm-pack` was exclusively a packaging and publishing tool, which naively assumed
that the crate author would simply run `wasm-pack` when they were ready to publish a wasm package. As a result,
`wasm-pack` always ran `cargo build` in `--release` mode. Since then, `wasm-pack` has grown into an integrated build
tool used at all stages of development, from idea conception to publishing, and as such has developed new needs.
In previous releases, we've supported a flag called `--debug` which will run `cargo build` in `dev` mode, which
trades faster compilation speed for a lack of optimizations. We've renamed this flag to `--dev` to match `cargo`
and added an additional flag, representing a third, intermediary, build profile, called `--profiling` which
is useful for investigating performance issues. You can see all three flags and their uses in the table below:
| 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. |
The meaning of these flags will evolve as the platform grows, and always be tied to the behavior of these flags
in `cargo`. You can learn more about these in the [`cargo profile` documentation].
This PR also introduces a way to configure `wasm-pack` in your `Cargo.toml` file that we intend to use much more
in the future. As a largely convention-based tool, `wasm-pack` will never require that you configure it manually,
however, as our community and their projects mature alongside the tool, it became clear that allowing folks the
ability to drop down and configure things was something we needed to do to meet their needs.
Currently, you can only configure things related to the above-mentioned build profiles. To learn more,
[check out the documentation][profile-config-docs]. It leverages the `package.metadata.wasm-pack` key in your
`Cargo.toml`, and looks like this:
```toml
# Cargo.toml
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
# Should we enable wasm-bindgen's debug assertions in its generated JS glue?
debug-js-glue = true
# Should wasm-bindgen demangle the symbols in the "name" custom section?
demangle-name-section = true
# Should we emit the DWARF debug info custom sections?
dwarf-debug-info = false
```
As always- there are defaults for you to use, but if you love to configure (or have a project that requires it),
get excited, as your options have grown now and will continue to!
[profile-config-docs]: https://rustwasm.github.io/wasm-pack/book/cargo-toml-configuration.html
[`cargo profile` documentation]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections
[issue/153]: https://github.com/rustwasm/wasm-pack/issues/153
[issue/160]: https://github.com/rustwasm/wasm-pack/issues/160
[pull/440]: https://github.com/rustwasm/wasm-pack/pull/440
- **DEPRECATION: Rename `--debug` to `--dev` to match `cargo` - [fitzgen], [pull/439]**
See the discussion of the build profiles feature above. This is a strict renaming of the previous `--debug` flag,
which will now warn as deprecated.
[pull/439]: https://github.com/rustwasm/wasm-pack/pull/439
- **Add an option to pass an arbitrary set of arguments to `cargo build` - [torkve], [issue/455] [pull/461]**
As an integrated build tool, `wasm-pack` orchestrates many secondary command line tools to build your package
in a single command. Notably, one of these tools is `cargo`. `cargo` has a wide array of features and flags, and
we couldn't reasonably expect to implement them all as first class features of `wasm-pack`. As a result, we've
created the option to allow users to pass an arbitrary number of additional flags to `wasm-pack` by appending them
to the `wasm-pack build` command, after passing `--`. For example:
```
wasm-pack build examples/js-hello-world --mode no-install -- -Z offline
```
In the above example, the flag `-Z offline` will be passed to `cargo build`. This feature is documented
[here][cargo opts docs].
[cargo opts docs]: https://rustwasm.github.io/wasm-pack/book/commands/build.html#extra-options
[torkve]: https://github.com/torkve
[issue/455]: https://github.com/rustwasm/wasm-pack/issues/455
[pull/461]: https://github.com/rustwasm/wasm-pack/pull/461
- **Pre-build before wasm-pack publish - [csmoe], [issue/438] [pull/444]**
Previously, if you ran `wasm-pack publish` before you had successfully run `wasm-pack build`,
you'd receive an error that a package could not be found- because there would be no `pkg` or
out-directory containing a `package.json`.
In this situation, you would hope that `wasm-pack` would build your package for you when you
ran `wasm-pack publish`. This is slightly complicated by the fact that not everyone wants to
build their package to the default target or to a directory named `pkg`.
To solve this, running `wasm-pack publish` before a successful build will give you an interactive
prompt to build your package- allowing you to specify your out directory as well as the target you'd
like to build to. Check it out in the gif below:
![pre-build publish workflow](https://user-images.githubusercontent.com/35686186/50500909-5984fe80-0a8f-11e9-9de6-43d1423b2969.gif)
[issue/438]: https://github.com/rustwasm/wasm-pack/issues/438
[pull/444]: https://github.com/rustwasm/wasm-pack/pull/444
- **Generate self-.gitignore as part of pkg folder - [RReverser], [pull/453]**
Since `wasm-pack` was first published, the `pkg` directory was intended to be treated as a
build artifact, and as such should never be published to version control. This was
never enforced by any assets generated by `wasm-pack`, however.
Now, when building your package, `wasm-pack` will also generate a `.gitignore` file so that the
`pkg`, or out-directory, will be ignored.
If you use another version control tool, you'll need to still create or edit your own ignore file-
pull requests to support other version control tools are welcome!
If you require editing of the generated `package.json` or add additonal assets to your package
before publishing, you'll want to remove the `.gitignore` file and commit to version control. We
intend to have a solution that makes this workflow significantly easier in upcoming releases!
[RReverser]: https://github.com/RReverser
[pull/453]: https://github.com/rustwasm/wasm-pack/pull/453
- **Support cargo workspaces - [fitzgen], [issue/252] [issue/305] [pull/430]**
Workspaces are a well-liked and used feature of cargo that allow you to build multiple crates
in a single cargo project. Because of how `wasm-pack` handled paths for `target` and out-directories,
we did not support cargo workspaces out of the box. Now they should work well and the feature is
well guarded by tests!
[issue/252]: https://github.com/rustwasm/wasm-pack/issues/252
[issue/305]: https://github.com/rustwasm/wasm-pack/issues/305
[pull/430]: https://github.com/rustwasm/wasm-pack/pull/430
- **Use a global cache for all downloaded binaries - [alexcrichton], [pull/426]**
`wasm-pack` is an integrated build tool that orchestrates several other command line tools to build
your wasm project for you. How `wasm-pack` does this has evolved significantly since it's early versions.
In the last version, a `bin` directory was created to house the tool binaries that `wasm-pack` needed to
build our project, but this had several limitations. Firstly, it created a `bin` directory in your project's
root, which could be confusing. Secondly, it meant that sharing these tools across multiple projects was
not possible. We did this because it gaves us the fine-grained control over the version of these tools that
you used.
Now, `wasm-pack` will not generate a `bin` directory, but rather will use a global cache. We retain the
fine-grained control over the versions of these tools that are used, but allow multiple projects that use
the same tools at the same versions to share the already installed asset. Your global cache will generally
be in your user's home directory- we use the [`dirs` crate] to determine where to place this global cache.
This is not currently customizable but is something we intend to look into doing!
This feature ensures that `wasm-pack` users are downloading a minimal number of binaries from the network,
which, for `wasm-pack` users with multiple projects, should speed up build times.
[`dirs` crate]: https://docs.rs/dirs/1.0.4/dirs/fn.cache_dir.html
[pull/426]: https://github.com/rustwasm/wasm-pack/pull/426
- ### 🤕 Fixes
- **Fix `pack`, `login`, and `publish` for Windows users - [danwilhelm], [issue/277] [pull/489]**
Rust's behavior for spawning processes on some Windows targets introduced an interesting case where
Rust would fail unless the command was explicitly spawned with a prepended `cmd /c`. This failure
of `wasm-pack` was well noticed by our community - and thanks to the efforts of `danwilhelm` is now
fixed! You can read more on the background of this issue in [rust-lang/rust issue/44542].
[rust-lang/rust issue/44542]: https://github.com/rust-lang/rust/pull/44542
[issue/277]: https://github.com/rustwasm/wasm-pack/issues/277
[pull/489]: https://github.com/rustwasm/wasm-pack/pull/489
- **Validate `--target` argument - [csmoe], [issue/483] [pull/484]**
For a few releases now, `wasm-pack` has supported allowing users to specifying the target module system
they'd like their package built for- `browser`, `nodejs`, and `no-modules`. We did not however, validate
this input, and so if a user made even a slight mistake, e.g. `node`, `wasm-pack` would not catch the
error and would build your project using the default, `browser`. This is of course, surprising, and
unpleasant behavior and so now we'll error out with a message containing the supported target names.
[issue/483]: https://github.com/rustwasm/wasm-pack/issues/483
[pull/484]: https://github.com/rustwasm/wasm-pack/pull/484
- **Fix login - [danwilhelm], [issue/486] [pull/487]**
[danwilhelm]: https://github.com/danwilhelm
[issue/486]: https://github.com/rustwasm/wasm-pack/issues/486
[pull/487]: https://github.com/rustwasm/wasm-pack/pull/487
- **Eliminate unecessary escaping in build success terminal output - [huangjj27], [issue/390] [pull/396]**
Previously, on some systems, a successful `wasm-pack build` would print a unfortunate looking string:
```
| :-) Your wasm pkg is ready to publish at "\\\\?\\C:\\Users\\Ferris\\tmp\\wasm-bug\\pkg".
```
We've updated this to make sure the path to your project is well-formed, and most importantly,
human-readable.
[issue/390]: https://github.com/rustwasm/wasm-pack/issues/390
[pull/396]: https://github.com/rustwasm/wasm-pack/pull/396
- **Copy license file(s) to out directory - [mstallmo], [issue/407] [pull/411]**
Since `wasm-pack` was first published, we've copied over your `Cargo.toml` license definition over to
your `package.json`. However, we overlooked copying the actual `LICENSE` files over! Now we do!
[issue/407]: https://github.com/rustwasm/wasm-pack/issues/407
[pull/411]: https://github.com/rustwasm/wasm-pack/pull/411
- **Don't require cdylib crate-type for testing - [alexcrichton], [pull/442]**
`wasm-pack` was unecssarily checking `Cargo.toml` for the `cdylib` crate type during calls to `wasm-pack test`.
The `cdylib` output isn't necessary for the `wasm-pack test` stage because `wasm-bindgen` isn't being run over
a wasm file during testing. This check is now removed!
[pull/442]: https://github.com/rustwasm/wasm-pack/pull/442
- **Fix wasm-bindgen if lib is renamed via `lib.name` - [alexcrichton], [issue/339] [pull/435]**
In some circumstances, a library author may wish to specify a `name` in the `[package]` portion of their
`Cargo.toml`, as well as a different `name` in the `[lib]` portion, e.g.:
```toml
[package]
name = "hello-wasm"
[lib]
name = "wasm-lib"
```
This would cause the `wasm-bindgen` build stage of `wasm-pack` to error out because `wasm-pack` would attempt
to run `wasm-bindgen-cli` on a path using the `[package]` name, which wouldn't exist (because it would be using
the `[lib]` name). Now it works- thanks to more usage of [`cargo_metadata`] in `wasm-pack` internals!
[`cargo_metadata`]: https://crates.io/crates/cargo_metadata
[issue/339]: https://github.com/rustwasm/wasm-pack/issues/339
[pull/435]: https://github.com/rustwasm/wasm-pack/pull/435
- **Print standard error only once for failing commands - [fitzgen], [issue/422] [pull/424]**
Previously, `wasm-pack` may have printed `stderr` twice in some circumstances. This was both confusing and not
a pleasant experience, so now we've ensued that `wasm-pack` prints `stderr` exactly once! (It's hard enough to have
errors, you don't want `wasm-pack` rubbing it in, right?)
[issue/422]: https://github.com/rustwasm/wasm-pack/issues/422
[pull/424]: https://github.com/rustwasm/wasm-pack/pull/424
- **Add no-modules to --target flag's help text - [fitzgen], [issue/416] [pull/417]**
This is an interesting one! `fitzgen` very reasonably filed an issue asking to add `wasm-bindgen`'s
`--target no-modules` feature to `wasm-pack`. This was confusing as this feature was indeed already implemented,
and documented- BUT, notably missing from the `wasm-pack --help` text. We've fixed that now- and it was an omission
so glaring we definitely considered it a bug!
[issue/416]: https://github.com/rustwasm/wasm-pack/issues/416
[pull/417]: https://github.com/rustwasm/wasm-pack/pull/417
- ### 🛠 Maintenance
- **Replace `slog` with `log` - [alexcrichton], [issue/425] [pull/434]**
For internal maintenance reasons, as well as several end-user ones, we've migrated away from the `slog` family
of crates, and are now using the `log` crate plus `env_logger`. Now, `wasm-pack` won't create a `wasm-pack.log`.
Additionally, enabling logging will now be done through `RUST_LOG=wasm_pack` instead of `-v` flags.
[issue/425]: https://github.com/rustwasm/wasm-pack/issues/425
[pull/434]: https://github.com/rustwasm/wasm-pack/pull/434
- **Move binary installation to its own crate - [drager], [issue/384] [pull/415]**
In `wasm-pack 0.5.0`, we move away from `cargo install`ing many of the tools that `wasm-pack` orchestrates. Because
we used `cargo install`, this required an end user to sit through the compilation of each tool, which was a
prohibitively long time. We moved, instead, to building, and then installing, binaries of the tools. This sped up
build times dramatically!
This pattern has been very beneficial to `wasm-pack` and is potentially something that could be beneficial to other
projects! As a result, we've refactored it out into a crate and have published it as it's own crate, [`binary-install`].
[`binary-install`]: https://crates.io/crates/binary-install
[drager]: https://github.com/drager
[issue/384]: https://github.com/rustwasm/wasm-pack/issues/384
[pull/415]: https://github.com/rustwasm/wasm-pack/pull/415
- **Replace internal `Error` with `failure::Error` - [alexcrichton], [pull/436]**
The story of error message handling in `wasm-pack` has not been the prettiest. We originally were manually implementing
errors, adding the [`failure` crate] at one point, but not fully updating the entire codebase. With this PR, we are
nearly completely handling errors with `failure`, bringing the code into a much more maintainable and
pleasant-to-work-on place.
[`failure` crate]: https://crates.io/crates/failure
[pull/436]: https://github.com/rustwasm/wasm-pack/pull/436
- **Update `mdbook` version used by Travis - [fitzgen], [pull/433]**
[pull/433]: https://github.com/rustwasm/wasm-pack/pull/433
- **Read the `Cargo.toml` file only once - [fitzgen], [issue/25] [pull/431]**
This is a very fun one since it fixes one of the original issues filed by `ag_dubs` at the very beginning of `wasm-pack`
development. In a rush to implement a POC tool, `ag_dubs` noted for posterity that the `Cargo.toml` was being read
multiple times (twice), when it did not need to be. Thanks to `fitzgen` now it's read only once! A minor performance
improvement in the scheme of things, but a nice one :)
[issue/25]: https://github.com/rustwasm/wasm-pack/issues/25
[pull/431]: https://github.com/rustwasm/wasm-pack/pull/431
- **Use `name` field for Travis CI jobs - [fitzgen], [pull/432]**
[pull/432]: https://github.com/rustwasm/wasm-pack/pull/432
- **Add a test for build command - [huangjj27], [pull/408]**
[huangjj27]: https://github.com/huangjj27
[pull/408]: https://github.com/rustwasm/wasm-pack/pull/408
- **Test paths on Windows - [xmclark], [issue/380] [pull/389]**
[xmclark]: https://github.com/xmclark
[issue/380]: https://github.com/rustwasm/wasm-pack/issues/380
[pull/389]: https://github.com/rustwasm/wasm-pack/pull/389
- **Fix typo in test function name for copying the README - [mstallmo], [pull/412]**
[pull/412]: https://github.com/rustwasm/wasm-pack/pull/412
- ### 📖 Documentation
- **Complete template deep dive docs - [danwilhelm], [issue/345] [issue/346] [pull/490]**
In a rush to publish a release, `ag_dubs` left some "Coming soon!" comments on most pages
of the "Template Deep Dive" docs. These docs help walk new users through the boilerplate
that using the `wasm-pack` template generates for you. Thanks so much to `danwilhem` for
picking this up and doing an excellent job!
[issue/345]: https://github.com/rustwasm/wasm-pack/issues/345
[issue/346]: https://github.com/rustwasm/wasm-pack/issues/346
[pull/490]: https://github.com/rustwasm/wasm-pack/pull/490
- **Minor docs updates - [fitzgen], [issue/473] [pull/485]**
[issue/473]: https://github.com/rustwasm/wasm-pack/issues/473
[pull/485]: https://github.com/rustwasm/wasm-pack/pull/485
## 🌄 0.5.1
- ### 🤕 Fixes

1061
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -1,7 +1,7 @@
[package]
name = "wasm-pack"
description = "pack up the wasm and publish it to npm!"
version = "0.5.1"
description = "📦✨ your favorite rust -> wasm workflow tool!"
version = "0.7.0"
authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
repository = "https://github.com/ashleygwilliams/wasm-pack.git"
license = "MIT/Apache-2.0"
@ -13,31 +13,32 @@ documentation = "https://rustwasm.github.io/wasm-pack/"
atty = "0.2.11"
cargo_metadata = "0.6.0"
console = "0.6.1"
dialoguer = "0.3.0"
curl = "0.4.13"
dirs = "1.0.4"
env_logger = { version = "0.5.13", default-features = false }
failure = "0.1.2"
flate2 = "1.0.2"
hex = "0.3"
human-panic = "1.0.1"
indicatif = "0.9.0"
lazy_static = "1.1.0"
glob = "0.2"
log = "0.4.6"
openssl = { version = '0.10.11', optional = true }
parking_lot = "0.6"
serde = "1.0.74"
serde_derive = "1.0.74"
serde_ignored = "0.0.4"
serde_json = "1.0.26"
strsim = "0.8.0"
siphasher = "0.2.3"
slog = "2.3"
slog-term = "2.4"
slog-async = "2.3"
structopt = "0.2"
tar = "0.4.16"
toml = "0.4"
which = "2.0.0"
zip = "0.4.2"
chrono = "0.4.6"
binary-install = { version = "0.0.2", path = "./binary-install" }
walkdir = "2"
[dev-dependencies]
assert_cmd = "0.10.2"
lazy_static = "1.1.0"
predicates = "1.0.0"
tempfile = "3"
[features]

@ -1,15 +1,17 @@
# 📦✨ wasm-pack
> Your favorite rust -> wasm workflow tool!
[![Build Status](https://travis-ci.org/rustwasm/wasm-pack.svg?branch=master)](https://travis-ci.org/rustwasm/wasm-pack)
[![Build Status](https://travis-ci.com/rustwasm/wasm-pack.svg?branch=master)](https://travis-ci.com/rustwasm/wasm-pack)
[![Build status](https://ci.appveyor.com/api/projects/status/iv1qtnqtv168ef8h?svg=true)](https://ci.appveyor.com/project/ashleygwilliams/wasm-pack-071k0)
[![crates.io](https://meritbadge.herokuapp.com/wasm-pack)](https://crates.io/crates/wasm-pack)
This tool seeks to be a one-stop shop for building and working with rust-
generated WebAssembly that you would like to interop with JavaScript, in the
browser or with Node.js. `wasm-pack` helps you build and publish rust-generated
WebAssembly to the npm registry to be used alongside any other javascript
package in workflows that you already use, such as [webpack] or [greenkeeper].
browser or with Node.js. `wasm-pack` helps you build rust-generated
WebAssembly packages that you could publish to the npm registry, or otherwise use
alongside any javascript packages in workflows that you already use, such as [webpack]
or [greenkeeper].
[bundler-support]: https://github.com/rustwasm/team/blob/master/goals/bundler-integration.md#details
[webpack]: https://webpack.js.org/
@ -32,20 +34,21 @@ This project requires Rust 1.30.0 or later.
## 🎙 Commands
- [`init` (⚠ DEPRECATED)](https://rustwasm.github.io/wasm-pack/book/commands/init.html): This command has been deprecated since release `0.5.0`, in favor of `build`. `0.4.2` and previous use this command.
- [`build`](https://rustwasm.github.io/wasm-pack/book/commands/build.html): Generate an npm wasm pkg from a rustwasm crate
- [`test`](https://rustwasm.github.io/wasm-pack/book/commands/test.html): Run browser tests
- [`pack` and `publish`](https://rustwasm.github.io/wasm-pack/book/commands/pack-and-publish.html): Create a tarball of your rustwasm pkg and/or publish to a registry
## 📝 Logging
We generate a `wasm-pack.log` file if `wasm-pack` errors on you, and you can
customize the log verbosity using the verbosity flag.
`wasm-pack` uses [`env_logger`] to produces logs when `wasm-pack` runs.
| Verbosity | Result |
| ------------- |-----------------------------------------------------|
| -v | All Info, Warn, and Errors are logged |
| -vv | All Debug, Info, Warn, and Errors are logged |
| -vvv | All Trace, Debug, Info, Warn, and Errors are logged |
To configure your log level, use the `RUST_LOG` environment variable. For example:
```
RUST_LOG=info wasm-pack build
```
[`env_logger`]: https://crates.io/crates/env_logger
## 👯 Contributing

@ -0,0 +1,23 @@
[package]
name = "binary-install"
description = "install a binary from a path to a global cache"
authors = ["The wasm-pack team"]
repository = "https://github.com/rustwasm/wasm-pack/tree/master/binary-install"
license = "MIT/Apache-2.0"
version = "0.0.2"
documentation = "https://docs.rs/binary-install"
readme = "./README.md"
[dependencies]
curl = "0.4.13"
dirs = "1.0.4"
failure = "0.1.2"
flate2 = "1.0.2"
hex = "0.3"
is_executable = "0.1.2"
siphasher = "0.2.3"
tar = "0.4.16"
zip = "0.5.0"
[dev-dependencies]
tempfile = "3.0.5"

@ -0,0 +1,2 @@
# `binary-install`
> install a binary from a path to a global cache

@ -1,10 +1,17 @@
//! Utilities for finding and installing binaries that we depend on.
use curl;
use dirs;
extern crate curl;
#[macro_use]
extern crate failure;
extern crate dirs;
extern crate flate2;
extern crate hex;
extern crate is_executable;
extern crate siphasher;
extern crate tar;
extern crate zip;
use failure::{Error, ResultExt};
use flate2;
use hex;
use siphasher::sip::SipHasher13;
use std::collections::HashSet;
use std::env;
@ -13,16 +20,16 @@ use std::fs;
use std::hash::{Hash, Hasher};
use std::io;
use std::path::{Path, PathBuf};
use tar;
use zip;
/// Global cache for wasm-pack, currently containing binaries downloaded from
/// urls like wasm-bindgen and such.
#[derive(Debug)]
pub struct Cache {
destination: PathBuf,
}
/// Representation of a downloaded tarball/zip
#[derive(Debug)]
pub struct Download {
root: PathBuf,
}
@ -32,12 +39,13 @@ impl Cache {
///
/// This function may return an error if a cache directory cannot be
/// determined.
pub fn new() -> Result<Cache, Error> {
pub fn new(name: &str) -> Result<Cache, Error> {
let cache_name = format!(".{}", name);
let destination = dirs::cache_dir()
.map(|p| p.join("wasm-pack"))
.map(|p| p.join(&cache_name))
.or_else(|| {
let home = dirs::home_dir()?;
Some(home.join(".wasm-pack"))
Some(home.join(&cache_name))
})
.ok_or_else(|| format_err!("couldn't find your home directory, is $HOME not set?"))?;
Ok(Cache::at(&destination))
@ -75,22 +83,10 @@ impl Cache {
binaries: &[&str],
url: &str,
) -> Result<Option<Download>, Error> {
let mut hasher = SipHasher13::new();
url.hash(&mut hasher);
let result = hasher.finish();
let hex = hex::encode(&[
(result >> 0) as u8,
(result >> 8) as u8,
(result >> 16) as u8,
(result >> 24) as u8,
(result >> 32) as u8,
(result >> 40) as u8,
(result >> 48) as u8,
(result >> 56) as u8,
]);
let dirname = format!("{}-{}", name, hex);
let dirname = hashed_dirname(url, name);
let destination = self.destination.join(&dirname);
if destination.exists() {
return Ok(Some(Download { root: destination }));
}
@ -218,13 +214,22 @@ impl Download {
}
/// Returns the path to the binary `name` within this download
pub fn binary(&self, name: &str) -> PathBuf {
pub fn binary(&self, name: &str) -> Result<PathBuf, Error> {
use is_executable::IsExecutable;
let ret = self
.root
.join(name)
.with_extension(env::consts::EXE_EXTENSION);
assert!(ret.exists(), "binary {} doesn't exist", ret.display());
return ret;
if !ret.is_file() {
bail!("{} binary does not exist", ret.display());
}
if !ret.is_executable() {
bail!("{} is not executable", ret.display());
}
Ok(ret)
}
}
@ -255,3 +260,87 @@ fn curl(url: &str) -> Result<Vec<u8>, Error> {
)
}
}
fn hashed_dirname(url: &str, name: &str) -> String {
let mut hasher = SipHasher13::new();
url.hash(&mut hasher);
let result = hasher.finish();
let hex = hex::encode(&[
(result >> 0) as u8,
(result >> 8) as u8,
(result >> 16) as u8,
(result >> 24) as u8,
(result >> 32) as u8,
(result >> 40) as u8,
(result >> 48) as u8,
(result >> 56) as u8,
]);
format!("{}-{}", name, hex)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_returns_same_hash_for_same_name_and_url() {
let name = "wasm-pack";
let url = "http://localhost:7878/wasm-pack-v0.6.0.tar.gz";
let first = hashed_dirname(url, name);
let second = hashed_dirname(url, name);
assert!(!first.is_empty());
assert!(!second.is_empty());
assert_eq!(first, second);
}
#[test]
fn it_returns_different_hashes_for_different_urls() {
let name = "wasm-pack";
let url = "http://localhost:7878/wasm-pack-v0.5.1.tar.gz";
let second_url = "http://localhost:7878/wasm-pack-v0.6.0.tar.gz";
let first = hashed_dirname(url, name);
let second = hashed_dirname(second_url, name);
assert_ne!(first, second);
}
#[test]
fn it_returns_cache_dir() {
let name = "wasm-pack";
let cache = Cache::new(name);
let expected = dirs::cache_dir()
.unwrap()
.join(PathBuf::from(".".to_owned() + name));
assert!(cache.is_ok());
assert_eq!(cache.unwrap().destination, expected);
}
#[test]
fn it_returns_destination_if_binary_already_exists() {
use std::fs;
let binary_name = "wasm-pack";
let binaries = vec![binary_name];
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
let url = &format!("{}/{}.tar.gz", "http://localhost:7878", binary_name);
let dirname = hashed_dirname(&url, &binary_name);
let full_path = dir.path().join(dirname);
// Create temporary directory and binary to simulate that
// a cached binary already exists.
fs::create_dir_all(full_path).unwrap();
let dl = cache.download(true, binary_name, &binaries, url);
assert!(dl.is_ok());
assert!(dl.unwrap().is_some())
}
}

@ -0,0 +1,142 @@
use binary_install::Cache;
use std::path::Path;
use utils;
#[test]
fn it_returns_none_if_install_is_not_permitted() {
let binary_name = "wasm-pack";
let binaries = vec![binary_name];
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
let dl = cache.download(
false,
binary_name,
&binaries,
&format!("{}/{}.tar.gz", "", binary_name),
);
assert!(dl.is_ok());
assert!(dl.unwrap().is_none())
}
#[test]
fn it_downloads_tarball() {
let binary_name = "wasm-pack";
let binaries = vec![binary_name];
// Create a temporary tarball.
let tarball = utils::create_tarball(binary_name).ok();
// Spin up a local TcpListener.
let server_port = utils::start_server(tarball, None).recv().unwrap();
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
let dl = cache.download(
true,
binary_name,
&binaries,
&format!("{}/{}.tar.gz", &url, binary_name),
);
assert!(dl.is_ok());
assert!(dl.unwrap().is_some())
}
#[test]
fn it_returns_error_when_it_failed_to_download() {
let server_port = 7881;
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
let binary_name = "wasm-pack";
let binaries = vec![binary_name];
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
let full_url = &format!("{}/{}.tar.gz", &url, binary_name);
let dl = cache.download(true, binary_name, &binaries, full_url);
assert!(dl.is_err());
assert_eq!(
&format!("failed to download from {}", full_url),
&format!("{}", dl.unwrap_err())
);
}
#[test]
fn it_returns_error_when_it_failed_to_extract_tarball() {
let binary_name = "wasm-pack";
let binaries = vec![binary_name];
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
// Spin up a local TcpListener.
let server_port = utils::start_server(None, None).recv().unwrap();
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
let full_url = &format!("{}/{}.tar.gz", &url, binary_name);
let dl = cache.download(true, binary_name, &binaries, full_url);
assert!(dl.is_err());
assert_eq!(
&format!("failed to extract tarball from {}", full_url),
&format!("{}", dl.unwrap_err())
);
}
#[test]
fn it_returns_error_when_it_failed_to_extract_zip() {
let binary_name = "wasm-pack";
let binaries = vec![binary_name];
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
// Spin up a local TcpListener.
let server_port = utils::start_server(None, None).recv().unwrap();
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
let full_url = &format!("{}/{}.zip", &url, binary_name);
let dl = cache.download(true, binary_name, &binaries, full_url);
assert!(dl.is_err());
assert_eq!(
&format!("failed to extract zip from {}", full_url),
&format!("{}", dl.unwrap_err())
);
}
#[test]
#[should_panic(expected = "don't know how to extract http://localhost:7884/wasm-pack.bin")]
fn it_panics_if_not_tarball_or_zip() {
let server_port = 7884;
let binary_name = "wasm-pack";
let binaries = vec![binary_name];
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
// Spin up a local TcpListener.
utils::start_server(None, Some(server_port)).recv().unwrap();
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
let full_url = &format!("{}/{}.bin", &url, binary_name);
let _ = cache.download(true, binary_name, &binaries, full_url);
}
#[test]
fn it_joins_path_with_destination() {
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
assert_eq!(dir.path().join("hello"), cache.join(Path::new("hello")));
}

@ -0,0 +1,125 @@
use binary_install::Download;
use std::fs::OpenOptions;
#[test]
#[cfg(unix)]
fn it_returns_binary_name_for_unix() {
use std::os::unix::fs::OpenOptionsExt;
let binary_name = "wasm-pack";
let dir = tempfile::TempDir::new().unwrap();
let download = Download::at(dir.path());
let full_path = dir.path().join(binary_name);
let mut options = OpenOptions::new();
options.create(true);
options.write(true);
// Make the "binary" an executable.
options.mode(0o755);
options.open(&full_path).unwrap();
let binary = download.binary(binary_name);
assert!(binary.is_ok());
assert_eq!(full_path, binary.unwrap());
}
#[test]
#[cfg(not(windows))]
fn it_bails_if_not_file_for_unix() {
let binary_name = "wasm-pack";
let dir = tempfile::TempDir::new().unwrap();
let download = Download::at(dir.path());
let full_path = dir.path().join(binary_name);
let mut options = OpenOptions::new();
options.create(true);
options.write(true);
let binary = download.binary(binary_name);
assert!(binary.is_err());
assert_eq!(
format!("{} binary does not exist", full_path.to_str().unwrap()),
binary.unwrap_err().to_string()
);
}
#[test]
#[cfg(windows)]
fn it_bails_if_not_file_for_windows() {
let binary_name = "wasm-pack.exe";
let dir = tempfile::TempDir::new().unwrap();
let download = Download::at(dir.path());
let full_path = dir.path().join(binary_name);
let mut options = OpenOptions::new();
options.create(true);
options.write(true);
let binary = download.binary(binary_name);
assert!(binary.is_err());
assert_eq!(
format!("{} binary does not exist", full_path.to_str().unwrap()),
binary.unwrap_err().to_string()
);
}
#[test]
#[cfg(not(windows))]
fn it_bails_if_not_executable_for_unix() {
let binary_name = "wasm-pack";
let dir = tempfile::TempDir::new().unwrap();
let download = Download::at(dir.path());
let full_path = dir.path().join(binary_name);
let mut options = OpenOptions::new();
options.create(true);
options.write(true);
options.open(&full_path).unwrap();
let binary = download.binary(binary_name);
assert!(binary.is_err());
assert_eq!(
format!("{} is not executable", full_path.to_str().unwrap()),
binary.unwrap_err().to_string()
);
}
#[test]
#[cfg(windows)]
fn it_bails_if_not_executable_for_windows() {
let binary_name = "wasm-pack.exe";
let dir = tempfile::TempDir::new().unwrap();
let download = Download::at(dir.path());
let full_path = dir.path().join(binary_name);
let mut options = OpenOptions::new();
options.create(true);
options.write(true);
options.open(&full_path).unwrap();
let binary = download.binary(binary_name);
assert!(binary.is_err());
assert_eq!(
format!("{} is not executable", full_path.to_str().unwrap()),
binary.unwrap_err().to_string()
);
}

@ -0,0 +1,7 @@
extern crate binary_install;
extern crate flate2;
extern crate tar;
mod cache;
mod download;
mod utils;

@ -0,0 +1,79 @@
use flate2::write::GzEncoder;
use flate2::Compression;
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Write};
use std::net::TcpListener;
use std::sync::mpsc::{channel, Receiver};
use std::thread;
pub const TEST_SERVER_HOST: &'static str = "localhost";
pub fn start_server(tarball: Option<Vec<u8>>, server_port: Option<u16>) -> Receiver<u16> {
let (sender, receiver) = channel();
thread::spawn(move || {
TcpListener::bind(format!(
"{}:{}",
TEST_SERVER_HOST,
server_port.unwrap_or_else(|| 0)
))
.map(|listener| {
sender.send(listener.local_addr().unwrap().port()).unwrap();
for stream in listener.incoming() {
let mut stream = stream.unwrap();
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();
let response = "HTTP/1.1 200 OK\r\n\r\n";
stream.write(response.as_bytes()).unwrap();
match tarball.to_owned() {
Some(tar) => {
stream.write(tar.as_ref()).unwrap();
}
None => {}
}
stream.flush().unwrap();
}
})
.unwrap();
});
receiver
}
pub fn create_tarball(binary_name: &str) -> Result<Vec<u8>, io::Error> {
let temp_dir = tempfile::TempDir::new().unwrap();
let full_path = temp_dir.path().join(binary_name.to_owned() + ".tar.gz");
let tar = OpenOptions::new()
.create(true)
.read(true)
.write(true)
.open(&full_path)?;
let mut file = OpenOptions::new()
.create(true)
.read(true)
.write(true)
.open(temp_dir.path().join(binary_name))?;
let mut encoder = GzEncoder::new(tar, Compression::default());
{
let mut archive = tar::Builder::new(&mut encoder);
archive.append_file(binary_name, &mut file)?;
}
let mut contents = vec![];
encoder.finish()?;
File::open(temp_dir.path().join(&full_path))?.read_to_end(&mut contents)?;
Ok(contents)
}

@ -0,0 +1 @@
too-many-arguments-threshold = 8

@ -0,0 +1,44 @@
<style>
header.warning {
background-color: rgb(242, 222, 222);
border-bottom-color: rgb(238, 211, 215);
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-style: solid;
border-bottom-width: 0.666667px;
border-image-outset: 0 0 0 0;
border-image-repeat: stretch stretch;
border-image-slice: 100% 100% 100% 100%;
border-image-source: none;
border-image-width: 1 1 1 1;
border-left-color: rgb(238, 211, 215);
border-left-style: solid;
border-left-width: 0.666667px;
border-right-color: rgb(238, 211, 215);
border-right-style: solid;
border-right-width: 0.666667px;
border-top-color: rgb(238, 211, 215);
border-top-left-radius: 4px;
border-top-right-radius: 4px;
border-top-style: solid;
border-top-width: 0.666667px;
color: rgb(185, 74, 72);
margin-bottom: 0px;
margin-left: 0px;
margin-right: 0px;
margin-top: 30px;
padding-bottom: 8px;
padding-left: 14px;
padding-right: 35px;
padding-top: 8px;
text-align: center;
}
</style>
<header class='warning'>
This is the <strong>unpublished</strong> documentation of
<code>wasm-pack</code>, the published documentation is available
<a href="https://rustwasm.github.io/docs/wasm-pack/">
on the main Rust and WebAssembly documentation site
</a>. Features documented here may not be available in released versions of
<code>wasm-pack</code>.
</header>

@ -42,9 +42,9 @@
<h2>📦✨ your favorite rust -> wasm workflow tool!</h2>
</div>
<div class="five columns" id="installer">
<a class="button button-primary" href="/wasm-pack/installer">✨ Install wasm-pack 0.5.1</a>
<p>9 Oct 2018 |
<a href="https://github.com/rustwasm/wasm-pack/releases/tag/v0.5.1">
<a class="button button-primary" href="/wasm-pack/installer">✨ Install wasm-pack 0.7.0</a>
<p>16 Mar 2019 |
<a href="https://github.com/rustwasm/wasm-pack/releases/tag/v0.7.0">
Release Notes
</a>
</p>

@ -24,6 +24,7 @@
position: relative;
float: left;
margin-bottom: 0;
bottom: -5px;
}
.navbar-logo a img {

@ -1,22 +1,28 @@
# Summary
- [Prerequisites](./prerequisites/index.md)
- [Rust](./prerequisites/rust.md)
- [npm](./prerequisites/npm.md)
- [Project Setup](./project-setup/index.md)
- [Using a Template](./project-setup/using-a-template.md)
- [Manual Setup](./project-setup/manual-setup.md)
- [npm (optional)](./prerequisites/npm.md)
- [Commands](./commands/index.md)
- [`init` (DEPRECATED)](./commands/init.md)
- [`build`](./commands/build.md)
- [`test`](./commands/test.md)
- [`pack` and `publish`](./commands/pack-and-publish.md)
- [Tutorial](./tutorial/index.md)
- [Getting Started](./tutorial/getting-started.md)
- [Template Deep Dive](./tutorial/template-deep-dive/index.md)
- [`Cargo.toml`](./tutorial/template-deep-dive/cargo-toml.md)
- [`src/lib.rs`](./tutorial/template-deep-dive/src-lib-rs.md)
- [`src/utils.rs`](./tutorial/template-deep-dive/src-utils-rs.md)
- [Packaging and Publishing](./tutorial/packaging-and-publishing.md)
- [Using your Library](./tutorial/using-your-library.md)
- [Tutorials](./tutorials/index.md)
- [Hybrid applications with Webpack](./tutorials/hybrid-applications-with-webpack/index.md)
- [Getting started](./tutorials/hybrid-applications-with-webpack/getting-started.md)
- [Using your library](./tutorials/hybrid-applications-with-webpack/using-your-library.md)
- [npm browser packages](./tutorials/npm-browser-packages/index.md)
- [Getting started](./tutorials/npm-browser-packages/getting-started.md)
- [Project setup](./tutorials/npm-browser-packages/project-setup/index.md)
- [Using a Template](./tutorials/npm-browser-packages/project-setup/using-a-template.md)
- [Manual Setup](./tutorials/npm-browser-packages/project-setup/manual-setup.md)
- [Template deep dive](./tutorials/npm-browser-packages/template-deep-dive/index.md)
- [`Cargo.toml`](./tutorials/npm-browser-packages/template-deep-dive/cargo-toml.md)
- [`src/lib.rs`](./tutorials/npm-browser-packages/template-deep-dive/src-lib-rs.md)
- [`src/utils.rs`](./tutorials/npm-browser-packages/template-deep-dive/src-utils-rs.md)
- [`wee_alloc`](./tutorials/npm-browser-packages/template-deep-dive/wee_alloc.md)
- [Building your project](./tutorials/npm-browser-packages/template-deep-dive/building-your-project.md)
- [Packaging and publishing](./tutorials/npm-browser-packages/packaging-and-publishing.md)
- [Using your library](./tutorials/npm-browser-packages/using-your-library.md)
- [`Cargo.toml` Configuration](./cargo-toml-configuration.md)
- [Contributing](./contributing.md)

@ -5,6 +5,10 @@ interoperability and for publishing a package to npm. This involves compiling
your code to wasm and generating a pkg folder. This pkg folder will contain the
wasm binary, a JS wrapper file, your `README`, and a `package.json` file.
The `pkg` directory is automatically `.gitignore`d by default, since it contains
build artifacts which are not intended to be checked into version
control.<sup>[0](#footnote-0)</sup>
## Path
The `wasm-pack build` command can be given an optional path argument, e.g.:
@ -43,20 +47,26 @@ The exact meaning of the profile flags may evolve as the platform matures.
## Target
The `build` command accepts a `--target` argument. This will customize the output files
to align with a particular type of JS module. This allows wasm-pack to generate either
ES6 modules or CommonJS modules for use in browser and in NodeJS. Defaults to `browser`.
The options are:
The `build` command accepts a `--target` argument. This will customize the JS
that is emitted and how the WebAssembly files are instantiated and loaded. For
more documentation on the various strategies here, see the [documentation on
using the compiled output][deploy].
```
wasm-pack build --target nodejs
```
| Option | Description |
|-----------|-----------------------------------------------------------------------------------------------------------------|
| `nodejs` | Outputs JS that uses CommonJS modules, for use with a `require` statement. `main` key in `package.json`. |
| `nomodules` | Outputs JS that use no modules. `browser` key in `package.json`. |
| `browser` | Outputs JS that uses ES6 modules, primarily for use with `import` statements and/or bundlers such as `webpack`. `module` key in `package.json`. `sideEffects: false` by default. |
| 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
[deploy-nodejs]: https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#nodejs
[deploy-web]: https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#without-a-bundler
## Scope
@ -85,3 +95,20 @@ wasm-pack build examples/js-hello-world --mode no-install
|---------------|------------------------------------------------------------------------------------------|
| `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`. |
## Extra options
The `build` command can pass extra options straight to `cargo build` even if they are not
supported in wasm-pack. To use them you should add standalone `--` argument at the very
end of your command, and all the arguments you want to pass to cargo should go after.
For example to build previous example using unstable cargo offline feature:
```
wasm-pack build examples/js-hello-world --mode no-install -- -Z offline
```
<hr style="font-size: 1.5em; margin-top: 2.5em"/>
<sup id="footnote-0">0</sup> If you need to include additional assets in the pkg
directory and your NPM package, we intend to have a solution for your use case
soon. [](#wasm-pack-build)

@ -1,9 +1,9 @@
# pack and publish
The `publish` and `pack` commands interact with the pkg directory that's
created when you run `wasm-pack init`. The `pack` command creates a tarball
from the pkg directory and the `publish` command creates a tarball from the
pkg directory __and__ publishes it to the NPM registry.
The `publish` and `pack` commands interact with the pkg directory that's
created when you run `wasm-pack build`. The `pack` command creates a tarball
from the pkg directory and the `publish` command creates a tarball from the
pkg directory __and__ publishes it to the NPM registry.
Underneath, these commands use `npm pack` and `npm publish`. You can read
more about these in the NPM documentation:
@ -11,8 +11,8 @@ more about these in the NPM documentation:
- [`npm pack`](https://docs.npmjs.com/cli/pack)
- [`npm publish`](https://docs.npmjs.com/cli/publish)
Both these commands take the path to the pkg directory as the first argument.
You can either set the argument directly to the pkg directory or to the parent
Both these commands take the path to the pkg directory as the first argument.
You can either set the argument directly to the pkg directory or to the parent
of the pkg directory:
```

@ -0,0 +1,84 @@
# wasm-pack test
The `wasm-pack test` command wraps the [wasm-bindgen-test-runner](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html)
CLI allowing you to run wasm tests in different browsers without needing to install the different
webdrivers yourself.
```
wasm-pack test --help
```
## Path
The `wasm-pack test` command can be given an optional path argument.
This path should point to a directory that contains a `Cargo.toml` file. If no
path is given, the `test` command will run in the current directory.
```
# Run tests for the current directory's crate
wasm-pack test
# Run tests for a specified crate
wasm-pack test crates/crate-in-my-workspace
```
## Profile
The `test` command accepts an optional profile argument: `--release`.
If none is supplied, then a debug test build will be used.
## Test environment
Choose where to run your tests by passing in any combination of testing environment flags.
`--headless` is useful for running browser tests in a headless browser as part of a CI process.
```
wasm-pack test --node --firefox --chrome --safari --headless
```
## Extra options
The `test` command can pass extra options straight to `cargo test` even if they are not
supported in wasm-pack.
To use them you should add standalone `--` argument at the very
end of your command, and all the arguments you want to pass to cargo should go after.
`cargo test -h` for a list of all options that you can pass through.
## Running only some tests
When debugging a specific issue, you may find yourself wanting to run a subset of tests, instead of your entire suite of tests.
Here are a few examples of how to run a subset of your tests:
```
# Example directory structure
$ tree crates/foo
├── Cargo.toml
├── README.md
├── src
   ├── diff
     ├── diff_test_case.rs
     └── mod.rs
   ├── lib.rs
└── tests
├── diff_patch.rs
```
```
# Run all tests in tests/diff_patch.rs
wasm-pack test crates/foo --firefox --headless -- --tests diff_patch
# Run all tests in tests/diff_patch.rs that contain the word "replace"
wasm-pack test crates/foo --firefox --headless -- --tests diff_patch replace
# Run all tests inside of a `tests` module inside of src/lib/diff.rs
wasm-pack test crates/foo --firefox --headless -- --lib diff::tests
# Same as the above, but only if they contain the word replace
wasm-pack test crates/foo --firefox --headless -- --lib diff::tests::replace
```

@ -1,10 +1,19 @@
# Prerequisites
To run `wasm-pack` you'll need to have both Rust and npm installed and configured.
First you'll want to [install the `wasm-pack` CLI][wasm-pack], and `wasm-pack
-V` should print the version that you just installed.
- [Rust](./rust.html)
- [npm](./npm.html)
[wasm-pack]: https://rustwasm.github.io/wasm-pack/installer/
In the future, we intend to rewrite the npm registry client bits so that the need
for a Node runtime is eliminated. If you're excited about that work- you should
reach out to the maintainers and get involved!
Next, since `wasm-pack` is a build tool, you'll want to make sure you have
[Rust][rust] installed. Make sure `rustc -V` prints out at least 1.30.0.
[rust]: https://www.rust-lang.org/tools/install
Finally, if you're using `wasm-pack` to install to publish to NPM, you'll want
to [install and configure `npm`][npm]. In the future, we intend to rewrite the
npm registry client bits so that the need for a Node runtime is eliminated. If
you're excited about that work- you should reach out to the maintainers and get
involved!
[npm]: npm.html

@ -1,35 +0,0 @@
# Rust
`wasm-pack` is a Command Line Interface tool written in Rust, and distributed with `cargo`.
As a result, you'll need Rust and `cargo` to use `wasm-pack`.
### Installing Rust and Cargo
To install Rust, visit this [page](https://www.rust-lang.org/en-US/install.html), which will
walk you through installing Rust and `cargo` on your machine using a tool called `rustup`.
To confirm you have Rust and `cargo` installed, run:
```
rustc --version
cargo --version
```
### Rust Versions
`wasm-pack` depends on a library called `wasm-bindgen`. `wasm-bindgen` requires that you use
Rust 1.30.0 or higher. This version is currently only available on the `nightly` or `beta`
channels.
To get the correct version of Rust, you'll use `rustup` a Rust version manager that comes
bundled with Rust. Run this command to install the latest Rust on the `beta` channel:
```
rustup install beta
```
You can set your project directory to always use this version of Rust by running:
```
rustup override set beta
```

@ -1,3 +0,0 @@
# src/lib.rs
🚧 COMING SOON 🚧

@ -1,3 +0,0 @@
# src/utils.rs
🚧 COMING SOON 🚧

@ -0,0 +1,15 @@
# Getting Started
You can create a new Rust-WebAssembly webpack project by using the [rustwasm webpack-template].
Run:
```
npm init rust-webpack my-app
```
The last argument will be your project name. After you run the command, you will have a
directory with a new project, ready to go. We'll talk about what's been included in this
template further in this guide.
[rustwasm webpack-template]: https://github.com/rustwasm/rust-webpack-template

@ -0,0 +1,14 @@
# Hybrid Applications with Webpack
The goal of this tutorial is to introduce you to the [`rust-webpack-template`]
and the `wasm-pack` workflow by building the example app in the template.
This tutorial is aimed at folks who are both beginners to WebAssembly and Rust- you don't need
much Rust knowledge to complete this tutorial.
Be sure to have read and followed the [Prerequisites](../prerequisites/index.html).
[Rust]: https://www.rust-lang.org
[Node.js]: https://nodejs.org
[npm]: https://npmjs.com
[`rust-webpack-template`]: https://github.com/rustwasm/rust-webpack-template

@ -0,0 +1,85 @@
# Run The Code
The Rust Webpack template is designed for creating monorepo-style Web applications with
Rust-generated WebAssembly and Webpack without publishing your wasm to NPM.
This portion of the tutorial will explain how to build a [Webpack] JavaScript project
that will run your WebAssembly code in the browser.
[Webpack]: https://webpack.js.org/
## Scaffold a JavaScript Project
To generate a new Rust Webpack project, we've used the [`rust-webpack`] npm template.
[`rust-webpack`]: https://github.com/rustwasm/rust-webpack-template
```
npm init rust-webpack your-package-name
```
A new project folder will be created with the name you supply.
If we look in the project, we'll see the following:
- `.gitignore`: ignores `node_modules`
- `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you
- `README.md`: the file you are reading now!
- `index.html`: a bare bones html document that includes the webpack bundle
- `js/index.js`: example JS file with a comment showing how to import and use a wasm pkg
- `package.json` and `package-lock.json`:
- pulls in devDependencies for using webpack:
- [`webpack`](https://www.npmjs.com/package/webpack)
- [`webpack-cli`](https://www.npmjs.com/package/webpack-cli)
- [`webpack-dev-server`](https://www.npmjs.com/package/webpack-dev-server)
- defines a `start` script to run `webpack-dev-server`
- `webpack.config.js`: configuration file for bundling your JS with webpack
- `crate/src/lib.rs`: your Rust crate code!
## Your Rust Crate
The scaffolded project includes an example Rust WebAssembly webpack crate.
Inside the `crate/src/lib.rs` file we see a `run` function that's callable from our JS file:
```rust
// Called by our JS entry point to run the example.
#[wasm_bindgen]
pub fn run() -> Result<(), JsValue> {
set_panic_hook();
// ...
let p: web_sys::Node = document.create_element("p")?.into();
p.set_text_content(Some("Hello from Rust, WebAssembly, and Webpack!"));
// ...
Ok(())
}
```
Now, open up the `js/index.js` file. We see our Rust-generated wasm `run` function being
called inside our JS file.
```js
import("../crate/pkg").then(module => {
module.run();
});
```
## Run The Project
To generate our Rust-compiled to wasm code, in the root directory we run:
```bash
npm run build
```
This will create our bundled JavaScript module in a new directory `dist`.
We should be ready to run our project now!
In the root directory, we'll run:
```bash
npm start
```
Then in a web browser navigate to `http://localhost:8080` and you should be greeted
with text in the body of the page that says "Hello from Rust, WebAssembly, and Webpack!"
If you did congrats! You've successfully used the rust-webpack template!

@ -0,0 +1,9 @@
# Tutorials
We have two tutorials that help you get started with `wasm-pack`:
- If you want to create and publish a package: [npm browser packages]
- If you'd like to develop a Wasm library alongside a JavaScript application using Webpack: [Hybrid applications with Webpack]
[npm browser packages]: npm-browser-packages/index.html
[Hybrid applications with Webpack]: hybrid-applications-with-webpack/index.html

@ -0,0 +1,24 @@
# Building your package
We've written our code so now we need to package it all up.
We are writing a package that should be used in the browser, so we run this in our terminal:
```bash
$ wasm-pack build --scope MYSCOPE
```
If you were writing a package that should be used in Node.js (with CommonJS modules, e.g. `require`),
you would run this in your terminal:
```bash
$ wasm-pack build --scope MYSCOPE --target nodejs
```
where `MYSCOPE` is your npm username. Normally you could just type `wasm-pack init` but since
other people are doing this tutorial as well we don't want conflicts with the `wasm-add` package
name! This command when run does a few things:
1. It'll compile your code to wasm if you haven't already
2. It'll generate a pkg folder with the wasm file, a JS wrapper file around the wasm, your README,
and a `package.json` file.

@ -25,4 +25,4 @@ further in this guide.
If you'd rather not use a template, or are having trouble with the template, you can
do a manual setup by following [these instructions].
[these instructions]: ../project-setup/manual-setup/index.html
[these instructions]: ../project-setup/manual-setup.html

@ -1,6 +1,7 @@
# Tutorial
# npm Browser Package Tutorial
The goal of this tutorial is to introduce you to the `wasm-pack` workflow.
The goal of this tutorial is to introduce you to the `wasm-pack` workflow by building a small npm
package designed to be used in a browser application.
This tutorial is aimed at folks who are both beginners to WebAssembly and Rust- you don't need
much Rust knowledge to complete this tutorial.
@ -10,8 +11,8 @@ Be sure to have done the following before starting:
1. [Install `wasm-pack`](../../installer)
1. Read and install the [Prerequisites](../prerequisites/index.html).
- You'll need [Rust], version 1.30 or higher. (Currently either `beta` or `nightly` channels). [Learn more](../project-setup/rust.html).
- You'll need [Node.js] and [npm] installed. You'll also need an npm Account. [Learn more](../project-setup/npm.html).
- You'll need [Rust], version 1.30 or higher. (Currently either `beta` or `nightly` channels). [Learn more](../prerequisites/rust.html).
- You'll need [Node.js] and [npm] installed. You'll also need an npm Account. [Learn more](../prerequisites/npm.html).
We strongly recommend that you install [Node.js] using a version manager. You can learn more [here](https://npmjs.com/get-npm).

@ -1,13 +1,15 @@
# Cargo.toml
`Cargo.toml` is the manifest file for Rust's package manager, `cargo`. This file contains
`Cargo.toml` is the manifest file for Rust's package manager, [`cargo`]. This file contains
metadata such as name, version, and dependencies for packages, which are call "crates" in Rust.
There's a bunch of metadata that the template gives us, but there are 3 key parts to discuss:
[`cargo`]: https://doc.rust-lang.org/cargo/
- [`crate-type`](#a1-crate-type)
- [`wasm-bindgen` dependency](#a2-wasm-bindgen-dependency)
- [`[features]` and `wee-alloc`, `console-error-panic-hook` dependencies](#a3-features-and-wee-alloc-console-error-panic-hook-dependencies)
There's a bunch of metadata that the template gives us, but there are three key parts to discuss:
1. [`crate-type`](#a1-crate-type)
2. [`wasm-bindgen` dependency](#a2-wasm-bindgen-dependency)
3. [`[features]` and `wee_alloc`, `console_error_panic_hook` dependencies](#a3-features-and-wee_alloc-console_error_panic_hook-dependencies)
<hr/>
@ -15,7 +17,7 @@ There's a bunch of metadata that the template gives us, but there are 3 key part
```toml
[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]
```
A Rust-`wasm` crate is a bit different from a normal crate, and as a result, we need to note
@ -33,15 +35,20 @@ as a dynamic library to be loaded from another language. In our case, we'll be c
`.wasm` file, but this output type will create `*.so` files on Linux, `*.dylib` files on
macOS, and `*.dll` files on Windows in non-`wasm` circumstances.
`#[crate_type = "rlib"]` signifies that an intermediate "Rust library" file will be produced.
This allows tests to use the main crate.
You can read more about linking and crate types, [here](https://doc.rust-lang.org/reference/linkage.html).
## 2. `wasm-bindgen` dependency
`wasm-bindgen` is our most important dependency. This package allows us to use the
[`wasm-bindgen`] is our most important dependency. This package allows us to use the
`#[wasm-bindgen]` attribute to tag code that represents the interface we want between
our JavaScript and Rust-generated `wasm`. We can import JS and export Rust by using this
attribute.
[`wasm-bindgen`]: https://rustwasm.github.io/docs/wasm-bindgen/
```toml
wasm-bindgen = "0.2"
```
@ -50,25 +57,32 @@ We'll see more about how to use this library when we discuss what has been gener
If you are coming from JavaScript, you might note that when we add the dependency
there is no `^` or `~` symbol- it looks like we're locking to the `0.2` version.
However, that's not the case! In Rust, the `^` is implied.
However, that's not the case! In Rust, the `^` is implied. You can read more about this in the
[cargo documentation on specifying dependencies].
[cargo documentation on specifying dependencies]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
## 3. `[features]` and [`wee_alloc`], [`console_error_panic_hook`] dependencies
## 3. `[features]` and `wee-alloc`, `console-error-panic-hook` dependencies
[`wee_alloc`]: https://github.com/rustwasm/wee_alloc
[`console_error_panic_hook`]: https://github.com/rustwasm/console_error_panic_hook
As part of our effort to design a template that helps people discover useful crates
for their particular use case, this template includes 2 dependencies that can be
for their particular use case, this template includes two dependencies that can be
very useful for folks developing Rust-`wasm` crates: `console-error-panic-hook` and
`wee-alloc`.
Because these dependencies are useful primarily in a specifc portion of the Rust-`wasm`
Because these dependencies are useful primarily in a specific portion of the Rust-`wasm`
crate development workflow, we've also set up a bit of glue code that allows us to include
them both as dependences, but allowing for them to be optionally included.
them both as dependencies, but also allows them to be optionally included.
```toml
[features]
default-features = ["console_error_panic_hook", "wee_alloc"]
default = ["console_error_panic_hook"]
[dependencies]
cfg-if = "0.1.2"
wasm-bindgen = "0.2"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
@ -79,12 +93,25 @@ console_error_panic_hook = { version = "0.1.1", optional = true }
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
wee_alloc = { version = "0.4.1", optional = true }
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }
```
[`cfg-if`] allows us to check if certain features are enabled on a rust crate. We'll
use this crate in `utils.rs` to optionally enable `console_error_panic_hook` or
`wee_alloc`. By default, we have them enabled. To disable them, we can remove their
entry from the `default-features` vector.
[`cfg-if`] allows us to check if certain features are enabled on a Rust crate. We'll
use this crate later to optionally enable `console_error_panic_hook` or
`wee_alloc`.
By default, only `console_error_panic_hook` is enabled. To disable either
feature, we can remove its name from the `default` vector.
To learn more about these features, we discuss them in-depth in the [`src/lib.rs`] and
[`src/utils.rs`] sections.
[`src/lib.rs`]: src-lib-rs.html
[`src/utils.rs`]: src-utils-rs.html
Briefly, they include:
To learn more about these features, we discuss them in depth in the `utils.rs` section.
+ **console_error_panic_hook** for logging panic messages to the developer console.
+ **wee_alloc**, an allocator optimized for small code size.

@ -0,0 +1,99 @@
# src/lib.rs
`lib.rs` is the template's main source file. The name `lib.rs` commonly implies that this Rust project will be compiled as a library.
It contains three key parts:
1. [`#[wasm_bindgen] functions`](#a1-wasm_bindgen-functions)
2. [Crate imports](#a2-crate-imports)
3. [`wee_alloc` optional dependecy](#a3-wee_alloc-optional-dependecy)
- [What is `wee_alloc`?](#what-is-wee_alloc)
---
We'll start with the most important part of `lib.rs` -- the two `#[wasm_bindgen]` functions (which you can find at the bottom of the file). In many cases, this is the only part of `lib.rs` you will need to modify.
## 1. `#[wasm_bindgen]` functions
The `#[wasm_bindgen]` attribute indicates that the function below it will be accessible both in JavaScript and Rust.
```rust
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
```
The `extern` block imports the external JavaScript function `alert` into Rust. This declaration is required to call `alert` from Rust. By declaring it in this way, `wasm-bindgen` will create JavaScript stubs for `alert` which allow us to pass strings back and forth between Rust and JavaScript.
We can see that the `alert` function requires a single parameter `s` of type `&str`, a string. In Rust, any string literal such as `"Hello, test-wasm!"` is of type `&str`. So, `alert` could be called by writing `alert("Hello, test-wasm!");`.
We knew to declare `alert` in this way because it is how we would call `alert` in JavaScript -- by passing it a string argument.
```rust
#[wasm_bindgen]
pub fn greet() {
alert("Hello, test-wasm!");
}
```
If we were to write the `greet` function without the `#[wasm_bindgen]` attribute, then `greet` would not be easily accessible within JavaScript. Furthermore, we wouldn't be able to natively convert certain types such as `&str` between JavaScript and Rust. So, both the `#[wasm_bindgen]` attribute and the prior import of `alert` allow `greet` to be called from JavaScript.
This is all you need to know to interface with JavaScript, at least to start! You can learn a bunch more by reading the
[`wasm-bindgen` documentation]!
[`wasm-bindgen` documentation]: https://rustwasm.github.io/docs/wasm-bindgen/
If you are curious about the rest, read on.
## 2. Crate imports
```rust
mod utils;
```
This statement declares a new module named `utils` that is defined by the contents of `utils.rs`. Equivalently, we could place the contents of `utils.rs` inside the `utils` declaration, replacing the line with:
```rust
mod utils {
// contents of utils.rs
}
```
Either way, the contents of `utils.rs` define a single public function `set_panic_hook`. Because we are placing it inside the `utils` module, we will be able to call the function directly by writing `utils::set_panic_hook()`. We will discuss how and why to use this function in `src/utils.rs`.
```rust
use cfg_if::cfg_if;
```
`use` allows us to conveniently refer to parts of a crate or module. For example, suppose the crate `cfg_if` contains a function `func`. It is always possible to call this function directly by writing `cfg_if::func()`. However, this is often tedious to write. If we first specify `use cfg_if::func;`, then `func` can be called by just writing `func()` instead. You can learn more about how Rust let's you
write modular code in [this chapter of the book](https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html).
With this in mind, this `use` allows us to call the macro `cfg_if!` inside the crate `cfg_if` without writing `cfg_if::cfg_if!`. We use `cfg_if!` to configure `wee_alloc`, which we will talk more about in a [separate section](./wee_alloc.md):
```rust
cfg_if! {
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}
```
We immediately notice that `cfg_if!` is a macro because it ends in `!`, similarly to other Rust macros such as `println!` and `vec!`. A macro is directly replaced by other code during compile time.
During compile time, `cfg_if!` evaluates the `if` statement. This tests whether the feature `wee_alloc` is present in the `[features]` section of `Cargo.toml` (among other possible ways to set it).
As we saw earlier, the `default` vector in `[features]` only contains `"console_error_panic_hook"` and not `"wee_alloc"`. So, in this case, the `cfg_if!` block will be replaced by no code at all, and hence the default memory allocator will be used instead of `wee_alloc`.
```rust
use wasm_bindgen::prelude::*;
```
Many modules contain a prelude, a list of things that should be automatically imported. This allows common features of the module to be conveniently accessed without a lengthy prefix. For example, in this file we can use `#[wasm_bindgen]` only because it is brought into scope by the prelude.
The asterisk at the end of this `use` indicates that everything inside the module `wasm_bindgen::prelude` (i.e. the module `prelude` inside the crate `wasm_bindgen`) can be referred to without prefixing it with `wasm_bindgen::prelude`.
For example, `#[wasm_bindgen]` could also be written as `#[wasm_bindgen::prelude::wasm_bindgen]`, although this is not recommended.

@ -0,0 +1,66 @@
# src/utils.rs
The purpose of `utils.rs` is to define the `utils` module, which contains a single function `set_panic_hook`. This function becomes part of the `utils` module in `lib.rs`, as described in the preceding section.
If the `console_error_panic_hook` feature is not enabled, then `set_panic_hook` is defined to be an inlined empty function. So, there is no run-time performance or code-size penalty incurred by its use.
We will discuss:
1. [Defining `set_panic_hook`](#a1-defining-set_panic_hook)
2. [What is `console_error_panic_hook`?](#a2-what-is-console_error_panic_hook)
---
## 1. Defining `set_panic_hook`
```rust
use cfg_if::cfg_if;
```
This allows us to write `cfg_if!` instead of `cfg_if::cfg_if!`, identically to the line in `src/lib.rs`.
```rust
cfg_if! {
if #[cfg(feature = "console_error_panic_hook")] {
extern crate console_error_panic_hook;
pub use self::console_error_panic_hook::set_once as set_panic_hook;
} else {
#[inline]
pub fn set_panic_hook() {}
}
}
```
As described in the preceding section, the macro `cfg_if!` evaluates the `if` statement during compile time. This is possible because it is essentially testing whether `"console_error_panic_hook"` is defined in the `[features]` section of `Cargo.toml`, which is available during compile time.
The entire macro block will either be replaced with the statements in the `if` block or with those in the `else` block. These two cases are now described in turn:
```rust
extern crate console_error_panic_hook;
pub use self::console_error_panic_hook::set_once as set_panic_hook;
```
Due to the `use` statement, the function `self::console_error_panic_hook::set_once` can now be accessed more conveniently as `set_panic_hook`. Due to `pub`, this function will be publicly accessible outside of the `utils` module as `utils::set_panic_hook`.
```rust
#[inline]
pub fn set_panic_hook() {}
```
An inline function replaces the function call with the contents of the function during compile time. Here, `set_panic_hook` is defined to be an empty inline function. This allows the use of `set_panic_hook` without any run-time or code-size performance penalty if the feature is not enabled.
## 2. What is `console_error_panic_hook`?
The crate `console_error_panic_hook` enhances error messages in the web browser. This allows you to easily debug WebAssembly code.
Let's compare error messages before and after enabling the feature:
**Before:** `"RuntimeError: Unreachable executed"`
**After:** `"panicked at 'index out of bounds: the len is 3 but the index is 4', libcore/slice/mod.rs:2046:10"`
To do this, a panic hook for WebAssembly is provided that logs panics to the developer console via the JavaScript `console.error` function.
Note that although the template sets up the function, your error messages will not automatically be enhanced. To enable the enhanced errors, call the function `utils::set_panic_hook()` in `lib.rs` when your code first runs. The function may be called multiple times if needed.
For more details, see the [`console_error_panic_hook` repository](https://github.com/rustwasm/console_error_panic_hook).

@ -0,0 +1,56 @@
# wee_alloc
1. [What is `wee_alloc`?](#what-is-wee_alloc)
2. [Enabling `wee_alloc`](#enabling-wee_alloc)
3. [Rust nightly](#rust-nightly)
## What is `wee_alloc`?
Reducing the size of compiled WebAssembly code is important, since it is often transmitted over the Internet or placed on embedded devices.
*Want to learn more about code sizein the rustwasm toolchain? Check out this [documentation](https://rustwasm.github.io/docs/book/reference/code-size.html).
> `wee_alloc` is a tiny allocator designed for WebAssembly that has a (pre-compression) code-size footprint of only a single kilobyte.
[An analysis](http://fitzgeraldnick.com/2018/02/09/wee-alloc.html) suggests that over half of the bare minimum WebAssembly memory footprint is required by Rust's default memory allocator. Yet, WebAssembly code often does not require a sophisticated allocator, since it often just requests a couple of large initial allocations.
`wee_alloc` trades off size for speed. Although it has a tiny code-size footprint, it is relatively slow if additional allocations are needed.
For even more details, see the [`wee_alloc` repository](https://github.com/rustwasm/wee_alloc).
## Enabling `wee_alloc`
In `lib.rs`, we have the configuration for `wee_alloc` inside a `cfg_if!` macro:
```rust
cfg_if! {
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}
```
This code block is intended to initialize `wee_alloc` as the global memory allocator, but only if the `wee_alloc` feature is enabled in `Cargo.toml`.
To do so we need to append `"wee_alloc"` to the `default` vector in `Cargo.toml`. Then, the `cfg_if!` block is replaced with the contents of the `if` block, shown above.
```toml
[features]
default = ["console_error_panic_hook", "wee_alloc"]
```
## Rust nightly
`wee_alloc` currently relies on features only available in Rust nightly. As such it requires you to use the nightly toolchain for compilation. If you have [Rustup](https://rustup.rs/) set up, you can install the nightly toolchain as follows:
```
rustup toolchain add nightly
```
To use `wasm-pack` with Rust nightly run:
```
rustup run nightly wasm-pack build
```

@ -1,13 +1,14 @@
//! Functionality related to installing and running `wasm-bindgen`.
use binaries::{Cache, Download};
use binary_install::{Cache, Download};
use child;
use command::build::BuildProfile;
use command::build::{BuildProfile, Target};
use emoji;
use failure::{self, ResultExt};
use log::debug;
use log::{info, warn};
use manifest::CrateData;
use progressbar::Step;
use slog::Logger;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
@ -25,8 +26,6 @@ pub fn install_wasm_bindgen(
cache: &Cache,
version: &str,
install_permitted: bool,
step: &Step,
log: &Logger,
) -> Result<Download, failure::Error> {
// If `wasm-bindgen` is installed globally and it has the right version, use
// that. Assume that other tools are installed next to it.
@ -34,32 +33,27 @@ pub fn install_wasm_bindgen(
// This situation can arise if `wasm-bindgen` is already installed via
// `cargo install`, for example.
if let Ok(path) = which("wasm-bindgen") {
debug!(
log,
"found global wasm-bindgen binary at: {}",
path.display()
);
if wasm_bindgen_version_check(&path, version, log) {
debug!("found global wasm-bindgen binary at: {}", path.display());
if wasm_bindgen_version_check(&path, version) {
return Ok(Download::at(path.parent().unwrap()));
}
}
let msg = format!("{}Installing wasm-bindgen...", emoji::DOWN_ARROW);
PBAR.step(step, &msg);
PBAR.info(&msg);
let dl = download_prebuilt_wasm_bindgen(&cache, version, install_permitted);
match dl {
Ok(dl) => return Ok(dl),
Err(e) => {
warn!(
log,
"could not download pre-built `wasm-bindgen`: {}. Falling back to `cargo install`.",
e
);
}
}
cargo_install_wasm_bindgen(log, &cache, version, install_permitted)
cargo_install_wasm_bindgen(&cache, version, install_permitted)
}
/// Downloads a precompiled copy of wasm-bindgen, if available.
@ -102,14 +96,23 @@ fn prebuilt_url(version: &str) -> Option<String> {
/// Use `cargo install` to install the `wasm-bindgen` CLI locally into the given
/// crate.
pub fn cargo_install_wasm_bindgen(
logger: &Logger,
cache: &Cache,
version: &str,
install_permitted: bool,
) -> Result<Download, failure::Error> {
debug!(
"Attempting to use a `cargo install`ed version of `wasm-bindgen={}`",
version
);
let dirname = format!("wasm-bindgen-cargo-install-{}", version);
let destination = cache.join(dirname.as_ref());
if destination.exists() {
debug!(
"`cargo install`ed `wasm-bindgen={}` already exists at {}",
version,
destination.display()
);
return Ok(Download::at(&destination));
}
@ -121,7 +124,12 @@ pub fn cargo_install_wasm_bindgen(
// and ensure we don't accidentally use stale files in the future
let tmp = cache.join(format!(".{}", dirname).as_ref());
drop(fs::remove_dir_all(&tmp));
fs::create_dir_all(&tmp)?;
debug!(
"cargo installing wasm-bindgen to tempdir: {}",
tmp.display()
);
fs::create_dir_all(&tmp)
.context("failed to create temp dir for `cargo install wasm-bindgen`")?;
let mut cmd = Command::new("cargo");
cmd.arg("install")
@ -132,9 +140,30 @@ pub fn cargo_install_wasm_bindgen(
.arg("--root")
.arg(&tmp);
child::run(logger, cmd, "cargo install").context("Installing wasm-bindgen with cargo")?;
child::run(cmd, "cargo install").context("Installing wasm-bindgen with cargo")?;
// `cargo install` will put the installed binaries in `$root/bin/*`, but we
// just want them in `$root/*` directly (which matches how the tarballs are
// laid out, and where the rest of our code expects them to be). So we do a
// little renaming here.
for f in ["wasm-bindgen", "wasm-bindgen-test-runner"].iter().cloned() {
let from = tmp
.join("bin")
.join(f)
.with_extension(env::consts::EXE_EXTENSION);
let to = tmp.join(from.file_name().unwrap());
fs::rename(&from, &to).with_context(|_| {
format!(
"failed to move {} to {} for `cargo install`ed `wasm-bindgen`",
from.display(),
to.display()
)
})?;
}
// Finally, move the `tmp` directory into our binary cache.
fs::rename(&tmp, &destination)?;
Ok(Download::at(&destination))
}
@ -145,14 +174,9 @@ pub fn wasm_bindgen_build(
bindgen: &Download,
out_dir: &Path,
disable_dts: bool,
target: &str,
target: &Target,
profile: BuildProfile,
step: &Step,
log: &Logger,
) -> Result<(), failure::Error> {
let msg = format!("{}Running WASM-bindgen...", emoji::RUNNER);
PBAR.step(step, &msg);
let release_or_debug = match profile {
BuildProfile::Release | BuildProfile::Profiling => "release",
BuildProfile::Dev => "debug",
@ -173,11 +197,12 @@ pub fn wasm_bindgen_build(
"--typescript"
};
let target_arg = match target {
"nodejs" => "--nodejs",
"no-modules" => "--no-modules",
_ => "--browser",
Target::Nodejs => "--nodejs",
Target::NoModules => "--no-modules",
Target::Web => "--web",
Target::Bundler => "--browser",
};
let bindgen_path = bindgen.binary("wasm-bindgen");
let bindgen_path = bindgen.binary("wasm-bindgen")?;
let mut cmd = Command::new(bindgen_path);
cmd.arg(&wasm_path)
.arg("--out-dir")
@ -196,15 +221,15 @@ pub fn wasm_bindgen_build(
cmd.arg("--keep-debug");
}
child::run(log, cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
child::run(cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
Ok(())
}
/// Check if the `wasm-bindgen` dependency is locally satisfied.
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str, log: &Logger) -> bool {
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str) -> bool {
let mut cmd = Command::new(bindgen_path);
cmd.arg("--version");
child::run(log, cmd, "wasm-bindgen")
child::run_capture_stdout(cmd, "wasm-bindgen")
.map(|stdout| {
stdout
.trim()
@ -212,10 +237,8 @@ fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str, log: &L
.nth(1)
.map(|v| {
info!(
log,
"Checking installed `wasm-bindgen` version == expected version: {} == {}",
v,
dep_version
v, dep_version
);
v == dep_version
})

@ -4,18 +4,14 @@ use child;
use command::build::BuildProfile;
use emoji;
use failure::{Error, ResultExt};
use manifest::Crate;
use progressbar::Step;
use slog::Logger;
use std::path::Path;
use log::info;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
use PBAR;
/// Ensure that `rustc` is present and that it is >= 1.30.0
pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
let msg = format!("{}Checking `rustc` version...", emoji::CRAB);
PBAR.step(step, &msg);
pub fn check_rustc_version() -> Result<String, Error> {
let local_minor_version = rustc_minor_version();
match local_minor_version {
Some(mv) => {
@ -27,24 +23,11 @@ pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
} else {
Ok(mv.to_string())
}
},
}
None => bail!("We can't figure out what your Rust version is- which means you might not have Rust installed. Please install Rust version 1.30.0 or higher."),
}
}
/// Checks if `wasm-pack` is updated to the latest version
pub fn check_wasm_pack_versions() -> Result<(String, String), Error> {
match wasm_pack_local_version() {
Some(local) => {
match Crate::return_wasm_pack_latest_version() {
Some(latest) => Ok((local, latest)),
None => Ok((local, "".to_string()))
}
},
None => bail!("We can't figure out what your wasm-pack version is, make sure the installation path is correct.")
}
}
// from https://github.com/alexcrichton/proc-macro2/blob/79e40a113b51836f33214c6d00228934b41bd4ad/build.rs#L44-L61
fn rustc_minor_version() -> Option<u32> {
macro_rules! otry {
@ -64,50 +47,88 @@ fn rustc_minor_version() -> Option<u32> {
otry!(pieces.next()).parse().ok()
}
fn wasm_pack_local_version() -> Option<String> {
macro_rules! otry {
($e:expr) => {
match $e {
Some(e) => e,
None => return None,
}
};
/// Get rustc's sysroot as a PathBuf
fn get_rustc_sysroot() -> Result<PathBuf, Error> {
let command = Command::new("rustc")
.args(&["--print", "sysroot"])
.output()?;
if command.status.success() {
Ok(String::from_utf8(command.stdout)?.trim().into())
} else {
Err(format_err!(
"Getting rustc's sysroot wasn't successful. Got {}",
command.status
))
}
}
let output = otry!(Command::new("wasm-pack").arg("--version").output().ok());
let version = otry!(str::from_utf8(&output.stdout).ok());
let mut pieces = version.split(' ');
if pieces.next() != Some("wasm-pack") {
return None;
/// Checks if the wasm32-unknown-unknown is present in rustc's sysroot.
fn is_wasm32_target_in_sysroot(sysroot: &PathBuf) -> bool {
let wasm32_target = "wasm32-unknown-unknown";
let rustlib_path = sysroot.join("lib/rustlib");
info!("Looking for {} in {:?}", wasm32_target, rustlib_path);
if rustlib_path.join(wasm32_target).exists() {
info!("Found {} in {:?}", wasm32_target, rustlib_path);
true
} else {
info!("Failed to find {} in {:?}", wasm32_target, rustlib_path);
false
}
otry!(pieces.next())
.to_string()
.trim()
.parse::<String>()
.ok()
}
/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
/// current toolchain
pub fn rustup_add_wasm_target(log: &Logger, step: &Step) -> Result<(), Error> {
let msg = format!("{}Adding WASM target...", emoji::TARGET);
PBAR.step(step, &msg);
fn check_wasm32_target() -> Result<bool, Error> {
let sysroot = get_rustc_sysroot()?;
// If wasm32-unknown-unknown already exists we're ok.
if is_wasm32_target_in_sysroot(&sysroot) {
Ok(true)
// If it doesn't exist, then we need to check if we're using rustup.
} else {
// If sysroot contains .rustup, then we can assume we're using rustup
// and use rustup to add the wasm32-unknown-unknown target.
if sysroot.to_string_lossy().contains(".rustup") {
rustup_add_wasm_target().map(|()| true)
} else {
Ok(false)
}
}
}
/// Add wasm32-unknown-unknown using `rustup`.
fn rustup_add_wasm_target() -> Result<(), Error> {
let mut cmd = Command::new("rustup");
cmd.arg("target").arg("add").arg("wasm32-unknown-unknown");
child::run(log, cmd, "rustup")
.context("Adding the wasm32-unknown-unknown target with rustup")?;
child::run(cmd, "rustup").context("Adding the wasm32-unknown-unknown target with rustup")?;
Ok(())
}
/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
/// current toolchain
pub fn check_for_wasm32_target() -> Result<(), Error> {
let msg = format!("{}Checking for the Wasm target...", emoji::TARGET);
PBAR.info(&msg);
// Check if wasm32 target is present, otherwise bail.
match check_wasm32_target() {
Ok(true) => Ok(()),
Ok(false) => bail!("wasm32-unknown-unknown target not found!"),
Err(err) => Err(err),
}
}
/// Run `cargo build` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm(
log: &Logger,
path: &Path,
profile: BuildProfile,
step: &Step,
extra_options: &Vec<String>,
) -> Result<(), Error> {
let msg = format!("{}Compiling to WASM...", emoji::CYCLONE);
PBAR.step(step, &msg);
let msg = format!("{}Compiling to Wasm...", emoji::CYCLONE);
PBAR.info(&msg);
let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--lib");
match profile {
@ -128,18 +149,22 @@ pub fn cargo_build_wasm(
}
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(log, cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
cmd.args(extra_options);
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
Ok(())
}
/// Run `cargo build --tests` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm_tests(log: &Logger, path: &Path, debug: bool) -> Result<(), Error> {
///
/// This generates the `Cargo.lock` file that we use in order to know which version of
/// wasm-bindgen-cli to use when running tests.
pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> {
let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--tests");
if !debug {
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(log, cmd, "cargo build").context("Compilation of your program failed")?;
child::run(cmd, "cargo build").context("Compilation of your program failed")?;
Ok(())
}

@ -0,0 +1,14 @@
//! Getting and configuring wasm-pack's binary cache.
use binary_install::Cache;
use std::env;
use std::path::Path;
/// Get wasm-pack's binary cache.
pub fn get_wasm_pack_cache() -> Result<Cache, failure::Error> {
if let Ok(path) = env::var("WASM_PACK_CACHE") {
Ok(Cache::at(Path::new(&path)))
} else {
Cache::new("wasm-pack")
}
}

@ -4,173 +4,58 @@
//! properly logged and their output is logged as well.
use failure::Error;
use slog::Logger;
use std::{
io::{self, Read},
mem, process, string,
sync::mpsc,
thread,
};
use PBAR;
#[derive(Debug)]
enum OutputFragment {
Stdout(Vec<u8>),
Stderr(Vec<u8>),
}
/// Read data from the give reader and send it as an `OutputFragment` over the
/// given sender.
fn read_and_send<R, F>(
mut reader: R,
sender: mpsc::Sender<OutputFragment>,
mut map: F,
) -> io::Result<()>
where
R: Read,
F: FnMut(Vec<u8>) -> OutputFragment,
{
let mut buf = vec![0; 1024];
loop {
match reader.read(&mut buf) {
Err(e) => {
if e.kind() == io::ErrorKind::Interrupted {
continue;
} else {
return Err(e);
}
}
Ok(0) => return Ok(()),
Ok(n) => {
buf.truncate(n);
let buf = mem::replace(&mut buf, vec![0; 1024]);
sender.send(map(buf)).unwrap();
}
}
use log::info;
use std::process::{Command, Stdio};
/// Return a new Command object
pub fn new_command(program: &str) -> Command {
// On Windows, initializes launching <program> as `cmd /c <program>`.
// Initializing only with `Command::new("npm")` will launch
// `npm` with quotes, `"npm"`, causing a run-time error on Windows.
// See rustc: #42436, #42791, #44542
if cfg!(windows) {
let mut cmd = Command::new("cmd");
cmd.arg("/c").arg(program);
cmd
} else {
Command::new(program)
}
}
/// Accumulates output from a stream of output fragments and calls a callback on
/// each complete line as it is accumulating.
struct OutputAccumulator<F> {
result: String,
in_progress: Vec<u8>,
on_each_line: F,
}
impl<F> OutputAccumulator<F>
where
F: FnMut(&str),
{
/// Construct a new output accumulator with the given `on_each_line`
/// callback.
fn new(on_each_line: F) -> OutputAccumulator<F> {
OutputAccumulator {
result: String::new(),
in_progress: Vec::new(),
on_each_line,
}
}
/// Add another fragment of output to the accumulation, calling the
/// `on_each_line` callback for any complete lines we accumulate.
fn push(&mut self, fragment: Vec<u8>) -> Result<(), string::FromUtf8Error> {
debug_assert!(!fragment.is_empty());
self.in_progress.extend(fragment);
/// Run the given command and return its stdout.
pub fn run(mut command: Command, command_name: &str) -> Result<(), Error> {
info!("Running {:?}", command);
if let Some((last_newline, _)) = self
.in_progress
.iter()
.cloned()
.enumerate()
.rev()
.find(|(_, ch)| *ch == b'\n')
{
let next_in_progress: Vec<u8> = self.in_progress[last_newline + 1..]
.iter()
.cloned()
.collect();
let mut these_lines = mem::replace(&mut self.in_progress, next_in_progress);
these_lines.truncate(last_newline + 1);
let these_lines = String::from_utf8(these_lines)?;
for line in these_lines.lines() {
(self.on_each_line)(line);
}
self.result.push_str(&these_lines);
}
let status = command.status()?;
if status.success() {
Ok(())
}
/// Finish accumulation, run the `on_each_line` callback on the final line
/// (if any), and return the accumulated output.
fn finish(mut self) -> Result<String, string::FromUtf8Error> {
if !self.in_progress.is_empty() {
let last_line = String::from_utf8(self.in_progress)?;
(self.on_each_line)(&last_line);
self.result.push_str(&last_line);
}
Ok(self.result)
} else {
bail!(
"failed to execute `{}`: exited with {}",
command_name,
status
)
}
}
/// Run the given command and return its stdout.
pub fn run(
logger: &Logger,
mut command: process::Command,
command_name: &str,
) -> Result<String, Error> {
info!(logger, "Running {:?}", command);
let mut child = command
.stdout(process::Stdio::piped())
.stderr(process::Stdio::piped())
.spawn()?;
let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();
let (send, recv) = mpsc::channel();
let stdout_send = send.clone();
let stderr_send = send;
// Because pipes have a fixed-size buffer, we need to keep reading stdout
// and stderr on a separate thread to avoid potential dead locks with
// waiting on the child process.
let stdout_handle =
thread::spawn(move || read_and_send(stdout, stdout_send, OutputFragment::Stdout));
let stderr_handle =
thread::spawn(move || read_and_send(stderr, stderr_send, OutputFragment::Stderr));
let mut stdout = OutputAccumulator::new(|line| {
info!(logger, "{} (stdout): {}", command_name, line);
PBAR.message(line)
});
let mut stderr = OutputAccumulator::new(|line| {
info!(logger, "{} (stderr): {}", command_name, line);
PBAR.message(line)
});
for output in recv {
match output {
OutputFragment::Stdout(line) => stdout.push(line)?,
OutputFragment::Stderr(line) => stderr.push(line)?,
};
}
let stdout = stdout.finish()?;
let stderr = stderr.finish()?;
pub fn run_capture_stdout(mut command: Command, command_name: &str) -> Result<String, Error> {
info!("Running {:?}", command);
// Join the threads reading the child's output to make sure the finish OK.
stdout_handle.join().unwrap()?;
stderr_handle.join().unwrap()?;
let output = command
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.output()?;
let exit = child.wait()?;
if exit.success() {
return Ok(stdout);
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
} else {
drop((stdout, stderr));
bail!("failed to execute `{}`: exited with {}", command_name, exit)
bail!(
"failed to execute `{}`: exited with {}",
command_name,
output.status
)
}
}

@ -1,35 +1,36 @@
//! Implementation of the `wasm-pack build` command.
use binaries::{Cache, Download};
use binary_install::{Cache, Download};
use bindgen;
use build;
use cache;
use command::utils::{create_pkg_dir, set_crate_path};
use emoji;
use failure::Error;
use indicatif::HumanDuration;
use license;
use lockfile::Lockfile;
use log::info;
use manifest;
use progressbar::Step;
use readme;
use slog::Logger;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Instant;
use PBAR;
/// Everything required to configure and run the `wasm-pack init` command.
/// Everything required to configure and run the `wasm-pack build` command.
#[allow(missing_docs)]
pub struct Build {
pub crate_path: PathBuf,
pub crate_data: manifest::CrateData,
pub scope: Option<String>,
pub disable_dts: bool,
pub target: String,
pub target: Target,
pub profile: BuildProfile,
pub mode: BuildMode,
pub out_dir: PathBuf,
pub bindgen: Option<Download>,
pub cache: Cache,
pub extra_options: Vec<String>,
}
/// The `BuildMode` determines which mode of initialization we are running, and
@ -63,6 +64,44 @@ impl FromStr for BuildMode {
}
}
/// What sort of output we're going to be generating and flags we're invoking
/// `wasm-bindgen` with.
#[derive(Clone, Copy, Debug)]
pub enum Target {
/// Default output mode or `--target bundler`, indicates output will be
/// used with a bundle in a later step.
Bundler,
/// Correspond to `--target web` where the output is natively usable as an
/// ES module in a browser and the wasm is manually instantiated.
Web,
/// Correspond to `--target nodejs` where the output is natively usable as
/// a Node.js module loaded with `require`.
Nodejs,
/// Correspond to `--target no-modules` where the output is natively usable
/// in a browser but pollutes the global namespace and must be manually
/// instantiated.
NoModules,
}
impl Default for Target {
fn default() -> Target {
Target::Bundler
}
}
impl FromStr for Target {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"bundler" | "browser" => Ok(Target::Bundler),
"web" => Ok(Target::Web),
"nodejs" => Ok(Target::Nodejs),
"no-modules" => Ok(Target::NoModules),
_ => bail!("Unknown target: {}", s),
}
}
}
/// The build profile controls whether optimizations, debug info, and assertions
/// are enabled or disabled.
#[derive(Clone, Copy, Debug)]
@ -87,7 +126,7 @@ pub struct BuildOptions {
pub scope: Option<String>,
#[structopt(long = "mode", short = "m", default_value = "normal")]
/// Sets steps to be run. [possible values: no-install, normal]
/// Sets steps to be run. [possible values: no-install, normal, force]
pub mode: BuildMode,
#[structopt(long = "no-typescript")]
@ -96,32 +135,54 @@ pub struct BuildOptions {
pub disable_dts: bool,
#[structopt(long = "target", short = "t", default_value = "browser")]
/// Sets the target environment. [possible values: browser, nodejs, no-modules]
pub target: String,
/// Sets the target environment. [possible values: browser, nodejs, web, no-modules]
pub target: Target,
#[structopt(long = "debug")]
/// Deprecated. Renamed to `--dev`.
debug: bool,
pub debug: bool,
#[structopt(long = "dev")]
/// Create a development build. Enable debug info, and disable
/// optimizations.
dev: bool,
pub dev: bool,
#[structopt(long = "release")]
/// Create a release build. Enable optimizations and disable debug info.
release: bool,
pub release: bool,
#[structopt(long = "profiling")]
/// Create a profiling build. Enable optimizations and debug info.
profiling: bool,
pub profiling: bool,
#[structopt(long = "out-dir", short = "d", default_value = "pkg")]
/// Sets the output directory with a relative path.
pub out_dir: String,
#[structopt(last = true)]
/// List of extra options to pass to `cargo build`
pub extra_options: Vec<String>,
}
impl Default for BuildOptions {
fn default() -> Self {
Self {
path: None,
scope: None,
mode: BuildMode::default(),
disable_dts: false,
target: Target::default(),
debug: false,
dev: false,
release: false,
profiling: false,
out_dir: String::new(),
extra_options: Vec::new(),
}
}
}
type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>;
type BuildStep = fn(&mut Build) -> Result<(), Error>;
impl Build {
/// Construct a build command from the given options.
@ -150,7 +211,8 @@ impl Build {
mode: build_opts.mode,
out_dir,
bindgen: None,
cache: Cache::new()?,
cache: cache::get_wasm_pack_cache()?,
extra_options: build_opts.extra_options,
})
}
@ -160,36 +222,33 @@ impl Build {
}
/// Execute this `Build` command.
pub fn run(&mut self, log: &Logger) -> Result<(), Error> {
let process_steps = Build::get_process_steps(&self.mode);
let mut step_counter = Step::new(process_steps.len());
pub fn run(&mut self) -> Result<(), Error> {
let process_steps = Build::get_process_steps(self.mode);
let started = Instant::now();
for (_, process_step) in process_steps {
process_step(self, &step_counter, log)?;
step_counter.inc();
process_step(self)?;
}
let duration = HumanDuration(started.elapsed());
info!(&log, "Done in {}.", &duration);
let duration = crate::command::utils::elapsed(started.elapsed());
info!("Done in {}.", &duration);
info!(
&log,
"Your wasm pkg is ready to publish at {:#?}.", &self.out_dir
"Your wasm pkg is ready to publish at {}.",
self.out_dir.display()
);
PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration));
PBAR.info(&format!("{} Done in {}", emoji::SPARKLE, &duration));
PBAR.message(&format!(
"{} Your wasm pkg is ready to publish at {:#?}.",
PBAR.info(&format!(
"{} Your wasm pkg is ready to publish at {}.",
emoji::PACKAGE,
self.out_dir.canonicalize().unwrap_or(self.out_dir.clone())
self.out_dir.display()
));
Ok(())
}
fn get_process_steps(mode: &BuildMode) -> Vec<(&'static str, BuildStep)> {
fn get_process_steps(mode: BuildMode) -> Vec<(&'static str, BuildStep)> {
macro_rules! steps {
($($name:ident),+) => {
{
@ -204,67 +263,63 @@ impl Build {
BuildMode::Normal => steps![
step_check_rustc_version,
step_check_crate_config,
step_add_wasm_target,
step_check_for_wasm_target,
step_build_wasm,
step_create_dir,
step_create_json,
step_copy_readme,
step_copy_license,
step_install_wasm_bindgen,
step_run_wasm_bindgen,
step_create_json,
],
BuildMode::Noinstall => steps![
step_check_rustc_version,
step_check_crate_config,
step_build_wasm,
step_create_dir,
step_create_json,
step_copy_readme,
step_run_wasm_bindgen
step_copy_license,
step_run_wasm_bindgen,
step_create_json,
],
BuildMode::Force => steps![
step_build_wasm,
step_create_dir,
step_create_json,
step_copy_readme,
step_run_wasm_bindgen
step_copy_license,
step_run_wasm_bindgen,
step_create_json,
],
}
}
/// Returns local and latest versions of wasm-pack
pub fn return_wasm_pack_versions() -> Result<(String, String), Error> {
let (local, latest) = build::check_wasm_pack_versions()?;
Ok((local, latest))
}
fn step_check_rustc_version(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Checking rustc version...");
let version = build::check_rustc_version(step)?;
fn step_check_rustc_version(&mut self) -> Result<(), Error> {
info!("Checking rustc version...");
let version = build::check_rustc_version()?;
let msg = format!("rustc version is {}.", version);
info!(&log, "{}", &msg);
info!("{}", &msg);
Ok(())
}
fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Checking crate configuration...");
self.crate_data.check_crate_config(step)?;
info!(&log, "Crate is correctly configured.");
fn step_check_crate_config(&mut self) -> Result<(), Error> {
info!("Checking crate configuration...");
self.crate_data.check_crate_config()?;
info!("Crate is correctly configured.");
Ok(())
}
fn step_add_wasm_target(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Adding wasm-target...");
build::rustup_add_wasm_target(log, step)?;
info!(&log, "Adding wasm-target was successful.");
fn step_check_for_wasm_target(&mut self) -> Result<(), Error> {
info!("Checking for wasm-target...");
build::check_for_wasm32_target()?;
info!("Checking for wasm-target was successful.");
Ok(())
}
fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Building wasm...");
build::cargo_build_wasm(log, &self.crate_path, self.profile, step)?;
fn step_build_wasm(&mut self) -> Result<(), Error> {
info!("Building wasm...");
build::cargo_build_wasm(&self.crate_path, self.profile, &self.extra_options)?;
info!(
&log,
"wasm built at {:#?}.",
&self
.crate_path
@ -275,61 +330,60 @@ impl Build {
Ok(())
}
fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Creating a pkg directory...");
create_pkg_dir(&self.out_dir, step)?;
info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path);
fn step_create_dir(&mut self) -> Result<(), Error> {
info!("Creating a pkg directory...");
create_pkg_dir(&self.out_dir)?;
info!("Created a pkg directory at {:#?}.", &self.crate_path);
Ok(())
}
fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Writing a package.json...");
fn step_create_json(&mut self) -> Result<(), Error> {
self.crate_data.write_package_json(
&self.out_dir,
&self.scope,
self.disable_dts,
&self.target,
step,
)?;
info!(
&log,
"Wrote a package.json at {:#?}.",
&self.out_dir.join("package.json")
);
Ok(())
}
fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Copying readme from crate...");
readme::copy_from_crate(&self.crate_path, &self.out_dir, step)?;
info!(&log, "Copied readme from crate to {:#?}.", &self.out_dir);
fn step_copy_readme(&mut self) -> Result<(), Error> {
info!("Copying readme from crate...");
readme::copy_from_crate(&self.crate_path, &self.out_dir)?;
info!("Copied readme from crate to {:#?}.", &self.out_dir);
Ok(())
}
fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Identifying wasm-bindgen dependency...");
fn step_copy_license(&mut self) -> Result<(), failure::Error> {
info!("Copying license from crate...");
license::copy_from_crate(&self.crate_data, &self.crate_path, &self.out_dir)?;
info!("Copied license from crate to {:#?}.", &self.out_dir);
Ok(())
}
fn step_install_wasm_bindgen(&mut self) -> Result<(), failure::Error> {
info!("Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
info!(&log, "Installing wasm-bindgen-cli...");
info!("Installing wasm-bindgen-cli...");
let install_permitted = match self.mode {
BuildMode::Normal => true,
BuildMode::Force => true,
BuildMode::Noinstall => false,
};
let bindgen = bindgen::install_wasm_bindgen(
&self.cache,
&bindgen_version,
install_permitted,
step,
log,
)?;
let bindgen =
bindgen::install_wasm_bindgen(&self.cache, &bindgen_version, install_permitted)?;
self.bindgen = Some(bindgen);
info!(&log, "Installing wasm-bindgen-cli was successful.");
info!("Installing wasm-bindgen-cli was successful.");
Ok(())
}
fn step_run_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Building the wasm bindings...");
fn step_run_wasm_bindgen(&mut self) -> Result<(), Error> {
info!("Building the wasm bindings...");
bindgen::wasm_bindgen_build(
&self.crate_data,
self.bindgen.as_ref().unwrap(),
@ -337,10 +391,8 @@ impl Build {
self.disable_dts,
&self.target,
self.profile,
step,
log,
)?;
info!(&log, "wasm bindings were built at {:#?}.", &self.out_dir);
info!("wasm bindings were built at {:#?}.", &self.out_dir);
Ok(())
}
}

@ -1,30 +1,25 @@
use log::info;
use npm;
use slog::Logger;
use std::result;
use PBAR;
pub fn login(
registry: Option<String>,
scope: Option<String>,
scope: &Option<String>,
always_auth: bool,
auth_type: Option<String>,
log: &Logger,
auth_type: &Option<String>,
) -> result::Result<(), failure::Error> {
let registry = registry.unwrap_or(npm::DEFAULT_NPM_REGISTRY.to_string());
let registry = registry.unwrap_or_else(|| npm::DEFAULT_NPM_REGISTRY.to_string());
info!(&log, "Logging in to npm...");
info!("Logging in to npm...");
info!(
&log,
"Scope: {:?} Registry: {}, Always Auth: {}, Auth Type: {:?}.",
&scope,
&registry,
always_auth,
&auth_type
&scope, &registry, always_auth, &auth_type
);
info!(&log, "npm info located in the npm debug log");
npm::npm_login(log, &registry, &scope, always_auth, &auth_type)?;
info!(&log, "Logged you in!");
info!("npm info located in the npm debug log");
npm::npm_login(&registry, &scope, always_auth, &auth_type)?;
info!("Logged you in!");
PBAR.message(&format!("👋 logged you in!"));
PBAR.info(&"👋 logged you in!".to_string());
Ok(())
}

@ -14,11 +14,9 @@ use self::pack::pack;
use self::publish::{access::Access, publish};
use self::test::{Test, TestOptions};
use failure::Error;
use slog::Logger;
use log::info;
use std::path::PathBuf;
use std::result;
use std::sync::mpsc;
use std::thread;
/// The various kinds of commands that `wasm-pack` can execute.
#[derive(Debug, StructOpt)]
@ -38,6 +36,10 @@ pub enum Command {
#[structopt(name = "publish")]
/// 🎆 pack up your npm package and publish!
Publish {
#[structopt(long = "target", short = "t", default_value = "browser")]
/// Sets the target environment. [possible values: browser, nodejs, no-modules]
target: String,
/// The access level for the package to be published
#[structopt(long = "access", short = "a")]
access: Option<Access>,
@ -48,7 +50,7 @@ pub enum Command {
},
#[structopt(name = "login", alias = "adduser", alias = "add-user")]
/// 👤 Add a registry user account! (aliases: adduser, add-user)
/// 👤 Add an npm registry user account! (aliases: adduser, add-user)
Login {
#[structopt(long = "registry", short = "r")]
/// Default: 'https://registry.npmjs.org/'.
@ -84,41 +86,28 @@ pub enum Command {
Test(TestOptions),
}
/// Spawn a thread for wasm-pack version check
fn background_check_for_updates() -> mpsc::Receiver<(String, String)> {
let (sender, receiver) = mpsc::channel();
let _detached_thread = thread::spawn(move || {
if let Ok((local, latest)) = Build::return_wasm_pack_versions() {
if !local.is_empty() && !latest.is_empty() && local != latest {
sender.send((local, latest)).unwrap();
}
}
});
receiver
}
/// Run a command with the given logger!
pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error> {
pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
// Run the correct command based off input and store the result of it so that we can clear
// the progress bar then return it
let update_available = background_check_for_updates();
let status = match command {
match command {
Command::Build(build_opts) => {
info!(&log, "Running build command...");
Build::try_from_opts(build_opts).and_then(|mut b| b.run(&log))
info!("Running build command...");
Build::try_from_opts(build_opts).and_then(|mut b| b.run())
}
Command::Pack { path } => {
info!(&log, "Running pack command...");
info!(&log, "Path: {:?}", &path);
pack(path, &log)
info!("Running pack command...");
info!("Path: {:?}", &path);
pack(path)
}
Command::Publish { path, access } => {
info!(&log, "Running publish command...");
info!(&log, "Path: {:?}", &path);
publish(path, access, &log)
Command::Publish {
target,
path,
access,
} => {
info!("Running publish command...");
info!("Path: {:?}", &path);
publish(&target, path, access)
}
Command::Login {
registry,
@ -126,27 +115,16 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
always_auth,
auth_type,
} => {
info!(&log, "Running login command...");
info!("Running login command...");
info!(
&log,
"Registry: {:?}, Scope: {:?}, Always Auth: {}, Auth Type: {:?}",
&registry,
&scope,
&always_auth,
&auth_type
&registry, &scope, &always_auth, &auth_type
);
login(registry, scope, always_auth, auth_type, &log)
login(registry, &scope, always_auth, &auth_type)
}
Command::Test(test_opts) => {
info!(&log, "Running test command...");
Test::try_from_opts(test_opts).and_then(|t| t.run(&log))
info!("Running test command...");
Test::try_from_opts(test_opts).and_then(|t| t.run())
}
};
if let Ok(update_available) = update_available.try_recv() {
println!("There's a newer version of wasm-pack available, the new version is: {}, you are using: {}", update_available.1, update_available.0);
}
// Return the actual status of the program to the main function
status
}

@ -1,17 +1,17 @@
use command::utils::{find_pkg_directory, set_crate_path};
use failure::Error;
use log::info;
use npm;
use slog::Logger;
use std::path::PathBuf;
use std::result;
use PBAR;
/// Executes the 'npm pack' command on the 'pkg' directory
/// which creates a tarball that can be published to the NPM registry
pub fn pack(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error> {
pub fn pack(path: Option<PathBuf>) -> result::Result<(), Error> {
let crate_path = set_crate_path(path)?;
info!(&log, "Packing up the npm package...");
info!("Packing up the npm package...");
let pkg_directory = find_pkg_directory(&crate_path).ok_or_else(|| {
format_err!(
"Unable to find the pkg directory at path {:#?}, or in a child directory of {:#?}",
@ -19,13 +19,9 @@ pub fn pack(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error> {
&crate_path
)
})?;
npm::npm_pack(log, &pkg_directory.to_string_lossy())?;
info!(
&log,
"Your package is located at {:#?}",
crate_path.join("pkg")
);
npm::npm_pack(&pkg_directory.to_string_lossy())?;
info!("Your package is located at {:#?}", crate_path.join("pkg"));
PBAR.message("🎒 packed up your package!");
PBAR.info("🎒 packed up your package!");
Ok(())
}

@ -2,36 +2,81 @@
pub mod access;
use self::access::Access;
use command::build::{Build, BuildOptions, Target};
use command::utils::{find_pkg_directory, set_crate_path};
use dialoguer::{Confirmation, Input, Select};
use failure::Error;
use log::info;
use npm;
use slog::Logger;
use std::path::PathBuf;
use std::result;
use std::str::FromStr;
use PBAR;
/// Creates a tarball from a 'pkg' directory
/// and publishes it to the NPM registry
pub fn publish(
_target: &str,
path: Option<PathBuf>,
access: Option<Access>,
log: &Logger,
) -> result::Result<(), Error> {
let crate_path = set_crate_path(path)?;
info!(&log, "Publishing the npm package...");
info!(&log, "npm info located in the npm debug log");
let pkg_directory = find_pkg_directory(&crate_path).ok_or_else(|| {
format_err!(
"Unable to find the pkg directory at path '{:#?}', or in a child directory of '{:#?}'",
&crate_path,
&crate_path
)
})?;
info!("Publishing the npm package...");
info!("npm info located in the npm debug log");
npm::npm_publish(log, &pkg_directory.to_string_lossy(), access)?;
info!(&log, "Published your package!");
let pkg_directory = match find_pkg_directory(&crate_path) {
Some(path) => Ok(path),
None => {
// while `wasm-pack publish`, if the pkg directory cannot be found,
// then try to `wasm-pack build`
if Confirmation::new()
.with_text("Your package hasn't been built, build it?")
.interact()?
{
let out_dir = Input::new()
.with_prompt("out_dir[default: pkg]")
.default(".".to_string())
.show_default(false)
.interact()?;
let out_dir = format!("{}/pkg", out_dir);
let target = Select::new()
.with_prompt("target[default: browser]")
.items(&["browser", "nodejs", "no-modules"])
.default(0)
.interact()?
.to_string();
let target = Target::from_str(&target)?;
let build_opts = BuildOptions {
path: Some(crate_path.clone()),
target,
out_dir: out_dir.clone(),
..Default::default()
};
Build::try_from_opts(build_opts)
.and_then(|mut build| build.run())
.map(|()| crate_path.join(out_dir))
.map_err(|_| {
format_err!(
"Unable to find the pkg directory at path '{:#?}',\
or in a child directory of '{:#?}'",
&crate_path,
&crate_path
)
})
} else {
bail!(
"Unable to find the pkg directory at path '{:#?}',\
or in a child directory of '{:#?}'",
&crate_path,
&crate_path
)
}
}
}?;
npm::npm_publish(&pkg_directory.to_string_lossy(), access)?;
info!("Published your package!");
PBAR.message("💥 published your package!");
PBAR.info("💥 published your package!");
Ok(())
}

@ -1,22 +1,19 @@
//! Implementation of the `wasm-pack test` command.
use super::build::BuildMode;
use binaries::Cache;
use binary_install::Cache;
use bindgen;
use build;
use cache;
use command::utils::set_crate_path;
use console::style;
use emoji;
use failure::Error;
use indicatif::HumanDuration;
use lockfile::Lockfile;
use log::info;
use manifest;
use progressbar::Step;
use slog::Logger;
use std::path::PathBuf;
use std::time::Instant;
use test::{self, webdriver};
use PBAR;
#[derive(Debug, Default, StructOpt)]
/// Everything required to configure the `wasm-pack test` command.
@ -77,6 +74,10 @@ pub struct TestOptions {
#[structopt(long = "release", short = "r")]
/// Build with the release profile.
pub release: bool,
#[structopt(last = true)]
/// List of extra options to pass to `cargo test`
pub extra_options: Vec<String>,
}
/// A configured `wasm-pack test` command.
@ -95,9 +96,10 @@ pub struct Test {
headless: bool,
release: bool,
test_runner_path: Option<PathBuf>,
extra_options: Vec<String>,
}
type TestStep = fn(&mut Test, &Step, &Logger) -> Result<(), Error>;
type TestStep = fn(&mut Test) -> Result<(), Error>;
impl Test {
/// Construct a test command from the given options.
@ -114,6 +116,7 @@ impl Test {
geckodriver,
safari,
safaridriver,
extra_options,
} = test_opts;
let crate_path = set_crate_path(path)?;
@ -132,7 +135,7 @@ impl Test {
}
Ok(Test {
cache: Cache::new()?,
cache: cache::get_wasm_pack_cache()?,
crate_path,
crate_data,
node,
@ -146,6 +149,7 @@ impl Test {
headless,
release,
test_runner_path: None,
extra_options,
})
}
@ -155,17 +159,15 @@ impl Test {
}
/// Execute this test command.
pub fn run(mut self, log: &Logger) -> Result<(), Error> {
pub fn run(mut self) -> Result<(), Error> {
let process_steps = self.get_process_steps();
let mut step_counter = Step::new(process_steps.len());
let started = Instant::now();
for (_, process_step) in process_steps {
process_step(&mut self, &step_counter, log)?;
step_counter.inc();
process_step(&mut self)?;
}
let duration = HumanDuration(started.elapsed());
info!(&log, "Done in {}.", &duration);
let duration = crate::command::utils::elapsed(started.elapsed());
info!("Done in {}.", &duration);
Ok(())
}
@ -188,7 +190,7 @@ impl Test {
match self.mode {
BuildMode::Normal => steps![
step_check_rustc_version,
step_add_wasm_target,
step_check_for_wasm_target,
step_build_tests,
step_install_wasm_bindgen,
step_test_node if self.node,
@ -200,7 +202,7 @@ impl Test {
step_test_safari if self.safari,
],
BuildMode::Force => steps![
step_add_wasm_target,
step_check_for_wasm_target,
step_build_tests,
step_install_wasm_bindgen,
step_test_node if self.node,
@ -225,34 +227,31 @@ impl Test {
}
}
fn step_check_rustc_version(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(log, "Checking rustc version...");
let _ = build::check_rustc_version(step)?;
info!(log, "Rustc version is correct.");
fn step_check_rustc_version(&mut self) -> Result<(), Error> {
info!("Checking rustc version...");
let _ = build::check_rustc_version()?;
info!("Rustc version is correct.");
Ok(())
}
fn step_add_wasm_target(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Adding wasm-target...");
build::rustup_add_wasm_target(log, step)?;
info!(&log, "Adding wasm-target was successful.");
fn step_check_for_wasm_target(&mut self) -> Result<(), Error> {
info!("Adding wasm-target...");
build::check_for_wasm32_target()?;
info!("Adding wasm-target was successful.");
Ok(())
}
fn step_build_tests(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(log, "Compiling tests to wasm...");
let msg = format!("{}Compiling tests to WASM...", emoji::CYCLONE);
PBAR.step(step, &msg);
fn step_build_tests(&mut self) -> Result<(), Error> {
info!("Compiling tests to wasm...");
build::cargo_build_wasm_tests(log, &self.crate_path, !self.release)?;
build::cargo_build_wasm_tests(&self.crate_path, !self.release)?;
info!(log, "Finished compiling tests to wasm.");
info!("Finished compiling tests to wasm.");
Ok(())
}
fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Identifying wasm-bindgen dependency...");
fn step_install_wasm_bindgen(&mut self) -> Result<(), Error> {
info!("Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
@ -272,52 +271,44 @@ impl Test {
let install_permitted = match self.mode {
BuildMode::Normal => {
info!(&log, "Ensuring wasm-bindgen-cli is installed...");
info!("Ensuring wasm-bindgen-cli is installed...");
true
}
BuildMode::Force => {
info!(&log, "Ensuring wasm-bindgen-cli is installed...");
info!("Ensuring wasm-bindgen-cli is installed...");
true
}
BuildMode::Noinstall => {
info!(&log, "Searching for existing wasm-bindgen-cli install...");
info!("Searching for existing wasm-bindgen-cli install...");
false
}
};
let dl = bindgen::install_wasm_bindgen(
&self.cache,
&bindgen_version,
install_permitted,
step,
log,
)?;
let dl = bindgen::install_wasm_bindgen(&self.cache, &bindgen_version, install_permitted)?;
self.test_runner_path = Some(dl.binary("wasm-bindgen-test-runner"));
self.test_runner_path = Some(dl.binary("wasm-bindgen-test-runner")?);
info!(&log, "Getting wasm-bindgen-cli was successful.");
info!("Getting wasm-bindgen-cli was successful.");
Ok(())
}
fn step_test_node(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
fn step_test_node(&mut self) -> Result<(), Error> {
assert!(self.node);
info!(log, "Running tests in node...");
PBAR.step(step, "Running tests in node...");
info!("Running tests in node...");
test::cargo_test_wasm(
&self.crate_path,
self.release,
log,
Some((
"CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER",
&self.test_runner_path.as_ref().unwrap(),
)),
&self.extra_options,
)?;
info!(log, "Finished running tests in node.");
info!("Finished running tests in node.");
Ok(())
}
fn step_get_chromedriver(&mut self, step: &Step, _log: &Logger) -> Result<(), Error> {
PBAR.step(step, "Getting chromedriver...");
fn step_get_chromedriver(&mut self) -> Result<(), Error> {
assert!(self.chrome && self.chromedriver.is_none());
self.chromedriver = Some(webdriver::get_or_install_chromedriver(
@ -327,14 +318,12 @@ impl Test {
Ok(())
}
fn step_test_chrome(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
PBAR.step(step, "Running tests in Chrome...");
fn step_test_chrome(&mut self) -> Result<(), Error> {
let chromedriver = self.chromedriver.as_ref().unwrap().display().to_string();
let chromedriver = chromedriver.as_str();
info!(
log,
"Running tests in Chrome with chromedriver at {}", chromedriver
"Running tests in Chrome with chromedriver at {}",
chromedriver
);
let test_runner = self
@ -344,7 +333,7 @@ impl Test {
.display()
.to_string();
let test_runner = test_runner.as_str();
info!(log, "Using wasm-bindgen test runner at {}", test_runner);
info!("Using wasm-bindgen test runner at {}", test_runner);
let mut envs = vec![
("CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER", test_runner),
@ -354,12 +343,11 @@ impl Test {
envs.push(("NO_HEADLESS", "1"));
}
test::cargo_test_wasm(&self.crate_path, self.release, log, envs)?;
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
Ok(())
}
fn step_get_geckodriver(&mut self, step: &Step, _log: &Logger) -> Result<(), Error> {
PBAR.step(step, "Getting geckodriver...");
fn step_get_geckodriver(&mut self) -> Result<(), Error> {
assert!(self.firefox && self.geckodriver.is_none());
self.geckodriver = Some(webdriver::get_or_install_geckodriver(
@ -369,14 +357,12 @@ impl Test {
Ok(())
}
fn step_test_firefox(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
PBAR.step(step, "Running tests in Firefox...");
fn step_test_firefox(&mut self) -> Result<(), Error> {
let geckodriver = self.geckodriver.as_ref().unwrap().display().to_string();
let geckodriver = geckodriver.as_str();
info!(
log,
"Running tests in Firefox with geckodriver at {}", geckodriver
"Running tests in Firefox with geckodriver at {}",
geckodriver
);
let test_runner = self
@ -386,7 +372,7 @@ impl Test {
.display()
.to_string();
let test_runner = test_runner.as_str();
info!(log, "Using wasm-bindgen test runner at {}", test_runner);
info!("Using wasm-bindgen test runner at {}", test_runner);
let mut envs = vec![
("CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER", test_runner),
@ -396,26 +382,23 @@ impl Test {
envs.push(("NO_HEADLESS", "1"));
}
test::cargo_test_wasm(&self.crate_path, self.release, log, envs)?;
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
Ok(())
}
fn step_get_safaridriver(&mut self, step: &Step, _log: &Logger) -> Result<(), Error> {
PBAR.step(step, "Getting safaridriver...");
fn step_get_safaridriver(&mut self) -> Result<(), Error> {
assert!(self.safari && self.safaridriver.is_none());
self.safaridriver = Some(webdriver::get_safaridriver()?);
Ok(())
}
fn step_test_safari(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
PBAR.step(step, "Running tests in Safari...");
fn step_test_safari(&mut self) -> Result<(), Error> {
let safaridriver = self.safaridriver.as_ref().unwrap().display().to_string();
let safaridriver = safaridriver.as_str();
info!(
log,
"Running tests in Safari with safaridriver at {}", safaridriver
"Running tests in Safari with safaridriver at {}",
safaridriver
);
let test_runner = self
@ -425,7 +408,7 @@ impl Test {
.display()
.to_string();
let test_runner = test_runner.as_str();
info!(log, "Using wasm-bindgen test runner at {}", test_runner);
info!("Using wasm-bindgen test runner at {}", test_runner);
let mut envs = vec![
("CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER", test_runner),
@ -435,7 +418,7 @@ impl Test {
envs.push(("NO_HEADLESS", "1"));
}
test::cargo_test_wasm(&self.crate_path, self.release, log, envs)?;
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
Ok(())
}
}

@ -1,23 +1,21 @@
//! Utility functions for commands.
use emoji;
use failure;
use progressbar::Step;
use std::fs;
use std::path::{Path, PathBuf};
use PBAR;
use std::time::Duration;
use walkdir::WalkDir;
/// If an explicit path is given, then use it, otherwise assume the current
/// directory is the crate path.
pub fn set_crate_path(path: Option<PathBuf>) -> Result<PathBuf, failure::Error> {
Ok(path.unwrap_or(PathBuf::from(".")))
Ok(path.unwrap_or_else(|| PathBuf::from(".")))
}
/// Construct our `pkg` directory in the crate.
pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), failure::Error> {
let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
PBAR.step(step, &msg);
pub fn create_pkg_dir(out_dir: &Path) -> Result<(), failure::Error> {
fs::create_dir_all(&out_dir)?;
fs::write(out_dir.join(".gitignore"), "*")?;
Ok(())
}
@ -28,13 +26,23 @@ pub fn find_pkg_directory(path: &Path) -> Option<PathBuf> {
return Some(path.to_owned());
}
path.read_dir().ok().and_then(|entries| {
entries
.filter_map(|x| x.ok().map(|v| v.path()))
.find(|x| is_pkg_directory(&x))
})
WalkDir::new(path)
.into_iter()
.filter_map(|x| x.ok().map(|e| e.into_path()))
.find(|x| is_pkg_directory(&x))
}
fn is_pkg_directory(path: &Path) -> bool {
path.exists() && path.is_dir() && path.ends_with("pkg")
}
/// Render a `Duration` to a form suitable for display on a console
pub fn elapsed(duration: Duration) -> String {
let secs = duration.as_secs();
if secs >= 60 {
format!("{}m {:02}s", secs / 60, secs % 60)
} else {
format!("{}.{:02}s", secs, duration.subsec_nanos() / 10_000_000)
}
}

@ -114,7 +114,7 @@ fn confirm_can_overwrite(dst: &Path) -> Result<(), failure::Error> {
.read_line(&mut line)
.with_context(|_| "failed to read stdin")?;
if line.starts_with("y") || line.starts_with("Y") {
if line.starts_with('y') || line.starts_with('Y') {
return Ok(());
}

@ -4,41 +4,33 @@
extern crate cargo_metadata;
extern crate console;
extern crate curl;
extern crate dirs;
extern crate strsim;
#[macro_use]
extern crate failure;
extern crate flate2;
extern crate hex;
extern crate indicatif;
#[macro_use]
extern crate lazy_static;
extern crate glob;
extern crate parking_lot;
extern crate serde;
extern crate which;
#[macro_use]
extern crate serde_derive;
extern crate serde_ignored;
extern crate serde_json;
extern crate siphasher;
#[macro_use]
extern crate structopt;
#[macro_use]
extern crate slog;
extern crate chrono;
extern crate slog_async;
extern crate slog_term;
extern crate tar;
extern crate binary_install;
extern crate dialoguer;
extern crate log;
extern crate toml;
extern crate which;
extern crate zip;
extern crate walkdir;
pub mod binaries;
pub mod bindgen;
pub mod build;
pub mod cache;
pub mod child;
pub mod command;
pub mod emoji;
pub mod license;
pub mod lockfile;
pub mod logger;
pub mod manifest;
pub mod npm;
pub mod progressbar;
@ -48,10 +40,8 @@ pub mod test;
use progressbar::ProgressOutput;
lazy_static! {
/// The global progress bar and user-facing message output.
pub static ref PBAR: ProgressOutput = { ProgressOutput::new() };
}
/// The global progress bar and user-facing message output.
pub static PBAR: ProgressOutput = ProgressOutput;
/// 📦 ✨ pack and publish your wasm!
#[derive(Debug, StructOpt)]

@ -0,0 +1,89 @@
//! Copy `LICENSE` file(s) for the packaged wasm.
use failure;
use std::fs;
use std::path::Path;
use glob::glob;
use manifest::CrateData;
use PBAR;
fn glob_license_files(path: &Path) -> Result<Vec<String>, failure::Error> {
let mut license_files: Vec<String> = Vec::new();
let path_string = match path.join("LICENSE*").to_str() {
Some(path_string) => path_string.to_owned(),
None => {
return Err(format_err!(
"Could not convert joined license path to String"
));
}
};
for entry in glob(&path_string)? {
match entry {
Ok(globed_path) => {
let file_name = match globed_path.file_name() {
Some(file_name) => file_name,
None => return Err(format_err!("Could not get file name from path")),
};
let file_name_string = match file_name.to_str() {
Some(file_name_string) => file_name_string.to_owned(),
None => return Err(format_err!("Could not convert filename to String")),
};
license_files.push(file_name_string);
}
Err(e) => println!("{:?}", e),
}
}
Ok(license_files)
}
/// Copy the crate's license into the `pkg` directory.
pub fn copy_from_crate(
crate_data: &CrateData,
path: &Path,
out_dir: &Path,
) -> Result<(), failure::Error> {
assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist"
);
assert!(
fs::metadata(&out_dir).ok().map_or(false, |m| m.is_dir()),
"crate's pkg directory should exist"
);
match (crate_data.crate_license(), crate_data.crate_license_file()) {
(Some(_), _) => {
let license_files = glob_license_files(path);
match license_files {
Ok(files) => {
if files.is_empty() {
PBAR.info("License key is set in Cargo.toml but no LICENSE file(s) were found; Please add the LICENSE file(s) to your project directory");
return Ok(());
}
for license_file in files {
let crate_license_path = path.join(&license_file);
let new_license_path = out_dir.join(&license_file);
if fs::copy(&crate_license_path, &new_license_path).is_err() {
PBAR.info("origin crate has no LICENSE");
}
}
}
Err(_) => PBAR.info("origin crate has no LICENSE"),
}
}
(None, Some(license_file)) => {
let crate_license_path = path.join(&license_file);
let new_license_path = out_dir.join(&license_file);
if fs::copy(&crate_license_path, &new_license_path).is_err() {
PBAR.info("origin crate has no LICENSE");
}
}
(None, None) => {}
};
Ok(())
}

@ -1,5 +1,7 @@
//! Reading Cargo.lock lock file.
#![allow(clippy::new_ret_no_self)]
use std::fs;
use std::path::PathBuf;

@ -1,72 +0,0 @@
//! Logging facilities for `wasm-pack`.
use command::Command;
use failure;
use slog::{Drain, Level, Logger};
use slog_async::Async;
use slog_term::{FullFormat, PlainDecorator};
use std::fs::OpenOptions;
use std::path::PathBuf;
/// Create the logger for wasm-pack that will output any info warning or errors we encounter
pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, failure::Error> {
let log_path = log_file_path(&cmd);
let file = OpenOptions::new()
.create(true)
.append(true)
.open(log_path)?;
let decorator = PlainDecorator::new(file);
let drain = FullFormat::new(decorator).build().fuse();
// Set the log level based off the number of v passed in to the command line args.
// Level level means only messages of that level and higher are logged. If we have
// an error then we'll log it unconditionally, but extra levels are only available
// with extra v
let log_level = match verbosity {
0 => Level::Error,
1 => Level::Info,
2 => Level::Debug,
_ => Level::Trace,
};
let drain = Async::new(drain).build().filter_level(log_level).fuse();
Ok(Logger::root(drain, o!()))
}
/// Figure out where to stick the log based off the command arguments given
fn log_file_path(cmd: &Command) -> PathBuf {
let path = match cmd {
Command::Build(build_opts) => &build_opts.path,
Command::Pack { path } => path,
Command::Publish { path, access: _ } => path,
Command::Test(test_opts) => &test_opts.path,
Command::Login { .. } => &None,
};
// If the path exists attempt to use it, if not default to the current directory
if let Some(ref path) = path {
let mut path_buf = PathBuf::from(path);
path_buf.push("Cargo.toml");
// If the manifest file exists put the log in that directory otherwise default
// to the current directory.
if path_buf.exists() {
path_buf.pop();
path_buf.push("wasm-pack.log");
path_buf
} else {
let mut path_buf = this_dir();
path_buf.push("wasm-pack.log");
path_buf
}
} else {
let mut path_buf = this_dir();
path_buf.push("wasm-pack.log");
path_buf
}
}
/// Return a `PathBuf` for the current directory
fn this_dir() -> PathBuf {
PathBuf::from(".")
}

@ -1,4 +1,5 @@
extern crate atty;
extern crate env_logger;
#[macro_use]
extern crate failure;
#[macro_use]
@ -9,11 +10,12 @@ extern crate which;
use std::env;
use structopt::StructOpt;
use wasm_pack::{command::run_wasm_pack, logger, Cli};
use wasm_pack::{command::run_wasm_pack, Cli};
mod installer;
fn main() {
env_logger::init();
setup_panic!();
if let Err(e) = run() {
eprintln!("Error: {}", e);
@ -33,13 +35,17 @@ fn run() -> Result<(), failure::Error> {
if let Ok(me) = env::current_exe() {
// If we're actually running as the installer then execute our
// self-installation, otherwise just continue as usual.
if me.file_stem().and_then(|s| s.to_str()) == Some("wasm-pack-init") {
if me
.file_stem()
.and_then(|s| s.to_str())
.expect("executable should have a filename")
.starts_with("wasm-pack-init")
{
installer::install();
}
}
let args = Cli::from_args();
let log = logger::new(&args.cmd, args.verbosity)?;
run_wasm_pack(args.cmd, &log)?;
run_wasm_pack(args.cmd)?;
Ok(())
}

@ -1,5 +1,7 @@
//! Reading and writing Cargo.toml and package.json manifests.
#![allow(clippy::new_ret_no_self, clippy::needless_pass_by_value)]
mod npm;
use std::fs;
@ -9,20 +11,17 @@ use self::npm::{
repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage,
};
use cargo_metadata::Metadata;
use chrono::offset;
use chrono::DateTime;
use command::build::BuildProfile;
use curl::easy;
use emoji;
use command::build::{BuildProfile, Target};
use failure::{Error, ResultExt};
use progressbar::Step;
use serde::{self, Deserialize};
use serde_json;
use std::io::Write;
use std::collections::BTreeSet;
use strsim::levenshtein;
use toml;
use which;
use PBAR;
const WASM_PACK_METADATA_KEY: &str = "package.metadata.wasm-pack";
/// Store for metadata learned about a crate
pub struct CrateData {
data: Metadata,
@ -30,17 +29,9 @@ pub struct CrateData {
manifest: CargoManifest,
}
struct Collector(Vec<u8>);
impl easy::Handler for Collector {
fn write(&mut self, data: &[u8]) -> Result<usize, easy::WriteError> {
self.0.extend_from_slice(data);
Ok(data.len())
}
}
#[doc(hidden)]
#[derive(Deserialize)]
struct CargoManifest {
pub struct CargoManifest {
package: CargoPackage,
}
@ -49,7 +40,10 @@ struct CargoPackage {
name: String,
description: Option<String>,
license: Option<String>,
#[serde(rename = "license-file")]
license_file: Option<String>,
repository: Option<String>,
homepage: Option<String>,
#[serde(default)]
metadata: CargoMetadata,
@ -118,107 +112,6 @@ struct CargoWasmPackProfileWasmBindgen {
dwarf_debug_info: Option<bool>,
}
/// Struct for crate which is received from crates.io
#[derive(Deserialize, Debug)]
pub struct Crate {
#[serde(rename = "crate")]
crt: CrateInformation,
}
#[derive(Deserialize, Debug)]
struct CrateInformation {
max_version: String,
}
impl Crate {
/// Returns latest wasm-pack version
pub fn return_wasm_pack_latest_version() -> Option<String> {
let current_time = chrono::offset::Local::now();
Crate::return_wasm_pack_file().and_then(|contents| {
let last_updated = Crate::return_stamp_file_value(&contents, "created")
.and_then(|t| Some(DateTime::parse_from_str(t.as_str(), "%+").unwrap()));
let version = Crate::return_stamp_file_value(&contents, "version").and_then(|v| {
if current_time
.signed_duration_since(last_updated.unwrap())
.num_hours()
> 24
{
return Crate::return_api_call_result(current_time);
} else {
return Some(v);
}
});
version
});
return Crate::return_api_call_result(current_time);
}
fn return_api_call_result(current_time: DateTime<offset::Local>) -> Option<String> {
Crate::call_for_wasm_pack_version().and_then(|v| {
Crate::override_stamp_file(current_time, &v);
Some(v)
})
}
fn override_stamp_file(current_time: DateTime<offset::Local>, version: &String) {
if let Ok(path) = which::which("wasm-pack") {
let file = fs::OpenOptions::new()
.read(true)
.write(true)
.append(true)
.create(true)
.open(path.with_extension("stamp"));
if let Ok(()) = file.as_ref().unwrap().set_len(0) {
if let Err(_) = write!(
file.unwrap(),
"created {:?}\nversion {}",
current_time,
version
) {}
}
}
}
fn return_wasm_pack_file() -> Option<String> {
if let Ok(path) = which::which("wasm-pack") {
if let Ok(file) = fs::read_to_string(path.with_extension("stamp")) {
return Some(file);
}
}
None
}
fn call_for_wasm_pack_version() -> Option<String> {
if let Ok(crt) = Crate::check_wasm_pack_latest_version() {
return Some(crt.crt.max_version);
}
None
}
fn return_stamp_file_value(file: &String, word: &str) -> Option<String> {
let created = file
.lines()
.find(|line| line.starts_with(word))
.and_then(|l| l.split_whitespace().nth(1));
let value = created.map(|s| s.to_string());
value
}
/// Call to the crates.io api and return the latest version of `wasm-pack`
fn check_wasm_pack_latest_version() -> Result<Crate, Error> {
let mut easy = easy::Easy2::new(Collector(Vec::new()));
easy.get(true)?;
easy.url("https://crates.io/api/v1/crates/wasm-pack")?;
easy.perform()?;
let contents = easy.get_ref();
let result = String::from_utf8_lossy(&contents.0);
Ok(serde_json::from_str(result.into_owned().as_str())?)
}
}
impl CargoWasmPackProfile {
fn default_dev() -> Self {
CargoWasmPackProfile {
@ -255,7 +148,7 @@ impl CargoWasmPackProfile {
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(Self::default_dev());
profile.update_with_defaults(&Self::default_dev());
Ok(profile)
}
@ -264,7 +157,7 @@ impl CargoWasmPackProfile {
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(Self::default_release());
profile.update_with_defaults(&Self::default_release());
Ok(profile)
}
@ -273,11 +166,11 @@ impl CargoWasmPackProfile {
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(Self::default_profiling());
profile.update_with_defaults(&Self::default_profiling());
Ok(profile)
}
fn update_with_defaults(&mut self, defaults: Self) {
fn update_with_defaults(&mut self, defaults: &Self) {
macro_rules! d {
( $( $path:ident ).* ) => {
self. $( $path ).* .get_or_insert(defaults. $( $path ).* .unwrap());
@ -309,6 +202,13 @@ struct NpmData {
files: Vec<String>,
dts_file: Option<String>,
main: String,
homepage: Option<String>, // https://docs.npmjs.com/files/package.json#homepage
}
#[doc(hidden)]
pub struct ManifestAndUnsedKeys {
pub manifest: CargoManifest,
pub unused_keys: BTreeSet<String>,
}
impl CrateData {
@ -323,14 +223,14 @@ impl CrateData {
crate_path.display()
)
}
let manifest = fs::read_to_string(&manifest_path)
.with_context(|_| format!("failed to read: {}", manifest_path.display()))?;
let manifest: CargoManifest = toml::from_str(&manifest)
.with_context(|_| format!("failed to parse manifest: {}", manifest_path.display()))?;
let data =
cargo_metadata::metadata(Some(&manifest_path)).map_err(error_chain_to_failure)?;
let manifest_and_keys = CrateData::parse_crate_data(&manifest_path)?;
CrateData::warn_for_unused_keys(&manifest_and_keys);
let manifest = manifest_and_keys.manifest;
let current_idx = data
.packages
.iter()
@ -352,10 +252,54 @@ impl CrateData {
for e in errors[..errors.len() - 1].iter().rev() {
err = err.context(e.to_string()).into();
}
return err;
err
}
}
/// Read the `manifest_path` file and deserializes it using the toml Deserializer.
/// Returns a Result containing `ManifestAndUnsedKeys` which contains `CargoManifest`
/// and a `BTreeSet<String>` containing the unused keys from the parsed file.
///
/// # Errors
/// Will return Err if the file (manifest_path) couldn't be read or
/// if deserialize to `CargoManifest` fails.
pub fn parse_crate_data(manifest_path: &Path) -> Result<ManifestAndUnsedKeys, Error> {
let manifest = fs::read_to_string(&manifest_path)
.with_context(|_| format!("failed to read: {}", manifest_path.display()))?;
let manifest = &mut toml::Deserializer::new(&manifest);
let mut unused_keys = BTreeSet::new();
let levenshtein_threshold = 1;
let manifest: CargoManifest = serde_ignored::deserialize(manifest, |path| {
let path_string = path.to_string();
if path_string.starts_with("package.metadata")
&& (path_string.contains("wasm-pack")
|| levenshtein(WASM_PACK_METADATA_KEY, &path_string) <= levenshtein_threshold)
{
unused_keys.insert(path_string);
}
})
.with_context(|_| format!("failed to parse manifest: {}", manifest_path.display()))?;
Ok(ManifestAndUnsedKeys {
manifest,
unused_keys,
})
}
/// Iterating through all the passed `unused_keys` and output
/// a warning for each unknown key.
pub fn warn_for_unused_keys(manifest_and_keys: &ManifestAndUnsedKeys) {
manifest_and_keys.unused_keys.iter().for_each(|path| {
PBAR.warn(&format!(
"\"{}\" is an unknown key and will be ignored. Please check your Cargo.toml.",
path
));
});
}
/// Get the configured profile.
pub fn configured_profile(&self, profile: BuildProfile) -> &CargoWasmPackProfile {
match profile {
@ -366,9 +310,7 @@ impl CrateData {
}
/// Check that the crate the given path is properly configured.
pub fn check_crate_config(&self, step: &Step) -> Result<(), Error> {
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg);
pub fn check_crate_config(&self) -> Result<(), Error> {
self.check_crate_type()?;
Ok(())
}
@ -404,6 +346,16 @@ impl CrateData {
}
}
/// Get the license for the crate at the given path.
pub fn crate_license(&self) -> &Option<String> {
&self.manifest.package.license
}
/// Get the license file path for the crate at the given path.
pub fn crate_license_file(&self) -> &Option<String> {
&self.manifest.package.license_file
}
/// Returns the path to this project's target directory where artifacts are
/// located after a cargo build.
pub fn target_directory(&self) -> &Path {
@ -421,19 +373,14 @@ impl CrateData {
out_dir: &Path,
scope: &Option<String>,
disable_dts: bool,
target: &str,
step: &Step,
target: &Target,
) -> Result<(), Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO);
PBAR.step(step, &msg);
let pkg_file_path = out_dir.join("package.json");
let npm_data = if target == "nodejs" {
self.to_commonjs(scope, disable_dts)
} else if target == "no-modules" {
self.to_nomodules(scope, disable_dts)
} else {
self.to_esmodules(scope, disable_dts)
let npm_data = match target {
Target::Nodejs => self.to_commonjs(scope, disable_dts, out_dir),
Target::NoModules => self.to_nomodules(scope, disable_dts, out_dir),
Target::Bundler => self.to_esmodules(scope, disable_dts, out_dir),
Target::Web => self.to_web(scope, disable_dts, out_dir),
};
let npm_json = serde_json::to_string_pretty(&npm_data)?;
@ -447,6 +394,7 @@ impl CrateData {
scope: &Option<String>,
include_commonjs_shim: bool,
disable_dts: bool,
out_dir: &Path,
) -> NpmData {
let crate_name = self.crate_name();
let wasm_file = format!("{}_bg.wasm", crate_name);
@ -472,16 +420,39 @@ impl CrateData {
} else {
None
};
if let Ok(entries) = fs::read_dir(out_dir) {
let file_names = entries
.filter_map(|e| e.ok())
.filter(|e| e.metadata().map(|m| m.is_file()).unwrap_or(false))
.filter_map(|e| e.file_name().into_string().ok())
.filter(|f| f.starts_with("LICENSE"))
.filter(|f| f != "LICENSE");
for file_name in file_names {
files.push(file_name);
}
}
NpmData {
name: npm_name,
dts_file,
files,
main: js_file,
homepage: self.manifest.package.homepage.clone(),
}
}
fn to_commonjs(&self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let data = self.npm_data(scope, true, disable_dts);
fn license(&self) -> Option<String> {
self.manifest.package.license.clone().or_else(|| {
self.manifest.package.license_file.clone().map(|file| {
// When license is written in file: https://docs.npmjs.com/files/package.json#license
format!("SEE LICENSE IN {}", file)
})
})
}
fn to_commonjs(&self, scope: &Option<String>, disable_dts: bool, out_dir: &Path) -> NpmPackage {
let data = self.npm_data(scope, true, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];
self.check_optional_fields();
@ -491,7 +462,7 @@ impl CrateData {
collaborators: pkg.authors.clone(),
description: self.manifest.package.description.clone(),
version: pkg.version.clone(),
license: self.manifest.package.license.clone(),
license: self.license(),
repository: self
.manifest
.package
@ -503,12 +474,47 @@ impl CrateData {
}),
files: data.files,
main: data.main,
homepage: data.homepage,
types: data.dts_file,
})
}
fn to_esmodules(
&self,
scope: &Option<String>,
disable_dts: bool,
out_dir: &Path,
) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];
self.check_optional_fields();
NpmPackage::ESModulesPackage(ESModulesPackage {
name: data.name,
collaborators: pkg.authors.clone(),
description: self.manifest.package.description.clone(),
version: pkg.version.clone(),
license: self.license(),
repository: self
.manifest
.package
.repository
.clone()
.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: data.files,
module: data.main,
homepage: data.homepage,
types: data.dts_file,
side_effects: "false".to_string(),
})
}
fn to_esmodules(&self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts);
fn to_web(&self, scope: &Option<String>, disable_dts: bool, out_dir: &Path) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];
self.check_optional_fields();
@ -518,7 +524,7 @@ impl CrateData {
collaborators: pkg.authors.clone(),
description: self.manifest.package.description.clone(),
version: pkg.version.clone(),
license: self.manifest.package.license.clone(),
license: self.license(),
repository: self
.manifest
.package
@ -530,13 +536,19 @@ impl CrateData {
}),
files: data.files,
module: data.main,
homepage: data.homepage,
types: data.dts_file,
side_effects: "false".to_string(),
})
}
fn to_nomodules(&self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts);
fn to_nomodules(
&self,
scope: &Option<String>,
disable_dts: bool,
out_dir: &Path,
) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];
self.check_optional_fields();
@ -546,7 +558,7 @@ impl CrateData {
collaborators: pkg.authors.clone(),
description: self.manifest.package.description.clone(),
version: pkg.version.clone(),
license: self.manifest.package.license.clone(),
license: self.license(),
repository: self
.manifest
.package
@ -558,6 +570,7 @@ impl CrateData {
}),
files: data.files,
browser: data.main,
homepage: data.homepage,
types: data.dts_file,
})
}

@ -16,5 +16,7 @@ pub struct CommonJSPackage {
pub files: Vec<String>,
pub main: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
}

@ -16,6 +16,8 @@ pub struct ESModulesPackage {
pub files: Vec<String>,
pub module: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
#[serde(rename = "sideEffects")]
pub side_effects: String,

@ -16,5 +16,7 @@ pub struct NoModulesPackage {
pub files: Vec<String>,
pub browser: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
}

@ -3,71 +3,63 @@
use child;
use command::publish::access::Access;
use failure::{self, ResultExt};
use slog::Logger;
use std::process::{Command, Stdio};
use log::info;
/// The default npm registry used when we aren't working with a custom registry.
pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";
pub const DEFAULT_NPM_REGISTRY: &str = "https://registry.npmjs.org/";
/// Run the `npm pack` command.
pub fn npm_pack(log: &Logger, path: &str) -> Result<(), failure::Error> {
let mut cmd = Command::new("npm");
pub fn npm_pack(path: &str) -> Result<(), failure::Error> {
let mut cmd = child::new_command("npm");
cmd.current_dir(path).arg("pack");
child::run(log, cmd, "npm pack").context("Packaging up your code failed")?;
child::run(cmd, "npm pack").context("Packaging up your code failed")?;
Ok(())
}
/// Run the `npm publish` command.
pub fn npm_publish(log: &Logger, path: &str, access: Option<Access>) -> Result<(), failure::Error> {
let mut cmd = Command::new("npm");
pub fn npm_publish(path: &str, access: Option<Access>) -> Result<(), failure::Error> {
let mut cmd = child::new_command("npm");
match access {
Some(a) => cmd
.current_dir(path)
.arg("publish")
.arg(&format!("{}", a.to_string()))
.stdin(Stdio::inherit())
.stdout(Stdio::inherit()),
None => cmd
.current_dir(path)
.arg("publish")
.stdin(Stdio::inherit())
.stdout(Stdio::inherit()),
.arg(&format!("{}", a.to_string())),
None => cmd.current_dir(path).arg("publish"),
};
child::run(log, cmd, "npm publish").context("Publishing to npm failed")?;
child::run(cmd, "npm publish").context("Publishing to npm failed")?;
Ok(())
}
/// Run the `npm login` command.
pub fn npm_login(
log: &Logger,
registry: &String,
registry: &str,
scope: &Option<String>,
always_auth: bool,
auth_type: &Option<String>,
) -> Result<(), failure::Error> {
let mut args = String::new();
args.push_str(&format!("--registry={}", registry));
let mut args = vec![format!("login"), format!("--registry={}", registry)];
if let Some(scope) = scope {
args.push_str(&format!(" --scope={}", scope));
args.push(format!("--scope={}", scope));
}
if always_auth == true {
args.push_str(" --always_auth");
if always_auth {
args.push(format!("--always_auth"));
}
if let Some(auth_type) = auth_type {
args.push_str(&format!(" --auth_type={}", auth_type));
args.push(format!("--auth_type={}", auth_type));
}
let mut cmd = Command::new("npm");
cmd.arg("login")
.arg(args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit());
child::run(log, cmd, "npm login")
.with_context(|_| format!("Login to registry {} failed", registry))?;
Ok(())
// Interactively ask user for npm login info.
// (child::run does not support interactive input)
let mut cmd = child::new_command("npm");
cmd.args(args);
info!("Running {:?}", cmd);
match cmd.status()?.success() {
true => Ok(()),
false => bail!("Login to registry {} failed", registry),
}
}

@ -2,65 +2,20 @@
use console::style;
use emoji;
use indicatif::{ProgressBar, ProgressStyle};
use parking_lot::RwLock;
use std::fmt;
/// Synchronized progress bar and status message printing.
pub struct ProgressOutput {
spinner: RwLock<ProgressBar>,
messages: RwLock<String>,
}
pub struct ProgressOutput;
impl ProgressOutput {
/// Construct a new `ProgressOutput`.
pub fn new() -> Self {
Self {
spinner: RwLock::new(ProgressBar::new_spinner()),
messages: RwLock::new(String::from("")),
}
}
/// Inform the user that the given `step` is being executed, with details in
/// `message`.
pub fn step(&self, step: &Step, message: &str) {
let msg = format!("{} {}", style(step).bold().dim(), message);
self.message(&msg)
}
fn finish(&self) {
let spinner = self.spinner.read();
spinner.finish();
let mut message = self.messages.write();
print!("{}", *message);
message.clear();
}
/// Print the given message.
pub fn message(&self, message: &str) {
self.finish();
let mut spinner = self.spinner.write();
*spinner = Self::progressbar(message);
}
fn add_message(&self, msg: &str) {
let mut message = self.messages.write();
message.push_str(" ");
message.push_str(msg);
message.push('\n');
fn message(&self, message: &str) {
eprintln!("{}", message);
}
/// Add an informational message.
pub fn info(&self, message: &str) {
let info = format!(
"{} {}: {}",
emoji::INFO,
style("[INFO]").bold().dim(),
message
);
self.add_message(&info);
let info = format!("{}: {}", style("[INFO]").bold().dim(), message,);
self.message(&info);
}
/// Add a warning message.
@ -71,67 +26,23 @@ impl ProgressOutput {
style("[WARN]").bold().dim(),
message
);
self.add_message(&warn);
self.message(&warn);
}
/// Add an error message.
pub fn error(&self, message: String) {
pub fn error(&self, message: &str) {
let err = format!(
"{} {}: {}",
emoji::ERROR,
style("[ERR]").bold().dim(),
message
);
self.add_message(&err);
}
fn progressbar(msg: &str) -> ProgressBar {
let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(200);
pb.set_style(
ProgressStyle::default_spinner()
.tick_chars("/|\\- ")
.template("{spinner:.dim.bold} {wide_msg}"),
);
pb.set_message(&msg);
pb
}
/// After having built up a series of messages, print all of them out.
pub fn done(&self) {
self.finish();
}
}
/// For processes that can be broken down into N fractional steps, with messages
/// added for each step along the way like
///
/// > [2/5] Doing the second step out of five.
pub struct Step {
current: usize,
total: usize,
}
impl Step {
/// Construct a `Step` where there are `total` number of steps.
pub fn new(total: usize) -> Step {
Step { current: 1, total }
}
/// Increment the current step.
pub fn inc(&mut self) {
self.current += 1;
}
}
impl fmt::Display for Step {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}/{}]", self.current, self.total)
self.message(&err);
}
}
impl Drop for ProgressOutput {
fn drop(&mut self) {
self.done();
impl Default for ProgressOutput {
fn default() -> Self {
ProgressOutput
}
}

@ -1,15 +1,13 @@
//! Generating `README` files for the packaged wasm.
use failure;
use failure::{self, ResultExt};
use std::fs;
use std::path::Path;
use emoji;
use progressbar::Step;
use PBAR;
/// Copy the crate's README into the `pkg` directory.
pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), failure::Error> {
pub fn copy_from_crate(path: &Path, out_dir: &Path) -> Result<(), failure::Error> {
assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist"
@ -19,12 +17,12 @@ pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), f
"crate's pkg directory should exist"
);
let msg = format!("{}Copying over your README...", emoji::DANCERS);
PBAR.step(step, &msg);
let crate_readme_path = path.join("README.md");
let new_readme_path = out_dir.join("README.md");
if let Err(_) = fs::copy(&crate_readme_path, &new_readme_path) {
if crate_readme_path.exists() {
fs::copy(&crate_readme_path, &new_readme_path).context("failed to copy README")?;
} else {
PBAR.warn("origin crate has no README");
};
}
Ok(())
}

@ -4,7 +4,6 @@ pub mod webdriver;
use child;
use failure::{self, ResultExt};
use slog::Logger;
use std::ffi::OsStr;
use std::path::Path;
use std::process::Command;
@ -14,29 +13,24 @@ use std::process::Command;
pub fn cargo_test_wasm<I, K, V>(
path: &Path,
release: bool,
log: &Logger,
envs: I,
extra_options: &[String],
) -> Result<(), failure::Error>
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
let output = {
let mut cmd = Command::new("cargo");
cmd.envs(envs);
cmd.current_dir(path).arg("test");
if release {
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(log, cmd, "cargo test")
.context("Running Wasm tests with wasm-bindgen-test failed")?
};
for line in output.lines() {
info!(log, "test output: {}", line);
println!("{}", line);
let mut cmd = Command::new("cargo");
cmd.envs(envs);
cmd.current_dir(path).arg("test");
if release {
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.args(extra_options);
child::run(cmd, "cargo test").context("Running Wasm tests with wasm-bindgen-test failed")?;
// NB: `child::run` took care of ensuring that test output gets printed.
Ok(())
}

@ -1,10 +1,29 @@
//! Getting WebDriver client binaries.
use binaries::Cache;
use binary_install::Cache;
use command::build::BuildMode;
use failure;
use std::path::PathBuf;
use target;
use PBAR;
fn get_and_notify(
cache: &Cache,
installation_allowed: bool,
name: &str,
url: &str,
) -> Result<Option<PathBuf>, failure::Error> {
if let Some(dl) = cache.download(false, name, &[name], &url)? {
return Ok(Some(dl.binary(name)?));
}
if installation_allowed {
PBAR.info(&format!("Getting {}...", name));
}
match cache.download(installation_allowed, name, &[name], &url)? {
Some(dl) => Ok(Some(dl.binary(name)?)),
None => Ok(None),
}
}
/// Get the path to an existing `chromedriver`, or install it if no existing
/// binary is found.
@ -31,23 +50,18 @@ pub fn install_chromedriver(
"linux64"
} else if target::MACOS && target::x86_64 {
"mac64"
} else if target::WINDOWS && target::x86 {
} else if target::WINDOWS {
"win32"
} else {
bail!("geckodriver binaries are unavailable for this target")
bail!("chromedriver binaries are unavailable for this target")
};
let url = format!(
"https://chromedriver.storage.googleapis.com/2.41/chromedriver_{}.zip",
"https://chromedriver.storage.googleapis.com/2.46/chromedriver_{}.zip",
target
);
match cache.download(
installation_allowed,
"chromedriver",
&["chromedriver"],
&url,
)? {
Some(dl) => Ok(dl.binary("chromedriver")),
match get_and_notify(cache, installation_allowed, "chromedriver", &url)? {
Some(path) => Ok(path),
None => bail!(
"No cached `chromedriver` binary found, and could not find a global \
`chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \
@ -92,12 +106,12 @@ pub fn install_geckodriver(
};
let url = format!(
"https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-{}.{}",
"https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-{}.{}",
target,
ext,
);
match cache.download(installation_allowed, "geckodriver", &["geckodriver"], &url)? {
Some(dl) => Ok(dl.binary("geckodriver")),
match get_and_notify(cache, installation_allowed, "geckodriver", &url)? {
Some(path) => Ok(path),
None => bail!(
"No cached `geckodriver` binary found, and could not find a global `geckodriver` \
on the `$PATH`. Not installing `geckodriver` because of noinstall mode."

@ -1,5 +1,5 @@
use binary_install::Cache;
use tempfile;
use wasm_pack::binaries::Cache;
use wasm_pack::bindgen;
#[test]
@ -11,9 +11,9 @@ use wasm_pack::bindgen;
fn can_download_prebuilt_wasm_bindgen() {
let dir = tempfile::TempDir::new().unwrap();
let cache = Cache::at(dir.path());
let dl = bindgen::download_prebuilt_wasm_bindgen(&cache, "0.2.21", true).unwrap();
assert!(dl.binary("wasm-bindgen").is_file());
assert!(dl.binary("wasm-bindgen-test-runner").is_file())
let dl = bindgen::download_prebuilt_wasm_bindgen(&cache, "0.2.37", true).unwrap();
assert!(dl.binary("wasm-bindgen").unwrap().is_file());
assert!(dl.binary("wasm-bindgen-test-runner").unwrap().is_file())
}
#[test]
@ -24,7 +24,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.21-some-trailing-version-stuff-that-does-not-exist";
let bad_version = "0.2.37-some-trailing-version-stuff-that-does-not-exist";
let cache = Cache::at(dir.path());
let result = bindgen::download_prebuilt_wasm_bindgen(&cache, bad_version, true);
assert!(result.is_err());

@ -1,40 +1,23 @@
use assert_cmd::prelude::*;
use std::fs;
use std::path::Path;
use structopt::StructOpt;
use utils;
use wasm_pack::Cli;
#[test]
fn build_in_non_crate_directory_doesnt_panic() {
let fixture = utils::fixture::not_a_crate();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
&fixture.path.display().to_string(),
])
.unwrap();
let result = fixture.run(cli.cmd);
assert!(
result.is_err(),
"running wasm-pack in a non-crate directory should fail, but it should not panic"
);
let err = result.unwrap_err();
assert!(err
.iter_chain()
.any(|e| e.to_string().contains("missing a `Cargo.toml`")));
fixture
.wasm_pack()
.arg("build")
.assert()
.failure()
.stderr(predicates::str::contains("missing a `Cargo.toml`"));
}
#[test]
fn it_should_build_js_hello_world_example() {
let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
&fixture.path.display().to_string(),
])
.unwrap();
fixture.run(cli.cmd).unwrap();
fixture.wasm_pack().arg("build").assert().success();
}
#[test]
@ -63,7 +46,7 @@ fn it_should_build_crates_in_a_workspace() {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
"#,
)
.file(
@ -75,15 +58,14 @@ fn it_should_build_crates_in_a_workspace() {
#[wasm_bindgen]
pub fn hello() -> u32 { 42 }
"#,
);
fixture.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
&fixture.path.join("blah").display().to_string(),
])
.unwrap();
fixture.run(cli.cmd).unwrap();
)
.install_local_wasm_bindgen();
fixture
.wasm_pack()
.current_dir(&fixture.path.join("blah"))
.arg("build")
.assert()
.success();
}
#[test]
@ -104,7 +86,7 @@ fn renamed_crate_name_works() {
name = 'bar'
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
"#,
)
.file(
@ -116,28 +98,21 @@ fn renamed_crate_name_works() {
#[wasm_bindgen]
pub fn one() -> u32 { 1 }
"#,
);
fixture.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
&fixture.path.display().to_string(),
])
.unwrap();
fixture.run(cli.cmd).unwrap();
)
.install_local_wasm_bindgen();
fixture.wasm_pack().arg("build").assert().success();
}
#[test]
fn it_should_build_nested_project_with_transitive_dependencies() {
let fixture = utils::fixture::transitive_dependencies();
fixture.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
&fixture.path.join("main").display().to_string(),
])
.unwrap();
fixture.run(cli.cmd).unwrap();
fixture
.wasm_pack()
.current_dir(fixture.path.join("main"))
.arg("build")
.assert()
.success();
}
#[test]
@ -149,14 +124,12 @@ fn build_different_profiles() {
.iter()
.cloned()
{
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
profile,
&fixture.path.display().to_string(),
])
.unwrap();
fixture.run(cli.cmd).unwrap();
fixture
.wasm_pack()
.arg("build")
.arg(profile)
.assert()
.success();
}
}
@ -208,17 +181,15 @@ fn build_with_and_without_wasm_bindgen_debug() {
pub fn take(self) {}
}
"#,
);
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
"--dev",
&fixture.path.display().to_string(),
])
.unwrap();
)
.install_local_wasm_bindgen();
fixture.run(cli.cmd).unwrap();
fixture
.wasm_pack()
.arg("build")
.arg("--dev")
.assert()
.success();
let contents = fs::read_to_string(fixture.path.join("pkg/whatever.js")).unwrap();
assert_eq!(
@ -228,3 +199,16 @@ fn build_with_and_without_wasm_bindgen_debug() {
);
}
}
#[test]
fn build_with_arbitrary_cargo_options() {
let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen();
fixture
.wasm_pack()
.arg("build")
.arg("--")
.arg("--no-default-features")
.assert()
.success();
}

@ -0,0 +1,152 @@
extern crate failure;
extern crate wasm_pack;
use std::fs;
use utils::{self, fixture};
use wasm_pack::license;
use wasm_pack::manifest::CrateData;
#[test]
fn it_copies_a_license_default_path() {
let fixture = fixture::single_license();
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok());
let crate_license_path = fixture.path.join("LICENSE");
let pkg_license_path = out_dir.join("LICENSE");
println!(
"wasm-pack: should have copied LICENSE from '{}' to '{}'",
crate_license_path.display(),
pkg_license_path.display()
);
assert!(fs::metadata(&crate_license_path).is_ok());
assert!(fs::metadata(&pkg_license_path).is_ok());
let crate_license = utils::file::read_file(&crate_license_path).unwrap();
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap();
assert_eq!(crate_license, pkg_license);
}
#[test]
fn it_copies_a_license_provided_path() {
let fixture = fixture::single_license();
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok());
let crate_license_path = fixture.path.join("LICENSE");
let pkg_license_path = out_dir.join("LICENSE");
println!(
"wasm-pack: should have copied LICENSE from '{}' to '{}'",
crate_license_path.display(),
pkg_license_path.display()
);
assert!(fs::metadata(&crate_license_path).is_ok());
assert!(fs::metadata(&pkg_license_path).is_ok());
let crate_license = utils::file::read_file(&crate_license_path).unwrap();
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap();
assert_eq!(crate_license, pkg_license);
}
#[test]
fn it_copies_all_licenses_default_path() {
let fixture = fixture::dual_license();
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok());
let crate_license_path = fixture.path.join("LICENSE-WTFPL");
let pkg_license_path = out_dir.join("LICENSE-WTFPL");
let crate_license_path_2 = fixture.path.join("LICENSE-MIT");
let pkg_license_path_2 = out_dir.join("LICENSE-MIT");
println!(
"wasm-pack: should have copied LICENSE from '{}' to '{}'",
crate_license_path.display(),
pkg_license_path.display()
);
assert!(fs::metadata(&crate_license_path).is_ok());
assert!(fs::metadata(&pkg_license_path).is_ok());
assert!(fs::metadata(&crate_license_path_2).is_ok());
assert!(fs::metadata(&pkg_license_path_2).is_ok());
let crate_license = utils::file::read_file(&crate_license_path).unwrap();
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap();
assert_eq!(crate_license, pkg_license);
let crate_license_2 = utils::file::read_file(&crate_license_path_2).unwrap();
let pkg_license_2 = utils::file::read_file(&pkg_license_path_2).unwrap();
assert_eq!(crate_license_2, pkg_license_2);
}
#[test]
fn it_copies_all_licenses_provided_path() {
let fixture = fixture::dual_license();
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok());
let crate_license_path = fixture.path.join("LICENSE-WTFPL");
let pkg_license_path = out_dir.join("LICENSE-WTFPL");
let crate_license_path_2 = fixture.path.join("LICENSE-MIT");
let pkg_license_path_2 = out_dir.join("LICENSE-MIT");
println!(
"wasm-pack: should have copied LICENSE from '{}' to '{}'",
crate_license_path.display(),
pkg_license_path.display()
);
assert!(fs::metadata(&crate_license_path).is_ok());
assert!(fs::metadata(&pkg_license_path).is_ok());
assert!(fs::metadata(&crate_license_path_2).is_ok());
assert!(fs::metadata(&pkg_license_path_2).is_ok());
let crate_license = utils::file::read_file(&crate_license_path).unwrap();
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap();
assert_eq!(crate_license, pkg_license);
let crate_license_2 = utils::file::read_file(&crate_license_path_2).unwrap();
let pkg_license_2 = utils::file::read_file(&pkg_license_path_2).unwrap();
assert_eq!(crate_license_2, pkg_license_2);
}
#[test]
fn it_copies_a_non_standard_license_provided_path() {
let license_file = "NON-STANDARD-LICENSE";
let fixture = fixture::non_standard_license(license_file);
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok());
let crate_license_path = fixture.path.join(license_file);
let pkg_license_path = out_dir.join(license_file);
println!(
"wasm-pack: should have copied LICENSE from '{}' to '{}'",
crate_license_path.display(),
pkg_license_path.display()
);
assert!(fs::metadata(&crate_license_path).is_ok());
assert!(fs::metadata(&pkg_license_path).is_ok());
let crate_license = utils::file::read_file(&crate_license_path).unwrap();
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap();
assert_eq!(crate_license, pkg_license);
}

@ -8,7 +8,7 @@ fn it_gets_wasm_bindgen_version() {
fixture.cargo_check();
let data = CrateData::new(&fixture.path).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.21"),);
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
}
#[test]
@ -17,7 +17,7 @@ fn it_gets_wasm_bindgen_test_version() {
fixture.cargo_check();
let data = CrateData::new(&fixture.path).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_test_version(), Some("0.2.21"),);
assert_eq!(lock.wasm_bindgen_test_version(), Some("0.2.37"),);
}
#[test]
@ -46,7 +46,7 @@ fn it_gets_wasm_bindgen_version_in_crate_inside_workspace() {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "=0.2.37"
"#,
)
.file(
@ -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")).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.21"),);
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
}
#[test]
@ -91,7 +91,7 @@ fn it_gets_wasm_bindgen_version_from_dependencies() {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "=0.2.37"
"#,
)
.file(
@ -130,5 +130,5 @@ fn it_gets_wasm_bindgen_version_from_dependencies() {
fixture.cargo_check();
let data = CrateData::new(&fixture.path.join("parent")).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.21"),);
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),);
}

@ -1,17 +1,19 @@
extern crate assert_cmd;
extern crate failure;
extern crate predicates;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
extern crate binary_install;
extern crate serde_json;
#[macro_use]
extern crate slog;
extern crate structopt;
extern crate tempfile;
extern crate wasm_pack;
mod bindgen;
mod build;
mod license;
mod lockfile;
mod manifest;
mod readme;

@ -1,11 +1,10 @@
use assert_cmd::prelude::*;
use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;
use structopt::StructOpt;
use utils::{self, fixture};
use wasm_pack::{self, manifest, Cli};
use wasm_pack::command::build::Target;
use wasm_pack::{self, license, manifest};
#[test]
fn it_gets_the_crate_name_default_path() {
@ -28,8 +27,7 @@ fn it_checks_has_cdylib_default_path() {
// Ensure that there is a `Cargo.lock`.
fixture.cargo_check();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(crate_data.check_crate_config(&step).is_err());
assert!(crate_data.check_crate_config().is_err());
}
#[test]
@ -38,16 +36,14 @@ fn it_checks_has_cdylib_provided_path() {
// Ensure that there is a `Cargo.lock`.
fixture.cargo_check();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
crate_data.check_crate_config(&step).unwrap();
crate_data.check_crate_config().unwrap();
}
#[test]
fn it_checks_has_cdylib_wrong_crate_type() {
let fixture = fixture::bad_cargo_toml();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(crate_data.check_crate_config(&step).is_err());
assert!(crate_data.check_crate_config().is_err());
}
#[test]
@ -56,8 +52,7 @@ fn it_recognizes_a_map_during_depcheck() {
// Ensure that there is a `Cargo.lock`.
fixture.cargo_check();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
crate_data.check_crate_config(&step).unwrap();
crate_data.check_crate_config().unwrap();
}
#[test]
@ -65,10 +60,9 @@ fn it_creates_a_package_json_default_path() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
assert!(crate_data
.write_package_json(&out_dir, &None, false, "", &step)
.write_package_json(&out_dir, &None, false, &Target::Bundler)
.is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
fs::metadata(package_json_path).unwrap();
@ -101,10 +95,9 @@ fn it_creates_a_package_json_provided_path() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
assert!(crate_data
.write_package_json(&out_dir, &None, false, "", &step)
.write_package_json(&out_dir, &None, false, &Target::Bundler)
.is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
fs::metadata(package_json_path).unwrap();
@ -130,10 +123,9 @@ fn it_creates_a_package_json_provided_path_with_scope() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
assert!(crate_data
.write_package_json(&out_dir, &Some("test".to_string()), false, "", &step)
.write_package_json(&out_dir, &Some("test".to_string()), false, &Target::Bundler,)
.is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
fs::metadata(package_json_path).unwrap();
@ -159,10 +151,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
assert!(crate_data
.write_package_json(&out_dir, &None, false, "nodejs", &step)
.write_package_json(&out_dir, &None, false, &Target::Nodejs)
.is_ok());
let package_json_path = &out_dir.join("package.json");
fs::metadata(package_json_path).unwrap();
@ -195,10 +186,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
assert!(crate_data
.write_package_json(&out_dir, &None, false, "no-modules", &step)
.write_package_json(&out_dir, &None, false, &Target::NoModules)
.is_ok());
let package_json_path = &out_dir.join("package.json");
fs::metadata(package_json_path).unwrap();
@ -230,10 +220,9 @@ fn it_creates_a_pkg_json_in_out_dir() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("./custom/out");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
assert!(crate_data
.write_package_json(&out_dir, &None, false, "", &step)
.write_package_json(&out_dir, &None, false, &Target::Bundler)
.is_ok());
let package_json_path = &fixture.path.join(&out_dir).join("package.json");
@ -246,10 +235,9 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
assert!(crate_data
.write_package_json(&out_dir, &None, true, "", &step)
.write_package_json(&out_dir, &None, true, &Target::Bundler)
.is_ok());
let package_json_path = &out_dir.join("package.json");
fs::metadata(package_json_path).unwrap();
@ -275,8 +263,62 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
fn it_errors_when_wasm_bindgen_is_not_declared() {
let fixture = fixture::bad_cargo_toml();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(crate_data.check_crate_config(&step).is_err());
assert!(crate_data.check_crate_config().is_err());
}
#[test]
fn it_sets_homepage_field_if_available_in_cargo_toml() {
// When 'homepage' is available
let fixture = utils::fixture::Fixture::new();
fixture.hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "homepage-field-test"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
homepage = "https://rustwasm.github.io/wasm-pack/"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2"
[dev-dependencies]
wasm-bindgen-test = "=0.2"
"#,
);
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
crate_data
.write_package_json(&out_dir, &None, true, &Target::Bundler)
.unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(
pkg.homepage,
Some("https://rustwasm.github.io/wasm-pack/".to_string()),
);
// When 'homepage' is unavailable
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
crate_data
.write_package_json(&out_dir, &None, true, &Target::Bundler)
.unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.homepage, None);
}
#[test]
@ -285,12 +327,46 @@ fn it_does_not_error_when_wasm_bindgen_is_declared() {
// Ensure that there is a `Cargo.lock`.
fixture.cargo_check();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
crate_data.check_crate_config(&step).unwrap();
crate_data.check_crate_config().unwrap();
}
#[test]
fn configure_wasm_bindgen_debug_incorrectly_is_error() {
let fixture = utils::fixture::Fixture::new();
fixture.readme().hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "whatever"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
debug-js-glue = "not a boolean"
"#,
);
fixture
.wasm_pack()
.arg("build")
.arg("--dev")
.assert()
.failure()
.stderr(predicates::str::contains(
"package.metadata.wasm-pack.profile.dev.wasm-bindgen.debug",
));
}
#[test]
fn parse_crate_data_returns_unused_keys_in_cargo_toml() {
let fixture = utils::fixture::Fixture::new();
fixture
.readme()
@ -311,25 +387,50 @@ fn configure_wasm_bindgen_debug_incorrectly_is_error() {
[dependencies]
wasm-bindgen = "0.2"
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
debug-js-glue = "not a boolean"
# Note: production is not valid.
[package.metadata.wasm-pack.profile.production.wasm-bindgen]
debug-js-glue = true
"#,
)
.hello_world_src_lib();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
"--dev",
&fixture.path.display().to_string(),
])
.unwrap();
let result = fixture.run(cli.cmd);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.iter_chain().any(|c| c
.to_string()
.contains("package.metadata.wasm-pack.profile.dev.wasm-bindgen.debug")));
.hello_world_src_lib()
.install_local_wasm_bindgen();
fixture
.wasm_pack()
.arg("build")
.assert()
.success()
.stderr(predicates::str::contains(
"[WARN]: \"package.metadata.wasm-pack.profile.production\" is an unknown key and will \
be ignored. Please check your Cargo.toml.",
));
}
#[test]
fn it_lists_license_files_in_files_field_of_package_json() {
let fixture = fixture::dual_license();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
license::copy_from_crate(&crate_data, &fixture.path, &out_dir).unwrap();
crate_data
.write_package_json(&out_dir, &None, false, &Target::Bundler)
.unwrap();
let package_json_path = &fixture.path.join("pkg").join("package.json");
fs::metadata(package_json_path).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert!(
pkg.files.contains(&"LICENSE-WTFPL".to_string()),
"LICENSE-WTFPL is not in files: {:?}",
pkg.files,
);
assert!(
pkg.files.contains(&"LICENSE-MIT".to_string()),
"LICENSE-MIT is not in files: {:?}",
pkg.files,
);
}

@ -12,8 +12,7 @@ fn it_copies_a_readme_default_path() {
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let step = wasm_pack::progressbar::Step::new(1);
assert!(readme::copy_from_crate(&fixture.path, &out_dir, &step).is_ok());
assert!(readme::copy_from_crate(&fixture.path, &out_dir).is_ok());
let crate_readme_path = fixture.path.join("README.md");
let pkg_readme_path = out_dir.join("README.md");
@ -37,8 +36,7 @@ fn it_copies_a_readme_provided_path() {
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let step = wasm_pack::progressbar::Step::new(1);
assert!(readme::copy_from_crate(&fixture.path, &out_dir, &step).is_ok());
assert!(readme::copy_from_crate(&fixture.path, &out_dir).is_ok());
let crate_readme_path = fixture.path.join("README.md");
let pkg_readme_path = out_dir.join("README.md");
println!(

@ -1,41 +1,32 @@
use failure::Error;
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::env;
use utils::fixture;
use wasm_pack::command::{build, test, Command};
fn assert_err<T>(result: Result<T, Error>, msg: &str) -> Error {
let error = result.err().expect("should have failed");
for e in error.iter_chain() {
println!("err: {}", e);
}
assert!(error.iter_chain().any(|e| e.to_string().contains(msg)));
error
}
#[test]
fn it_can_run_node_tests() {
let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
node: true,
mode: build::BuildMode::Noinstall,
..Default::default()
});
fixture.run(cmd).unwrap();
let _lock = fixture.lock();
fixture
.wasm_pack()
.arg("test")
.arg("--node")
.assert()
.success();
}
#[test]
fn it_can_run_tests_with_different_wbg_test_and_wbg_versions() {
let fixture = fixture::wbg_test_diff_versions();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
node: true,
mode: build::BuildMode::Noinstall,
..Default::default()
});
fixture.run(cmd).unwrap();
let _lock = fixture.lock();
fixture
.wasm_pack()
.arg("test")
.arg("--node")
.assert()
.success();
}
#[test]
@ -75,33 +66,37 @@ fn it_can_run_browser_tests() {
return;
}
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
firefox,
chrome,
safari,
headless: true,
mode: build::BuildMode::Noinstall,
..Default::default()
});
fixture.run(cmd).unwrap();
let mut cmd = fixture.wasm_pack();
cmd.arg("test").arg("--headless");
if firefox {
cmd.arg("--firefox");
}
if chrome {
cmd.arg("--chrome");
}
if safari {
cmd.arg("--safari");
}
let _lock = fixture.lock();
cmd.assert().success();
}
#[test]
fn it_can_run_failing_tests() {
let fixture = fixture::wbg_test_fail();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
node: true,
mode: build::BuildMode::Noinstall,
..Default::default()
});
assert_err(
fixture.run(cmd),
"Running Wasm tests with wasm-bindgen-test failed",
);
let _lock = fixture.lock();
fixture
.wasm_pack()
.arg("test")
.arg("--node")
.assert()
.failure()
.stderr(predicates::str::contains(
"Running Wasm tests with wasm-bindgen-test failed",
));
}
#[test]
@ -113,8 +108,6 @@ fn it_can_run_failing_tests() {
all(target_os = "windows", target_arch = "x86_64")
))]
fn it_can_find_a_webdriver_on_path() {
use std::process::Command;
let fixture = fixture::wbg_test_browser();
let local_geckodriver = fixture.install_local_geckodriver();
let local_wasm_bindgen = fixture.install_local_wasm_bindgen();
@ -125,54 +118,44 @@ fn it_can_find_a_webdriver_on_path() {
let path = env::join_paths(paths).unwrap();
let _lock = fixture.lock();
let mut me = env::current_exe().unwrap();
me.pop();
me.pop();
me.push("wasm-pack");
let output = Command::new(&me)
fixture
.wasm_pack()
.env("PATH", &path)
.arg("test")
.arg("--firefox")
.arg("--headless")
.arg("--mode")
.arg("no-install")
.env("PATH", &path)
.arg(&fixture.path)
.output()
.unwrap();
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
println!("status: {}", output.status);
assert!(output.status.success());
.assert()
.success();
}
#[test]
fn it_requires_node_or_a_browser() {
let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
mode: build::BuildMode::Noinstall,
// Note: not setting node or any browser to true here.
..Default::default()
});
assert_err(fixture.run(cmd), "Must specify at least one of");
let _lock = fixture.lock();
fixture
.wasm_pack()
.arg("test")
.assert()
.failure()
.stderr(predicates::str::contains("Must specify at least one of"));
}
#[test]
fn the_headless_flag_requires_a_browser() {
let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
node: true,
mode: build::BuildMode::Noinstall,
headless: true,
..Default::default()
});
assert_err(fixture.run(cmd), "only applies to browser tests");
let _lock = fixture.lock();
fixture
.wasm_pack()
.arg("test")
.arg("--node")
.arg("--headless")
.assert()
.failure()
.stderr(predicates::str::contains("only applies to browser tests"));
}
#[test]
@ -195,7 +178,7 @@ fn complains_about_missing_wasm_bindgen_test_dependency() {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
[dev-dependencies]
# no wasm-bindgen-test dep here!
@ -203,44 +186,18 @@ fn complains_about_missing_wasm_bindgen_test_dependency() {
)
.hello_world_src_lib()
.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
node: true,
mode: build::BuildMode::Noinstall,
..Default::default()
});
let error = assert_err(fixture.run(cmd), "Ensure that you have");
// Test that the error message has two occurrences of "wasm-bindgen-test" in
// it. I am surprised to learn there is no `str` method to count
// occurrences, so we find the first and the last and assert that they
// aren't the same occurrence.
//
// This should protect against regresstions where we said:
//
// Ensure that you have "wasm-bindgen" as a dependency in your Cargo.toml file:
// [dev-dependencies]
// wasm-bindgen-test = "0.2"
//
// instead of
//
// Ensure that you have "wasm-bindgen-test" as a dependency in your Cargo.toml file:
// [dev-dependencies]
// wasm-bindgen-test = "0.2"
//
// Note that the whole reason we are doing this string manipulation instead
// of just doing `assert_eq!` is because the first occurrence of the
// dependency name is bolded with terminal escape codes and if I try to pipe
// the output to a text file, then the escape codes go away, so I can't
// figure out which exact escape codes are even used here.
let err_msg = error.to_string();
let first = err_msg.find("wasm-bindgen-test");
assert!(first.is_some());
let second = err_msg.rfind("wasm-bindgen-test");
assert!(second.is_some());
assert_ne!(first, second, "should have found two occurrences");
let _lock = fixture.lock();
fixture
.wasm_pack()
.arg("test")
.arg("--node")
.assert()
.failure()
.stderr(predicates::str::contains(
"Ensure that you have \"wasm-bindgen-test\" as a dependency in your Cargo.toml file",
))
.stderr(predicates::str::contains("[dev-dependencies]"))
.stderr(predicates::str::contains("wasm-bindgen-test = \"0.2\""));
}
#[test]
@ -261,10 +218,10 @@ fn renamed_crate_name_works() {
name = 'bar'
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
wasm-bindgen-test = "0.2"
"#,
)
.file(
@ -276,15 +233,15 @@ fn renamed_crate_name_works() {
#[wasm_bindgen]
pub fn one() -> u32 { 1 }
"#,
);
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
node: true,
mode: build::BuildMode::Noinstall,
..Default::default()
});
fixture.run(cmd).unwrap();
)
.install_local_wasm_bindgen();
let _lock = fixture.lock();
fixture
.wasm_pack()
.arg("test")
.arg("--node")
.assert()
.success();
}
#[test]
@ -301,10 +258,10 @@ fn cdylib_not_required() {
authors = []
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
wasm-bindgen-test = "0.2"
"#,
)
.file(
@ -324,13 +281,59 @@ fn cdylib_not_required() {
foo::foo();
}
"#,
);
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
node: true,
mode: build::BuildMode::Noinstall,
..Default::default()
});
fixture.run(cmd).unwrap();
)
.install_local_wasm_bindgen();
let _lock = fixture.lock();
fixture
.wasm_pack()
.arg("test")
.arg("--node")
.assert()
.success();
}
#[test]
fn test_output_is_printed_once_in_both_stdout_and_failures() {
let fixture = fixture::Fixture::new();
fixture
.readme()
.cargo_toml("test-output-printed-once")
.hello_world_src_lib()
.file(
"tests/node.rs",
r#"
extern crate wasm_bindgen;
extern crate wasm_bindgen_test;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen_test]
fn yabba() {
log("YABBA DABBA DOO");
assert_eq!(1, 2);
}
"#,
)
.install_local_wasm_bindgen();
let _lock = fixture.lock();
// there will be only one log in stdout, and only one log in failures
let log_cnt = 1;
fixture
.wasm_pack()
.arg("test")
.arg("--node")
.assert()
.failure()
.stdout(predicate::function(|out: &str| {
// but the out string will capture both stdout and failures,
// so we will get a log that count twice
out.matches("YABBA DABBA DOO").count() == log_cnt * 2
}));
}

@ -1,4 +1,4 @@
use super::logger::null_logger;
use binary_install::Cache;
use std::env;
use std::fs;
use std::mem::ManuallyDrop;
@ -8,7 +8,6 @@ use std::sync::{MutexGuard, Once, ONCE_INIT};
use std::thread;
use tempfile::TempDir;
use wasm_pack;
use wasm_pack::binaries::Cache;
/// A test fixture in a temporary directory.
pub struct Fixture {
@ -68,6 +67,54 @@ impl Fixture {
)
}
/// Add `LICENSE` file to the fixture.
pub fn license(&self) -> &Self {
self.file(
"LICENSE",
r#"
I'm a license!
"#,
)
}
/// Add `WTFPL LICENSE` file to the fixture.
pub fn wtfpl_license(&self) -> &Self {
self.file(
"LICENSE-WTFPL",
r#"
DO WHATEVER YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHATEVER YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHATEVER YOU WANT TO.
"#,
)
}
/// Add `MIT LICENSE` file to the fixture.
pub fn mit_license(&self) -> &Self {
self.file(
"LICENSE-MIT",
r#"
Copyright <YEAR> <COPYRIGHT HOLDER>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"#,
)
}
/// Add a `Cargo.toml` with a correctly configured `wasm-bindgen`
/// dependency, `wasm-bindgen-test` dev-dependency, and `crate-type =
/// ["cdylib"]`.
@ -89,13 +136,51 @@ impl Fixture {
[lib]
crate-type = ["cdylib"]
[dependencies]
# Note that this uses and `=` dependency because there are
# various tests which assert that the version of wasm
# 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"
[dev-dependencies]
wasm-bindgen-test = "0.2"
"#,
name
),
)
}
/// Add a `Cargo.toml` with a correctly configured `wasm-bindgen`
/// dependency, `wasm-bindgen-test` dev-dependency, and `crate-type =
/// ["cdylib"]`.
///
/// `name` is the crate's name.
/// `license_file` is license file path
pub fn cargo_toml_with_license_file(&self, name: &str, license_file: &str) -> &Self {
self.file(
"Cargo.toml",
&format!(
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
name = "{}"
license-file = "{}"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
"#,
name
name, license_file
),
)
}
@ -131,8 +216,7 @@ impl Fixture {
pub fn install_local_wasm_bindgen(&self) -> PathBuf {
static INSTALL_WASM_BINDGEN: Once = ONCE_INIT;
let cache = self.cache();
let version = "0.2.21";
let log = &null_logger();
let version = "0.2.37";
let download = || {
if let Ok(download) =
@ -141,7 +225,7 @@ impl Fixture {
return Ok(download);
}
wasm_pack::bindgen::cargo_install_wasm_bindgen(log, &cache, version, true)
wasm_pack::bindgen::cargo_install_wasm_bindgen(&cache, version, true)
};
// Only one thread can perform the actual download, and then afterwards
@ -149,7 +233,7 @@ impl Fixture {
INSTALL_WASM_BINDGEN.call_once(|| {
download().unwrap();
});
download().unwrap().binary("wasm-bindgen")
download().unwrap().binary("wasm-bindgen").unwrap()
}
/// Download `geckodriver` and return its path.
@ -182,9 +266,14 @@ impl Fixture {
wasm_pack::test::webdriver::install_chromedriver(&cache, true).unwrap()
}
pub fn cache_dir(&self) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("test_cache")
}
pub fn cache(&self) -> Cache {
let target_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("target");
Cache::at(&target_dir.join("test_cache"))
Cache::at(&self.cache_dir())
}
/// The `step_install_wasm_bindgen` and `step_run_wasm_bindgen` steps only
@ -204,22 +293,14 @@ impl Fixture {
self
}
pub fn run(&self, cmd: wasm_pack::command::Command) -> Result<(), failure::Error> {
let logger = wasm_pack::logger::new(&cmd, 3)?;
match cmd {
wasm_pack::command::Command::Test(cmd) => {
let _lock = self.lock();
let mut test = wasm_pack::command::test::Test::try_from_opts(cmd)?;
test.set_cache(self.cache());
test.run(&logger)
}
wasm_pack::command::Command::Build(cmd) => {
let mut build = wasm_pack::command::build::Build::try_from_opts(cmd)?;
build.set_cache(self.cache());
build.run(&logger)
}
_ => unreachable!(),
}
/// Get a `wasm-pack` command configured to run in this fixure's temp
/// directory and using the test cache.
pub fn wasm_pack(&self) -> Command {
use assert_cmd::prelude::*;
let mut cmd = Command::main_binary().unwrap();
cmd.current_dir(&self.path);
cmd.env("WASM_PACK_CACHE", self.cache_dir());
cmd
}
pub fn lock(&self) -> MutexGuard<'static, ()> {
@ -285,10 +366,10 @@ pub fn no_cdylib() -> Fixture {
# crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
wasm-bindgen-test = "0.2"
"#,
);
fixture
@ -337,14 +418,14 @@ pub fn wbg_test_diff_versions() -> Fixture {
crate-type = ["cdylib", "rlib"]
[dependencies]
# We depend on wasm-bindgen 0.2.21
wasm-bindgen = "=0.2.21"
# We depend on the latest wasm-bindgen 0.2
wasm-bindgen = "0.2"
[dev-dependencies]
# And we depend on wasm-bindgen-test 0.2.19. This should still
# work, and we should end up with `wasm-bindgen` at 0.2.21 and
# wasm-bindgen-test at 0.2.19, and everything should still work.
wasm-bindgen-test = "0.2.19"
# And we depend on wasm-bindgen-test 0.2.29. This should still
# work, and we should end up with the latest `wasm-bindgen` and
# wasm-bindgen-test at 0.2.29, and everything should still work.
wasm-bindgen-test = "0.2.29"
"#,
)
.file(
@ -456,12 +537,12 @@ pub fn transitive_dependencies() -> Fixture {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
project_a = { path = "../project_a" }
project_b = { path = "../project_b" }
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
wasm-bindgen-test = "0.2"
"#,
);
fixture.file(
@ -506,11 +587,11 @@ pub fn transitive_dependencies() -> Fixture {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
project_b = { path = "../project_b" }
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
wasm-bindgen-test = "0.2"
"#,
);
fixture.file(
@ -556,10 +637,10 @@ pub fn transitive_dependencies() -> Fixture {
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
wasm-bindgen = "0.2"
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
wasm-bindgen-test = "0.2"
"#,
);
fixture.file(
@ -590,3 +671,34 @@ pub fn transitive_dependencies() -> Fixture {
project_main_fixture(&mut fixture);
fixture
}
pub fn single_license() -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.cargo_toml("single_license")
.license()
.hello_world_src_lib();
fixture
}
pub fn dual_license() -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.cargo_toml("dual_license")
.wtfpl_license()
.mit_license()
.hello_world_src_lib();
fixture
}
pub fn non_standard_license(license_file: &str) -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.cargo_toml_with_license_file("dual_license", license_file)
.file(license_file, "license file for test")
.hello_world_src_lib();
fixture
}

@ -1,6 +0,0 @@
use slog::Logger;
// Create a logger that ignores log messages for testing.
pub fn null_logger() -> Logger {
Logger::root(slog::Discard, o!())
}

@ -23,6 +23,7 @@ pub struct NpmPackage {
pub types: String,
#[serde(default = "default_none", rename = "sideEffects")]
pub side_effects: String,
pub homepage: Option<String>,
}
fn default_none() -> String {

@ -1,4 +1,3 @@
pub mod file;
pub mod fixture;
pub mod logger;
pub mod manifest;

@ -1,12 +1,13 @@
use binary_install::Cache;
use utils::fixture;
use wasm_pack::binaries::Cache;
use wasm_pack::test::webdriver;
#[test]
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "x86_64"),
all(target_os = "windows", target_arch = "x86")
all(target_os = "windows", target_arch = "x86"),
all(target_os = "windows", target_arch = "x86_64")
))]
fn can_install_chromedriver() {
let fixture = fixture::js_hello_world();

Loading…
Cancel
Save