Skip to content

Commit 060e69e

Browse files
committed
build(web): relocate source files in WASM sourcemap
1 parent 40dfe6b commit 060e69e

File tree

1 file changed

+60
-17
lines changed

1 file changed

+60
-17
lines changed

lib/binding_web/script/build.js

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,67 @@
11
import esbuild from 'esbuild';
22
import fs from 'fs/promises';
3+
import path from 'path';
34

45
const format = process.env.CJS ? 'cjs' : 'esm';
56
const debug = process.argv.includes('--debug');
67
const outfile = `${debug ? 'debug/' : ''}tree-sitter.${format === 'esm' ? 'js' : 'cjs'}`;
78

8-
await esbuild.build({
9-
entryPoints: ['src/index.ts'],
10-
bundle: true,
11-
platform: 'node',
12-
format,
13-
outfile,
14-
sourcemap: true,
15-
sourcesContent: true,
16-
keepNames: true,
17-
external: ['fs/*', 'fs/promises'],
18-
resolveExtensions: ['.ts', '.js', format === 'esm' ? '.mjs' : '.cjs'],
19-
});
20-
21-
// Copy the generated WASM files to the appropriate spot, as esbuild doesn't "bundle" WASM files
22-
const outputWasmName = `${debug ? 'debug/' : ''}tree-sitter.wasm`;
23-
await fs.copyFile(`lib/tree-sitter.wasm`, outputWasmName);
24-
await fs.copyFile(`lib/tree-sitter.wasm.map`, `${outputWasmName}.map`);
9+
// Copy source files to lib directory - we'll map the wasm's sourecmap 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+
24+
async function processWasmSourceMap(inputPath, outputPath) {
25+
const mapContent = await fs.readFile(inputPath, 'utf8');
26+
const sourceMap = JSON.parse(mapContent);
27+
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/', 'lib/');
37+
}
38+
return source;
39+
});
40+
41+
await fs.writeFile(outputPath, JSON.stringify(sourceMap, null, 2));
42+
}
43+
44+
45+
async function build() {
46+
await esbuild.build({
47+
entryPoints: ['src/index.ts'],
48+
bundle: true,
49+
platform: 'node',
50+
format,
51+
outfile,
52+
sourcemap: true,
53+
sourcesContent: true,
54+
keepNames: true,
55+
external: ['fs/*', 'fs/promises'],
56+
resolveExtensions: ['.ts', '.js', format === 'esm' ? '.mjs' : '.cjs'],
57+
});
58+
59+
// Copy the WASM files to the appropriate spot, as esbuild doesn't "bundle" WASM files
60+
const outputWasmName = `${debug ? 'debug/' : ''}tree-sitter.wasm`;
61+
await fs.copyFile('lib/tree-sitter.wasm', outputWasmName);
62+
63+
await copySourceFiles();
64+
await processWasmSourceMap('lib/tree-sitter.wasm.map', `${outputWasmName}.map`);
65+
}
66+
67+
build().catch(console.error);

0 commit comments

Comments
 (0)