Skip to content

Commit d48f918

Browse files
authored
Merge pull request #176 from manzt/manzt/esm
feat: add ESM package export
2 parents 4a07bf4 + 8b29ad6 commit d48f918

File tree

4 files changed

+120
-115
lines changed

4 files changed

+120
-115
lines changed

lib/binary_parser.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,106 @@
1-
import { Context } from "./context";
1+
class Context {
2+
code = "";
3+
scopes = [["vars"]];
4+
bitFields: Parser[] = [];
5+
tmpVariableCount = 0;
6+
references: { [key: string]: { resolved: boolean; requested: boolean } } = {};
7+
importPath: string;
8+
imports: any[] = [];
9+
reverseImports = new Map<any, number>();
10+
useContextVariables: boolean = false;
11+
12+
constructor(importPath?: string, useContextVariables?: boolean) {
13+
this.importPath = importPath;
14+
this.useContextVariables = useContextVariables;
15+
}
16+
17+
generateVariable(name?: string) {
18+
const arr = [];
19+
20+
const scopes = this.scopes[this.scopes.length - 1];
21+
arr.push(...scopes);
22+
if (name) {
23+
arr.push(name);
24+
}
25+
26+
return arr.join(".");
27+
}
28+
29+
generateOption(val: number | string | Function) {
30+
switch (typeof val) {
31+
case "number":
32+
return val.toString();
33+
case "string":
34+
return this.generateVariable(val);
35+
case "function":
36+
return `${this.addImport(val)}.call(${this.generateVariable()}, vars)`;
37+
}
38+
}
39+
40+
generateError(err: string) {
41+
this.pushCode("throw new Error(" + err + ");");
42+
}
43+
44+
generateTmpVariable() {
45+
return "$tmp" + this.tmpVariableCount++;
46+
}
47+
48+
pushCode(code: string) {
49+
this.code += code + "\n";
50+
}
51+
52+
pushPath(name: string) {
53+
if (name) {
54+
this.scopes[this.scopes.length - 1].push(name);
55+
}
56+
}
57+
58+
popPath(name: string) {
59+
if (name) {
60+
this.scopes[this.scopes.length - 1].pop();
61+
}
62+
}
63+
64+
pushScope(name: string) {
65+
this.scopes.push([name]);
66+
}
67+
68+
popScope() {
69+
this.scopes.pop();
70+
}
71+
72+
addImport(im: any) {
73+
if (!this.importPath) return `(${im})`;
74+
let id = this.reverseImports.get(im);
75+
if (!id) {
76+
id = this.imports.push(im) - 1;
77+
this.reverseImports.set(im, id);
78+
}
79+
return `${this.importPath}[${id}]`;
80+
}
81+
82+
addReference(alias: string) {
83+
if (this.references[alias]) return;
84+
this.references[alias] = { resolved: false, requested: false };
85+
}
86+
87+
markResolved(alias: string) {
88+
this.references[alias].resolved = true;
89+
}
90+
91+
markRequested(aliasList: string[]) {
92+
aliasList.forEach((alias) => {
93+
this.references[alias].requested = true;
94+
});
95+
}
96+
97+
getUnresolvedReferences() {
98+
const references = this.references;
99+
return Object.keys(this.references).filter(
100+
(alias) => !references[alias].resolved && !references[alias].requested
101+
);
102+
}
103+
}
2104

3105
const aliasRegistry: { [key: string]: Parser } = {};
4106
const FUNCTION_PREFIX = "___parser_";

lib/context.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,24 @@
33
"version": "1.9.2",
44
"description": "Blazing-fast binary parser builder",
55
"main": "dist/binary_parser.js",
6+
"module": "dist/esm/binary_parser.mjs",
67
"devDependencies": {
78
"@types/node": "^16.9.1",
89
"mocha": "^9.1.1",
910
"nyc": "^15.1.0",
1011
"prettier": "^2.4.0",
1112
"typescript": "^4.4.3"
1213
},
14+
"exports": {
15+
".": {
16+
"import": "./dist/esm/binary_parser.mjs",
17+
"require": "./dist/binary_parser.js"
18+
},
19+
"./*": "./*"
20+
},
1321
"scripts": {
14-
"build": "tsc",
22+
"build": "tsc && npm run build:esm",
23+
"build:esm": "tsc --target esnext --module esnext --outDir dist/esm && mv dist/esm/binary_parser.js dist/esm/binary_parser.mjs",
1524
"fmt": "prettier --write \"{lib,example,test,benchmark}/**/*.{ts,js}\"",
1625
"check-fmt": "prettier --list-different \"{lib,example,test,benchmark}/**/*.{ts,js}\"",
1726
"test": "mocha",
@@ -20,8 +29,7 @@
2029
"prepare": "npm run build"
2130
},
2231
"files": [
23-
"dist/*.js",
24-
"dist/*.d.ts"
32+
"dist/**/*.{js,mjs,d.ts}"
2533
],
2634
"keywords": [
2735
"binary",

0 commit comments

Comments
 (0)