← 返回命令列表

Linux command

wasm-pack 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Build Rust project for WebAssembly

wasm-pack build

Build for npm publishing

wasm-pack build --target bundler

Build for web browser

wasm-pack build --target web

Build for Node.js

wasm-pack build --target nodejs

Build with custom output directory

wasm-pack build --out-dir [dist]

Build in development mode

wasm-pack build --dev

Publish to npm

wasm-pack publish

Create new project from template

wasm-pack new [project-name]

说明

wasm-pack is a build tool for Rust-generated WebAssembly. It compiles Rust code to WebAssembly, generates JavaScript bindings using wasm-bindgen, and produces npm-ready packages. The build process compiles Rust to the wasm32-unknown-unknown target, runs wasm-bindgen to generate JavaScript glue code, and optionally runs wasm-opt for optimization. The output includes .wasm files, TypeScript definitions, and package.json for npm. Different targets generate different JavaScript module formats: bundler for webpack/rollup, web for ES modules in browsers, nodejs for CommonJS, and no-modules for script tags. Install via cargo: `cargo install wasm-pack`

参数

--target _target_
Output target: bundler (default), web, nodejs, deno, no-modules.
--out-dir _dir_
Output directory (default: pkg).
--out-name _name_
Output file basename.
--dev
Development build with debug info.
--profiling
Release build with debug info.
--release
Optimized release build (default).
--scope _scope_
npm scope for publishing.
--mode _mode_
Build mode: normal, no-install, force.
--verbose
Enable verbose output.
--quiet
Suppress output.
-h, --help
Display help.
-V, --version
Display version.

FAQ

What is the wasm-pack command used for?

wasm-pack is a build tool for Rust-generated WebAssembly. It compiles Rust code to WebAssembly, generates JavaScript bindings using wasm-bindgen, and produces npm-ready packages. The build process compiles Rust to the wasm32-unknown-unknown target, runs wasm-bindgen to generate JavaScript glue code, and optionally runs wasm-opt for optimization. The output includes .wasm files, TypeScript definitions, and package.json for npm. Different targets generate different JavaScript module formats: bundler for webpack/rollup, web for ES modules in browsers, nodejs for CommonJS, and no-modules for script tags. Install via cargo: `cargo install wasm-pack`

How do I run a basic wasm-pack example?

Run `wasm-pack build` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --target _target_ do in wasm-pack?

Output target: bundler (default), web, nodejs, deno, no-modules.