feat: publish both CJS and ESM files

This way, users can pick whichever one works for their needs
This commit is contained in:
Amaan Qureshi 2025-01-19 23:07:26 -05:00
parent 10e6ecf162
commit a4b20c1c56
10 changed files with 225 additions and 55 deletions

View file

@ -0,0 +1,23 @@
import esbuild from 'esbuild';
import fs from 'fs/promises';
const format = process.env.CJS ? 'cjs' : 'esm';
const debug = process.argv.includes('--debug');
const outfile = `${debug ? 'debug/' : ''}tree-sitter.${format === 'esm' ? 'js' : 'cjs'}`;
await esbuild.build({
entryPoints: ['src/index.ts'],
bundle: true,
platform: 'node',
format,
outfile,
sourcemap: true,
sourcesContent: true,
keepNames: true,
external: ['fs/*', 'fs/promises'],
});
// Copy the generated WASM files to the appropriate spot, as esbuild doesn't "bundle" WASM files
const outputWasmName = `${debug ? 'debug/' : ''}tree-sitter.wasm`;
await fs.copyFile(`lib/tree-sitter.wasm`, outputWasmName);
await fs.copyFile(`lib/tree-sitter.wasm.map`, `${outputWasmName}.map`);

View file

@ -1,24 +0,0 @@
import fs from 'fs';
import path from 'path';
const isDebug = process.env.npm_config_debug === 'true';
if (isDebug) {
// Copy debug versions to root
fs.copyFileSync(
path.join(__dirname, '../debug/tree-sitter.js'),
path.join(__dirname, '../tree-sitter.js'),
);
fs.copyFileSync(
path.join(__dirname, '../debug/tree-sitter.wasm'),
path.join(__dirname, '../tree-sitter.wasm'),
);
fs.copyFileSync(
path.join(__dirname, '../debug/tree-sitter.js.map'),
path.join(__dirname, '../tree-sitter.js.map'),
);
fs.copyFileSync(
path.join(__dirname, '../debug/tree-sitter.wasm.map'),
path.join(__dirname, '../tree-sitter.wasm.map'),
);
}