tree-sitter / node-tree-sitter

Node.js bindings for tree-sitter
https://www.npmjs.com/package/tree-sitter
MIT License
649 stars 114 forks source link

Support compiling single-file executables via `bun build --compile` #230

Closed Jarred-Sumner closed 6 days ago

Jarred-Sumner commented 1 week ago

This unblocks using tree-sitter in single-file executables

node-gyp-build performs runtime checks for figuring out where the .node file is, but those runtime checks are difficult for static analysis to figure out where the .node file is. If we can rely on the prebuild to exist, we can use a template string and Bun will inline the process.platform, process.arch, and process.versions.bun fields within --compile.

bun build --compile --format=cjs ./foo.js

Where foo.js is:

const Parser = require('tree-sitter');
const JavaScript = require('tree-sitter-javascript');

const parser = new Parser();
parser.setLanguage(JavaScript);

const sourceCode = 'let x = 1; console.log(x);';
const tree = parser.parse(sourceCode);

console.log(tree);
rm -rf node_modules
./foo

Related PRs:

amaanq commented 6 days ago

thanks!

ObserverOfTime commented 6 days ago

If we can rely on the prebuild to exist

If we could, we wouldn't need node-gyp-build in the first place.