Skip to content

Commit

Permalink
Fix tsup configuration (#2220)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mrtenz authored Feb 27, 2024
1 parent 4774979 commit c521c04
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 16 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 ? {
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"luxon@^3.2.1": "patch:luxon@npm%3A3.3.0#./.yarn/patches/luxon-npm-3.3.0-bdbae9bfd5.patch",
"tsconfig-paths@^3.11.0": "patch:tsconfig-paths@npm%3A3.14.2#./.yarn/patches/tsconfig-paths-npm-3.14.2-90ce75420d.patch",
"tsconfig-paths@^3.14.1": "patch:tsconfig-paths@npm%3A3.14.2#./.yarn/patches/tsconfig-paths-npm-3.14.2-90ce75420d.patch",
"tsconfig-paths@^4.1.2": "patch:tsconfig-paths@npm%3A3.14.2#./.yarn/patches/tsconfig-paths-npm-3.14.2-90ce75420d.patch"
"tsconfig-paths@^4.1.2": "patch:tsconfig-paths@npm%3A3.14.2#./.yarn/patches/tsconfig-paths-npm-3.14.2-90ce75420d.patch",
"tsup@^8.0.1": "patch:tsup@npm%3A8.0.2#./.yarn/patches/tsup-npm-8.0.2-86e40f68a7.patch"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^3.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/create-snap/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { default: baseConfig } = require('../../tsup.config');

const config: Options = {
name: packageJson.name,
platform: 'node',
};

export default deepmerge<Options>(baseConfig, config);
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "C+SwHiqd5x46Y0F3OhL1oG9j/VrdZqisFIJcacSEBmY=",
"shasum": "jjkP8lDWArcnVeNB1YyZuFf9JSLlGvbmsOtDWVMcURc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-cli/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { default: baseConfig } = require('../../tsup.config');
const config: Options = {
name: packageJson.name,
external: ['@metamask/snaps-cli'],
platform: 'node',
};

export default deepmerge<Options>(baseConfig, config);
64 changes: 54 additions & 10 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
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',
Expand All @@ -15,26 +54,31 @@ 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'],

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

// 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,
// 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],

// 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,
Expand Down
47 changes: 43 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22275,9 +22275,48 @@ __metadata:
languageName: node
linkType: hard

"tsup@npm:^8.0.1":
version: 8.0.1
resolution: "tsup@npm:8.0.1"
"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."
dependencies:
bundle-require: ^4.0.0
cac: ^6.7.12
Expand Down Expand Up @@ -22310,7 +22349,7 @@ __metadata:
bin:
tsup: dist/cli-default.js
tsup-node: dist/cli-node.js
checksum: 7b9e7a412247e374be1f22d9aa68eec64e9bdebfdf36ac915fc24be995fc7b855d74cf210431122cec26351e4c22c0b87f0400181b1de1915a80531f4797d84a
checksum: 69cb678c075a49a4285c61ece6f70016b0c7ba7e2a958e95bce1a79b63b631563fef6b689a6729cdc0f59fcd40c99c2aac18bd14395c656d35182d670ec259bf
languageName: node
linkType: hard

Expand Down

0 comments on commit c521c04

Please sign in to comment.