|
1 | 1 | import * as fs from 'fs' |
2 | 2 | import * as path from 'path' |
3 | | -import { fileURLToPath } from 'url' |
4 | 3 |
|
5 | 4 | import { Parser } from 'web-tree-sitter' |
6 | 5 |
|
7 | | -import { getDirnameDynamically } from './utils' |
8 | | - |
9 | | -/** |
10 | | - * Helper function to get the current directory path that works in both ESM and CJS |
11 | | - * Uses runtime-only approach to prevent bundlers from inlining absolute paths |
12 | | - */ |
13 | | -function hereDir() { |
14 | | - const dirname = getDirnameDynamically() |
15 | | - if (typeof dirname !== 'undefined') { |
16 | | - return dirname |
17 | | - } |
18 | | - |
19 | | - // For ESM builds, use import.meta.url |
20 | | - if (typeof import.meta !== 'undefined' && import.meta.url) { |
21 | | - const dir = path.dirname(fileURLToPath(import.meta.url)) |
22 | | - return dir |
23 | | - } |
24 | | - |
25 | | - // Fallback to process.cwd() as last resort |
26 | | - return process.cwd() |
27 | | -} |
28 | | - |
29 | 6 | /** |
30 | 7 | * Initialize web-tree-sitter for Node.js environments with proper WASM file location |
31 | 8 | */ |
32 | 9 | export async function initTreeSitterForNode(): Promise<void> { |
33 | | - // Get the directory where our WASM files should be located |
34 | | - const dir = hereDir() |
35 | | - |
36 | | - // Try shared WASM directory first (new approach to avoid duplication) |
37 | | - const sharedWasm = path.join(dir, '..', 'wasm', 'tree-sitter.wasm') |
38 | | - |
39 | 10 | // Use locateFile to override where the runtime looks for tree-sitter.wasm |
40 | 11 | await Parser.init({ |
41 | 12 | locateFile: (name: string, scriptDir: string) => { |
42 | 13 | if (name === 'tree-sitter.wasm') { |
43 | | - // First try shared WASM directory (new approach) |
44 | | - if (fs.existsSync(sharedWasm)) { |
45 | | - return sharedWasm |
46 | | - } |
47 | 14 | // Fallback to script directory |
48 | 15 | const fallback = path.join(scriptDir, name) |
49 | 16 | if (fs.existsSync(fallback)) { |
|
0 commit comments