Skip to content

Commit affbb96

Browse files
authored
Publish ESM+CJS and harden exports (#469)
Switch to `ts-bridge` so that we can publish this package in both ES module format and CommonJS module format, and add `exports` to `package.json` to instruct bundlers which format they should choose. (Note that this is a breaking change as consumers will no longer be allowed to import subpaths.) The exports are double-checked with `@arethetypeswrong/cli` to ensure that they work correctly.
1 parent 4a2284f commit affbb96

File tree

7 files changed

+412
-13
lines changed

7 files changed

+412
-13
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- This package can now be used in ESM environments as well as CommonJS ([#469](https://github.com/MetaMask/smart-transactions-controller/pull/469))
13+
14+
### Changed
15+
16+
- **BREAKING:** Disallow subpath exports ([#469](https://github.com/MetaMask/smart-transactions-controller/pull/469))
17+
1018
## [18.1.0]
1119

1220
### Added

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
coverageReporters: ['text', 'html'],
77
coverageThreshold: {
88
global: {
9-
branches: 75.52,
9+
branches: 74.65,
1010
functions: 92.5,
1111
lines: 92.52,
1212
statements: 92.51,

package.json

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,35 @@
77
"url": "https://github.com/MetaMask/smart-transactions-controller.git"
88
},
99
"license": "SEE LICENSE IN LICENSE",
10-
"main": "dist/index.js",
11-
"types": "dist/index.d.ts",
10+
"sideEffects": false,
11+
"exports": {
12+
".": {
13+
"import": {
14+
"types": "./dist/index.d.mts",
15+
"default": "./dist/index.mjs"
16+
},
17+
"require": {
18+
"types": "./dist/index.d.cts",
19+
"default": "./dist/index.cjs"
20+
}
21+
},
22+
"./package.json": "./package.json"
23+
},
24+
"main": "./dist/index.cjs",
25+
"module": "./dist/index.mjs",
26+
"types": "./dist/index.d.cts",
1227
"files": [
13-
"dist/"
28+
"dist"
1429
],
1530
"scripts": {
16-
"build": "tsc --project tsconfig.build.json",
17-
"build:clean": "rm -rf dist && yarn build",
18-
"build:link": "yarn build && cd dist && yarn link && rm -rf node_modules && cd ..",
31+
"build": "ts-bridge --project tsconfig.build.json --clean",
1932
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:changelog",
2033
"lint:changelog": "auto-changelog validate --prettier",
2134
"lint:eslint": "eslint . --cache --ext js,ts",
2235
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write && yarn lint:changelog",
2336
"lint:misc": "prettier '**/*.json' '**/*.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
2437
"prepack": "./scripts/prepack.sh",
25-
"test": "jest",
38+
"test": "jest && attw --pack",
2639
"test:watch": "jest --watchAll"
2740
},
2841
"dependencies": {
@@ -42,6 +55,7 @@
4255
"lodash": "^4.17.21"
4356
},
4457
"devDependencies": {
58+
"@arethetypeswrong/cli": "^0.18.2",
4559
"@lavamoat/allow-scripts": "^3.2.1",
4660
"@lavamoat/preinstall-always-fail": "^2.1.0",
4761
"@metamask/auto-changelog": "^3.1.0",
@@ -53,6 +67,7 @@
5367
"@metamask/json-rpc-engine": "^10.0.1",
5468
"@metamask/network-controller": "^24.0.0",
5569
"@metamask/transaction-controller": "^42.0.0",
70+
"@ts-bridge/cli": "^0.6.3",
5671
"@types/jest": "^26.0.24",
5772
"@types/lodash": "^4.14.194",
5873
"@types/node": "^18.19.17",

scripts/prepack.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ if [[ -n $SKIP_PREPACK ]]; then
99
exit 0
1010
fi
1111

12-
yarn build:clean
12+
yarn build

tsconfig.build.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"declaration": true,
5+
"declarationMap": true,
6+
"emitDeclarationOnly": true,
57
"inlineSources": true,
68
"noEmit": false,
79
"outDir": "dist",

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"module": "CommonJS",
66
"moduleResolution": "node",
77
"noEmit": true,
8+
"noErrorTruncation": true,
89
"resolveJsonModule": true,
910
"skipLibCheck": true,
1011
"strict": true,

0 commit comments

Comments
 (0)