Skip to content

Commit 184d425

Browse files
authored
Use tsup for bundling (#144)
* Use tsup for bundling * Simplify CI workflows * Remove unused dependency * Remove unnecessary config option * Use `tsc` for generating declarations * Fix formatting of package.json * Enable code splitting * Revert some changes
1 parent 8732f00 commit 184d425

File tree

6 files changed

+553
-824
lines changed

6 files changed

+553
-824
lines changed

.github/workflows/build-lint-test.yml

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ jobs:
1717
- name: Install Yarn dependencies
1818
run: yarn --immutable
1919

20-
build-source:
21-
name: Build source
20+
build:
21+
name: Build
2222
runs-on: ubuntu-latest
2323
needs:
2424
- prepare
@@ -33,32 +33,7 @@ jobs:
3333
node-version: ${{ matrix.node-version }}
3434
cache: 'yarn'
3535
- run: yarn --immutable --immutable-cache
36-
- run: yarn build:source
37-
- name: Require clean working directory
38-
shell: bash
39-
run: |
40-
if ! git diff --exit-code; then
41-
echo "Working tree dirty at end of job"
42-
exit 1
43-
fi
44-
45-
build-types:
46-
name: Build types
47-
runs-on: ubuntu-latest
48-
needs:
49-
- prepare
50-
strategy:
51-
matrix:
52-
node-version: [16.x, 18.x, 20.x]
53-
steps:
54-
- uses: actions/checkout@v3
55-
- name: Use Node.js ${{ matrix.node-version }}
56-
uses: actions/setup-node@v3
57-
with:
58-
node-version: ${{ matrix.node-version }}
59-
cache: 'yarn'
60-
- run: yarn --immutable --immutable-cache
61-
- run: yarn build:types
36+
- run: yarn build
6237
# This step is dependent on the TypeScript build output, so it's run
6338
# here, rather than in the test job.
6439
- run: yarn test:types

constraints.pro

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ gen_enforced_field(WorkspaceCwd, 'license').
6969
gen_enforced_field(WorkspaceCwd, 'types', './dist/types/index.d.ts').
7070
gen_enforced_field(WorkspaceCwd, 'exports["."].types', './dist/types/index.d.ts').
7171

72-
% The entrypoint for the package must be `./dist/cjs/index.js`.
73-
gen_enforced_field(WorkspaceCwd, 'main', './dist/cjs/index.js').
74-
gen_enforced_field(WorkspaceCwd, 'exports["."].require', './dist/cjs/index.js').
72+
% The entrypoint for the package must be `./dist/index.js`.
73+
gen_enforced_field(WorkspaceCwd, 'main', './dist/index.js').
74+
gen_enforced_field(WorkspaceCwd, 'exports["."].require', './dist/index.js').
7575

7676
% The module entrypoint for the package must be `./dist/esm/index.js`.
77-
gen_enforced_field(WorkspaceCwd, 'module', './dist/esm/index.js').
78-
gen_enforced_field(WorkspaceCwd, 'exports["."].import', './dist/esm/index.js').
77+
gen_enforced_field(WorkspaceCwd, 'module', './dist/index.mjs').
78+
gen_enforced_field(WorkspaceCwd, 'exports["."].import', './dist/index.mjs').
7979

8080
gen_enforced_field(WorkspaceCwd, 'exports["./package.json"]', './package.json').
8181

8282
% The list of files included in the package must only include files generated
8383
% during the build step.
84-
gen_enforced_field(WorkspaceCwd, 'files', ['dist/cjs/**', 'dist/esm/**', 'dist/types/**']).
84+
gen_enforced_field(WorkspaceCwd, 'files', ['dist']).
8585

8686
% If a dependency is listed under "dependencies", it should not be listed under
8787
% "devDependencies".

package.json

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,24 @@
1111
"url": "https://github.com/MetaMask/utils.git"
1212
},
1313
"license": "ISC",
14+
"sideEffects": false,
1415
"exports": {
1516
".": {
16-
"import": "./dist/esm/index.js",
17-
"require": "./dist/cjs/index.js",
17+
"import": "./dist/index.mjs",
18+
"require": "./dist/index.js",
1819
"types": "./dist/types/index.d.ts"
1920
},
2021
"./package.json": "./package.json"
2122
},
22-
"main": "./dist/cjs/index.js",
23-
"module": "./dist/esm/index.js",
23+
"main": "./dist/index.js",
24+
"module": "./dist/index.mjs",
2425
"types": "./dist/types/index.d.ts",
2526
"files": [
26-
"dist/cjs/**",
27-
"dist/esm/**",
28-
"dist/types/**"
27+
"dist"
2928
],
3029
"scripts": {
31-
"build": "yarn build:source && yarn build:types",
32-
"build:cjs": "swc src --out-dir dist/cjs --config-file .swcrc.build.json --config module.type=commonjs",
33-
"build:clean": "rimraf dist && yarn build",
30+
"build": "tsup && yarn build:types",
3431
"build:docs": "typedoc",
35-
"build:esm": "swc src --out-dir dist/esm --config-file .swcrc.build.json --config module.type=es6 && yarn build:esm:package",
36-
"build:esm:package": "echo >dist/esm/package.json \"{\\\"type\\\":\\\"module\\\"}\"",
37-
"build:source": "yarn build:esm && yarn build:cjs",
3832
"build:types": "tsc --project tsconfig.build.json",
3933
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
4034
"lint:changelog": "auto-changelog validate",
@@ -69,8 +63,6 @@
6963
"@metamask/eslint-config-jest": "^12.0.0",
7064
"@metamask/eslint-config-nodejs": "^12.0.0",
7165
"@metamask/eslint-config-typescript": "^12.0.0",
72-
"@swc/cli": "^0.1.62",
73-
"@swc/core": "^1.3.66",
7466
"@types/jest": "^28.1.7",
7567
"@types/node": "^17.0.23",
7668
"@typescript-eslint/eslint-plugin": "^5.43.0",
@@ -88,11 +80,11 @@
8880
"jest-it-up": "^2.0.2",
8981
"prettier": "^2.7.1",
9082
"prettier-plugin-packagejson": "^2.3.0",
91-
"rimraf": "^3.0.2",
9283
"stdio-mock": "^1.2.0",
9384
"ts-jest": "^29.0.3",
9485
"ts-node": "^10.7.0",
9586
"tsd": "^0.24.1",
87+
"tsup": "^7.2.0",
9688
"typedoc": "^0.23.15",
9789
"typescript": "~4.8.4"
9890
},
@@ -107,7 +99,7 @@
10799
"lavamoat": {
108100
"allowScripts": {
109101
"@lavamoat/preinstall-always-fail": false,
110-
"@swc/core": true
102+
"tsup>esbuild": true
111103
}
112104
},
113105
"tsd": {

tsconfig.build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"./src/**/__tests__/**/*",
1919
"./src/**/__snapshots__/**/*",
2020
"./src/**/*.test.ts",
21+
"./src/**/*.test-d.ts",
2122
"./src/**/*.test.*.ts"
2223
]
2324
}

tsup.config.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { defineConfig } from 'tsup';
2+
3+
export default defineConfig({
4+
// The entry to bundle.
5+
entry: [
6+
'src/**/*.ts',
7+
'!src/**/__fixtures__/**/*',
8+
'!src/**/__mocks__/**/*',
9+
'!src/**/__test__/**/*',
10+
'!src/**/__tests__/**/*',
11+
'!src/**/__snapshots__/**/*',
12+
'!src/**/*.test.ts',
13+
'!src/**/*.test-d.ts',
14+
'!src/**/*.test.*.ts',
15+
],
16+
17+
// The output formats. We want to generate both CommonJS and ESM bundles.
18+
// https://tsup.egoist.dev/#bundle-formats
19+
format: ['cjs', 'esm'],
20+
21+
// Generate sourcemaps as separate files.
22+
// https://tsup.egoist.dev/#generate-sourcemap-file
23+
sourcemap: true,
24+
25+
// Clean the dist folder before bundling.
26+
clean: true,
27+
28+
// Enables shimming of `__dirname` and `import.meta.url`, so that they work
29+
// in both CommonJS and ESM.
30+
// https://tsup.egoist.dev/#inject-cjs-and-esm-shims
31+
shims: true,
32+
33+
// Hide unnecessary logs from the console. Warnings and errors will still be
34+
// shown.
35+
silent: true,
36+
37+
// Split the output into chunks. This is useful for tree-shaking.
38+
// https://tsup.egoist.dev/#code-splitting
39+
splitting: true,
40+
});

0 commit comments

Comments
 (0)