Skip to content

Commit

Permalink
GH-152: Use ES-module import syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
CMDJojo committed Mar 12, 2023
1 parent 234ca3f commit 18a4bfb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion playground/build-playground.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rm -r build
mkdir build
cp -r static/* build
wasm-pack build ./web_bindings --target no-modules --out-dir ../build/pkg
wasm-pack build ./web_bindings --target web --out-dir ../build/pkg
25 changes: 11 additions & 14 deletions playground/static/compiler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// FIXME: Replace with a normal ES module import once Firefox adds support for js modules in web workers
importScripts("./pkg/web_bindings.js");
// noinspection JSFileReferences
import init, {ast, ast_debug, json_output, package_info, transpile} from "./pkg/web_bindings.js";

let loaded = false;
wasm_bindgen("./pkg/web_bindings_bg.wasm").then(() => {
init().then(() => {
loaded = true;
postMessage({ type: "init" });
});
postMessage({type: "init"})
})

onmessage = (event) => {
if (!loaded) return;
Expand All @@ -14,27 +14,24 @@ onmessage = (event) => {
let result;
switch (event.data.type) {
case "ast":
result = wasm_bindgen.ast(event.data.source);
result = ast(event.data.source);
break;
case "ast_debug":
result = wasm_bindgen.ast_debug(event.data.source);
result = ast_debug(event.data.source);
break;
case "json_output":
result = wasm_bindgen.json_output(event.data.source);
result = json_output(event.data.source);
break;
case "package_info":
result = wasm_bindgen.package_info(event.data.source);
result = package_info(event.data.source);
break;
case "transpile":
result = wasm_bindgen.transpile(event.data.source, event.data.format);
result = transpile(event.data.source, event.data.format);
break;

case "package_info":
result = wasm_bindgen.package_info();
}
postMessage({ result: result, success: true, ...event.data });
} catch (error) {
console.log(error);
postMessage({ error: error, success: false, ...event.data })
}
};
};
2 changes: 1 addition & 1 deletion playground/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<script src="ace/ace.js" type="text/javascript" charset="utf-8"></script>
<title>ModMark Playground</title>
<link rel="stylesheet" href="style.css">
<script type="module" defer src="main.js"></script>
<script type="module" defer src="main.mjs"></script>
</head>

<body>
Expand Down
4 changes: 2 additions & 2 deletions playground/static/main.js → playground/static/main.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let seq = 0;
let compiler_callback;
let compiler_failure;
let compiler = new Worker("./compiler.js");
let compiler = new Worker("./compiler.js", {type: "module"});

compiler.onmessage = (event) => {
// Render the document once the wasm module containing the
Expand All @@ -11,7 +11,7 @@ compiler.onmessage = (event) => {
return;
}

if (event.data.seq != seq) return;
if (event.data.seq !== seq) return;
if (event.data.success) {
compiler_callback(event.data.result);
} else {
Expand Down

0 comments on commit 18a4bfb

Please sign in to comment.