Skip to content

web-tree-sitter-sfapex - __dirname is not defined #66

@colindjk

Description

@colindjk

The WASM code references __dirname which breaks tree sitter when trying to render in the browser.

Reproduce

npm create vite@latest fs-test -- --template react
cd fs-test
npm install
npm install web-tree-sitter-sfapex web-tree-sitter

Then use the code (pulled from the README) in src/App.jsx.

Example here:

import './App.css'
import { getApexParser, getSoqlParser } from "web-tree-sitter-sfapex";

const test = async () => {
	// create a parser instance
	let parser = await getApexParser();

	// we set the language to Apex so lets feed it some apex
	const apexTree = parser.parse(`
/**
 * block comment
 */
global class TestClass implements TestInterface {
    public static String Prop1 = 'TestVal';

    global Account setName(Account acct, String nameVal){
        acct.Name = nameVal;
        return acct;
    }
}`);

	// just a super simple example of printing the discovered nodes
	// to see the anonymous nodes (syntax without formal names) set this to `true`
	const includeAnonymousNodes = false;

	console.log("APEX TREE");
	printTree(apexTree.rootNode);

	// do it with some SOQL this time
	parser = await getSoqlParser();

	const soqlTree = parser.parse(`
SELECT Id, Name, Parent.Name,
    TYPEOF Owner
        WHEN User THEN Id, Username, FederationId
        WHEN Group THEN Name
    END,
    (SELECT Id, Name FROM Contacts)
FROM Account
WHERE Name = 'Robots' AND Are_Coming__c = FALSE
`);

	console.log("SOQL TREE");
	printTree(soqlTree.rootNode);

	function printTree(node, indent = 0) {
		console.log(
			" ".repeat(indent),
			(node.isNamed ? "(" : "") + node.type + (node.isNamed ? ")" : "")
		);
		for (let c of includeAnonymousNodes ? node.children : node.namedChildren) {
			printTree(c, indent + 2);
		}
	}
}

function App() {

	console.log({
		getApexParser,
		getSoqlParser
	})

	return (
		<>
			<div className="card">
				<button onClick={test}>
					Test Tree Sitter Parsers
				</button>
			</div>
		</>
	)
}

export default App

Clicking on the "Test Tree Sitter Parsers" button gets the error "__dirname is not defined", likely due to this code.

Is there a build step that's missing here, or is WASM just not supported yet? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions