Skip to content

Commit

Permalink
docs: Update tree-sitter
Browse files Browse the repository at this point in the history
Updated web-tree-sitter and the devicetree grammar.

web-tree-sitter now supports a custom function to locate its .wasm file,
so performing a string replace is no longer necessary to get it to work
with Docusaurus' Webpack configuration. We now check when tree-sitter is
locating its .wasm file and provide the Webpack-adjusted URL.
  • Loading branch information
joelspadin authored and petejohanson committed May 20, 2023
1 parent c6bf95a commit dae0207
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 39 deletions.
14 changes: 7 additions & 7 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"react-copy-to-clipboard": "^5.0.3",
"react-dom": "^17.0.2",
"react-toastify": "^7.0.4",
"web-tree-sitter": "^0.19.4"
"web-tree-sitter": "^0.20.8"
},
"browserslist": {
"production": [
Expand Down
30 changes: 0 additions & 30 deletions docs/src/docusaurus-tree-sitter-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,6 @@ module.exports = function () {
test: /web-tree-sitter/,
loader: "null-loader",
});
} else {
// The way web-tree-sitter loads tree-sitter.wasm isn't something that
// Docusaurus/Webpack identify as an asset. There is currently no way to
// set location of the file other than patching web-tree-sitter.
// (see https://github.com/tree-sitter/tree-sitter/issues/559)
rules.push({
test: /tree-sitter\.js$/,
loader: "string-replace-loader",
options: {
multiple: [
// Replace the path to tree-sitter.wasm with a "new URL()" to clue
// Webpack in that it is an asset.
{
search: '"tree-sitter.wasm"',
replace: '(new URL("tree-sitter.wasm", import.meta.url)).href',
strict: true,
},
// Webpack replaces "new URL()" with the full URL to the asset, but
// web-tree-sitter will still add a prefix to it unless there is a
// Module.locateFile() function.
{
search: "var Module=void 0!==Module?Module:{};",
replace: `var Module = {
locateFile: (path, prefix) => path.startsWith('http') ? path : prefix + path,
};`,
strict: true,
},
],
},
});
}

return {
Expand Down
15 changes: 14 additions & 1 deletion docs/src/keymap-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ import Parser from "web-tree-sitter";

import { Codes, Behaviors } from "./data/keymap-upgrade";

const TREE_SITTER_WASM_URL = new URL(
"/node_modules/web-tree-sitter/tree-sitter.wasm",
import.meta.url
);

let Devicetree;

export async function initParser() {
await Parser.init();
await Parser.init({
locateFile: (path, prefix) => {
// When locating tree-sitter.wasm, use a path that Webpack can map to the correct URL.
if (path == "tree-sitter.wasm") {
return TREE_SITTER_WASM_URL.href;
}
return prefix + path;
},
});
Devicetree = await Parser.Language.load("/tree-sitter-devicetree.wasm");
}

Expand Down
Binary file modified docs/static/tree-sitter-devicetree.wasm
Binary file not shown.

0 comments on commit dae0207

Please sign in to comment.