Skip to content

Commit 5c2ec4d

Browse files
committed
feat: export esm entrypoint
1 parent 4a07bf4 commit 5c2ec4d

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

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 && node rename",
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",

rename.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const outdir = path.resolve(__dirname, 'dist', 'esm');
5+
6+
const renameMjs = (path) => fs.promises.rename(path, path.replace('.js', '.mjs'));
7+
8+
const main = async () => {
9+
10+
// replace import
11+
let fp = path.join(outdir, 'binary_parser.js');
12+
const contents = await fs.promises.readFile(fp, { encoding: 'utf8' });
13+
await fs.promises.writeFile(fp, contents.replace('"./context"', '"./context.mjs"'));
14+
15+
// change filenames
16+
renameMjs(fp);
17+
renameMjs(path.join(outdir, 'context.js'));
18+
}
19+
20+
main()

0 commit comments

Comments
 (0)