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
pkg directory is automatically
.gitignored by default, since it contains
build artifacts which are not intended to be checked into version
wasm-pack build command can be given an optional path argument, e.g.:
wasm-pack build examples/js-hello-world
This path should point to a directory that contains a
Cargo.toml file. If no
path is given, the
build command will run in the current directory.
wasm-pack will generate a directory for it's build output called
If you'd like to customize this you can use the
wasm-pack build --out-dir out
The above command will put your build artifacts in a directory called
of the default
--out-name sets the prefix for output file names. If not provided, package name is used instead.
Usage examples, assuming our crate is named
wasm-pack build # will produce files # dom.d.ts dom.js dom_bg.d.ts dom_bg.wasm package.json README.md wasm-pack build --out-name index # will produce files # index.d.ts index.js index_bg.d.ts index_bg.wasm package.json README.md
build command accepts an optional profile argument: one of
--release. If none is supplied, then
--release is used.
This controls whether debug assertions are enabled, debug info is generated, and which (if any) optimizations are enabled.
|Profile||Debug Assertions||Debug Info||Optimizations||Notes|
|Yes||Yes||No||Useful for development and debugging.|
|No||Yes||Yes||Useful when profiling and investigating performance issues.|
|No||No||Yes||Useful for shipping to production.|
--dev profile will build the output package using cargo's default
non-release profile. Building this way is
faster but applies few optimizations to the output, and enables debug assertions
and other runtime correctness checks. The
use cargo's release profile, but the former enables debug info as well, which
helps when investigating performance issues in a profiler.
The exact meaning of the profile flags may evolve as the platform matures.
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.
wasm-pack build --target nodejs
|not specified or ||Bundler||Outputs JS that is suitable for interoperation with a Bundler like Webpack. You'll |
|Node.js||Outputs JS that uses CommonJS modules, for use with a |
|Native in browser||Outputs JS that can be natively imported as an ES module in a browser, but the WebAssembly must be manually instantiated and loaded.|
|Native in browser||Same as |
The init command also accepts an optional
--scope argument. This will scope
your package name, which is useful if your package name might conflict with
something in the public registry. For example:
wasm-pack build examples/js-hello-world --scope test
This command would create a
package.json file for a package called
@test/js-hello-world. For more information about scoping, you can refer to
the npm documentation here.
build command accepts an optional
wasm-pack build examples/js-hello-world --mode no-install
|do all the stuffs of |
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
0 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. ↩