Skip to content

Commit

Permalink
Add separate exports for node and wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Apr 25, 2022
1 parent 3ab3ded commit 3a1a6b1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
"files": [
"dist/cjs",
"dist/esm",
"*.wasm"
"dist/*.wasm"
],
"module": "dist/esm/src/index.js",
"jsnext:main": "dist/esm/src/index.js",
"exports": {
".": {
"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": {
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LanguageName, Parser.Language>

async init(wasmBaseUrl: string) {
await Parser.init()
// @ts-ignore
this.parser = new Parser()

const languages = await Promise.all(
Expand 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)
}

Expand Down
1 change: 0 additions & 1 deletion src/tree-sitter/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './ExpressionBuilder.js'
export * from './NodeParserAdapter.js'
export * from './types.js'
5 changes: 2 additions & 3 deletions test/tree-sitter/ExpressionBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParserAdapter>) {
let expressionBuilder: ExpressionBuilder
Expand Down Expand Up @@ -41,7 +41,6 @@ describe('ExpressionBuilder', () => {
})

context('with WasmParserAdapter', () => {
// @ts-ignore
defineContract(async () => {
const wasmParserAdapter = new WasmParserAdapter()
await wasmParserAdapter.init('dist')
Expand Down

0 comments on commit 3a1a6b1

Please sign in to comment.