2025-01-19 23:07:26 -05:00
|
|
|
import esbuild from 'esbuild';
|
|
|
|
|
import fs from 'fs/promises';
|
2025-01-31 20:12:39 -05:00
|
|
|
import path from 'path';
|
2025-01-19 23:07:26 -05:00
|
|
|
|
|
|
|
|
const format = process.env.CJS ? 'cjs' : 'esm';
|
|
|
|
|
const debug = process.argv.includes('--debug');
|
2025-02-08 13:06:44 -05:00
|
|
|
const outfile = `${debug ? 'debug/' : ''}web-tree-sitter.${format === 'esm' ? 'js' : 'cjs'}`;
|
2025-01-19 23:07:26 -05:00
|
|
|
|
2025-01-31 20:12:39 -05:00
|
|
|
// Copy source files to lib directory - we'll map the wasm's sourecmap to these files.
|
|
|
|
|
async function copySourceFiles() {
|
|
|
|
|
const sourceDir = '../src';
|
|
|
|
|
const files = await fs.readdir(sourceDir);
|
|
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
if (file.endsWith('.c') || file.endsWith('.h')) {
|
|
|
|
|
await fs.copyFile(
|
|
|
|
|
path.join(sourceDir, file),
|
|
|
|
|
path.join('lib', file)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function processWasmSourceMap(inputPath, outputPath) {
|
|
|
|
|
const mapContent = await fs.readFile(inputPath, 'utf8');
|
|
|
|
|
const sourceMap = JSON.parse(mapContent);
|
|
|
|
|
|
|
|
|
|
// Filter out emscripten files and normalize paths
|
|
|
|
|
sourceMap.sources = sourceMap.sources
|
|
|
|
|
.filter(source => {
|
|
|
|
|
// Keep only tree-sitter source files
|
|
|
|
|
return source.includes('../../src/') || source === 'tree-sitter.c';
|
|
|
|
|
})
|
|
|
|
|
.map(source => {
|
|
|
|
|
if (source.includes('../../src/')) {
|
2025-02-08 13:06:44 -05:00
|
|
|
return source.replace('../../src/', debug ? '../lib/' : 'lib/');
|
|
|
|
|
} else if (source === 'tree-sitter.c') {
|
|
|
|
|
return debug ? '../lib/tree-sitter.c' : 'lib/tree-sitter.c';
|
|
|
|
|
} else {
|
|
|
|
|
return source;
|
2025-01-31 20:12:39 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
await fs.writeFile(outputPath, JSON.stringify(sourceMap, null, 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function build() {
|
|
|
|
|
await esbuild.build({
|
|
|
|
|
entryPoints: ['src/index.ts'],
|
|
|
|
|
bundle: true,
|
|
|
|
|
platform: 'node',
|
|
|
|
|
format,
|
|
|
|
|
outfile,
|
|
|
|
|
sourcemap: true,
|
|
|
|
|
sourcesContent: true,
|
|
|
|
|
keepNames: true,
|
|
|
|
|
external: ['fs/*', 'fs/promises'],
|
|
|
|
|
resolveExtensions: ['.ts', '.js', format === 'esm' ? '.mjs' : '.cjs'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Copy the WASM files to the appropriate spot, as esbuild doesn't "bundle" WASM files
|
2025-02-08 13:06:44 -05:00
|
|
|
const outputWasmName = `${debug ? 'debug/' : ''}web-tree-sitter.wasm`;
|
|
|
|
|
await fs.copyFile('lib/web-tree-sitter.wasm', outputWasmName);
|
2025-01-31 20:12:39 -05:00
|
|
|
|
|
|
|
|
await copySourceFiles();
|
2025-02-08 13:06:44 -05:00
|
|
|
await processWasmSourceMap('lib/web-tree-sitter.wasm.map', `${outputWasmName}.map`);
|
2025-01-31 20:12:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
build().catch(console.error);
|