Skip to content

Commit

Permalink
Restore CommonJS compatibility (#5)
Browse files Browse the repository at this point in the history
In [1] I made the package ESM-only to make it possible to import
it easily from Lune's ESM code. This is not great so I found a way
to restore the CommonJS compatibility while still resolving the
ESM-related issues[2] found in the original ts-results package.
The solution I found can be found in [3].

[1] 0be48c5 ("Make the package ESM-only")
[2] vultix#37
[3] https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html
  • Loading branch information
jstasiak authored Mar 14, 2022
1 parent 0a5ed01 commit befb3c5
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions package-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "commonjs"}
1 change: 1 addition & 0 deletions package-esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "module"}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "npm run clean && npm run build:ts && npm run build:copy",
"build:ts": "tsc -p tsconfig-cjs.json && tsc -p tsconfig-esm.json",
"build:copy": "copyfiles README.md LICENSE src/package.json dist --flat && copyfiles src/rxjs-operators/package.json dist --up 1",
"build:copy": "copyfiles README.md LICENSE src/package.json dist --flat && copyfiles src/rxjs-operators/package.json dist/cjs --up 1 && cp package-cjs.json dist/cjs/package.json && cp package-esm.json dist/esm/package.json",
"clean": "rm -rf dist",
"test": "jest",
"format": "prettier . --write"
Expand Down
12 changes: 9 additions & 3 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
"name": "ts-results-es",
"version": "3.3.0",
"description": "A typescript implementation of Rust's Result and Option objects.",
"exports": "./esm/index.js",
"types": "index.d.ts",
"type": "module",
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "esm/index.d.ts",
"exports": {
".": {
"import": "./esm/index.js",
"require": "./cjs/index.js"
}
},
"keywords": [
"Rust",
"Result",
Expand Down
2 changes: 1 addition & 1 deletion src/rxjs-operators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"name": "ts-results/rxjs-operators",
"types": "index.d.ts",
"main": "index.js",
"module": "../esm/rxjs-operators/index.js"
"module": "../../esm/rxjs-operators/index.js"
}
2 changes: 1 addition & 1 deletion tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"outDir": "./dist/",
"outDir": "./dist/cjs",
"module": "commonjs"
}
}
4 changes: 1 addition & 3 deletions tsconfig-esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"extends": "./tsconfig-base.json",
"compilerOptions": {
"outDir": "dist/esm",
"module": "esnext",
"declaration": false,
"declarationMap": false
"module": "esnext"
}
}

0 comments on commit befb3c5

Please sign in to comment.