Skip to content

Commit

Permalink
Compile to both ESM and CJS (#114)
Browse files Browse the repository at this point in the history
* Compile to both ESM and CJS

When trying to use the new Redlock library I noticed it was able to run
in NodeJS, but failed in Jest. Looks like that was because Jest (like
many tools right now) doesn't yet support native ESM modules yet.
I think its awesome Redlock is outputting native ESM, but while the
world is still converting to ESM I think its nice to duel output to CJS
as well.

This updates the building of the library to support both ESM and CJS
clients. During the build process it now builds the library twice, once
with the main `tsconfig.json` file and again with a new
`tsconfig.cjs.json` file. Finally, it runs a new `tools/fixup` bash
script to insert some build specific `package.json` files that add the
correct "type" attribute to the folder. The final dist tree structure
looks like:

```
dist
├── cjs
│   ├── index.js
│   ├── index.js.map
│   ├── index.test.js
│   ├── index.test.js.map
│   └── package.json
├── esm
│   ├── index.js
│   ├── index.js.map
│   ├── index.test.js
│   ├── index.test.js.map
│   └── package.json
├── index.d.ts
└── index.test.d.ts
```

The rest of the change is updating the `package.json` file to point to
these new files.

* "main" - Old entry point. Points to CJS index.js
* "module" - Deprecated entry point (but many clients still use it).
  Points to ESM index.js
* "types" - Entry point for typescript types. Points to top level
  index.d.ts
* "exports" - New entry point router for Node14+. Points require
  statements to the CJS index.js and import statements to the ESM
  index.js

* Fix test command

Co-authored-by: Mike Marcacci <mike.marcacci@gmail.com>
  • Loading branch information
ekosz and mike-marcacci authored Nov 26, 2021
1 parent b75a5f4 commit a48e563
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
22 changes: 16 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
"repository": "https://github.com/mike-marcacci/node-redlock.git",
"homepage": "https://github.com/mike-marcacci/node-redlock#readme",
"bugs": "https://github.com/mike-marcacci/node-redlock/issues",
"main": "dist/index.js",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"keywords": [
"nodejs",
"redlock",
Expand All @@ -20,8 +28,10 @@
],
"files": [
"dist/index.d.ts",
"dist/index.js",
"dist/index.js.map"
"dist/esm/index.js",
"dist/esm/index.js.map",
"dist/cjs/index.js",
"dist/cjs/index.js.map"
],
"engines": {
"node": ">=12"
Expand Down Expand Up @@ -49,10 +59,10 @@
"scripts": {
"format": "prettier --list-different --write '**/*.{json,yml,md,ts}'",
"lint": "prettier -c '**/*.{json,yml,md,ts}' && eslint src --ext ts",
"build": "rm -rf dist && tsc",
"build": "rm -rf dist && tsc && tsc -p tsconfig.cjs.json && ./tools/fixup",
"build:development": "rm -rf dist && tsc --watch",
"test": "ava --verbose dist/*.test.js",
"test:development": "ava --verbose --watch dist/*.test.js",
"test": "cd dist/esm && ava --verbose *.test.js",
"test:development": "cd dist/esm && ava --verbose --watch *.test.js",
"prepare": "yarn build",
"prepublishOnly": "yarn install && yarn lint && yarn build"
},
Expand Down
16 changes: 16 additions & 0 deletions tools/fixup
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
#
# Add package.json files to cjs/esm subtrees
#

cat >dist/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >dist/esm/package.json <<!EOF
{
"type": "module"
}
!EOF
9 changes: 9 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declarationDir": null,
"declaration": false,
"module": "commonjs",
"outDir": "./dist/cjs"
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"lib": ["es2020"],
"target": "es2018",
"module": "es2020",
"outDir": "./dist",
"outDir": "./dist/esm",
"declaration": true,
"declarationDir": "./dist",
"moduleResolution": "node",
Expand Down

0 comments on commit a48e563

Please sign in to comment.