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

@ -53,8 +53,8 @@ fn main() {
fn web_playground_files_present() -> bool {
let paths = [
"../docs/src/assets/js/playground.js",
"../lib/binding_web/tree-sitter.js",
"../lib/binding_web/tree-sitter.wasm",
"../lib/binding_web/web-tree-sitter.js",
"../lib/binding_web/web-tree-sitter.wasm",
];
paths.iter().all(|p| Path::new(p).exists())

View file

@ -92,7 +92,7 @@
<script>LANGUAGE_BASE_URL = "";</script>
<script type="module" src="playground.js"></script>
<script type="module">
import * as TreeSitter from './tree-sitter.js';
import * as TreeSitter from './web-tree-sitter.js';
window.TreeSitter = TreeSitter;
setTimeout(() => window.initializePlayground({local: true}), 1)
</script>

View file

@ -34,8 +34,8 @@ macro_rules! optional_resource {
}
optional_resource!(get_playground_js, "docs/src/assets/js/playground.js");
optional_resource!(get_lib_js, "lib/binding_web/tree-sitter.js");
optional_resource!(get_lib_wasm, "lib/binding_web/tree-sitter.wasm");
optional_resource!(get_lib_js, "lib/binding_web/web-tree-sitter.js");
optional_resource!(get_lib_wasm, "lib/binding_web/web-tree-sitter.wasm");
fn get_main_html(tree_sitter_dir: Option<&Path>) -> Cow<'static, [u8]> {
tree_sitter_dir.map_or(
@ -79,16 +79,16 @@ pub fn serve(grammar_path: &Path, open_in_browser: bool) -> Result<()> {
response(&playground_js, &js_header)
}
}
"/tree-sitter.js" => {
"/web-tree-sitter.js" => {
if lib_js.is_empty() {
redirect("https://tree-sitter.github.io/tree-sitter.js")
redirect("https://tree-sitter.github.io/web-tree-sitter.js")
} else {
response(&lib_js, &js_header)
}
}
"/tree-sitter.wasm" => {
"/web-tree-sitter.wasm" => {
if lib_wasm.is_empty() {
redirect("https://tree-sitter.github.io/tree-sitter.wasm")
redirect("https://tree-sitter.github.io/web-tree-sitter.wasm")
} else {
response(&lib_wasm, &wasm_header)
}

View file

@ -197,7 +197,7 @@ and the tree-sitter module is fetched from [here][js url]. This, along with the
[go package]: https://pkg.go.dev/github.com/tree-sitter/go-tree-sitter
[go ts]: https://github.com/tree-sitter/go-tree-sitter
[highlight crate]: https://crates.io/crates/tree-sitter-highlight
[js url]: https://tree-sitter.github.io/tree-sitter.js
[js url]: https://tree-sitter.github.io/web-tree-sitter.js
[lib crate]: https://crates.io/crates/tree-sitter
[mdBook]: https://rust-lang.github.io/mdBook
[mdbook cli]: https://rust-lang.github.io/mdBook/guide/installation.html

View file

@ -102,7 +102,7 @@ you must use at least one capture, like <code>(node_name) @capture-name</code></
<script>LANGUAGE_BASE_URL = "https://tree-sitter.github.io";</script>
<script type="module">
import * as TreeSitter from 'https://tree-sitter.github.io/tree-sitter.js';
import * as TreeSitter from 'https://tree-sitter.github.io/web-tree-sitter.js';
window.TreeSitter = TreeSitter;
setTimeout(() => window.initializePlayground({local: false}), 1);
</script>

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/**',

View file

@ -136,10 +136,10 @@ pub fn run_wasm(args: &BuildWasm) -> Result<()> {
// Clean up old files from prior runs
for file in [
"tree-sitter.mjs",
"tree-sitter.cjs",
"tree-sitter.wasm",
"tree-sitter.wasm.map",
"web-tree-sitter.mjs",
"web-tree-sitter.cjs",
"web-tree-sitter.wasm",
"web-tree-sitter.wasm.map",
] {
fs::remove_file(PathBuf::from("lib/binding_web/lib").join(file)).ok();
}
@ -173,12 +173,12 @@ pub fn run_wasm(args: &BuildWasm) -> Result<()> {
"-I", "lib/include",
"--js-library", "lib/binding_web/lib/imports.js",
"--pre-js", "lib/binding_web/lib/prefix.js",
"-o", if args.cjs { "lib/binding_web/lib/tree-sitter.cjs" } else { "lib/binding_web/lib/tree-sitter.mjs" },
"-o", if args.cjs { "lib/binding_web/lib/web-tree-sitter.cjs" } else { "lib/binding_web/lib/web-tree-sitter.mjs" },
"lib/src/lib.c",
"lib/binding_web/lib/tree-sitter.c",
]);
if args.emit_tsd {
emscripten_flags.extend(["--emit-tsd", "tree-sitter.d.ts"]);
emscripten_flags.extend(["--emit-tsd", "web-tree-sitter.d.ts"]);
}
let command = command.args(&emscripten_flags);

View file

@ -71,7 +71,7 @@ fn check_wasm_exports() -> Result<()> {
let wasm_objdump = Command::new("wasm-objdump")
.args([
"--details",
"lib/binding_web/debug/tree-sitter.wasm",
"lib/binding_web/debug/web-tree-sitter.wasm",
"--section",
"Name",
])

View file

@ -23,7 +23,7 @@ enum Commands {
/// Runs `cargo benchmark` with some optional environment variables set.
Benchmark(Benchmark),
/// Compile the Tree-sitter WASM library. This will create two files in the
/// `lib/binding_web` directory: `tree-sitter.js` and `tree-sitter.wasm`.
/// `lib/binding_web` directory: `web-tree-sitter.js` and `web-tree-sitter.wasm`.
BuildWasm(BuildWasm),
/// Compile the Tree-sitter WASM standard library.
BuildWasmStdlib,