Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vite-plugin-wasm-esm

omnysecurity334MIT1.0.3TypeScript support: included

ESM integration of wasm-pack generated modules with SSR support

vite, plugin, vite-plugin, wasm, webassembly, wasm-pack, esm, es-modules, modules

readme

vite-plugin-wasm-esm

Vite plugin with SSR support to use wasm-pack generated packages as regular ES modules.

Installation

Install using your favorite package manager

npm i -D vite-plugin-wasm-esm

Usage

To use this plugin, you must support await at module top level. This has been supported in all major browsers for a while, but if you need backwards compatibility, you can use vite-plugin-top-level-await.

After installation, add it to your vite config and specify all packages you want the plugin to handle:

// vite.config.js
import wasm from "vite-plugin-wasm-esm";

/** @type {import('vite').UserConfig} */
const config = {
    plugins: [wasm(["@acme/wasm-calculator"])],
};

export default config;

You probably also need to specify custom build targets, as the default targets provided by vite doesn't support await at the module top level:

// vite.config.js
import wasm from "vite-plugin-wasm-esm";

/** @type {import('vite').UserConfig} */
const config = {
    plugins: [wasm(["@acme/wasm-calculator"])],
    build: {
        target: ["chrome89", "safari15", "firefox89"],
    },
    esbuild: {
        target: ["chrome89", "safari15", "firefox89"],
    },
};

export default config;

All wasm packages handled by the plugin can be used as regular ES modules, both on the server during SSR and in the browser:

import { plus } from "@acme/wasm-calculator";

console.log(plus(1, 1)); // 3

changelog

vite-plugin-wasm-esm

1.0.3

Patch Changes

  • 599b3f4: bump dependencies

1.0.2

Patch Changes

  • c996cdd: bump dependencies

1.0.1

Patch Changes

  • aa9ead4: chore(deps): update dependencies

1.0.0

Major Changes

  • initial release