Skip to content

Commit 94996b2

Browse files
committed
refactor(web): clean up preserving sourcemap logic, don't copy lib files
1 parent f2e71ec commit 94996b2

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

lib/binding_web/script/build.js

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,37 @@
11
import esbuild from 'esbuild';
22
import fs from 'fs/promises';
3-
import path from 'path';
43

54
const format = process.env.CJS ? 'cjs' : 'esm';
65
const debug = process.argv.includes('--debug');
76
const outfile = `${debug ? 'debug/' : ''}web-tree-sitter.${format === 'esm' ? 'js' : 'cjs'}`;
87

9-
// Copy source files to lib directory - we'll map the Wasm's sourcemap to these files.
10-
async function copySourceFiles() {
11-
const sourceDir = '../src';
12-
const files = await fs.readdir(sourceDir);
13-
14-
for (const file of files) {
15-
if (file.endsWith('.c') || file.endsWith('.h')) {
16-
await fs.copyFile(
17-
path.join(sourceDir, file),
18-
path.join('lib', file)
19-
);
20-
}
21-
}
22-
}
23-
248
async function processWasmSourceMap(inputPath, outputPath) {
259
const mapContent = await fs.readFile(inputPath, 'utf8');
2610
const sourceMap = JSON.parse(mapContent);
2711

28-
// Filter out emscripten files and normalize paths
29-
sourceMap.sources = sourceMap.sources
30-
.filter(source => {
31-
// Keep only tree-sitter source files
32-
return source.includes('../../src/') || source === 'tree-sitter.c';
33-
})
34-
.map(source => {
35-
if (source.includes('../../src/')) {
36-
return source.replace('../../src/', debug ? '../lib/' : 'lib/');
37-
} else if (source === 'tree-sitter.c') {
38-
return debug ? '../lib/tree-sitter.c' : 'lib/tree-sitter.c';
39-
} else {
40-
return source;
41-
}
42-
});
12+
const isTreeSitterSource = (source) =>
13+
source.includes('../../src/') || source === 'tree-sitter.c';
14+
15+
const normalizePath = (source) => {
16+
if (source.includes('../../src/')) {
17+
return source.replace('../../src/', debug ? '../lib/' : 'lib/');
18+
} else if (source === 'tree-sitter.c') {
19+
return debug ? '../lib/tree-sitter.c' : 'lib/tree-sitter.c';
20+
}
21+
return source;
22+
};
23+
24+
const filtered = sourceMap.sources
25+
.map((source, index) => ({ source, content: sourceMap.sourcesContent?.[index] }))
26+
.filter(item => isTreeSitterSource(item.source))
27+
.map(item => ({ source: normalizePath(item.source), content: item.content }));
28+
29+
sourceMap.sources = filtered.map(item => item.source);
30+
sourceMap.sourcesContent = filtered.map(item => item.content);
31+
4332
await fs.writeFile(outputPath, JSON.stringify(sourceMap, null, 2));
4433
}
4534

46-
4735
async function build() {
4836
await esbuild.build({
4937
entryPoints: ['src/index.ts'],
@@ -62,7 +50,6 @@ async function build() {
6250
const outputWasmName = `${debug ? 'debug/' : ''}web-tree-sitter.wasm`;
6351
await fs.copyFile('lib/web-tree-sitter.wasm', outputWasmName);
6452

65-
await copySourceFiles();
6653
await processWasmSourceMap('lib/web-tree-sitter.wasm.map', `${outputWasmName}.map`);
6754
}
6855

0 commit comments

Comments
 (0)