{"name":"web-tree-sitter","version":"0.26.9","keywords":["incremental","parsing","tree-sitter","wasm"],"author":{"name":"Max Brunsfeld","email":"maxbrunsfeld@gmail.com"},"license":"MIT","_id":"web-tree-sitter@0.26.9","maintainers":[{"name":"ahlinc","email":"ahlincq@gmail.com"},{"name":"maxbrunsfeld","email":"maxbrunsfeld@gmail.com"},{"name":"amaanq","email":"amaanq12@gmail.com"},{"name":"tclem","email":"timothy.clem@gmail.com"},{"name":"rewinfrey","email":"rick.winfrey@gmail.com"},{"name":"robrix","email":"rob.rix@github.com"},{"name":"patrickthomson","email":"patrickt@github.com"},{"name":"dcreager1847","email":"dcreager@dcreager.net"}],"homepage":"https://github.com/tree-sitter/tree-sitter#readme","bugs":{"url":"https://github.com/tree-sitter/tree-sitter/issues"},"dist":{"shasum":"9e44cb876c68082a2129ee8aee20ee8b702d286b","tarball":"http://registry-npm.ywwl.com/web-tree-sitter/download/web-tree-sitter-0.26.9.tgz","fileCount":19,"integrity":"sha512-YJwSHANl6XFgeEjB8nitgj0qZYt5gkIesJ4w2srS2wcLB4GUa4xcOkM0YaMsU6WNR53YVIkDSY7Ej4pf3IXtCA==","signatures":[{"sig":"MEUCIQCjdR4R7HhGf3D9cMNz8UqGsoG6WDF2/5A1noRguijX9AIgZc5wgowQwoqOeGXaygTH+hfn1k7Y2Rx5ocNDo0/Z57E=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/web-tree-sitter@0.26.9","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":4561862,"size":1128993,"noattachment":false,"key":"/web-tree-sitter/-/web-tree-sitter-0.26.9.tgz"},"type":"module","types":"web-tree-sitter.d.ts","exports":{".":{"import":{"types":"./web-tree-sitter.d.ts","default":"./web-tree-sitter.js"},"require":{"types":"./web-tree-sitter.d.cts","default":"./web-tree-sitter.cjs"}},"./debug":{"import":{"types":"./web-tree-sitter.d.ts","default":"./debug/web-tree-sitter.js"},"require":{"types":"./web-tree-sitter.d.cts","default":"./debug/web-tree-sitter.cjs"}},"./web-tree-sitter.wasm":"./web-tree-sitter.wasm","./debug/web-tree-sitter.wasm":"./debug/web-tree-sitter.wasm"},"gitHead":"7f534862c3ec939c3a6ee147f7600ef5c1bf900f","scripts":{"lint":"eslint src/*.ts script/*.ts test/*.ts","test":"vitest run","build":"npm run build:wasm && npm run build:ts","prepack":"cp ../../LICENSE .","build:ts":"tsc -b . && node script/build.js","lint:fix":"eslint src/*.ts script/*.ts test/*.ts --fix","postpack":"rm LICENSE","build:dts":"node script/generate-dts.js","build:wasm":"cd ../../ && cargo xtask build-wasm","test:watch":"vitest","build:debug":"npm run build:wasm:debug && npm run build:ts -- --debug","prepublishOnly":"tsx script/check-artifacts-fresh.ts","build:wasm:debug":"cd ../../ && cargo xtask build-wasm --debug"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:e1d0b66b-55ae-406d-a46d-00daf328c3ac"}},"repository":{"url":"git+https://github.com/tree-sitter/tree-sitter.git","type":"git","directory":"lib/binding_web"},"_npmVersion":"11.12.1","description":"Tree-sitter bindings for the web","directories":{},"_nodeVersion":"24.15.0","_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","eslint":"^9.39.1","vitest":"^3.0.5","esbuild":"^0.27.1","dts-buddy":"^0.6.2","@eslint/js":"^9.39.1","source-map":"^0.7.4","typescript":"^5.7.3","@types/node":"^24.10.1","@types/emscripten":"^1.41.5","typescript-eslint":"^8.48.1","@vitest/coverage-v8":"^3.0.5"},"_npmOperationalInternal":{"tmp":"tmp/web-tree-sitter_0.26.9_1779214365951_0.21886442907599313","host":"s3://npm-registry-packages-npm-production"},"readme":"# Web Tree-sitter\n\n[![npmjs.com badge]][npmjs.com]\n\n[npmjs.com]: https://www.npmjs.org/package/web-tree-sitter\n[npmjs.com badge]: https://img.shields.io/npm/v/web-tree-sitter.svg?color=%23BF4A4A\n\nWebAssembly bindings to the [Tree-sitter](https://github.com/tree-sitter/tree-sitter) parsing library.\n\n## Setup\n\nYou can download the `web-tree-sitter.js` and `web-tree-sitter.wasm` files from [the latest GitHub release][gh release] and load\nthem using a standalone script:\n\n```html\n<script src=\"/the/path/to/web-tree-sitter.js\"></script>\n\n<script>\n  const { Parser } = window.TreeSitter;\n  Parser.init().then(() => { /* the library is ready */ });\n</script>\n```\n\nYou can also install [the `web-tree-sitter` module][npm module] from NPM and load it using a system like Webpack:\n\n```js\nconst { Parser } = require('web-tree-sitter');\nParser.init().then(() => { /* the library is ready */ });\n```\n\nor Vite:\n\n```js\nimport { Parser }  from 'web-tree-sitter';\nParser.init().then(() => { /* the library is ready */ });\n```\n\nWith Vite, you also need to make sure your server provides the `tree-sitter.wasm`\nfile to your `public` directory. You can do this automatically with a `postinstall`\n[script](https://docs.npmjs.com/cli/v10/using-npm/scripts) in your `package.json`:\n\n```js\n\"postinstall\": \"cp node_modules/web-tree-sitter/tree-sitter.wasm public\"\n```\n\nYou can also use this module with [deno](https://deno.land/):\n\n```js\nimport { Parser } from \"npm:web-tree-sitter\";\nawait Parser.init();\n// the library is ready\n```\n\nTo use the debug version of the library, replace your import of `web-tree-sitter` with `web-tree-sitter/debug`:\n\n```js\nimport { Parser } from 'web-tree-sitter/debug'; // or require('web-tree-sitter/debug')\n\nParser.init().then(() => { /* the library is ready */ });\n```\n\nThis will load the debug version of the `.js` and `.wasm` file, which includes debug symbols and assertions.\n\n> [!NOTE]\n> The `web-tree-sitter.js` file on GH releases is an ES6 module. If you are interested in using a pure CommonJS library, such\n> as for Electron, you should use the `web-tree-sitter.cjs` file instead.\n\n### Basic Usage\n\nFirst, create a parser:\n\n```js\nconst parser = new Parser();\n```\n\nThen assign a language to the parser. Tree-sitter languages are packaged as individual `.wasm` files (more on this below):\n\n```js\nconst { Language } = require('web-tree-sitter');\nconst JavaScript = await Language.load('/path/to/tree-sitter-javascript.wasm');\nparser.setLanguage(JavaScript);\n```\n\nNow you can parse source code:\n\n```js\nconst sourceCode = 'let x = 1; console.log(x);';\nconst tree = parser.parse(sourceCode);\n```\n\nand inspect the syntax tree.\n\n```javascript\nconsole.log(tree.rootNode.toString());\n\n// (program\n//   (lexical_declaration\n//     (variable_declarator (identifier) (number)))\n//   (expression_statement\n//     (call_expression\n//       (member_expression (identifier) (property_identifier))\n//       (arguments (identifier)))))\n\nconst callExpression = tree.rootNode.child(1).firstChild;\nconsole.log(callExpression);\n\n// { type: 'call_expression',\n//   startPosition: {row: 0, column: 16},\n//   endPosition: {row: 0, column: 30},\n//   startIndex: 0,\n//   endIndex: 30 }\n```\n\n### Editing\n\nIf your source code *changes*, you can update the syntax tree. This will take less time than the first parse.\n\n```javascript\n// Replace 'let' with 'const'\nconst newSourceCode = 'const x = 1; console.log(x);';\n\ntree.edit({\n  startIndex: 0,\n  oldEndIndex: 3,\n  newEndIndex: 5,\n  startPosition: {row: 0, column: 0},\n  oldEndPosition: {row: 0, column: 3},\n  newEndPosition: {row: 0, column: 5},\n});\n\nconst newTree = parser.parse(newSourceCode, tree);\n```\n\n### Parsing Text From a Custom Data Structure\n\nIf your text is stored in a data structure other than a single string, you can parse it by supplying a callback to `parse`\ninstead of a string:\n\n```javascript\nconst sourceLines = [\n  'let x = 1;',\n  'console.log(x);'\n];\n\nconst tree = parser.parse((index, position) => {\n  let line = sourceLines[position.row];\n  if (line) return line.slice(position.column);\n});\n```\n\n### Getting the `.wasm` language files\n\nThere are several options on how to get the `.wasm` files for the languages you want to parse.\n\n#### From npmjs.com\n\nThe recommended way is to just install the package from npm. For example, to parse JavaScript, you can install the `tree-sitter-javascript`\npackage:\n\n```sh\nnpm install tree-sitter-javascript\n```\n\nThen you can find the `.wasm` file in the `node_modules/tree-sitter-javascript` directory.\n\n#### From GitHub\n\nYou can also download the `.wasm` files from GitHub releases, so long as the repository uses our reusable workflow to publish\nthem.\nFor example, you can download the JavaScript `.wasm` file from the tree-sitter-javascript [releases page][gh release js].\n\n#### Generating `.wasm` files\n\nYou can also generate the `.wasm` file for your desired grammar. Shown below is an example of how to generate the `.wasm`\nfile for the JavaScript grammar.\n\n> [!NOTE]\n> Since v0.26.1, `tree-sitter build --wasm` uses [wasi-sdk][] and will automatically download it on first use.\n> No additional tools need to be installed.\n\nFirst install `tree-sitter-cli`, and the tree-sitter language for which to generate `.wasm`\n(`tree-sitter-javascript` in this example):\n\n```sh\nnpm install --save-dev tree-sitter-cli tree-sitter-javascript\n```\n\nThen just use tree-sitter cli tool to generate the `.wasm`.\n\n```sh\nnpx tree-sitter build --wasm node_modules/tree-sitter-javascript\n```\n\nIf everything is fine, file `tree-sitter-javascript.wasm` should be generated in current directory.\n\n### Wasm compatibility\n\n`web-tree-sitter` supports the same parser ABI versions as the corresponding tree-sitter library:\n\n| web-tree-sitter version | Min parser ABI version | Max parser ABI version |\n|-------------------------|------------------------|------------------------|\n| 0.24.x                  | 13                     | 14                     |\n| >= 0.25.0               | 13                     | 15                     |\n\n> [!WARNING]\n> Some prebuilt `.wasm` files use an older dynamic-linking format that newer versions of `web-tree-sitter` cannot\n> load, even if their parser ABI is supported. Rebuild these files using a current tree-sitter CLI.\n\n### Running .wasm in Node.js\n\nNotice that executing `.wasm` files in Node.js is considerably slower than running [Node.js bindings][node bindings].\nHowever, this could be useful for testing purposes:\n\n```javascript\nconst Parser = require('web-tree-sitter');\n\n(async () => {\n  await Parser.init();\n  const parser = new Parser();\n  const Lang = await Parser.Language.load('tree-sitter-javascript.wasm');\n  parser.setLanguage(Lang);\n  const tree = parser.parse('let x = 1;');\n  console.log(tree.rootNode.toString());\n})();\n```\n\n### Loading a pre-compiled WebAssembly module\n\nSome environments, such as Cloudflare Workers and Vercel Edge Functions, import\n`.wasm` files as `WebAssembly.Module` objects. You can pass those modules to `Language.loadSync`:\n\n```javascript\nimport treeSitterJavaScript from 'tree-sitter-javascript.wasm';\n// treeSitterJavaScript is of type `WebAssembly.Module`\nconst JavaScript = Language.loadSync(treeSitterJavaScript);\nparser.setLanguage(JavaScript);\n```\n\n### Running .wasm in browser\n\n`web-tree-sitter` can run in the browser, but there are some common pitfalls.\n\n#### Loading the .wasm file\n\n`web-tree-sitter` needs to load the `tree-sitter.wasm` file. By default, it assumes that this file is available in the\nsame path as the JavaScript code. Therefore, if the code is being served from `http://localhost:3000/bundle.js`, then\nthe Wasm file should be at `http://localhost:3000/tree-sitter.wasm`.\n\nFor server side frameworks like NextJS, this can be tricky as pages are often served from a path such as\n`http://localhost:3000/_next/static/chunks/pages/index.js`. The loader will therefore look for the Wasm file at\n`http://localhost:3000/_next/static/chunks/pages/tree-sitter.wasm`. The solution is to pass a `locateFile` function in\nthe `moduleOptions` argument to `Parser.init()`:\n\n```javascript\nawait Parser.init({\n  locateFile(scriptName: string, scriptDirectory: string) {\n    return scriptName;\n  },\n});\n```\n\n`locateFile` takes in two parameters, `scriptName`, i.e. the Wasm file name, and `scriptDirectory`, i.e. the directory\nwhere the loader expects the script to be. It returns the path where the loader will look for the Wasm file. In the NextJS\ncase, we want to return just the `scriptName` so that the loader will look at `http://localhost:3000/tree-sitter.wasm`\nand not `http://localhost:3000/_next/static/chunks/pages/tree-sitter.wasm`.\n\nFor more information on the module options you can pass in, see the [emscripten documentation][emscripten-module-options].\n\n#### \"Can't resolve 'fs' in 'node_modules/web-tree-sitter\"\n\nMost bundlers will notice that the `web-tree-sitter.js` file is attempting to import `fs`, i.e. node's file system library.\nSince this doesn't exist in the browser, the bundlers will get confused. For Webpack, you can fix this by adding the\nfollowing to your webpack config:\n\n```javascript\n{\n  resolve: {\n    fallback: {\n      fs: false\n    }\n  }\n}\n```\n\n[docker]: https://www.docker.com\n[emscripten]: https://emscripten.org\n[emscripten-module-options]: https://emscripten.org/docs/api_reference/module.html#affecting-execution\n[gh release]: https://github.com/tree-sitter/tree-sitter/releases/latest\n[gh release js]: https://github.com/tree-sitter/tree-sitter-javascript/releases/latest\n[node bindings]: https://github.com/tree-sitter/node-tree-sitter\n[npm module]: https://www.npmjs.com/package/web-tree-sitter\n[podman]: https://podman.io\n[wasi-sdk]: https://github.com/WebAssembly/wasi-sdk\n","publish_time":1779214366154,"_cnpm_publish_time":1779214366154,"dist-tags":{"latest":"0.27.2"}}