From ab1e622dc1daad537fa9300a14375b9626755213 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 22 Feb 2024 10:48:52 +0100 Subject: [PATCH] Build packages with `tsup` (#2120) This changes all packages to be built with `tsup`, instead of SWC. `tsup` uses `esbuild` under the hood, so performance should be comparable. More context here: MetaMask/utils#144. --- .github/workflows/build-lint-test.yml | 4 +- constraints.pro | 8 +- packages/create-snap/tsup.config.ts | 2 - .../packages/bip32/snap.manifest.json | 2 +- .../packages/bip44/snap.manifest.json | 2 +- .../packages/browserify/snap.manifest.json | 2 +- .../packages/cronjobs/snap.manifest.json | 2 +- .../packages/dialogs/snap.manifest.json | 2 +- .../packages/ethers-js/snap.manifest.json | 2 +- .../packages/get-entropy/snap.manifest.json | 2 +- .../packages/home-page/snap.manifest.json | 2 +- .../packages/images/snap.manifest.json | 2 +- .../interactive-ui/snap.manifest.json | 2 +- .../packages/core-signer/snap.manifest.json | 2 +- .../lifecycle-hooks/snap.manifest.json | 2 +- .../packages/manage-state/snap.manifest.json | 2 +- .../packages/notifications/snap.manifest.json | 2 +- .../signature-insights/snap.manifest.json | 2 +- .../transaction-insights/snap.manifest.json | 2 +- packages/snaps-cli/package.json | 6 +- packages/snaps-cli/tsup.config.ts | 1 - packages/snaps-controllers/package.json | 16 +- .../lavamoat/browserify/iframe/policy.json | 5 + .../browserify/node-process/policy.json | 5 + .../browserify/node-thread/policy.json | 5 + .../lavamoat/browserify/webview/policy.json | 5 + .../browserify/worker-executor/policy.json | 5 + .../browserify/worker-pool/policy.json | 5 + .../snaps-execution-environments/package.json | 2 +- packages/snaps-rpc-methods/package.json | 2 +- packages/snaps-sdk/tsup.config.ts | 8 - packages/snaps-utils/package.json | 15 +- tsup.config.ts | 64 +---- yarn.lock | 240 ++++++++---------- 34 files changed, 182 insertions(+), 248 deletions(-) diff --git a/.github/workflows/build-lint-test.yml b/.github/workflows/build-lint-test.yml index dce7f83c98..21c8a2b8b1 100644 --- a/.github/workflows/build-lint-test.yml +++ b/.github/workflows/build-lint-test.yml @@ -48,9 +48,9 @@ jobs: runs-on: ubuntu-latest needs: prepare steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v3 - name: Setup Node - uses: actions/setup-node@v4 + uses: actions/setup-node@v3 with: node-version-file: '.nvmrc' cache: yarn diff --git a/constraints.pro b/constraints.pro index 258537ce64..89891f0a5a 100644 --- a/constraints.pro +++ b/constraints.pro @@ -194,8 +194,7 @@ gen_enforced_field(WorkspaceCwd, 'files', ['dist']) :- \+ is_example(WorkspaceCwd), \+ workspace_field(WorkspaceCwd, 'private', true), WorkspaceCwd \= '.', - WorkspaceCwd \= 'packages/snaps-jest', - WorkspaceCwd \= 'packages/snaps-cli'. + WorkspaceCwd \= 'packages/snaps-jest'. gen_enforced_field(WorkspaceCwd, 'files', ['dist', 'jest-preset.js']) :- WorkspaceCwd = 'packages/snaps-jest'. @@ -205,9 +204,8 @@ gen_enforced_field(WorkspaceCwd, 'scripts.build', 'tsup --clean && yarn build:ty \+ workspace_field(WorkspaceCwd, 'private', true), WorkspaceCwd \= '.', WorkspaceCwd \= 'packages/snaps-simulator', - WorkspaceCwd \= 'packages/snaps-cli', - WorkspaceCwd \= 'packages/snaps-execution-environments'. -gen_enforced_field(WorkspaceCwd, 'scripts.build:types', 'tsc --project tsconfig.build.json') :- + WorkspaceCwd \= 'packages/snaps-cli'. +gen_enforced_field(WorkspaceCwd, 'build:types', 'tsc --project tsconfig.build.json') :- \+ is_example(WorkspaceCwd), \+ workspace_field(WorkspaceCwd, 'private', true), WorkspaceCwd \= '.'. diff --git a/packages/create-snap/tsup.config.ts b/packages/create-snap/tsup.config.ts index d62dc9943a..3eaf645296 100644 --- a/packages/create-snap/tsup.config.ts +++ b/packages/create-snap/tsup.config.ts @@ -9,8 +9,6 @@ const { default: baseConfig } = require('../../tsup.config'); const config: Options = { name: packageJson.name, - external: ['@metamask/create-snap'], - platform: 'node', }; export default deepmerge(baseConfig, config); diff --git a/packages/examples/packages/bip32/snap.manifest.json b/packages/examples/packages/bip32/snap.manifest.json index 3b267e324d..806f6a80d9 100644 --- a/packages/examples/packages/bip32/snap.manifest.json +++ b/packages/examples/packages/bip32/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "NVydltk67wE3e1uAxlmal62P2EcP+cMfsNZ66EyJ7O4=", + "shasum": "AaLbshZePKcSV2NORnmd5AjzkX5vNQToD3PaE4YpUUQ=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/bip44/snap.manifest.json b/packages/examples/packages/bip44/snap.manifest.json index c293c60441..535056c361 100644 --- a/packages/examples/packages/bip44/snap.manifest.json +++ b/packages/examples/packages/bip44/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "cGgUQlMw+WDBaoWHvG4s/xaZO47vBrEp9sa+4DhBmTA=", + "shasum": "q5WeZJXrE5mgP2Z6Fo/aJrVuspPsQgsUn3/SmHuwsGY=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/browserify/snap.manifest.json b/packages/examples/packages/browserify/snap.manifest.json index 8b6c7845f5..dbb4d0e47b 100644 --- a/packages/examples/packages/browserify/snap.manifest.json +++ b/packages/examples/packages/browserify/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "8pImYekIrQ0oaGuxf8Lta25MPIq2td4KPEVQ8jWgos8=", + "shasum": "anrkssxnQgvz6naNxnXCSIHGLkc2FoDcKx0BeDoWR/k=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/cronjobs/snap.manifest.json b/packages/examples/packages/cronjobs/snap.manifest.json index e34c77c957..70429b363e 100644 --- a/packages/examples/packages/cronjobs/snap.manifest.json +++ b/packages/examples/packages/cronjobs/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "J8ekYCK9+rqN9d6qBw85RomNqdXMbkB9s1eHnQkaABs=", + "shasum": "wXAAi/QR4BT1Gp6OiSqCpq+3U7pyRtMQ/LP9LeWwEPk=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/dialogs/snap.manifest.json b/packages/examples/packages/dialogs/snap.manifest.json index 4ea43ce894..e2064466bd 100644 --- a/packages/examples/packages/dialogs/snap.manifest.json +++ b/packages/examples/packages/dialogs/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "2omN9u/lT2cXPZ3O6j4wG3W3HBn/6No9JxH4ncqUG6M=", + "shasum": "EepV6igtZ0ZCX579Ws6f9u0/kYnp65+6OK1SgzCm9Dg=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/ethers-js/snap.manifest.json b/packages/examples/packages/ethers-js/snap.manifest.json index aa6eb589dd..ab57b9e44e 100644 --- a/packages/examples/packages/ethers-js/snap.manifest.json +++ b/packages/examples/packages/ethers-js/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "dHiE+9mYVkqNRma5GsZprTawv/bDUzcdbeciRzCmkhQ=", + "shasum": "AjzlrjmJQTKwGM77EXcpEzjF9bsKK7up36BSA4qhasU=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/get-entropy/snap.manifest.json b/packages/examples/packages/get-entropy/snap.manifest.json index 1d6dde395d..9f165f4448 100644 --- a/packages/examples/packages/get-entropy/snap.manifest.json +++ b/packages/examples/packages/get-entropy/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "dTL+fLvEFKI0vtcRZ0ZGxU2menzmWl3rPuOwnsqNMIQ=", + "shasum": "9jRaEw4/0RCp3b3oAI+PK2zcgwupdOnBhaL4zX4pKRg=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/home-page/snap.manifest.json b/packages/examples/packages/home-page/snap.manifest.json index bd88f90b88..90b32d9063 100644 --- a/packages/examples/packages/home-page/snap.manifest.json +++ b/packages/examples/packages/home-page/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "+RC3pwNreHxiMjByPmX0vglQye6wRfdfqC3/Y20tnc8=", + "shasum": "4LSF+30bmPlizM/wYBrq4dp72wkcca9rwYyqkz7ZPvY=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/images/snap.manifest.json b/packages/examples/packages/images/snap.manifest.json index 1e11745c25..eb945df190 100644 --- a/packages/examples/packages/images/snap.manifest.json +++ b/packages/examples/packages/images/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "SxUO9T9xJ/R+jHURdjaLvy0Wfq+6+7CkovvO0CB0wxY=", + "shasum": "v2cJ3d1fomwpUzSR+XZCLF88e5k0NOm5aVo8dftKKSU=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/interactive-ui/snap.manifest.json b/packages/examples/packages/interactive-ui/snap.manifest.json index 77c4462af9..fc3025d10a 100644 --- a/packages/examples/packages/interactive-ui/snap.manifest.json +++ b/packages/examples/packages/interactive-ui/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "opdcmTNMX2T252PmyJIM5ctTJvFr2OzddoIHYXMBgew=", + "shasum": "sKqHlIDCKGyzDhV7wlJTv7RR6m6lrJF4KgyoTacwYao=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/invoke-snap/packages/core-signer/snap.manifest.json b/packages/examples/packages/invoke-snap/packages/core-signer/snap.manifest.json index c992acccd7..36b4bcbcb8 100644 --- a/packages/examples/packages/invoke-snap/packages/core-signer/snap.manifest.json +++ b/packages/examples/packages/invoke-snap/packages/core-signer/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "p2DDVeRmZ1peIJOSFP/mkzIX+8TUj8R0Rrh+6cgB1bA=", + "shasum": "ojUZu1+nqCNBVoWHAaDkNRqWx+JVQfaKTOcyGDFD2Wg=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/lifecycle-hooks/snap.manifest.json b/packages/examples/packages/lifecycle-hooks/snap.manifest.json index 9886fb0582..5dfafd0a6a 100644 --- a/packages/examples/packages/lifecycle-hooks/snap.manifest.json +++ b/packages/examples/packages/lifecycle-hooks/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "BXlcBtRno7GhLlq0cqal08ALFJa9G+DP/a1Ad7cTUKY=", + "shasum": "cyr+aslb2DLAQOj2fs5pBwp3l1WKZU6muxrmhsbAtks=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/manage-state/snap.manifest.json b/packages/examples/packages/manage-state/snap.manifest.json index 679cc28ee6..6923ea5c6f 100644 --- a/packages/examples/packages/manage-state/snap.manifest.json +++ b/packages/examples/packages/manage-state/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "f9PoXGMUUbGJtHHvKt715/pvWiHR3KogkfWAV07itaM=", + "shasum": "bTGiQYMk4+EtULq0PhJ3qru/EhL+2gW7G2SPh8mz4XY=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/notifications/snap.manifest.json b/packages/examples/packages/notifications/snap.manifest.json index 2a39342155..14194e5316 100644 --- a/packages/examples/packages/notifications/snap.manifest.json +++ b/packages/examples/packages/notifications/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "MV7VAvTwHITNDAKVY71bURgbuwxSegyMd/mTAwrjiSg=", + "shasum": "vR6A7OJh/69dD8OisLbtz+PPHEd2oK+cS6h01E0K+/U=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/signature-insights/snap.manifest.json b/packages/examples/packages/signature-insights/snap.manifest.json index 68f0b6a33d..95c0dff0cf 100644 --- a/packages/examples/packages/signature-insights/snap.manifest.json +++ b/packages/examples/packages/signature-insights/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "8vwJeiNQLkL9YZxlLrfL6tK7Vd3cITEax5aSm6xiDlU=", + "shasum": "cn53UKSOkd/fFq93Nwl2DBkMartFXwyyeoArG9Zxp8w=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/transaction-insights/snap.manifest.json b/packages/examples/packages/transaction-insights/snap.manifest.json index 982f499a53..4daf1c2920 100644 --- a/packages/examples/packages/transaction-insights/snap.manifest.json +++ b/packages/examples/packages/transaction-insights/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "76NEiG+oW2yBCnvjY9TAjkitGYBfZ9hipf+i2l2e8uE=", + "shasum": "fT6pbcejyNwb8cjgHPXgFWhpzCNYFuWUIrkuqLP70vU=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snaps-cli/package.json b/packages/snaps-cli/package.json index c3a4274480..a94afd8cda 100644 --- a/packages/snaps-cli/package.json +++ b/packages/snaps-cli/package.json @@ -14,8 +14,7 @@ "require": "./dist/index.js", "types": "./dist/types/index.d.ts" }, - "./package.json": "./package.json", - "./browserslistrc": "./.browserslistrc" + "./package.json": "./package.json" }, "main": "./dist/index.js", "module": "./dist/index.mjs", @@ -24,8 +23,7 @@ "mm-snap": "./dist/main.js" }, "files": [ - "dist", - ".browserslistrc" + "dist" ], "scripts": { "build": "tsup --clean && yarn build:types && yarn build:chmod && yarn build:readme", diff --git a/packages/snaps-cli/tsup.config.ts b/packages/snaps-cli/tsup.config.ts index 3cfca96324..5af30151f5 100644 --- a/packages/snaps-cli/tsup.config.ts +++ b/packages/snaps-cli/tsup.config.ts @@ -10,7 +10,6 @@ const { default: baseConfig } = require('../../tsup.config'); const config: Options = { name: packageJson.name, external: ['@metamask/snaps-cli'], - platform: 'node', }; export default deepmerge(baseConfig, config); diff --git a/packages/snaps-controllers/package.json b/packages/snaps-controllers/package.json index 2db99c7e10..20a2e0f57e 100644 --- a/packages/snaps-controllers/package.json +++ b/packages/snaps-controllers/package.json @@ -13,20 +13,16 @@ "require": "./dist/index.js", "types": "./dist/types/index.d.ts" }, - "./node": { - "import": "./dist/node.mjs", - "require": "./dist/node.js", - "types": "./dist/types/node.d.ts" - }, - "./react-native": { - "import": "./dist/react-native.mjs", - "require": "./dist/react-native.js", - "types": "./dist/types/react-native.d.ts" - }, "./package.json": "./package.json" }, "main": "./dist/index.js", "module": "./dist/index.mjs", + "browser": { + "./dist/cjs/services": "./dist/cjs/services/browser.js", + "./dist/esm/services": "./dist/esm/services/browser.js" + }, + "main": "./dist/index.js", + "module": "./dist/index.mjs", "types": "./dist/types/index.d.ts", "files": [ "dist" diff --git a/packages/snaps-execution-environments/lavamoat/browserify/iframe/policy.json b/packages/snaps-execution-environments/lavamoat/browserify/iframe/policy.json index 0dfa39a79b..a192975819 100644 --- a/packages/snaps-execution-environments/lavamoat/browserify/iframe/policy.json +++ b/packages/snaps-execution-environments/lavamoat/browserify/iframe/policy.json @@ -190,6 +190,11 @@ "depcheck>semver>lru-cache>yallist": true } }, + "depcheck>semver>lru-cache": { + "packages": { + "depcheck>semver>lru-cache>yallist": true + } + }, "readable-stream": { "packages": { "browserify>browser-resolve": true, diff --git a/packages/snaps-execution-environments/lavamoat/browserify/node-process/policy.json b/packages/snaps-execution-environments/lavamoat/browserify/node-process/policy.json index ae4dafe535..25a050789c 100644 --- a/packages/snaps-execution-environments/lavamoat/browserify/node-process/policy.json +++ b/packages/snaps-execution-environments/lavamoat/browserify/node-process/policy.json @@ -209,6 +209,11 @@ "depcheck>semver>lru-cache>yallist": true } }, + "depcheck>semver>lru-cache": { + "packages": { + "depcheck>semver>lru-cache>yallist": true + } + }, "istanbul-lib-report>supports-color>has-flag": { "globals": { "process.argv": true diff --git a/packages/snaps-execution-environments/lavamoat/browserify/node-thread/policy.json b/packages/snaps-execution-environments/lavamoat/browserify/node-thread/policy.json index ae4dafe535..25a050789c 100644 --- a/packages/snaps-execution-environments/lavamoat/browserify/node-thread/policy.json +++ b/packages/snaps-execution-environments/lavamoat/browserify/node-thread/policy.json @@ -209,6 +209,11 @@ "depcheck>semver>lru-cache>yallist": true } }, + "depcheck>semver>lru-cache": { + "packages": { + "depcheck>semver>lru-cache>yallist": true + } + }, "istanbul-lib-report>supports-color>has-flag": { "globals": { "process.argv": true diff --git a/packages/snaps-execution-environments/lavamoat/browserify/webview/policy.json b/packages/snaps-execution-environments/lavamoat/browserify/webview/policy.json index 63e69a30b9..03fc4b2e24 100644 --- a/packages/snaps-execution-environments/lavamoat/browserify/webview/policy.json +++ b/packages/snaps-execution-environments/lavamoat/browserify/webview/policy.json @@ -129,6 +129,11 @@ "depcheck>semver>lru-cache>yallist": true } }, + "depcheck>semver>lru-cache": { + "packages": { + "depcheck>semver>lru-cache>yallist": true + } + }, "readable-stream": { "packages": { "browserify>browser-resolve": true, diff --git a/packages/snaps-execution-environments/lavamoat/browserify/worker-executor/policy.json b/packages/snaps-execution-environments/lavamoat/browserify/worker-executor/policy.json index 0dfa39a79b..a192975819 100644 --- a/packages/snaps-execution-environments/lavamoat/browserify/worker-executor/policy.json +++ b/packages/snaps-execution-environments/lavamoat/browserify/worker-executor/policy.json @@ -190,6 +190,11 @@ "depcheck>semver>lru-cache>yallist": true } }, + "depcheck>semver>lru-cache": { + "packages": { + "depcheck>semver>lru-cache>yallist": true + } + }, "readable-stream": { "packages": { "browserify>browser-resolve": true, diff --git a/packages/snaps-execution-environments/lavamoat/browserify/worker-pool/policy.json b/packages/snaps-execution-environments/lavamoat/browserify/worker-pool/policy.json index 63e69a30b9..03fc4b2e24 100644 --- a/packages/snaps-execution-environments/lavamoat/browserify/worker-pool/policy.json +++ b/packages/snaps-execution-environments/lavamoat/browserify/worker-pool/policy.json @@ -129,6 +129,11 @@ "depcheck>semver>lru-cache>yallist": true } }, + "depcheck>semver>lru-cache": { + "packages": { + "depcheck>semver>lru-cache>yallist": true + } + }, "readable-stream": { "packages": { "browserify>browser-resolve": true, diff --git a/packages/snaps-execution-environments/package.json b/packages/snaps-execution-environments/package.json index 1a08f4cd96..26517c5550 100644 --- a/packages/snaps-execution-environments/package.json +++ b/packages/snaps-execution-environments/package.json @@ -39,7 +39,7 @@ "lint:changelog": "../../scripts/validate-changelog.sh @metamask/snaps-execution-environments", "lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:changelog && yarn lint:dependencies", "clean": "rimraf '*.tsbuildinfo' 'dist' 'src/__GENERATED__/' 'coverage/*' '__test__/*'", - "build": "tsup --clean && yarn build:types && yarn build:lavamoat", + "build": "tsup --clean && yarn build:types", "build:types": "tsc --project tsconfig.build.json", "build:lavamoat": "lavamoat scripts/build.js --policy lavamoat/build-system/policy.json --policyOverride lavamoat/build-system/policy-override.json", "build:lavamoat:policy": "yarn build:lavamoat --writeAutoPolicy && node scripts/build.js --writeAutoPolicy", diff --git a/packages/snaps-rpc-methods/package.json b/packages/snaps-rpc-methods/package.json index 404617ffcb..a7216012ba 100644 --- a/packages/snaps-rpc-methods/package.json +++ b/packages/snaps-rpc-methods/package.json @@ -56,7 +56,7 @@ "@metamask/eslint-config-jest": "^12.1.0", "@metamask/eslint-config-nodejs": "^12.1.0", "@metamask/eslint-config-typescript": "^12.1.0", - "@metamask/json-rpc-engine": "^8.0.1", + "@metamask/json-rpc-engine": "^7.3.2", "@swc/core": "1.3.78", "@swc/jest": "^0.2.26", "@types/node": "18.14.2", diff --git a/packages/snaps-sdk/tsup.config.ts b/packages/snaps-sdk/tsup.config.ts index d1b43a0b64..3eaf645296 100644 --- a/packages/snaps-sdk/tsup.config.ts +++ b/packages/snaps-sdk/tsup.config.ts @@ -7,16 +7,8 @@ import packageJson from './package.json'; // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires const { default: baseConfig } = require('../../tsup.config'); -delete baseConfig.entry; - const config: Options = { name: packageJson.name, - entry: ['src/index.ts'], - - // Esbuild is not deterministic when code splitting is enabled. This is - // problematic for building the example Snaps in this repository, so we - // disable code splitting here. - splitting: false, }; export default deepmerge(baseConfig, config); diff --git a/packages/snaps-utils/package.json b/packages/snaps-utils/package.json index d36de5524a..329457d2ad 100644 --- a/packages/snaps-utils/package.json +++ b/packages/snaps-utils/package.json @@ -8,15 +8,14 @@ "sideEffects": false, "exports": { ".": { + "browser": { + "import": "./dist/index.browser.mjs", + "require": "./dist/index.browser.js" + }, "import": "./dist/index.mjs", "require": "./dist/index.js", "types": "./dist/types/index.d.ts" }, - "./node": { - "import": "./dist/node.mjs", - "require": "./dist/node.js", - "types": "./dist/types/node.d.ts" - }, "./test-utils": { "import": "./dist/test-utils/index.mjs", "require": "./dist/test-utils/index.js", @@ -26,6 +25,12 @@ }, "main": "./dist/index.js", "module": "./dist/index.mjs", + "browser": { + "./dist/index.js": "./dist/index.browser.js", + "./dist/index.mjs": "./dist/index.browser.mjs" + }, + "main": "./dist/index.js", + "module": "./dist/index.mjs", "types": "./dist/types/index.d.ts", "files": [ "dist" diff --git a/tsup.config.ts b/tsup.config.ts index 614d77fef1..785addc8d6 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,45 +1,6 @@ -import { builtinModules } from 'module'; import type { Options } from 'tsup'; -// `Plugin` is not exported from `tsup`, so we have to define it ourselves. -type Plugin = Options['plugins'][number]; - -const DIRNAME_SHIM = `import { fileURLToPath } from 'url' -import path from 'path' - -const getFilename = () => fileURLToPath(import.meta.url) -const getDirname = () => path.dirname(getFilename()) - -export const __dirname = /* @__PURE__ */ getDirname()`; - -/** - * A `tsup` plugin that adds a `__dirname` shim to the beginning of each chunk - * that uses `__dirname`. This is necessary because `__dirname` is not available - * in ESM, so we have to use `import.meta.url` and `fileURLToPath` to get the - * dirname of the current file. - * - * Note: This breaks source maps in the files that use `__dirname`. - */ -const dirnameShimPlugin: Plugin = { - name: 'dirname-shim-plugin', - - renderChunk(code, info) { - if ( - info.type !== 'chunk' || - this.format === 'cjs' || - !code.includes('__dirname') - ) { - return undefined; - } - - return { code: `${DIRNAME_SHIM}\n${code}`, map: info.map }; - }, -}; - const config: Options = { - // Clean the dist folder before bundling. - clean: true, - // The entry to bundle. entry: [ 'src/**/*.ts', @@ -54,31 +15,26 @@ const config: Options = { '!src/**/*.test.*.ts', ], - // External modules that should not be processed by `tsup`. We want to - // exclude built-in Node.js modules from the bundle. - // https://tsup.egoist.dev/#excluding-packages - external: builtinModules, - // The output formats. We want to generate both CommonJS and ESM bundles. // https://tsup.egoist.dev/#bundle-formats format: ['cjs', 'esm'], - // The platform to target when generating the bundles. `neutral` means that - // the bundles will work in both Node.js and browsers. - platform: 'neutral', + // Generate sourcemaps as separate files. + // https://tsup.egoist.dev/#generate-sourcemap-file + sourcemap: true, - // The plugins to use when bundling. We add a plugin that adds a `__dirname` - // shim to the beginning of each chunk that uses `__dirname`. - plugins: [dirnameShimPlugin], + // Clean the dist folder before bundling. + clean: true, + + // Enables shimming of `__dirname` and `import.meta.url`, so that they work + // in both CommonJS and ESM. + // https://tsup.egoist.dev/#inject-cjs-and-esm-shims + shims: true, // Hide unnecessary logs from the console. Warnings and errors will still be // shown. silent: true, - // Generate sourcemaps as separate files. - // https://tsup.egoist.dev/#generate-sourcemap-file - sourcemap: true, - // Split the output into chunks. This is useful for tree-shaking. // https://tsup.egoist.dev/#code-splitting splitting: true, diff --git a/yarn.lock b/yarn.lock index 13d0bfac93..3824f17c93 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2843,9 +2843,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/aix-ppc64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/aix-ppc64@npm:0.19.12" +"@esbuild/aix-ppc64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/aix-ppc64@npm:0.19.11" conditions: os=aix & cpu=ppc64 languageName: node linkType: hard @@ -2857,9 +2857,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/android-arm64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/android-arm64@npm:0.19.12" +"@esbuild/android-arm64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/android-arm64@npm:0.19.11" conditions: os=android & cpu=arm64 languageName: node linkType: hard @@ -2871,9 +2871,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/android-arm@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/android-arm@npm:0.19.12" +"@esbuild/android-arm@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/android-arm@npm:0.19.11" conditions: os=android & cpu=arm languageName: node linkType: hard @@ -2885,9 +2885,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/android-x64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/android-x64@npm:0.19.12" +"@esbuild/android-x64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/android-x64@npm:0.19.11" conditions: os=android & cpu=x64 languageName: node linkType: hard @@ -2899,9 +2899,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/darwin-arm64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/darwin-arm64@npm:0.19.12" +"@esbuild/darwin-arm64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/darwin-arm64@npm:0.19.11" conditions: os=darwin & cpu=arm64 languageName: node linkType: hard @@ -2913,9 +2913,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/darwin-x64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/darwin-x64@npm:0.19.12" +"@esbuild/darwin-x64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/darwin-x64@npm:0.19.11" conditions: os=darwin & cpu=x64 languageName: node linkType: hard @@ -2927,9 +2927,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/freebsd-arm64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/freebsd-arm64@npm:0.19.12" +"@esbuild/freebsd-arm64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/freebsd-arm64@npm:0.19.11" conditions: os=freebsd & cpu=arm64 languageName: node linkType: hard @@ -2941,9 +2941,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/freebsd-x64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/freebsd-x64@npm:0.19.12" +"@esbuild/freebsd-x64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/freebsd-x64@npm:0.19.11" conditions: os=freebsd & cpu=x64 languageName: node linkType: hard @@ -2955,9 +2955,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-arm64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-arm64@npm:0.19.12" +"@esbuild/linux-arm64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-arm64@npm:0.19.11" conditions: os=linux & cpu=arm64 languageName: node linkType: hard @@ -2969,9 +2969,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-arm@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-arm@npm:0.19.12" +"@esbuild/linux-arm@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-arm@npm:0.19.11" conditions: os=linux & cpu=arm languageName: node linkType: hard @@ -2983,9 +2983,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-ia32@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-ia32@npm:0.19.12" +"@esbuild/linux-ia32@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-ia32@npm:0.19.11" conditions: os=linux & cpu=ia32 languageName: node linkType: hard @@ -3004,9 +3004,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-loong64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-loong64@npm:0.19.12" +"@esbuild/linux-loong64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-loong64@npm:0.19.11" conditions: os=linux & cpu=loong64 languageName: node linkType: hard @@ -3018,9 +3018,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-mips64el@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-mips64el@npm:0.19.12" +"@esbuild/linux-mips64el@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-mips64el@npm:0.19.11" conditions: os=linux & cpu=mips64el languageName: node linkType: hard @@ -3032,9 +3032,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-ppc64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-ppc64@npm:0.19.12" +"@esbuild/linux-ppc64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-ppc64@npm:0.19.11" conditions: os=linux & cpu=ppc64 languageName: node linkType: hard @@ -3046,9 +3046,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-riscv64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-riscv64@npm:0.19.12" +"@esbuild/linux-riscv64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-riscv64@npm:0.19.11" conditions: os=linux & cpu=riscv64 languageName: node linkType: hard @@ -3060,9 +3060,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-s390x@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-s390x@npm:0.19.12" +"@esbuild/linux-s390x@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-s390x@npm:0.19.11" conditions: os=linux & cpu=s390x languageName: node linkType: hard @@ -3074,9 +3074,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/linux-x64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/linux-x64@npm:0.19.12" +"@esbuild/linux-x64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/linux-x64@npm:0.19.11" conditions: os=linux & cpu=x64 languageName: node linkType: hard @@ -3088,9 +3088,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/netbsd-x64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/netbsd-x64@npm:0.19.12" +"@esbuild/netbsd-x64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/netbsd-x64@npm:0.19.11" conditions: os=netbsd & cpu=x64 languageName: node linkType: hard @@ -3102,9 +3102,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/openbsd-x64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/openbsd-x64@npm:0.19.12" +"@esbuild/openbsd-x64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/openbsd-x64@npm:0.19.11" conditions: os=openbsd & cpu=x64 languageName: node linkType: hard @@ -3116,9 +3116,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/sunos-x64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/sunos-x64@npm:0.19.12" +"@esbuild/sunos-x64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/sunos-x64@npm:0.19.11" conditions: os=sunos & cpu=x64 languageName: node linkType: hard @@ -3130,9 +3130,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/win32-arm64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/win32-arm64@npm:0.19.12" +"@esbuild/win32-arm64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/win32-arm64@npm:0.19.11" conditions: os=win32 & cpu=arm64 languageName: node linkType: hard @@ -3144,9 +3144,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/win32-ia32@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/win32-ia32@npm:0.19.12" +"@esbuild/win32-ia32@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/win32-ia32@npm:0.19.11" conditions: os=win32 & cpu=ia32 languageName: node linkType: hard @@ -3158,9 +3158,9 @@ __metadata: languageName: node linkType: hard -"@esbuild/win32-x64@npm:0.19.12": - version: 0.19.12 - resolution: "@esbuild/win32-x64@npm:0.19.12" +"@esbuild/win32-x64@npm:0.19.11": + version: 0.19.11 + resolution: "@esbuild/win32-x64@npm:0.19.11" conditions: os=win32 & cpu=x64 languageName: node linkType: hard @@ -4205,6 +4205,7 @@ __metadata: "@metamask/eslint-config-nodejs": ^12.1.0 "@metamask/eslint-config-typescript": ^12.1.0 "@metamask/snaps-utils": "workspace:^" + "@metamask/utils": ^8.3.0 "@swc/core": 1.3.78 "@swc/jest": ^0.2.26 "@types/jest": ^27.5.1 @@ -12443,32 +12444,32 @@ __metadata: linkType: hard "esbuild@npm:^0.19.2": - version: 0.19.12 - resolution: "esbuild@npm:0.19.12" - dependencies: - "@esbuild/aix-ppc64": 0.19.12 - "@esbuild/android-arm": 0.19.12 - "@esbuild/android-arm64": 0.19.12 - "@esbuild/android-x64": 0.19.12 - "@esbuild/darwin-arm64": 0.19.12 - "@esbuild/darwin-x64": 0.19.12 - "@esbuild/freebsd-arm64": 0.19.12 - "@esbuild/freebsd-x64": 0.19.12 - "@esbuild/linux-arm": 0.19.12 - "@esbuild/linux-arm64": 0.19.12 - "@esbuild/linux-ia32": 0.19.12 - "@esbuild/linux-loong64": 0.19.12 - "@esbuild/linux-mips64el": 0.19.12 - "@esbuild/linux-ppc64": 0.19.12 - "@esbuild/linux-riscv64": 0.19.12 - "@esbuild/linux-s390x": 0.19.12 - "@esbuild/linux-x64": 0.19.12 - "@esbuild/netbsd-x64": 0.19.12 - "@esbuild/openbsd-x64": 0.19.12 - "@esbuild/sunos-x64": 0.19.12 - "@esbuild/win32-arm64": 0.19.12 - "@esbuild/win32-ia32": 0.19.12 - "@esbuild/win32-x64": 0.19.12 + version: 0.19.11 + resolution: "esbuild@npm:0.19.11" + dependencies: + "@esbuild/aix-ppc64": 0.19.11 + "@esbuild/android-arm": 0.19.11 + "@esbuild/android-arm64": 0.19.11 + "@esbuild/android-x64": 0.19.11 + "@esbuild/darwin-arm64": 0.19.11 + "@esbuild/darwin-x64": 0.19.11 + "@esbuild/freebsd-arm64": 0.19.11 + "@esbuild/freebsd-x64": 0.19.11 + "@esbuild/linux-arm": 0.19.11 + "@esbuild/linux-arm64": 0.19.11 + "@esbuild/linux-ia32": 0.19.11 + "@esbuild/linux-loong64": 0.19.11 + "@esbuild/linux-mips64el": 0.19.11 + "@esbuild/linux-ppc64": 0.19.11 + "@esbuild/linux-riscv64": 0.19.11 + "@esbuild/linux-s390x": 0.19.11 + "@esbuild/linux-x64": 0.19.11 + "@esbuild/netbsd-x64": 0.19.11 + "@esbuild/openbsd-x64": 0.19.11 + "@esbuild/sunos-x64": 0.19.11 + "@esbuild/win32-arm64": 0.19.11 + "@esbuild/win32-ia32": 0.19.11 + "@esbuild/win32-x64": 0.19.11 dependenciesMeta: "@esbuild/aix-ppc64": optional: true @@ -12518,7 +12519,7 @@ __metadata: optional: true bin: esbuild: bin/esbuild - checksum: 2936e29107b43e65a775b78b7bc66ddd7d76febd73840ac7e825fb22b65029422ff51038a08d19b05154f543584bd3afe7d1ef1c63900429475b17fbe61cb61f + checksum: ae949a796d1d06b55275ae7491ce137857468f69a93d8cc9c0943d2a701ac54e14dbb250a2ba56f2ad98283669578f1ec3bd85a4681910a5ff29a2470c3bd62c languageName: node linkType: hard @@ -22135,48 +22136,9 @@ __metadata: languageName: node linkType: hard -"tsup@npm:8.0.2": - version: 8.0.2 - resolution: "tsup@npm:8.0.2" - dependencies: - bundle-require: ^4.0.0 - cac: ^6.7.12 - chokidar: ^3.5.1 - debug: ^4.3.1 - esbuild: ^0.19.2 - execa: ^5.0.0 - globby: ^11.0.3 - joycon: ^3.0.1 - postcss-load-config: ^4.0.1 - resolve-from: ^5.0.0 - rollup: ^4.0.2 - source-map: 0.8.0-beta.0 - sucrase: ^3.20.3 - tree-kill: ^1.2.2 - peerDependencies: - "@microsoft/api-extractor": ^7.36.0 - "@swc/core": ^1 - postcss: ^8.4.12 - typescript: ">=4.5.0" - peerDependenciesMeta: - "@microsoft/api-extractor": - optional: true - "@swc/core": - optional: true - postcss: - optional: true - typescript: - optional: true - bin: - tsup: dist/cli-default.js - tsup-node: dist/cli-node.js - checksum: ebd0c662efdc2a04e80251aa11832d3def9cf3bf120c579975af6d50183fa0397d07d5bcee0688258a91c154a3c5db72ee4c5dca367b58552d225bc8a89d67d0 - languageName: node - linkType: hard - -"tsup@patch:tsup@npm%3A8.0.2#./.yarn/patches/tsup-npm-8.0.2-86e40f68a7.patch::locator=root%40workspace%3A.": - version: 8.0.2 - resolution: "tsup@patch:tsup@npm%3A8.0.2#./.yarn/patches/tsup-npm-8.0.2-86e40f68a7.patch::version=8.0.2&hash=ce4dd6&locator=root%40workspace%3A." +"tsup@npm:^8.0.1": + version: 8.0.1 + resolution: "tsup@npm:8.0.1" dependencies: bundle-require: ^4.0.0 cac: ^6.7.12 @@ -22209,7 +22171,7 @@ __metadata: bin: tsup: dist/cli-default.js tsup-node: dist/cli-node.js - checksum: 69cb678c075a49a4285c61ece6f70016b0c7ba7e2a958e95bce1a79b63b631563fef6b689a6729cdc0f59fcd40c99c2aac18bd14395c656d35182d670ec259bf + checksum: 7b9e7a412247e374be1f22d9aa68eec64e9bdebfdf36ac915fc24be995fc7b855d74cf210431122cec26351e4c22c0b87f0400181b1de1915a80531f4797d84a languageName: node linkType: hard