Set up code to publish web bindings to npm
This commit is contained in:
parent
572f290ec0
commit
9a82bd9d83
8 changed files with 113 additions and 30 deletions
8
lib/README.md
Normal file
8
lib/README.md
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Subdirectories
|
||||
--------------
|
||||
|
||||
* [`src`](./src) - C source code for the Tree-sitter library
|
||||
* [`include`](./include) - C headers for the Tree-sitter library
|
||||
* [`utf8proc`](./utf8proc) - A submodule for [`utf8proc`](https://juliastrings.github.io/utf8proc/), Tree-sitter's one library dependency.
|
||||
* [`binding_rust`](./binding_rust) - Rust bindings to the Tree-sitter library
|
||||
* [`binding_web`](./binding_web) - JavaScript bindings to the Tree-sitter library, using WebAssembly
|
||||
4
lib/binding_web/.gitignore
vendored
Normal file
4
lib/binding_web/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/tree-sitter.js
|
||||
/tree-sitter.wasm
|
||||
node_modules
|
||||
*.tgz
|
||||
4
lib/binding_web/.npmignore
Normal file
4
lib/binding_web/.npmignore
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
*
|
||||
!README.md
|
||||
!tree-sitter.js
|
||||
!tree-sitter.wasm
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
tree-sitter.wasm
|
||||
================
|
||||
Web Tree-sitter
|
||||
===============
|
||||
|
||||
[](https://travis-ci.org/tree-sitter/tree-sitter)
|
||||
|
||||
Wasm bindings to the [Tree-sitter](https://github.com/tree-sitter/tree-sitter) parsing library.
|
||||
WebAssembly bindings to the [Tree-sitter](https://github.com/tree-sitter/tree-sitter) parsing library.
|
||||
|
||||
### Basic Usage
|
||||
### Setup
|
||||
|
||||
You can either load the library as a standalone script:
|
||||
You can download the the `tree-sitter.js` and `tree-sitter.wasm` files from [the latest GitHub release](https://github.com/tree-sitter/tree-sitter/releases/tag/0.14.7) and load them using a standalone script:
|
||||
|
||||
```html
|
||||
<script src="/public/js/tree-sitter.js"/>
|
||||
<script src="/the/path/to/tree-sitter.js"/>
|
||||
|
||||
<script>
|
||||
const Parser = window.TreeSitter;
|
||||
|
|
@ -18,14 +18,16 @@ You can either load the library as a standalone script:
|
|||
</script>
|
||||
```
|
||||
|
||||
or using a packaging system like Webpack:
|
||||
You can also install [the `web-tree-sitter` module](https://www.npmjs.com/package/web-tree-sitter) from NPM and load it using a system like Webpack:
|
||||
|
||||
```js
|
||||
const Parser = require('tree-sitter');
|
||||
const Parser = require('web-tree-sitter');
|
||||
Parser.init().then(() => { /* the library is ready */ });
|
||||
```
|
||||
|
||||
Create a parser:
|
||||
### Basic Usage
|
||||
|
||||
First, create a parser:
|
||||
|
||||
```js
|
||||
const parser = new Parser;
|
||||
|
|
|
|||
38
lib/binding_web/check-artifacts-fresh.js
Executable file
38
lib/binding_web/check-artifacts-fresh.js
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const inputFiles = [
|
||||
'binding.c',
|
||||
'binding.js',
|
||||
'exports.json',
|
||||
'imports.js',
|
||||
'prefix.js',
|
||||
...list('../include/tree_sitter'),
|
||||
...list('../src')
|
||||
]
|
||||
|
||||
const outputFiles = [
|
||||
'tree-sitter.js',
|
||||
'tree-sitter.wasm',
|
||||
]
|
||||
|
||||
const outputMtime = Math.min(...outputFiles.map(mtime));
|
||||
|
||||
for (const inputFile of inputFiles) {
|
||||
if (mtime(inputFile) > outputMtime) {
|
||||
console.log(`File '${inputFile}' has changed. Re-run 'script/build-wasm'.`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function list(dir) {
|
||||
return fs.readdirSync(path.join(__dirname, dir), 'utf8')
|
||||
.filter(p => !p.startsWith('.'))
|
||||
.map(p => path.join(dir, p));
|
||||
}
|
||||
|
||||
function mtime(p) {
|
||||
return fs.statSync(path.join(__dirname, p)).mtime;
|
||||
}
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
{
|
||||
"name": "tree-sitter.wasm",
|
||||
"version": "0.0.1",
|
||||
"name": "web-tree-sitter",
|
||||
"version": "0.15.1",
|
||||
"description": "Tree-sitter bindings for the web",
|
||||
"main": "index.js",
|
||||
"main": "tree-sitter.js",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
"test": "mocha",
|
||||
"prepublish": "node check-artifacts-fresh.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
@ -22,7 +23,7 @@
|
|||
"bugs": {
|
||||
"url": "https://github.com/tree-sitter/tree-sitter/issues"
|
||||
},
|
||||
"homepage": "https://github.com/tree-sitter/tree-sitter#readme",
|
||||
"homepage": "https://github.com/tree-sitter/tree-sitter/tree/master/lib/binding_web",
|
||||
"devDependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"mocha": "^6.1.4",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
const release = '../../../target/release'
|
||||
const Parser = require(`${release}/tree-sitter.js`);
|
||||
const languageURL = name => require.resolve(`${release}/tree-sitter-${name}.wasm`);
|
||||
const Parser = require(`..`);
|
||||
|
||||
function languageURL(name) {
|
||||
return require.resolve(`../../../target/release/tree-sitter-${name}.wasm`);
|
||||
}
|
||||
|
||||
module.exports = Parser.init().then(async () => ({
|
||||
Parser,
|
||||
|
|
|
|||
|
|
@ -2,16 +2,40 @@
|
|||
|
||||
set -e
|
||||
|
||||
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
|
||||
cat <<EOF
|
||||
USAGE
|
||||
|
||||
$0 [--debug]
|
||||
|
||||
SUMMARY
|
||||
|
||||
Compile the Tree-sitter WASM library. This will create two files in the
|
||||
\`lib/binding_web\` directory: \`tree-sitter.js\` and \`tree-sitter.wasm\`.
|
||||
|
||||
REQUIREMENTS
|
||||
|
||||
You must have the \`docker\` command on your PATH for this to work.
|
||||
|
||||
OPTIONS
|
||||
|
||||
--debug: Compile the library more quickly, with fewer optimizations and more runtime assertions.
|
||||
|
||||
EOF
|
||||
exit 0
|
||||
fi
|
||||
|
||||
web_dir=lib/binding_web
|
||||
exports=$(cat ${web_dir}/exports.json)
|
||||
args="-Os"
|
||||
minify=1
|
||||
|
||||
if [[ "$1" == "--debug" ]]; then
|
||||
minify=0
|
||||
args="-s ASSERTIONS=1 -s SAFE_HEAP=1 -Os"
|
||||
fi
|
||||
|
||||
exports=$(cat lib/binding_web/exports.json)
|
||||
|
||||
mkdir -p target/scratch target/release
|
||||
mkdir -p target/scratch
|
||||
|
||||
docker run \
|
||||
--rm \
|
||||
|
|
@ -33,30 +57,30 @@ docker run \
|
|||
-I lib/src \
|
||||
-I lib/include \
|
||||
-I lib/utf8proc \
|
||||
--js-library lib/binding_web/imports.js \
|
||||
--pre-js lib/binding_web/prefix.js \
|
||||
--post-js lib/binding_web/binding.js \
|
||||
--js-library ${web_dir}/imports.js \
|
||||
--pre-js ${web_dir}/prefix.js \
|
||||
--post-js ${web_dir}/binding.js \
|
||||
lib/src/lib.c \
|
||||
lib/binding_web/binding.c \
|
||||
${web_dir}/binding.c \
|
||||
-o target/scratch/tree-sitter.js
|
||||
|
||||
|
||||
if [[ "$minify" == "1" ]]; then
|
||||
if [ ! -d lib/binding_web/node_modules/terser ]; then
|
||||
if [ ! -d ${web_dir}/node_modules/terser ]; then
|
||||
(
|
||||
cd lib/binding_web
|
||||
cd ${web_dir}
|
||||
npm install
|
||||
)
|
||||
fi
|
||||
lib/binding_web/node_modules/.bin/terser \
|
||||
${web_dir}/node_modules/.bin/terser \
|
||||
--compress \
|
||||
--mangle \
|
||||
--keep-fnames \
|
||||
--keep-classnames \
|
||||
-- target/scratch/tree-sitter.js \
|
||||
> target/release/tree-sitter.js
|
||||
> $web_dir/tree-sitter.js
|
||||
else
|
||||
cp target/scratch/tree-sitter.js target/release/tree-sitter.js
|
||||
cp target/scratch/tree-sitter.js $web_dir/tree-sitter.js
|
||||
fi
|
||||
|
||||
mv target/scratch/tree-sitter.wasm target/release/tree-sitter.wasm
|
||||
mv target/scratch/tree-sitter.wasm $web_dir/tree-sitter.wasm
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue