From 3a1a6b1a107185b68628f1fadd90a386cfc495fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Mon, 25 Apr 2022 13:04:50 +0100 Subject: [PATCH] Add separate exports for node and wasm --- package.json | 12 ++++++++++-- .../NodeParserAdapter.ts | 2 +- .../WasmParserAdapter.ts | 10 +++++----- src/tree-sitter/index.ts | 1 - test/tree-sitter/ExpressionBuilder.test.ts | 5 ++--- 5 files changed, 18 insertions(+), 12 deletions(-) rename src/{tree-sitter => tree-sitter-node}/NodeParserAdapter.ts (90%) rename src/{tree-sitter => tree-sitter-wasm}/WasmParserAdapter.ts (78%) diff --git a/package.json b/package.json index 9dd799d6..72f159d1 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "files": [ "dist/cjs", "dist/esm", - "*.wasm" + "dist/*.wasm" ], "module": "dist/esm/src/index.js", "jsnext:main": "dist/esm/src/index.js", @@ -16,6 +16,14 @@ ".": { "import": "./dist/esm/src/index.js", "require": "./dist/cjs/src/index.js" + }, + "./node": { + "import": "./dist/esm/src/tree-sitter-node/NodeParserAdapter.js", + "require": "./dist/cjs/src/tree-sitter-node/NodeParserAdapter.js" + }, + "./wasm": { + "import": "./dist/esm/src/tree-sitter-wasm/WasmParserAdapter.js", + "require": "./dist/cjs/src/tree-sitter-wasm/WasmParserAdapter.js" } }, "scripts": { @@ -30,7 +38,7 @@ "upgrade": "npm-check-updates --upgrade", "prepare": "husky install", "pretty-quick-staged": "pretty-quick --staged", - "postinstall": "scripts/build.js" + "postinstall": "scripts/build.js && cp node_modules/web-tree-sitter/tree-sitter.wasm dist" }, "repository": { "type": "git", diff --git a/src/tree-sitter/NodeParserAdapter.ts b/src/tree-sitter-node/NodeParserAdapter.ts similarity index 90% rename from src/tree-sitter/NodeParserAdapter.ts rename to src/tree-sitter-node/NodeParserAdapter.ts index ffd9f3d8..0f7d19cd 100644 --- a/src/tree-sitter/NodeParserAdapter.ts +++ b/src/tree-sitter-node/NodeParserAdapter.ts @@ -4,7 +4,7 @@ import Java from 'tree-sitter-java' // @ts-ignore import TypeScript from 'tree-sitter-typescript' -import { LanguageName, ParserAdapter } from './types' +import { LanguageName, ParserAdapter } from '../tree-sitter/types' export class NodeParserAdapter implements ParserAdapter { readonly parser = new Parser() diff --git a/src/tree-sitter/WasmParserAdapter.ts b/src/tree-sitter-wasm/WasmParserAdapter.ts similarity index 78% rename from src/tree-sitter/WasmParserAdapter.ts rename to src/tree-sitter-wasm/WasmParserAdapter.ts index c4ed8ff3..c7052e1c 100644 --- a/src/tree-sitter/WasmParserAdapter.ts +++ b/src/tree-sitter-wasm/WasmParserAdapter.ts @@ -1,14 +1,15 @@ +import NodeParser from 'tree-sitter' import Parser from 'web-tree-sitter' -import { LanguageName, LanguageNames, ParserAdapter } from './types.js' +import { LanguageName, LanguageNames, ParserAdapter } from '../tree-sitter/types.js' export class WasmParserAdapter implements ParserAdapter { - // @ts-ignore - public parser: Parser + public parser: NodeParser private languages: Record async init(wasmBaseUrl: string) { await Parser.init() + // @ts-ignore this.parser = new Parser() const languages = await Promise.all( @@ -23,8 +24,7 @@ export class WasmParserAdapter implements ParserAdapter { ) } - // @ts-ignore - query(source: string): Parser.Query { + query(source: string): NodeParser.Query { return this.parser.getLanguage().query(source) } diff --git a/src/tree-sitter/index.ts b/src/tree-sitter/index.ts index f7f52ade..07898489 100644 --- a/src/tree-sitter/index.ts +++ b/src/tree-sitter/index.ts @@ -1,3 +1,2 @@ export * from './ExpressionBuilder.js' -export * from './NodeParserAdapter.js' export * from './types.js' diff --git a/test/tree-sitter/ExpressionBuilder.test.ts b/test/tree-sitter/ExpressionBuilder.test.ts index 482243a0..d4864045 100644 --- a/test/tree-sitter/ExpressionBuilder.test.ts +++ b/test/tree-sitter/ExpressionBuilder.test.ts @@ -5,9 +5,9 @@ import glob from 'glob' import path from 'path' import { ExpressionBuilder, LanguageName } from '../../src/index.js' -import { NodeParserAdapter } from '../../src/tree-sitter/NodeParserAdapter.js' import { ParserAdapter } from '../../src/tree-sitter/types' -import { WasmParserAdapter } from '../../src/tree-sitter/WasmParserAdapter.js' +import { NodeParserAdapter } from '../../src/tree-sitter-node/NodeParserAdapter.js' +import { WasmParserAdapter } from '../../src/tree-sitter-wasm/WasmParserAdapter.js' function defineContract(makeParserAdapter: () => Promise) { let expressionBuilder: ExpressionBuilder @@ -41,7 +41,6 @@ describe('ExpressionBuilder', () => { }) context('with WasmParserAdapter', () => { - // @ts-ignore defineContract(async () => { const wasmParserAdapter = new WasmParserAdapter() await wasmParserAdapter.init('dist')