Skip to content

Commit c521c04

Browse files
authored
Fix tsup configuration (#2220)
This changes the `tsup` config to better support LavaMoat, and removes the need for some polyfills. Also makes the bundles compatible with the Storybook bundler in the extension.
1 parent 4774979 commit c521c04

File tree

7 files changed

+115
-16
lines changed

7 files changed

+115
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/dist/index.js b/dist/index.js
2+
index 4500c4e43c3bbd24aa60b7d4cf95aa3fee8eb185..9c442bc216f99b7cfadb5ac62cb98d3ae9ce2f56 100644
3+
--- a/dist/index.js
4+
+++ b/dist/index.js
5+
@@ -1813,6 +1813,8 @@ var cjsSplitting = () => {
6+
}
7+
const { transform: transform3 } = await Promise.resolve().then(() => require("sucrase"));
8+
const result = transform3(code, {
9+
+ // https://github.com/egoist/tsup/issues/1087
10+
+ disableESTransforms: true,
11+
filePath: info.path,
12+
transforms: ["imports"],
13+
sourceMapOptions: this.options.sourcemap ? {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
"luxon@^3.2.1": "patch:luxon@npm%3A3.3.0#./.yarn/patches/luxon-npm-3.3.0-bdbae9bfd5.patch",
6666
"tsconfig-paths@^3.11.0": "patch:tsconfig-paths@npm%3A3.14.2#./.yarn/patches/tsconfig-paths-npm-3.14.2-90ce75420d.patch",
6767
"tsconfig-paths@^3.14.1": "patch:tsconfig-paths@npm%3A3.14.2#./.yarn/patches/tsconfig-paths-npm-3.14.2-90ce75420d.patch",
68-
"tsconfig-paths@^4.1.2": "patch:tsconfig-paths@npm%3A3.14.2#./.yarn/patches/tsconfig-paths-npm-3.14.2-90ce75420d.patch"
68+
"tsconfig-paths@^4.1.2": "patch:tsconfig-paths@npm%3A3.14.2#./.yarn/patches/tsconfig-paths-npm-3.14.2-90ce75420d.patch",
69+
"tsup@^8.0.1": "patch:tsup@npm%3A8.0.2#./.yarn/patches/tsup-npm-8.0.2-86e40f68a7.patch"
6970
},
7071
"devDependencies": {
7172
"@lavamoat/allow-scripts": "^3.0.2",

packages/create-snap/tsup.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { default: baseConfig } = require('../../tsup.config');
99

1010
const config: Options = {
1111
name: packageJson.name,
12+
platform: 'node',
1213
};
1314

1415
export default deepmerge<Options>(baseConfig, config);

packages/examples/packages/browserify/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "C+SwHiqd5x46Y0F3OhL1oG9j/VrdZqisFIJcacSEBmY=",
10+
"shasum": "jjkP8lDWArcnVeNB1YyZuFf9JSLlGvbmsOtDWVMcURc=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snaps-cli/tsup.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { default: baseConfig } = require('../../tsup.config');
1010
const config: Options = {
1111
name: packageJson.name,
1212
external: ['@metamask/snaps-cli'],
13+
platform: 'node',
1314
};
1415

1516
export default deepmerge<Options>(baseConfig, config);

tsup.config.ts

+54-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
1+
import { builtinModules } from 'module';
12
import type { Options } from 'tsup';
23

4+
// `Plugin` is not exported from `tsup`, so we have to define it ourselves.
5+
type Plugin = Options['plugins'][number];
6+
7+
const DIRNAME_SHIM = `import { fileURLToPath } from 'url'
8+
import path from 'path'
9+
10+
const getFilename = () => fileURLToPath(import.meta.url)
11+
const getDirname = () => path.dirname(getFilename())
12+
13+
export const __dirname = /* @__PURE__ */ getDirname()`;
14+
15+
/**
16+
* A `tsup` plugin that adds a `__dirname` shim to the beginning of each chunk
17+
* that uses `__dirname`. This is necessary because `__dirname` is not available
18+
* in ESM, so we have to use `import.meta.url` and `fileURLToPath` to get the
19+
* dirname of the current file.
20+
*
21+
* Note: This breaks source maps in the files that use `__dirname`.
22+
*/
23+
const dirnameShimPlugin: Plugin = {
24+
name: 'dirname-shim-plugin',
25+
26+
renderChunk(code, info) {
27+
if (
28+
info.type !== 'chunk' ||
29+
this.format === 'cjs' ||
30+
!code.includes('__dirname')
31+
) {
32+
return undefined;
33+
}
34+
35+
return { code: `${DIRNAME_SHIM}\n${code}`, map: info.map };
36+
},
37+
};
38+
339
const config: Options = {
40+
// Clean the dist folder before bundling.
41+
clean: true,
42+
443
// The entry to bundle.
544
entry: [
645
'src/**/*.ts',
@@ -15,26 +54,31 @@ const config: Options = {
1554
'!src/**/*.test.*.ts',
1655
],
1756

57+
// External modules that should not be processed by `tsup`. We want to
58+
// exclude built-in Node.js modules from the bundle.
59+
// https://tsup.egoist.dev/#excluding-packages
60+
external: builtinModules,
61+
1862
// The output formats. We want to generate both CommonJS and ESM bundles.
1963
// https://tsup.egoist.dev/#bundle-formats
2064
format: ['cjs', 'esm'],
2165

22-
// Generate sourcemaps as separate files.
23-
// https://tsup.egoist.dev/#generate-sourcemap-file
24-
sourcemap: true,
66+
// The platform to target when generating the bundles. `neutral` means that
67+
// the bundles will work in both Node.js and browsers.
68+
platform: 'neutral',
2569

26-
// Clean the dist folder before bundling.
27-
clean: true,
28-
29-
// Enables shimming of `__dirname` and `import.meta.url`, so that they work
30-
// in both CommonJS and ESM.
31-
// https://tsup.egoist.dev/#inject-cjs-and-esm-shims
32-
shims: true,
70+
// The plugins to use when bundling. We add a plugin that adds a `__dirname`
71+
// shim to the beginning of each chunk that uses `__dirname`.
72+
plugins: [dirnameShimPlugin],
3373

3474
// Hide unnecessary logs from the console. Warnings and errors will still be
3575
// shown.
3676
silent: true,
3777

78+
// Generate sourcemaps as separate files.
79+
// https://tsup.egoist.dev/#generate-sourcemap-file
80+
sourcemap: true,
81+
3882
// Split the output into chunks. This is useful for tree-shaking.
3983
// https://tsup.egoist.dev/#code-splitting
4084
splitting: true,

yarn.lock

+43-4
Original file line numberDiff line numberDiff line change
@@ -22275,9 +22275,48 @@ __metadata:
2227522275
languageName: node
2227622276
linkType: hard
2227722277

22278-
"tsup@npm:^8.0.1":
22279-
version: 8.0.1
22280-
resolution: "tsup@npm:8.0.1"
22278+
"tsup@npm:8.0.2":
22279+
version: 8.0.2
22280+
resolution: "tsup@npm:8.0.2"
22281+
dependencies:
22282+
bundle-require: ^4.0.0
22283+
cac: ^6.7.12
22284+
chokidar: ^3.5.1
22285+
debug: ^4.3.1
22286+
esbuild: ^0.19.2
22287+
execa: ^5.0.0
22288+
globby: ^11.0.3
22289+
joycon: ^3.0.1
22290+
postcss-load-config: ^4.0.1
22291+
resolve-from: ^5.0.0
22292+
rollup: ^4.0.2
22293+
source-map: 0.8.0-beta.0
22294+
sucrase: ^3.20.3
22295+
tree-kill: ^1.2.2
22296+
peerDependencies:
22297+
"@microsoft/api-extractor": ^7.36.0
22298+
"@swc/core": ^1
22299+
postcss: ^8.4.12
22300+
typescript: ">=4.5.0"
22301+
peerDependenciesMeta:
22302+
"@microsoft/api-extractor":
22303+
optional: true
22304+
"@swc/core":
22305+
optional: true
22306+
postcss:
22307+
optional: true
22308+
typescript:
22309+
optional: true
22310+
bin:
22311+
tsup: dist/cli-default.js
22312+
tsup-node: dist/cli-node.js
22313+
checksum: ebd0c662efdc2a04e80251aa11832d3def9cf3bf120c579975af6d50183fa0397d07d5bcee0688258a91c154a3c5db72ee4c5dca367b58552d225bc8a89d67d0
22314+
languageName: node
22315+
linkType: hard
22316+
22317+
"tsup@patch:tsup@npm%3A8.0.2#./.yarn/patches/tsup-npm-8.0.2-86e40f68a7.patch::locator=root%40workspace%3A.":
22318+
version: 8.0.2
22319+
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."
2228122320
dependencies:
2228222321
bundle-require: ^4.0.0
2228322322
cac: ^6.7.12
@@ -22310,7 +22349,7 @@ __metadata:
2231022349
bin:
2231122350
tsup: dist/cli-default.js
2231222351
tsup-node: dist/cli-node.js
22313-
checksum: 7b9e7a412247e374be1f22d9aa68eec64e9bdebfdf36ac915fc24be995fc7b855d74cf210431122cec26351e4c22c0b87f0400181b1de1915a80531f4797d84a
22352+
checksum: 69cb678c075a49a4285c61ece6f70016b0c7ba7e2a958e95bce1a79b63b631563fef6b689a6729cdc0f59fcd40c99c2aac18bd14395c656d35182d670ec259bf
2231422353
languageName: node
2231522354
linkType: hard
2231622355

0 commit comments

Comments
 (0)