Skip to content

Latest commit

 

History

History

wasm

@farmfe/plugin-wasm

A farm plugin for importing Wasm modules.

Installation

npm i -D @farmfe/plugin-wasm

Usage

Create a farm.config.js configuration file and import the plugin:

import { defineConfig } from '@farmfe/core';
import wasm from '@farmfe/plugin-wasm';
export default defineConfig({
  plugins: [
    wasm(),
  ],
});

WebAssembly

Pre-compiled .wasm files can be imported using the ?init query. The default export is an initialization function, which returns a Promise resolving to a WebAssembly.Instance object:

import init from './example.wasm?init';
init().then((instance) => {
  instance.exports.test();
});

The init function can also take an import object as its second parameter, which is passed to WebAssembly.Instance:

import init from './example.wasm?init';
init({
  imports: {
    someFunc: () => {
      /* ... */
    },
  },
}).then(() => {
  /* ... */
});

In production builds, .wasm files smaller than the assetInlineLimit will be inlined as base64 strings. Otherwise, they will be treated as static assets and fetched on demand.