Skip to content

Commit

Permalink
Replace tsup with ts-bridge (#247)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mcmire authored Jun 25, 2024
1 parent 03f796f commit 076554c
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 568 deletions.
29 changes: 17 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@
"sideEffects": false,
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/types/index.d.ts",
"types": "./dist/index.d.cts",
"files": [
"dist"
],
"scripts": {
"build": "tsup --clean && yarn build:types",
"build": "ts-bridge --project tsconfig.build.json --clean",
"build:docs": "typedoc",
"build:types": "tsc --project tsconfig.build.json",
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
"lint:changelog": "auto-changelog validate --prettier",
"lint:constraints": "yarn constraints",
Expand All @@ -37,19 +41,22 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:constraints --fix && yarn lint:misc --write && yarn lint:dependencies && yarn lint:changelog",
"lint:misc": "prettier '**/*.json' '**/*.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
"prepack": "./scripts/prepack.sh",
"test": "jest && jest-it-up",
"test": "jest && jest-it-up && attw --pack",
"test:watch": "jest --watch"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.3",
"@lavamoat/allow-scripts": "^3.0.4",
"@lavamoat/preinstall-always-fail": "^2.0.0",
"@metamask/auto-changelog": "^3.4.3",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@ts-bridge/cli": "^0.1.4",
"@ts-bridge/shims": "^0.1.1",
"@types/jest": "^28.1.6",
"@types/node": "^16",
"@types/node": "^18.18",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"@yarnpkg/types": "^4.0.0-rc.52",
Expand All @@ -68,7 +75,6 @@
"prettier-plugin-packagejson": "^2.3.0",
"ts-jest": "^28.0.7",
"ts-node": "^10.7.0",
"tsup": "^7.2.0",
"typedoc": "^0.23.15",
"typescript": "~4.8.4"
},
Expand All @@ -82,8 +88,7 @@
},
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail": false,
"tsup>esbuild": true
"@lavamoat/preinstall-always-fail": false
}
}
}
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"emitDeclarationOnly": true,
"inlineSources": true,
"noEmit": false,
"outDir": "dist/types",
"outDir": "dist",
"rootDir": "src",
"sourceMap": true
},
Expand Down
40 changes: 0 additions & 40 deletions tsup.config.ts

This file was deleted.

21 changes: 11 additions & 10 deletions yarn.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,21 @@ module.exports = defineConfig({
workspace.set('repository.type', 'git');
workspace.set('repository.url', `${workspaceRepository}.git`);

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

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

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

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

0 comments on commit 076554c

Please sign in to comment.