diff --git a/lib/binding_web/script/build.js b/lib/binding_web/script/build.js index 6f2bedd6..737267a1 100644 --- a/lib/binding_web/script/build.js +++ b/lib/binding_web/script/build.js @@ -1,49 +1,37 @@ import esbuild from 'esbuild'; import fs from 'fs/promises'; -import path from 'path'; const format = process.env.CJS ? 'cjs' : 'esm'; const debug = process.argv.includes('--debug'); const outfile = `${debug ? 'debug/' : ''}web-tree-sitter.${format === 'esm' ? 'js' : 'cjs'}`; -// Copy source files to lib directory - we'll map the Wasm's sourcemap 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/')) { - 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; - } - }); + const isTreeSitterSource = (source) => + source.includes('../../src/') || source === 'tree-sitter.c'; + + const normalizePath = (source) => { + if (source.includes('../../src/')) { + return source.replace('../../src/', debug ? '../lib/' : 'lib/'); + } else if (source === 'tree-sitter.c') { + return debug ? '../lib/tree-sitter.c' : 'lib/tree-sitter.c'; + } + return source; + }; + + const filtered = sourceMap.sources + .map((source, index) => ({ source, content: sourceMap.sourcesContent?.[index] })) + .filter(item => isTreeSitterSource(item.source)) + .map(item => ({ source: normalizePath(item.source), content: item.content })); + + sourceMap.sources = filtered.map(item => item.source); + sourceMap.sourcesContent = filtered.map(item => item.content); + await fs.writeFile(outputPath, JSON.stringify(sourceMap, null, 2)); } - async function build() { await esbuild.build({ entryPoints: ['src/index.ts'], @@ -62,7 +50,6 @@ async function build() { const outputWasmName = `${debug ? 'debug/' : ''}web-tree-sitter.wasm`; await fs.copyFile('lib/web-tree-sitter.wasm', outputWasmName); - await copySourceFiles(); await processWasmSourceMap('lib/web-tree-sitter.wasm.map', `${outputWasmName}.map`); }