fix: use globalThis instead of window for non-browser compatibility#150
fix: use globalThis instead of window for non-browser compatibility#150fasterthanlime merged 2 commits intomainfrom
Conversation
…148) Replace all references to `window.arboriumHost` with `globalThis.arboriumHost` so the host interface works in Node.js, SSR, and Web Worker environments where `window` is not defined.
Allows pre-loading grammar modules without CDN resolution, fixing grammar loading in non-browser environments. Fixes #149
|
Sorry to bother, but was this PR written using LLMs? There doesn’t seem to be any sort of indication in the repo of agent usage, and it was not disclosed anywhere. |
Yes, large parts of arborium were co-developed with LLMs. Adding to my TODO to add disclosures to, uh, too many projects. I know some folks want to stay away completely — where would you expect such a disclosure? Bottom of README ok? |
|
Added: 2a3bb06 |
|
Btw.:
I figured, you’re usually less “move fast and break things”. I’m not complaining, since
I think the only think I could ask for is tests to avoid regressions like #122, which I think also exist now. |
|
The snippet above doesn't work in Deno, because:
Also it still won't work on Node because wasm-bindgen uses Here's a version which works in Deno: import { registerGrammar } from '@arborium/arborium';
import * as pythonGrammar from '@arborium/python';
import { readFile } from 'node:fs/promises';
import { dirname } from 'node:path';
const py = import.meta.resolve('@arborium/python/grammar_bg.wasm');
const hostUrl = dirname(import.meta.resolve('@arborium/arborium'));
const wasm = await readFile(new URL(py));
const grammar = await registerGrammar(pythonGrammar, wasm, { hostUrl });
const html = await grammar.highlight("print('hello')");
console.log({ html });Making this work in Node would require some additional work, probably most straightforwardly by having |
|
As said, |
|
@flying-sheep As I said, |
|
Oof, sorry! this time I was the one who didn’t read properly. |
Summary
windowwithglobalThisin the ESM loader soarboriumHostworks in Node.js, Deno, and workers (not just browsers)registerGrammar()API that lets users pre-load grammar modules, bypassing CDN/dynamic-import resolution entirelyregisterGrammarusageTest plan
globalThis === windowin browsers)registerGrammarworks in Deno withnpm:specifiersFixes #148
Fixes #149