esbuild-wasm: WebAssembly Bundler

0.28.0 · active · verified Sun Apr 19

esbuild-wasm is the WebAssembly-based distribution of esbuild, a high-performance JavaScript and TypeScript bundler and minifier. It provides the core bundling and transformation capabilities of esbuild in a cross-platform WebAssembly binary, suitable for environments like web browsers, Deno, and Bun, or Node.js where a native binary might not be ideal or available. The current stable version is v0.28.0. The project maintains a rapid release cadence, often introducing new features and bug fixes, and sometimes includes deliberate backwards-incompatible changes, as highlighted in releases like v0.27.0. Its key differentiator from the primary `esbuild` package is its WebAssembly implementation, offering broader environment compatibility, though it might have slight performance variations compared to the native binary. Users should be aware of the explicit `initialize` step required for its operation.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the esbuild-wasm service and performing a basic TypeScript transformation with minification and sourcemap generation.

import { initialize, transform } from 'esbuild-wasm';

async function runWasmEsbuild() {
  // esbuild-wasm requires initialization to load the WebAssembly binary.
  // This should only be called once. For Node.js, setting 'worker: true'
  // and explicitly providing wasmURL is robust for predictable loading.
  await initialize({
    worker: true,
    wasmURL: 'https://unpkg.com/esbuild-wasm@0.28.0/esbuild.wasm' // Specify a CDN URL for predictability
  });

  const sourceCode = `
    import { someHelper } from './utils';
    const message: string = "Hello, esbuild-wasm!";
    console.log(message + someHelper());

    function someHelper() {
      return " from a helper!";
    }
  `;

  console.log('Original code:\n', sourceCode);

  // Transform TypeScript code to JavaScript, minifying it.
  const result = await transform(sourceCode, {
    loader: 'ts',
    minify: true,
    sourcemap: true,
    target: 'es2018',
  });

  console.log('\nTransformed code:');
  console.log(result.code);
  console.log('\nSourcemap:');
  console.log(result.map);

  // When using worker: true, the worker process typically exits with the main process.
  // Explicit 'stop' is usually not needed unless managing a long-lived service manually.
}

runWasmEsbuild().catch(console.error);

view raw JSON →