From 664e2d5e5a5d0d2c3f8c984012f9f0761b7f2024 Mon Sep 17 00:00:00 2001 From: Lindsay Gaines Date: Thu, 24 Oct 2024 01:12:36 +1100 Subject: [PATCH] Add esm build configuration and package.json exports (#201) This will build a cjs compatible bundle and an esm compatible bundle and package them both, allowing downstream projects to import the module type that fits their project. --- package.json | 16 +++++++++++++--- tsconfig.base.json | 13 +++++++++++-- tsconfig.cjs.json | 8 ++++++++ tsconfig.esm.json | 8 ++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 tsconfig.cjs.json create mode 100644 tsconfig.esm.json diff --git a/package.json b/package.json index e42385f..c067c99 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,20 @@ "type": "git", "url": "https://github.com/jscheiny/safe-units.git" }, - "main": "dist/src/index.js", - "typings": "dist/src/index.d.ts", + "main": "./dist/cjs/index.js", + "module": "./dist/esm/index.js", + "types": "./dist/types/index.d.ts", + "exports": { + "import": "./dist/esm/index.js", + "require": "./dist/cjs/index.js" + }, + "files": [ + "dist" + ], "scripts": { - "build": "tsc -p src", + "build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json", + "build:esm": "tsc -p tsconfig.esm.json && echo '{\"type\":\"module\"}' > dist/esm/package.json", + "build": "yarn run build:cjs && yarn run build:esm", "clean": "rimraf dist docs/build", "compile:docs": "tsc -p docsgen", "compile:examples": "tsc -p docs/examples", diff --git a/tsconfig.base.json b/tsconfig.base.json index 4e8ae51..971b37f 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -1,7 +1,8 @@ { "compilerOptions": { - "module": "commonjs", "moduleResolution": "node", + "declaration": true, + "declarationDir": "./dist/types", "noFallthroughCasesInSwitch": true, "noImplicitAny": true, "noImplicitReturns": true, @@ -18,5 +19,13 @@ "es2015", "es2015.core" ] - } + }, + "include": [ + "src" + ], + "exclude": [ + "dist", + "node_modules", + "test" + ] } \ No newline at end of file diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..8411f0d --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "./dist/cjs", + "target": "ES2015" + } +} \ No newline at end of file diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..d6aae5e --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "module": "es6", + "outDir": "./dist/esm", + "target": "es6" + } +} \ No newline at end of file