-
Notifications
You must be signed in to change notification settings - Fork 914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vite and WASM files #661
Comments
Check the Network tab of the devtools to see what file path is actually getting requested and the actual data being sent. I bet it's requesting localhost:8888/tree-sitter.wasm and getting an HTML 404 page, which is obviously not a valid WASM file. Check the layout of the build directory (does Vite have something like that?) to see where the actual file is placed, it looks like it'll be localhost:8888/public/tree-sitter.wasm. curlconverter needs both Wasm files to be served from the root of your website, like localhost:8888/tree-sitter.wasm and localhost:8888/tree-sitter-bash.wasm |
The fact that wasm files aren't just import foo from 'foo.wasm' which could be manipulated by all the javascript tools that already work with importing javascript and instead are const foo = fetch('/some/url/foo.wasm').then(f => WebAssembly.instantiate(f)) (which also makes the object async, so technically all the javascript functions that ever use it have to be async, but that's "solved" with a top-level really sucks. Like all the open issues on this repo are either this problem or requests for generating code in various other languages. I would like to replace it with a JavaScript-based parser at some point. |
I confirmed that files placed in the public directory are successfully served at the project's root. However, in my network tab, I can see a single tree-sitter.wasm request, that's falsely HTTP 200, as it is sent to http://localhost:1420/**index.html/**tree-sitter.wasm for the reason uknown: and (correctly) responds with my index.html file: Do you have any idea why does it send an /index.html-prefixed request? |
It's 200 because Vite and other single-page app frameworks handle the URL in JavaScript, so it just always returns index.html which contains some js that then determines that the user should be shown a 404 page after all. To make it load /tree-sitter.wasm instead of /index.html/tree-sitter.wasm , you could fork curlconverter and then change this curlconverter/src/shell/webParser.ts Line 7 in cf2aab4
to await Parser.init({
locateFile(scriptName: string, scriptDirectory: string) {
return scriptName;
},
}); https://github.com/tree-sitter/tree-sitter/tree/master/lib/binding_web#running-wasm-in-browser |
Seems like a sane default for this package tho, doesn't it? You already require it to be at the server root, so it only makes sense to actually ignore the |
guess so. |
So it turns out that tree-sitter (Emscripten-compiled Wasm in general) loads tree-sitter.wasm relative to the directory of the JavaScript file that loads it, not relative to the HTML file that includes the JavaScript that loads it, like this So right now curlconverter requests these two URLs // actually some older code possibly, as well as more complex logic that checks if it's running in a workeror a blob
scriptDirectory = document.currentScript.src;
scriptDirectory = scriptDirectory.substr(0, scriptDirectory.replace(/[?#].*/, "").lastIndexOf("/") + 1);
scriptDirectory + 'tree-sitter.wasm';
'/tree-sitter-bash.wasm'; We could change it to (as you suggest and as our documentation claims it works)
or we could do what I tried to do in #649 properly
|
Where are your JavaScript files being loaded from, |
Go to the Sources tab, find web-tree-sitter/tree-sitter.js , unminify it ( function locateFile(e) {
return Module.locateFile ? Module.locateFile(e, scriptDirectory) : scriptDirectory + e
} and check the value of Then we need to understand why the value of (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && (ENVIRONMENT_IS_WORKER ? scriptDirectory = self.location.href : void 0 !== document && document.currentScript && (scriptDirectory = document.currentScript.src);
scriptDirectory = 0 !== scriptDirectory.indexOf("blob:") ? scriptDirectory.substr(0, scriptDirectory.replace(/[?#].*/, "").lastIndexOf("/") + 1) : ""; and debugging it. I'm guessing that it's running in a web worker, so it's doing |
Let me know if 4.10.0 doesn't fix your issue |
It's not using
https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript So my idea of using |
I'd love an example on how to use the package with Vite - can't get it quite right as of yet. Here's what I've tried so far:
vite.config.ts
But I'm not sure what else I need to do. In my React code, I do the following
The errors I get when trying to it
any help/advice would be much appreciated - thanks!
The text was updated successfully, but these errors were encountered: