Skip to content

Commit

Permalink
BREAKING: Build packages with tsup (#3998)
Browse files Browse the repository at this point in the history
## Explanation

This changes the build system to build all packages with `tsup`, instead
of `tsc`. `tsc` is still used for generating the declaration files and
type checking, and `tsup` is used for building the `.js` and (new)
`.mjs` files. The benefit of this is that we now have a ESM build as
well as a CJS build.

## References

See MetaMask/utils#144.

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/accounts-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/address-book-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/announcement-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/approval-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/assets-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/base-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/build-utils`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/composable-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/controller-utils`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/ens-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/eth-json-rpc-provider`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/gas-fee-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/json-rpc-engine`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/json-rpc-middleware-stream`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/keyring-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/logging-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/message-manager`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/name-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/network-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/notification-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/permission-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/permission-log-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/phishing-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/polling-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/preferences-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/queued-request-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/rate-limit-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/selected-network-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/signature-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/transaction-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

### `@metamask/user-operation-controller`

- **BREAKING**: Add ESM build.
  - It's no longer possible to import files from `./dist` directly.

## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
Mrtenz authored Mar 7, 2024
1 parent 964e389 commit 87a661d
Show file tree
Hide file tree
Showing 74 changed files with 1,156 additions and 70 deletions.
13 changes: 13 additions & 0 deletions .yarn/patches/tsup-npm-8.0.2-86e40f68a7.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/dist/index.js b/dist/index.js
index 4500c4e43c3bbd24aa60b7d4cf95aa3fee8eb185..9c442bc216f99b7cfadb5ac62cb98d3ae9ce2f56 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1813,6 +1813,8 @@ var cjsSplitting = () => {
}
const { transform: transform3 } = await Promise.resolve().then(() => require("sucrase"));
const result = transform3(code, {
+ // https://github.com/egoist/tsup/issues/1087
+ disableESTransforms: true,
filePath: info.path,
transforms: ["imports"],
sourceMapOptions: this.options.sourcemap ? {
25 changes: 25 additions & 0 deletions constraints.pro
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ gen_enforced_field(WorkspaceCwd, 'types', './dist/index.d.ts') :-
gen_enforced_field(WorkspaceCwd, 'types', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% The exports for all published packages must be the same.
gen_enforced_field(WorkspaceCwd, 'exports["."].import', './dist/index.mjs') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'exports["."].require', './dist/index.js') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'exports["."].types', './dist/types/index.d.ts') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
gen_enforced_field(WorkspaceCwd, 'exports["./package.json"]', './package.json') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
% Non-published packages must not specify exports.
gen_enforced_field(WorkspaceCwd, 'exports', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% Published packages must not have side effects.
gen_enforced_field(WorkspaceCwd, 'sideEffects', false) :-
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= 'packages/base-controller'.
% Non-published packages must not specify side effects.
gen_enforced_field(WorkspaceCwd, 'sideEffects', null) :-
workspace_field(WorkspaceCwd, 'private', true).

% The list of files included in published packages must only include files
% generated during the build step.
gen_enforced_field(WorkspaceCwd, 'files', ['dist/']) :-
Expand All @@ -254,6 +275,10 @@ gen_enforced_field(WorkspaceCwd, 'files', ['dist/']) :-
gen_enforced_field(WorkspaceCwd, 'files', []) :-
WorkspaceCwd = '.'.

% All non-root packages must have the same "build" script.
gen_enforced_field(WorkspaceCwd, 'scripts.build', 'tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean') :-
WorkspaceCwd \= '.'.

% All non-root packages must have the same "build:docs" script.
gen_enforced_field(WorkspaceCwd, 'scripts.build:docs', 'typedoc') :-
WorkspaceCwd \= '.'.
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"packages/*"
],
"scripts": {
"build": "tsc --build tsconfig.build.json --verbose",
"build": "yarn run build:source && yarn run build:types",
"build:clean": "rimraf dist '**/*.tsbuildinfo' && yarn build",
"build:docs": "yarn workspaces foreach --parallel --interlaced --verbose run build:docs",
"build:source": "yarn workspaces foreach --exclude @metamask/core-monorepo --parallel --interlaced --verbose run build",
"build:types": "tsc --build tsconfig.build.json --verbose",
"build:watch": "yarn run build --watch",
"changelog:update": "yarn workspaces foreach --parallel --interlaced --verbose run changelog:update",
"changelog:validate": "yarn workspaces foreach --parallel --interlaced --verbose run changelog:validate",
Expand All @@ -40,6 +42,9 @@
"simple-git-hooks": {
"pre-push": "yarn lint"
},
"resolutions": {
"tsup@^8.0.2": "patch:tsup@npm%3A8.0.2#./.yarn/patches/tsup-npm-8.0.2-86e40f68a7.patch"
},
"devDependencies": {
"@babel/core": "^7.23.5",
"@babel/plugin-transform-modules-commonjs": "^7.23.3",
Expand Down Expand Up @@ -77,9 +82,9 @@
"nock": "^13.3.1",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.4.5",
"rimraf": "^3.0.2",
"simple-git-hooks": "^2.8.0",
"ts-node": "^10.9.1",
"tsup": "^8.0.2",
"typescript": "~4.8.4",
"yargs": "^17.7.2"
},
Expand All @@ -92,7 +97,8 @@
"@lavamoat/preinstall-always-fail": false,
"@keystonehq/bc-ur-registry-eth>hdkey>secp256k1": true,
"babel-runtime>core-js": false,
"simple-git-hooks": false
"simple-git-hooks": false,
"tsup>esbuild": true
}
}
}
10 changes: 10 additions & 0 deletions packages/accounts-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/accounts-controller",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/accounts-controller",
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src",
"skipLibCheck": true
},
Expand Down
10 changes: 10 additions & 0 deletions packages/address-book-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/address-book-controller",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/address-book-controller",
Expand Down
2 changes: 1 addition & 1 deletion packages/address-book-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
10 changes: 10 additions & 0 deletions packages/announcement-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/announcement-controller",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/announcement-controller",
Expand Down
2 changes: 1 addition & 1 deletion packages/announcement-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [{ "path": "../base-controller/tsconfig.build.json" }],
Expand Down
10 changes: 10 additions & 0 deletions packages/approval-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/approval-controller",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/approval-controller",
Expand Down
2 changes: 1 addition & 1 deletion packages/approval-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
8 changes: 4 additions & 4 deletions packages/assets-controllers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 88.67,
functions: 97,
lines: 97.36,
statements: 97.41,
branches: 90.35,
functions: 96.74,
lines: 97.34,
statements: 97.36,
},
},

Expand Down
10 changes: 10 additions & 0 deletions packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/assets-controllers",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/assets-controllers",
Expand Down
2 changes: 1 addition & 1 deletion packages/assets-controllers/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
9 changes: 9 additions & 0 deletions packages/base-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/base-controller",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/base-controller",
Expand Down
2 changes: 1 addition & 1 deletion packages/base-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
10 changes: 10 additions & 0 deletions packages/build-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/build-utils",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/build-utils",
Expand Down
2 changes: 1 addition & 1 deletion packages/build-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"include": ["../../types", "./src"]
Expand Down
10 changes: 10 additions & 0 deletions packages/composable-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/composable-controller",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/composable-controller",
Expand Down
2 changes: 1 addition & 1 deletion packages/composable-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"references": [
Expand Down
10 changes: 10 additions & 0 deletions packages/controller-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/controller-utils",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/controller-utils",
Expand Down
3 changes: 1 addition & 2 deletions packages/controller-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"lib": ["ES2017", "DOM"],
"outDir": "./dist",
"outDir": "./dist/types",
"rootDir": "./src"
},
"include": ["../../types", "./src"]
Expand Down
Loading

0 comments on commit 87a661d

Please sign in to comment.