|
| 1 | +# wasm-pack build |
| 2 | + |
| 3 | +The `wasm-pack build` command creates the files neccessary for JavaScript |
| 4 | +interoperability and for publishing a package to npm. This involves compiling |
| 5 | +your code to wasm and generating a pkg folder. This pkg folder will contain the |
| 6 | +wasm binary, a JS wrapper file, your `README`, and a `package.json` file. |
| 7 | + |
| 8 | +## Path |
| 9 | + |
| 10 | +The `wasm-pack build` command can be given an optional path argument, e.g.: |
| 11 | + |
| 12 | +``` |
| 13 | +wasm-pack build examples/js-hello-world |
| 14 | +``` |
| 15 | + |
| 16 | +This path should point to a directory that contains a `Cargo.toml` file. If no |
| 17 | +path is given, the `build` command will run in the current directory. |
| 18 | + |
| 19 | +## Debug |
| 20 | + |
| 21 | +The init command accepts an optional `--debug` argument. This will build the |
| 22 | +output package using cargo's |
| 23 | +[default non-release profile][cargo-profile-sections-documentation]. Building |
| 24 | +this way is faster but applies few optimizations to the output, and enables |
| 25 | +debug assertions and other runtime correctness checks. |
| 26 | + |
| 27 | +The exact meaning of this flag may evolve as the platform matures. |
| 28 | + |
| 29 | +[cargo-profile-sections-documentation]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections |
| 30 | + |
| 31 | +## Target |
| 32 | + |
| 33 | +The `build` command accepts a `--target` argument. This will customize the output files |
| 34 | +to align with a particular type of JS module. This allows wasm-pack to generate either |
| 35 | +ES6 modules or CommonJS modules for use in browser and in NodeJS. Defaults to `browser`. |
| 36 | +The options are: |
| 37 | + |
| 38 | +``` |
| 39 | +wasm-pack build --target nodejs |
| 40 | +``` |
| 41 | + |
| 42 | +| Option | Description | |
| 43 | +|-----------|-----------------------------------------------------------------------------------------------------------------| |
| 44 | +| `nodejs` | Outputs JS that uses CommonJS modules, for use with a `require` statement. | |
| 45 | +| `browser` | Outputs JS that uses ES6 modules, primarily for use with `import` statements and/or bundlers such as `webpack`. | |
| 46 | + |
| 47 | +## Scope |
| 48 | + |
| 49 | +The init command also accepts an optional `--scope` argument. This will scope |
| 50 | +your package name, which is useful if your package name might conflict with |
| 51 | +something in the public registry. For example: |
| 52 | + |
| 53 | +``` |
| 54 | +wasm-pack build examples/js-hello-world --scope test |
| 55 | +``` |
| 56 | + |
| 57 | +This command would create a `package.json` file for a package called |
| 58 | +`@test/js-hello-world`. For more information about scoping, you can refer to |
| 59 | +the npm documentation [here][npm-scope-documentation]. |
| 60 | + |
| 61 | +[npm-scope-documentation]: https://docs.npmjs.com/misc/scope |
| 62 | + |
| 63 | +## Mode |
| 64 | + |
| 65 | +The `build` command accepts an optional `--mode` argument. |
| 66 | +``` |
| 67 | +wasm-pack build examples/js-hello-world --mode no-install |
| 68 | +``` |
| 69 | + |
| 70 | +| Option | Description | |
| 71 | +|---------------|------------------------------------------------------------------------------------------| |
| 72 | +| `no-install` | `wasm-pack init` implicitly and create wasm binding without installing `wasm-bindgen`. | |
| 73 | +| `normal` | do all the stuffs of `no-install` with installed `wasm-bindgen`. | |
0 commit comments