Skip to content

Commit 076554c

Browse files
authored
Replace tsup with ts-bridge (#247)
Thanks to `tsup`, we publish CommonJS- and ESM-compatible versions of our libraries. However, there are two problems with our builds: 1. We produce two versions of JavaScript files, but we only produce one version of TypeScript type declaration files (which happens to be CommonJS-compatible). This violates best practices communicated both by from the TypeScript team and also the "Are the Types Wrong" tool, which both dictate that there be a 1-to-1 mapping between source and declaration files. You can read more about this problem here: <https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md> 2. `tsup` identifies and extracts common code to "chunk" files. These make tree shaking possible, but make debugging difficult for our client teams. This commit replaces `tsup` with `ts-bridge`, which solves both of these problems and more. It also adds `attw` to the `test` script so that we ensure that we don't have incompatibilities with builds in the future.
1 parent 03f796f commit 076554c

File tree

5 files changed

+314
-568
lines changed

5 files changed

+314
-568
lines changed

package.json

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,26 @@
1313
"sideEffects": false,
1414
"exports": {
1515
".": {
16-
"types": "./dist/types/index.d.ts",
17-
"import": "./dist/index.mjs",
18-
"require": "./dist/index.js"
16+
"import": {
17+
"types": "./dist/index.d.mts",
18+
"default": "./dist/index.mjs"
19+
},
20+
"require": {
21+
"types": "./dist/index.d.cts",
22+
"default": "./dist/index.cjs"
23+
}
1924
},
2025
"./package.json": "./package.json"
2126
},
22-
"main": "./dist/index.js",
27+
"main": "./dist/index.cjs",
2328
"module": "./dist/index.mjs",
24-
"types": "./dist/types/index.d.ts",
29+
"types": "./dist/index.d.cts",
2530
"files": [
2631
"dist"
2732
],
2833
"scripts": {
29-
"build": "tsup --clean && yarn build:types",
34+
"build": "ts-bridge --project tsconfig.build.json --clean",
3035
"build:docs": "typedoc",
31-
"build:types": "tsc --project tsconfig.build.json",
3236
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
3337
"lint:changelog": "auto-changelog validate --prettier",
3438
"lint:constraints": "yarn constraints",
@@ -37,19 +41,22 @@
3741
"lint:fix": "yarn lint:eslint --fix && yarn lint:constraints --fix && yarn lint:misc --write && yarn lint:dependencies && yarn lint:changelog",
3842
"lint:misc": "prettier '**/*.json' '**/*.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
3943
"prepack": "./scripts/prepack.sh",
40-
"test": "jest && jest-it-up",
44+
"test": "jest && jest-it-up && attw --pack",
4145
"test:watch": "jest --watch"
4246
},
4347
"devDependencies": {
48+
"@arethetypeswrong/cli": "^0.15.3",
4449
"@lavamoat/allow-scripts": "^3.0.4",
4550
"@lavamoat/preinstall-always-fail": "^2.0.0",
4651
"@metamask/auto-changelog": "^3.4.3",
4752
"@metamask/eslint-config": "^12.2.0",
4853
"@metamask/eslint-config-jest": "^12.1.0",
4954
"@metamask/eslint-config-nodejs": "^12.1.0",
5055
"@metamask/eslint-config-typescript": "^12.1.0",
56+
"@ts-bridge/cli": "^0.1.4",
57+
"@ts-bridge/shims": "^0.1.1",
5158
"@types/jest": "^28.1.6",
52-
"@types/node": "^16",
59+
"@types/node": "^18.18",
5360
"@typescript-eslint/eslint-plugin": "^5.43.0",
5461
"@typescript-eslint/parser": "^5.43.0",
5562
"@yarnpkg/types": "^4.0.0-rc.52",
@@ -68,7 +75,6 @@
6875
"prettier-plugin-packagejson": "^2.3.0",
6976
"ts-jest": "^28.0.7",
7077
"ts-node": "^10.7.0",
71-
"tsup": "^7.2.0",
7278
"typedoc": "^0.23.15",
7379
"typescript": "~4.8.4"
7480
},
@@ -82,8 +88,7 @@
8288
},
8389
"lavamoat": {
8490
"allowScripts": {
85-
"@lavamoat/preinstall-always-fail": false,
86-
"tsup>esbuild": true
91+
"@lavamoat/preinstall-always-fail": false
8792
}
8893
}
8994
}

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"emitDeclarationOnly": true,
77
"inlineSources": true,
88
"noEmit": false,
9-
"outDir": "dist/types",
9+
"outDir": "dist",
1010
"rootDir": "src",
1111
"sourceMap": true
1212
},

tsup.config.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

yarn.config.cjs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,20 +253,21 @@ module.exports = defineConfig({
253253
workspace.set('repository.type', 'git');
254254
workspace.set('repository.url', `${workspaceRepository}.git`);
255255

256-
// The package must specify a minimum Node.js version of 16.
256+
// The package must specify a minimum Node.js version of 18.18.
257257
workspace.set('engines.node', '^18.18 || >=20');
258258

259-
// The package must specify a `types` entrypoint, and an `import`
260-
// entrypoint.
261-
workspace.set('types', './dist/types/index.d.ts');
262-
workspace.set('exports["."].types', './dist/types/index.d.ts');
259+
// The package must provide the location of the CommonJS-compatible
260+
// entrypoint and its matching type declaration file.
261+
workspace.set('main', './dist/index.cjs');
262+
workspace.set('exports["."].require.default', './dist/index.cjs');
263+
workspace.set('types', './dist/index.d.cts');
264+
workspace.set('exports["."].require.types', './dist/index.d.cts');
263265

264-
// The package must specify a `main` and `module` entrypoint, and a
265-
// `require` and `import` entrypoint.
266-
workspace.set('main', './dist/index.js');
267-
workspace.set('exports["."].require', './dist/index.js');
266+
// The package must provide the location of the ESM-compatible JavaScript
267+
// entrypoint and its matching type declaration file.
268268
workspace.set('module', './dist/index.mjs');
269-
workspace.set('exports["."].import', './dist/index.mjs');
269+
workspace.set('exports["."].import.default', './dist/index.mjs');
270+
workspace.set('exports["."].import.types', './dist/index.d.mts');
270271

271272
// The package must export a `package.json` file.
272273
workspace.set('exports["./package.json"]', './package.json');

0 commit comments

Comments
 (0)