Skip to content

Commit f8a34ec

Browse files
committed
feat: fix local dev so it falls back to server
Previously it would always require the WASM module, requiring a full WASM compile cycle.
1 parent 6827057 commit f8a34ec

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

cmd/chromad/static/chroma.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// chroma.js - TinyGo WASM runtime initialization for Chroma syntax highlighter
22

3-
// Import wasm_exec.js so that it initialises the Go WASM runtime.
4-
import "./wasm_exec.js";
5-
63
class ChromaWASM {
74
constructor() {
85
this.go = null;
@@ -13,7 +10,7 @@ class ChromaWASM {
1310

1411
async init() {
1512
try {
16-
// Create a new Go instance
13+
// Create a new Go instance (wasm_exec.js already imported in initChroma)
1714
const go = new Go();
1815
WebAssembly.instantiateStreaming(fetch("./static/chroma.wasm"), go.importObject).then((result) => {
1916
go.run(result.instance);
@@ -70,5 +67,18 @@ export function isWasmSupported() {
7067
return false;
7168
}
7269

73-
// Create global instance, null if WASM is not supported.
74-
export const chroma = isWasmSupported() ? new ChromaWASM() : null;
70+
async function initChroma() {
71+
if (!isWasmSupported()) {
72+
return null;
73+
}
74+
75+
try {
76+
await import("./wasm_exec.js");
77+
return new ChromaWASM();
78+
} catch (error) {
79+
return null;
80+
}
81+
}
82+
83+
// Create global instance, null if WASM is not supported or file doesn't exist.
84+
export const chroma = await initChroma();

cmd/chromad/static/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Base64 from "./base64.js";
22
import { chroma } from "./chroma.js";
33

4-
document.addEventListener("DOMContentLoaded", () => {
4+
function init() {
55
const darkMode =
66
window.matchMedia?.("(prefers-color-scheme: dark)").matches;
77
const style = document.createElement("style");
@@ -25,8 +25,7 @@ document.addEventListener("DOMContentLoaded", () => {
2525
});
2626

2727
async function renderServer(formData) {
28-
return (
29-
await fetch("api/render", {
28+
const response = await fetch("/api/render", {
3029
method: "POST",
3130
mode: "cors",
3231
cache: "no-cache",
@@ -38,8 +37,8 @@ document.addEventListener("DOMContentLoaded", () => {
3837
redirect: "follow",
3938
referrer: "no-referrer",
4039
body: JSON.stringify(formData),
41-
})
42-
).json();
40+
});
41+
return await response.json();
4342
}
4443

4544
async function renderWasm(formData) {
@@ -196,6 +195,8 @@ document.addEventListener("DOMContentLoaded", () => {
196195
} else if (darkMode) {
197196
styleSelect.value = "monokai";
198197
update(new Event("change"));
198+
} else {
199+
update(new Event("change"));
199200
}
200201

201202
const eventHandler = (event) => update(event);
@@ -210,5 +211,11 @@ document.addEventListener("DOMContentLoaded", () => {
210211
copyButton.addEventListener("click", share);
211212

212213
textArea.addEventListener("keydown", handleTab);
213-
textArea.addEventListener("change", debouncedEventHandler);
214-
});
214+
textArea.addEventListener("input", debouncedEventHandler);
215+
}
216+
217+
if (document.readyState === "loading") {
218+
document.addEventListener("DOMContentLoaded", init);
219+
} else {
220+
init();
221+
}

0 commit comments

Comments
 (0)