How to update tree-sitter
Tree-sitter is packaged in a way that makes it difficult to load it up with wds
, it is packaged as a CommonJS module (modifying a module.exports
object) and as such does not play well with es modules.
The simplest solution I have found is to turn the CommonJS module into an ES module by adding the following at the end of the file:
await TreeSitter.init({
locateFile(scriptName, scriptDirectory) {
return "/assets/tree-sitter.wasm";
}
});
export const Parser = TreeSitter;
export const Language = TreeSitter.Language;
This also calls init
once, with the proper location of the wasm file.
So to update tree-sitter
:
- run
npm install --no-save web-tree-sitter
(check if that’s the correct version) cp node_modules/web-tree-sitter/tree-sitter.wasm assets/
cp node_modules/web-tree-sitter/tree-sitter-web.d.ts ./tree-sitter.d.ts
- edit
tree-sitter.d.ts
:
- remove the
declare module
scope - remove the
export = Parser
at the end - add
export
before class Parser
- add
export
before namespace Parser
cp node_modules/web-tree-sitter/tree-sitter.js ./
-
cat << 'EOF' >> tree-sitter.js
await TreeSitter.init({
locateFile(scriptName, scriptDirectory) {
return "/assets/tree-sitter.wasm";
}
});
export const Parser = TreeSitter;
export const Language = TreeSitter.Language;
EOF