From f0d438f298bcb834f905181c5b2826ef4978cb7c Mon Sep 17 00:00:00 2001 From: Franco Zalamena Date: Tue, 7 Apr 2026 15:59:11 +0100 Subject: [PATCH] Add ESM package exports with conditional imports Add tsconfig.esm.json for ES module builds and configure package.json with conditional exports so that import resolves to ESM and require continues to resolve to the existing CommonJS output. Fixes #488 Co-Authored-By: Claude Opus 4.6 --- package.json | 16 +++++++++++++--- tsconfig.esm.json | 8 ++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tsconfig.esm.json diff --git a/package.json b/package.json index f67df8fd..d8dd73f4 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,16 @@ "typescript" ], "main": "./index.node.js", + "module": "./dist/esm/index.js", "browser": "./index.js", "types": "./dist/index.d.ts", + "exports": { + ".": { + "import": "./dist/esm/index.js", + "require": "./dist/index.js", + "default": "./dist/index.js" + } + }, "unpkg": "./index.js", "files": [ "index.js", @@ -29,7 +37,8 @@ "index.node.js", "dist/*", "package.json", - "README.md" + "README.md", + "dist/esm/*" ], "dependencies": { "@pabra/sortby": "^1.0.1", @@ -57,8 +66,9 @@ "lint": "eslint src --ignore-pattern '*.test.*' --ignore-pattern '*.spec.*' --ext '.ts,.tsx'", "typecheck": "ttsc --noEmit true --emitDeclarationOnly false", "check-deps": "madge --extensions js,ts --circular dist", - "prepublishOnly": "yarn lint && yarn typecheck && yarn format && yarn build && yarn build:node", - "clean": "find . -name \"node_modules\" -exec rm -rf '{}' +" + "prepublishOnly": "yarn lint && yarn typecheck && yarn format && yarn build && yarn build:node && yarn build:esm", + "clean": "find . -name \"node_modules\" -exec rm -rf '{}' +", + "build:esm": "ttsc --project tsconfig.esm.json && babel src --out-dir ./dist/esm --extensions \".ts,.tsx\"" }, "devDependencies": { "@babel/cli": "^7.5.5", diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 00000000..af361926 --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "ESNext", + "outDir": "./dist/esm", + "declaration": false + } +}