refactor(web): rename tree-sitter.js to web-tree-sitter.js

This is not breaking for consumers of the web bindings, nor the
playground as both filenames will be kept in the .github.io repo
This commit is contained in:
Amaan Qureshi 2025-02-08 13:06:44 -05:00
parent 74d7ca8582
commit a40265cbeb
19 changed files with 853 additions and 374 deletions

View file

@ -1,6 +1,8 @@
debug/
dist/
tree-sitter*
web-tree-sitter*
!web-tree-sitter.d.ts
!web-tree-sitter.d.ts.map
lib/tree-sitter*
lib/*.c
lib/*.h

View file

@ -120,7 +120,7 @@ npm test
### Debugging
You might have noticed that when you ran `npm build`, the build process generated a couple of [sourcemaps][sourcemap]:
`tree-sitter.js.map` and `tree-sitter.wasm.map`. These sourcemaps can be used to debug the library in the browser, and are
`web-tree-sitter.js.map` and `web-tree-sitter.wasm.map`. These sourcemaps can be used to debug the library in the browser, and are
shipped with the library on both NPM and the GitHub releases.
#### Tweaking the Emscripten build

View file

@ -9,11 +9,11 @@ WebAssembly bindings to the [Tree-sitter](https://github.com/tree-sitter/tree-si
## Setup
You can download the `tree-sitter.js` and `tree-sitter.wasm` files from [the latest GitHub release][gh release] and load
You can download the `web-tree-sitter.js` and `web-tree-sitter.wasm` files from [the latest GitHub release][gh release] and load
them using a standalone script:
```html
<script src="/the/path/to/tree-sitter.js"></script>
<script src="/the/path/to/web-tree-sitter.js"></script>
<script>
const { Parser } = window.TreeSitter;
@ -62,11 +62,8 @@ Parser.init().then(() => { /* the library is ready */ });
This will load the debug version of the `.js` and `.wasm` file, which includes debug symbols and assertions.
> [!NOTE]
> The `tree-sitter.js` file on GH releases is an ES6 module. If you are interested in using a pure CommonJS library, such
> as for Electron, you should note that on our NPM package, we use [conditional exports][cond export] to provide both the
> ES6 and CommonJS modules. If you've set up your project correctly, and need to use CommonJS, your package manager will
> automatically handle this for you. As of writing, we do not host a CommonJS version of the library on GH releases, and
> if you do not use the NPM registry, you'll have to build the library yourself.
> The `web-tree-sitter.js` file on GH releases is an ES6 module. If you are interested in using a pure CommonJS library, such
> as for Electron, you should use the `web-tree-sitter.cjs` file instead.
### Basic Usage
@ -244,7 +241,7 @@ For more information on the module options you can pass in, see the [emscripten
#### "Can't resolve 'fs' in 'node_modules/web-tree-sitter"
Most bundlers will notice that the `tree-sitter.js` file is attempting to import `fs`, i.e. node's file system library.
Most bundlers will notice that the `web-tree-sitter.js` file is attempting to import `fs`, i.e. node's file system library.
Since this doesn't exist in the browser, the bundlers will get confused. For Webpack, you can fix this by adding the
following to your webpack config:
@ -258,7 +255,6 @@ following to your webpack config:
}
```
[cond export]: https://nodejs.org/api/packages.html#conditional-exports
[docker]: https://www.docker.com
[emscripten]: https://emscripten.org
[emscripten-module-options]: https://emscripten.org/docs/api_reference/module.html#affecting-execution

File diff suppressed because it is too large Load diff

View file

@ -18,13 +18,13 @@
"type": "module",
"exports": {
".": {
"import": "./tree-sitter.js",
"require": "./tree-sitter.cjs",
"import": "./web-tree-sitter.js",
"require": "./web-tree-sitter.cjs",
"types": "./web-tree-sitter.d.ts"
},
"./debug": {
"import": "./debug/tree-sitter.js",
"require": "./debug/tree-sitter.cjs",
"import": "./debug/web-tree-sitter.js",
"require": "./debug/web-tree-sitter.cjs",
"types": "./web-tree-sitter.d.ts"
}
},
@ -37,18 +37,18 @@
],
"files": [
"README.md",
"tree-sitter.cjs",
"tree-sitter.cjs.map",
"tree-sitter.js",
"tree-sitter.js.map",
"tree-sitter.wasm",
"tree-sitter.wasm.map",
"debug/tree-sitter.cjs",
"debug/tree-sitter.cjs.map",
"debug/tree-sitter.js",
"debug/tree-sitter.js.map",
"debug/tree-sitter.wasm",
"debug/tree-sitter.wasm.map",
"web-tree-sitter.cjs",
"web-tree-sitter.cjs.map",
"web-tree-sitter.js",
"web-tree-sitter.js.map",
"web-tree-sitter.wasm",
"web-tree-sitter.wasm.map",
"debug/web-tree-sitter.cjs",
"debug/web-tree-sitter.cjs.map",
"debug/web-tree-sitter.js",
"debug/web-tree-sitter.js.map",
"debug/web-tree-sitter.wasm",
"debug/web-tree-sitter.wasm.map",
"web-tree-sitter.d.ts",
"web-tree-sitter.d.ts.map",
"src/**/*.ts",
@ -56,18 +56,18 @@
"lib/*.h"
],
"devDependencies": {
"@eslint/js": "^9.19.0",
"@eslint/js": "^9.20.0",
"@types/emscripten": "^1.40.0",
"@types/node": "^22.12.0",
"@vitest/coverage-v8": "^3.0.4",
"@types/node": "^22.13.1",
"@vitest/coverage-v8": "^3.0.5",
"dts-buddy": "^0.5.4",
"esbuild": "^0.24.2",
"eslint": "^9.19.0",
"esbuild": "^0.25.0",
"eslint": "^9.20.0",
"source-map": "^0.7.4",
"tsx": "^4.19.2",
"typescript": "^5.7.3",
"typescript-eslint": "^8.22.0",
"vitest": "^3.0.4"
"typescript-eslint": "^8.23.0",
"vitest": "^3.0.5"
},
"scripts": {
"build:ts": "node script/build.js",

View file

@ -4,7 +4,7 @@ import path from 'path';
const format = process.env.CJS ? 'cjs' : 'esm';
const debug = process.argv.includes('--debug');
const outfile = `${debug ? 'debug/' : ''}tree-sitter.${format === 'esm' ? 'js' : 'cjs'}`;
const outfile = `${debug ? 'debug/' : ''}web-tree-sitter.${format === 'esm' ? 'js' : 'cjs'}`;
// Copy source files to lib directory - we'll map the wasm's sourecmap to these files.
async function copySourceFiles() {
@ -33,11 +33,13 @@ async function processWasmSourceMap(inputPath, outputPath) {
})
.map(source => {
if (source.includes('../../src/')) {
return source.replace('../../src/', 'lib/');
return source.replace('../../src/', debug ? '../lib/' : 'lib/');
} else if (source === 'tree-sitter.c') {
return debug ? '../lib/tree-sitter.c' : 'lib/tree-sitter.c';
} else {
return source;
}
return source;
});
await fs.writeFile(outputPath, JSON.stringify(sourceMap, null, 2));
}
@ -57,11 +59,11 @@ async function build() {
});
// Copy the WASM files to the appropriate spot, as esbuild doesn't "bundle" WASM files
const outputWasmName = `${debug ? 'debug/' : ''}tree-sitter.wasm`;
await fs.copyFile('lib/tree-sitter.wasm', outputWasmName);
const outputWasmName = `${debug ? 'debug/' : ''}web-tree-sitter.wasm`;
await fs.copyFile('lib/web-tree-sitter.wasm', outputWasmName);
await copySourceFiles();
await processWasmSourceMap('lib/tree-sitter.wasm.map', `${outputWasmName}.map`);
await processWasmSourceMap('lib/web-tree-sitter.wasm.map', `${outputWasmName}.map`);
}
build().catch(console.error);

View file

@ -23,7 +23,7 @@ const inputFiles = [
...listFiles('../../src'),
];
const outputFiles = ['../tree-sitter.js', '../tree-sitter.wasm'];
const outputFiles = ['../web-tree-sitter.js', '../web-tree-sitter.wasm'];
const outputMtime = Math.min(...outputFiles.map(getMtime));
for (const inputFile of inputFiles) {

View file

@ -1,4 +1,4 @@
import createModule, { type MainModule } from '../lib/tree-sitter';
import createModule, { type MainModule } from '../lib/web-tree-sitter';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { type Parser } from './parser';

View file

@ -1,4 +1,4 @@
import { type MainModule } from '../lib/tree-sitter';
import { type MainModule } from '../lib/web-tree-sitter';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { ParseState, type Parser } from './parser';

View file

@ -6,7 +6,7 @@ export default defineConfig({
environment: 'node',
coverage: {
include: [
'tree-sitter.js',
'web-tree-sitter.js',
],
exclude: [
'test/**',