curlconverter / curlconverter

Transpile curl commands into Python, JavaScript and 27 other languages
https://curlconverter.com
MIT License
7.21k stars 876 forks source link

Is there a way to pass locateFile fn on Parser.init #659

Open agniokas opened 4 weeks ago

agniokas commented 4 weeks ago

There is a problem, when trying to host curlconverter in nextjs project, which is aknowleged by tree-sitter. It also has a solution (see below), however, i am not able to use it, i assume because, when curl converter initializes Parser, it overrides locateFunction. For the moment, i relocate tree-sitter.wasm files to each page in .next/static/chunk/pages, but i would like a nicer solution to this. Is there a way?

For server side frameworks like NextJS, this can be tricky as pages are often served from a path such as http://localhost:3000/_next/static/chunks/pages/index.js. The loader will therefore look for the wasm file at http://localhost:3000/_next/static/chunks/pages/tree-sitter.wasm. The solution is to pass a locateFile function in the moduleOptions argument to Parser.init():

await Parser.init({
  locateFile(scriptName: string, scriptDirectory: string) {
    return scriptName;
  },
});
verhovsky commented 4 weeks ago

You'll have to make a personal fork of curlconverter, sorry. I want to keep the interface simple.

Not exactly what you're asking, I but I tried making it look for the wasm files in a relative directory #649 but that doesn't work for https://curlconverter.com/ because you can open it like https://curlconverter.com/go/ (for example), which requests the wasm from https://curlconverter.com/go/tree-sitter.wasm which wouldn't work, unless we copy the wasm to each directory but I don't want to duplicate it like that...

verhovsky commented 4 weeks ago

Another alternative would be replacing tree-sitter with https://github.com/withfig/lezer-bash