MDN self-hosted library · e01.org
import source / notebook

import source

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
const myModuleSource = await WebAssembly.compileStreaming(
  fetch("./my-module.wasm"),
);
js
import source myModuleSource from "./my-module.wasm";
js
import source myModuleSource from "./my-module.wasm";

const instance = await WebAssembly.instantiate(myModuleSource, {
  env: { log: console.log },
});
const { exports } = instance;
js
import * as namespace from "./my-module.js";

// Is equivalent to:

import source myModuleSource from "./my-module.js";

const namespace = await import(myModuleSource);
js
import * as x from "foo";
import source xSource from "foo";

console.log((await import(xSource)) === x); // true
js
import * as mod from "./module.js";
import text from "./module.js" with { type: "text" };
js
import source counterSource from "./counter.wasm";

const first = await WebAssembly.instantiate(counterSource, {
  env: { initial: 0 },
});
const second = await WebAssembly.instantiate(counterSource, {
  env: { initial: 100 },
});

console.log(first.exports.increment()); // 1
console.log(first.exports.increment()); // 2
console.log(second.exports.increment()); // 101
js
// -- sources.js --
import source counterSource from "./counter.wasm";

export { counterSource };
js
// -- main.js --
import { counterSource } from "./sources.js";

const instance = new WebAssembly.Instance(counterSource, {
  env: { initial: 0 },
});