npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

esbuild-plugin-wat

v0.2.7

Published

esbuild plugin for importing WebAssembly files

Downloads

221

Readme

esbuild-plugin-wat

This is a plugin for esbuild which allows you to import .wasm (WebAssembly) and .wat (WebAssembly text format) files.

Both files types will resolve in a default export which is a Uint8Array holding the Wasm binary. It can be directly passed into WebAssembly.instantiate() or WebAssembly.compile().

yarn add esbuild-plugin-wat

Example:

import exampleWasm from 'example.wat';
let {instance} = await WebAssembly.instantiate(exampleWasm);

The bundle produced by esbuild will look similar to this:

var example_default = __toBinary('AGFzbQEAAAABDAJgAn9/AGA...'); // <-- Wasm binary gets inlined as base64
var {instance} = await WebAssembly.instantiate(example_default);

.wat is converted to .wasm with wabt (and cached, for performance).

Usage

import {build} from 'esbuild';
import watPlugin from 'esbuild-plugin-wat';

build({
  /* ... */
  plugins: [watPlugin()],
});

Optionally, you can pass a configuration object which currently supports three options:

watPlugin({
  loader: 'file', // how esbuild should load the .wasm file. Default: 'binary'
  inlineFunctions: true, // optimize wasm by inlining all functions. Default: false
  wasmFeatures: {simd: false}, // selectively disable wasm features
  bundle: true // experimental: bundle multiple wat files using import syntax. Default: false
});

The loader option directly translates into choosing an esbuild loader for the .wasm file. For example, instead of "binary" (the default) you could use "base64" if you want to do the base64-decoding yourself, or "file" if you don't want to inline the binary and rather want to fetch it from a separate file.

If inlineFunctions is true, we use binaryen to inline all Wasm functions. This is the only binary optimization so far that I identified as useful when writing raw .wat. If you compile to Wasm from a different language, you will most likely have your own optimization pipeline and you can ignore this option.

The wasmFeatures are passed to parseWat() from wabt.js, see WasmFeatures in the wabt API. Contrary to wabt.js, we enable all features by default (since supporting additional language features at compile time is unlikely to break code not using that feature), so passing true for a feature does nothing and passing false disables it.

Bundling

The experimental bundle option allows you to combine multiple wasm/wat files into one. This is achieved with the following import syntax:

;; main.wat
(import "./util.wat" "some_function" (func $f (param i32) (result i32)))

The statement above tells our bundler to look for the file "./util.wat" at a path relative to main.wat, take the code for "some_function" and include it into our bundled wat (the import statement is removed).

Note that this is normal, spec-compliant wasm syntax, which requires two strings to specify an import. The original intended usage is to specify imports that are dynamically supplied by the host runtime. However, here we instead statically link the wasm modules together.

This enables two things:

  • Split code into multiple files when writing raw wat
  • When importing .wasm, allows to pack code compiled from different languages into one wasm binary