Skip to content

Commit 8ef8902

Browse files
add iife style script for casual use
1 parent 194f4a1 commit 8ef8902

File tree

5 files changed

+79
-8
lines changed

5 files changed

+79
-8
lines changed

packages/npm-packages/ruby-wasm-wasi/package-lock.json

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

packages/npm-packages/ruby-wasm-wasi/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727
"format": "prettier --write ."
2828
},
2929
"devDependencies": {
30+
"@rollup/plugin-json": "^4.1.0",
3031
"@rollup/plugin-node-resolve": "^13.1.3",
3132
"@rollup/plugin-typescript": "^8.3.0",
3233
"@types/jest": "^27.4.0",
34+
"@wasmer/wasi": "^0.12.0",
35+
"@wasmer/wasmfs": "^0.12.0",
3336
"prettier": "^2.5.1",
3437
"rollup": "^2.62.0",
3538
"ts-jest": "^27.1.2",
36-
"typescript": "^4.5.4",
37-
"@wasmer/wasi": "^0.12.0",
38-
"@wasmer/wasmfs": "^0.12.0"
39+
"typescript": "^4.5.4"
3940
},
4041
"dependencies": {
4142
"tslib": "^2.3.1"

packages/npm-packages/ruby-wasm-wasi/rollup.config.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import typescript from "@rollup/plugin-typescript";
22
import { nodeResolve } from "@rollup/plugin-node-resolve";
3+
import json from "@rollup/plugin-json";
4+
5+
const typescriptOptions = { tsconfig: "./tsconfig.json", declaration: false, exclude: ["src/browser.script.ts"] }
36

47
function variant(basename) {
58
return {
@@ -22,8 +25,9 @@ function variant(basename) {
2225
},
2326
],
2427
plugins: [
25-
typescript({ tsconfig: "./tsconfig.json", declaration: false }),
28+
typescript(typescriptOptions),
2629
nodeResolve(),
30+
json(),
2731
],
2832
};
2933
}
@@ -32,6 +36,20 @@ function variant(basename) {
3236
export default [
3337
variant("index"),
3438
variant("browser"),
39+
{
40+
input: "src/browser.script.ts",
41+
output: [
42+
{
43+
file: "dist/browser.script.js",
44+
format: "iife"
45+
}
46+
],
47+
plugins: [
48+
typescript(typescriptOptions),
49+
nodeResolve(),
50+
json(),
51+
],
52+
},
3553
{
3654
input: `src/node.ts`,
3755
output: [
@@ -47,7 +65,7 @@ export default [
4765
},
4866
],
4967
plugins: [
50-
typescript({ tsconfig: "./tsconfig.json", declaration: false }),
68+
typescript(typescriptOptions),
5169
nodeResolve(),
5270
],
5371
external: ["wasi"],
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { DefaultRubyVM } from "./browser";
2+
import * as pkg from "../package.json";
3+
4+
const main = async () => {
5+
const response = await fetch(
6+
`https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@${pkg.version}/dist/ruby.wasm`
7+
);
8+
const buffer = await response.arrayBuffer();
9+
const module = await WebAssembly.compile(buffer);
10+
const { vm } = await DefaultRubyVM(module);
11+
12+
vm.printVersion();
13+
14+
runRubyScriptsInHtml(vm);
15+
};
16+
17+
const runRubyScriptsInHtml = (vm) => {
18+
const tags = document.getElementsByTagName("script");
19+
for (var i = 0, len = tags.length; i < len; i++) {
20+
const tag = tags[i];
21+
if (tag.type === "text/ruby") {
22+
if (tag.innerHTML) {
23+
vm.eval(tag.innerHTML);
24+
}
25+
}
26+
}
27+
};
28+
29+
main();

packages/npm-packages/ruby-wasm-wasi/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"outDir": "./dist",
77
"target": "es2017",
88
"esModuleInterop": true,
9-
"moduleResolution": "node"
9+
"moduleResolution": "node",
10+
"resolveJsonModule": true
1011
},
1112
"include": ["src/**/*.ts", "./src/bindgen/*"],
1213
"exclude": ["node_modules", "dist"]

0 commit comments

Comments
 (0)