Skip to content

Commit 664e2d5

Browse files
authored
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.
1 parent d68b7d3 commit 664e2d5

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@
1717
"type": "git",
1818
"url": "https://github.com/jscheiny/safe-units.git"
1919
},
20-
"main": "dist/src/index.js",
21-
"typings": "dist/src/index.d.ts",
20+
"main": "./dist/cjs/index.js",
21+
"module": "./dist/esm/index.js",
22+
"types": "./dist/types/index.d.ts",
23+
"exports": {
24+
"import": "./dist/esm/index.js",
25+
"require": "./dist/cjs/index.js"
26+
},
27+
"files": [
28+
"dist"
29+
],
2230
"scripts": {
23-
"build": "tsc -p src",
31+
"build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
32+
"build:esm": "tsc -p tsconfig.esm.json && echo '{\"type\":\"module\"}' > dist/esm/package.json",
33+
"build": "yarn run build:cjs && yarn run build:esm",
2434
"clean": "rimraf dist docs/build",
2535
"compile:docs": "tsc -p docsgen",
2636
"compile:examples": "tsc -p docs/examples",

tsconfig.base.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
43
"moduleResolution": "node",
4+
"declaration": true,
5+
"declarationDir": "./dist/types",
56
"noFallthroughCasesInSwitch": true,
67
"noImplicitAny": true,
78
"noImplicitReturns": true,
@@ -18,5 +19,13 @@
1819
"es2015",
1920
"es2015.core"
2021
]
21-
}
22+
},
23+
"include": [
24+
"src"
25+
],
26+
"exclude": [
27+
"dist",
28+
"node_modules",
29+
"test"
30+
]
2231
}

tsconfig.cjs.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"outDir": "./dist/cjs",
6+
"target": "ES2015"
7+
}
8+
}

tsconfig.esm.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "es6",
5+
"outDir": "./dist/esm",
6+
"target": "es6"
7+
}
8+
}

0 commit comments

Comments
 (0)