Skip to content

Commit 703ea90

Browse files
committed
fix: convert the examples to ES modules
1 parent 8966965 commit 703ea90

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed

examples/quickstart/browser-puppeteer.js renamed to examples/quickstart/browser-puppeteer.mjs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// Use pupeteer to run in the browser
2-
const puppeteer = require("puppeteer");
2+
import puppeteer from "puppeteer";
33

4-
// Require rollup to compile our browser.js
5-
const rollup = require("rollup");
6-
const { nodeResolve } = require("@rollup/plugin-node-resolve");
4+
// Import rollup to compile our browser.js
5+
import { rollup } from "rollup";
6+
import { nodeResolve } from "@rollup/plugin-node-resolve";
7+
import desm from "desm";
78

89
// Get some native node libs, in order to host a static server
9-
const path = require("path");
10-
const fs = require("fs");
11-
const http = require("http");
10+
import path from "path";
11+
import fs from "fs";
12+
import http from "http";
13+
14+
const __dirname = desm(import.meta.url);
1215

1316
// Host a static server of the local directory
1417
// https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/
@@ -28,16 +31,16 @@ http
2831

2932
(async () => {
3033
// Create a rollup bundle and get our compiled browser.js as a string
31-
const bundle = await rollup.rollup({
32-
input: "./browser.js",
34+
const bundle = await rollup({
35+
input: `${__dirname}/browser.mjs`,
3336
plugins: [nodeResolve()]
3437
});
3538
const { output } = await bundle.generate({
3639
format: "iife"
3740
});
3841
const browserQuickstartJs = output[0].code;
3942

40-
// Launch the pupeteer browser and page
43+
console.log("Launching the pupeteer browser and page...");
4144
const browser = await puppeteer.launch();
4245
const page = await browser.newPage();
4346
await page.goto("http://localhost:8000/browser.html");

examples/quickstart/browser.js renamed to examples/quickstart/browser.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ const asyncTask = async () => {
2121
asBindInstance.exports.myExportedFunctionThatTakesAString("Hello World!");
2222
console.log(response); // AsBind: Hello World!
2323
};
24-
asyncTask();
24+
asyncTask().catch(e => {
25+
throw e;
26+
});
File renamed without changes.

examples/quickstart/nodejs.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// We need to import the direct as-bind.cjs.js for Node applications.
2+
// This is because the default "main" key in the `package.json`,
3+
// is the as-bind transform script
4+
import AsBind from "as-bind/dist/as-bind.cjs.js";
5+
import fs from "fs";
6+
7+
const wasm = fs.readFileSync("./path-to-my-wasm.wasm");
8+
9+
const asyncTask = async () => {
10+
const asBindInstance = await AsBind.instantiate(wasm);
11+
12+
// You can now use your wasm / as-bind instance!
13+
const response =
14+
asBindInstance.exports.myExportedFunctionThatTakesAString("Hello World!");
15+
console.log(response); // AsBind: Hello World!
16+
};
17+
asyncTask();

examples/quickstart/package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/quickstart/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"name": "as-bind-quickstart",
33
"version": "0.0.1",
44
"description": "Project that tests the README QuickStart examples to ensure that the README works. This depends on node modules from the root package.json",
5-
"main": "index.js",
65
"scripts": {
76
"build": "asc your-entryfile.ts --exportRuntime --transform as-bind -b path-to-my-wasm.wasm",
87
"test": "npm run build && npm run test:node && npm run test:browser",
9-
"test:node": "node nodejs.js",
10-
"test:browser": "node browser-puppeteer.js"
8+
"test:node": "node nodejs.mjs",
9+
"test:node:cjs": "node nodejs.cjs",
10+
"test:browser": "node browser-puppeteer.mjs"
1111
},
1212
"author": "Aaron Turner",
1313
"license": "MIT",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"assemblyscript": "~0.19.8",
6060
"chokidar-cli": "^2.0.0",
6161
"cpy-cli": "^3.1.1",
62+
"desm": "^1.3.0",
6263
"express": "^4.17.1",
6364
"gh-pages": "^2.1.1",
6465
"glob": "^7.1.6",

0 commit comments

Comments
 (0)