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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue