esbuild-plugin-wasm-pack

raw JSON →
1.1.0 verified Fri May 01 auth: no javascript

esbuild-plugin-wasm-pack is an esbuild plugin that runs wasm-pack before each build to compile Rust/Wasm crates. Version 1.1.0 is the latest stable release. It integrates esbuild with the wasm-pack toolchain, automatically generating WebAssembly modules during the bundling process. Key differentiators include seamless integration with esbuild's plugin system, TypeScript support via generated .d.ts files, and full configuration parity with wasm-pack build arguments. It requires Node.js >=0.10.0, esbuild >=0.11.15, Rust/Cargo, and wasm-pack to be installed. The plugin is actively maintained on GitHub and distributed via npm.

error Error: spawn wasm-pack ENOENT
cause wasm-pack is not installed or not in PATH.
fix
Install wasm-pack globally: 'cargo install wasm-pack'. Alternatively, set the 'wasmPackPath' option or WASM_PACK_PATH environment variable to the executable's path.
error TypeError: wasmpack is not a function
cause Incorrect import style; the plugin exports a function, not an object.
fix
Use 'import wasmpack from "esbuild-plugin-wasm-pack"' (default import) or 'const wasmpack = require("esbuild-plugin-wasm-pack")'. Do not destructure.
error wasm-pack build failed with exit code 101
cause wasm-pack encountered an error while building the Rust crate.
fix
Check the Rust crate for compilation errors. Ensure all dependencies are present. Run 'wasm-pack build' in the crate directory to see detailed output.
gotcha wasm-pack must be installed and available in PATH, otherwise the plugin will fail silently or throw an obscure error.
fix Ensure wasm-pack is installed globally (e.g., cargo install wasm-pack) and verify with 'which wasm-pack' or set WASM_PACK_PATH environment variable.
gotcha The plugin runs wasm-pack synchronously via execSync, which can block the event loop during builds.
fix Consider using wasm-pack programmatically or running it as a separate build step for large crates.
breaking As of version 1.0.0, the plugin changed from a class-based API to a function-based API. Users upgrading from 0.x may need to adjust their usage.
fix Use the functional import: import wasmpack from 'esbuild-plugin-wasm-pack' and call it as a function with options.
deprecated The 'node' engine requirement is >=0.10.0, which is extremely outdated. There is a risk of using unsupported Node.js versions.
fix Use Node.js >=12 (or current LTS) to ensure compatibility with esbuild and modern JavaScript features.
gotcha The plugin does not validate that the specified 'path' contains a valid Cargo.toml. Misconfiguration leads to confusing errors.
fix Double-check the 'path' option points to a directory with a Cargo.toml file.
npm install esbuild-plugin-wasm-pack
yarn add esbuild-plugin-wasm-pack
pnpm add esbuild-plugin-wasm-pack

Demonstrates how to use esbuild-plugin-wasm-pack to build a Rust WebAssembly crate during an esbuild build.

import esbuild from 'esbuild';
import wasmpack from 'esbuild-plugin-wasm-pack';

await esbuild.build({
  entryPoints: ['src/index.ts'],
  bundle: true,
  outfile: 'dist/bundle.js',
  plugins: [
    wasmpack({
      path: './rust-crate',   // path to your Rust crate
      target: 'bundler',
      profile: 'release',
      logLevel: 'warn',
    }),
  ],
});