feat(web)!: rewrite the library in TypeScript
This commit is contained in:
parent
07a86b1729
commit
2cae67892e
39 changed files with 7856 additions and 3629 deletions
62
lib/binding_web/script/build-sourcemap.js
Normal file
62
lib/binding_web/script/build-sourcemap.js
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import { readFileSync, writeFileSync } from 'fs';
|
||||
import { SourceMapGenerator, SourceMapConsumer } from 'source-map';
|
||||
|
||||
async function fixSourceMap() {
|
||||
const distMap = JSON.parse(readFileSync('dist/tree-sitter.js.map', 'utf8'));
|
||||
const distJs = readFileSync('dist/tree-sitter.js', 'utf8').split('\n');
|
||||
const finalJs = readFileSync('tree-sitter.js', 'utf8').split('\n');
|
||||
|
||||
const lineMap = new Map();
|
||||
|
||||
for (let distLine = 0; distLine < distJs.length; distLine++) {
|
||||
const line = distJs[distLine].trim();
|
||||
if (!line) continue;
|
||||
|
||||
for (let finalLine = 0; finalLine < finalJs.length; finalLine++) {
|
||||
if (finalJs[finalLine].trim() === line) {
|
||||
lineMap.set(distLine + 1, finalLine + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const consumer = await new SourceMapConsumer(distMap);
|
||||
const generator = new SourceMapGenerator({
|
||||
file: 'tree-sitter.js',
|
||||
sourceRoot: ''
|
||||
});
|
||||
|
||||
consumer.eachMapping(mapping => {
|
||||
const finalLine = lineMap.get(mapping.generatedLine);
|
||||
if (finalLine) {
|
||||
generator.addMapping({
|
||||
generated: {
|
||||
line: finalLine,
|
||||
column: mapping.generatedColumn
|
||||
},
|
||||
original: {
|
||||
line: mapping.originalLine,
|
||||
column: mapping.originalColumn
|
||||
},
|
||||
// Fix the source path to be relative to binding_web
|
||||
source: `src/${mapping.source.split('/').pop()}`,
|
||||
name: mapping.name
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
for (const source of consumer.sources) {
|
||||
const content = consumer.sourceContentFor(source);
|
||||
if (content) {
|
||||
generator.setSourceContent(
|
||||
`src/${source.split('/').pop()}`,
|
||||
content
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
consumer.destroy();
|
||||
writeFileSync('tree-sitter.js.map', generator.toString());
|
||||
}
|
||||
|
||||
fixSourceMap().catch(console.error);
|
||||
34
lib/binding_web/script/check-artifacts-fresh.js
Executable file
34
lib/binding_web/script/check-artifacts-fresh.js
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const inputFiles = [
|
||||
'lib/binding.c',
|
||||
'binding.js',
|
||||
'wasm/exports.txt',
|
||||
'wasm/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;
|
||||
}
|
||||
25
lib/binding_web/script/postinstall.js
Normal file
25
lib/binding_web/script/postinstall.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const isDebug = process.env.npm_config_web_tree_sitter_debug === 'true';
|
||||
|
||||
if (isDebug) {
|
||||
// Copy debug versions to root
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, '../debug/tree-sitter.js'),
|
||||
path.join(__dirname, '../tree-sitter.js')
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, '../debug/tree-sitter.wasm'),
|
||||
path.join(__dirname, '../tree-sitter.wasm')
|
||||
);
|
||||
// Copy sourcemaps too
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, '../debug/tree-sitter.js.map'),
|
||||
path.join(__dirname, '../tree-sitter.js.map')
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, '../debug/tree-sitter.wasm.map'),
|
||||
path.join(__dirname, '../tree-sitter.wasm.map')
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue