Set up code to publish web bindings to npm

This commit is contained in:
Max Brunsfeld 2019-05-07 13:07:36 -07:00
parent 572f290ec0
commit 9a82bd9d83
8 changed files with 113 additions and 30 deletions

4
lib/binding_web/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/tree-sitter.js
/tree-sitter.wasm
node_modules
*.tgz

View file

@ -0,0 +1,4 @@
*
!README.md
!tree-sitter.js
!tree-sitter.wasm

View file

@ -1,16 +1,16 @@
tree-sitter.wasm
================
Web Tree-sitter
===============
[![Build Status](https://travis-ci.org/tree-sitter/tree-sitter.svg?branch=master)](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;

View 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;
}

View file

@ -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",

View file

@ -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,