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.
build command accepts an optional profile argument: one of
--release. If none is supplied, then
--release is used.
Th 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 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
The options are:
wasm-pack build --target nodejs
| || Outputs JS that uses CommonJS modules, for use with a |
| || Outputs JS that use no modules. |
| || Outputs JS that uses ES6 modules, primarily for use with |
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. ↩