Git Product home page Git Product logo

esbuild-plugin-wat's Introduction

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

// esbuild.config.js // Or:
// esbuild.config.mjs // Use if you get a set "type": "module" error
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

esbuild-plugin-wat's People

Contributors

benkoshy avatar mitschabaude avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

esbuild-plugin-wat's Issues

Is 0.2.5 yarn only?

Have been using v0.2.3 via npm quite happily. Noticed an upgrade to 0.2.5 but it gives the following error using npm i:

npm ERR! code EUNSUPPORTEDPROTOCOL
npm ERR! Unsupported URL Type "link:": link:.yalc/@webassemblyjs/ast

I'm using npm 7.21.1

error: cannot use import statement outside a module?

I'm getting an annoying error. Am I doing something obviously wrong? Any pointers would be much appreciated.

$ node ./esbuild.config.js --watch
(node:3948) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/koshy/Documents/www/BensRails/mto/esbuild.config.js:3
import watPlugin from 'esbuild-plugin-wat';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47
error Command failed with exit code 1.
// esbuild.config.js
const path = require('path')
const esbuild = require('esbuild')
import watPlugin from 'esbuild-plugin-wat';

esbuild.build({
  entryPoints: ['./application.js'],
  bundle: true,
  outdir: path.join(process.cwd(), "app/assets/builds"),
  absWorkingDir: path.join(process.cwd(), "app/javascript"),
  watch: process.argv.includes("--watch"),
  sourcemap: true,
  mainFields: ['module', 'main'],
  plugins: [
    watPlugin() // options are documented below
  ],
}).catch(e => (console.error(e), process.exit(1)))

My package.json

{
  "name": "app",
  "private": "true",
  "dependencies": {
    "@hotwired/stimulus": "3.0.1",
    "@hotwired/turbo-rails": "7.1.1",
    "@tailwindcss/forms": "0.5.1",
    "autoprefixer": "10.4.5",
    "esbuild": "0.14.38",
    "esbuild-plugin-wat": "0.2.7",
    "postcss": "8.4.12",
    "tailwindcss": "3.0.24",
    "tailwindcss-stimulus-components": "3.0.4",
    "three": "0.141.0",
    "web-ifc-three": "0.0.117"
  },
  "scripts": {
    "build": "node ./esbuild.config.js",
    "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.