Skip to content

Commit 53f01f4

Browse files
Use tsup for building (#229)
* Use tsup for building * Combine build steps in CI * Update constraints.pro Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com> --------- Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
1 parent 163d7fd commit 53f01f4

File tree

7 files changed

+539
-847
lines changed

7 files changed

+539
-847
lines changed

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

Lines changed: 3 additions & 25 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
@@ -30,29 +30,7 @@ jobs:
3030
node-version-file: '.nvmrc'
3131
cache: 'yarn'
3232
- run: yarn --immutable --immutable-cache
33-
- run: yarn build:source
34-
- name: Require clean working directory
35-
shell: bash
36-
run: |
37-
if ! git diff --exit-code; then
38-
echo "Working tree dirty at end of job"
39-
exit 1
40-
fi
41-
42-
build-types:
43-
name: Build types
44-
runs-on: ubuntu-latest
45-
needs:
46-
- prepare
47-
steps:
48-
- uses: actions/checkout@v3
49-
- name: Use Node.js
50-
uses: actions/setup-node@v3
51-
with:
52-
node-version-file: '.nvmrc'
53-
cache: 'yarn'
54-
- run: yarn --immutable --immutable-cache
55-
- run: yarn build:types
33+
- run: yarn build
5634
- name: Require clean working directory
5735
shell: bash
5836
run: |

.swcrc.build.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

constraints.pro

Lines changed: 7 additions & 7 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

76-
% 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').
76+
% The module entrypoint for the package must be `./dist/index.mjs`.
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: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,21 @@
1313
"sideEffects": false,
1414
"exports": {
1515
".": {
16-
"import": "./dist/esm/index.js",
17-
"require": "./dist/cjs/index.js",
16+
"import": "./dist/index.mjs",
17+
"require": "./dist/index.js",
1818
"types": "./dist/types/index.d.ts"
1919
},
2020
"./package.json": "./package.json"
2121
},
22-
"main": "./dist/cjs/index.js",
23-
"module": "./dist/esm/index.js",
22+
"main": "./dist/index.js",
23+
"module": "./dist/index.mjs",
2424
"types": "./dist/types/index.d.ts",
2525
"files": [
26-
"dist/cjs/**",
27-
"dist/esm/**",
28-
"dist/types/**"
26+
"dist"
2927
],
3028
"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",
29+
"build": "tsup && yarn build:types",
3430
"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",
3831
"build:types": "tsc --project tsconfig.build.json",
3932
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
4033
"lint:changelog": "auto-changelog validate",
@@ -55,8 +48,6 @@
5548
"@metamask/eslint-config-jest": "^12.0.0",
5649
"@metamask/eslint-config-nodejs": "^12.0.0",
5750
"@metamask/eslint-config-typescript": "^12.0.0",
58-
"@swc/cli": "^0.1.62",
59-
"@swc/core": "^1.3.66",
6051
"@types/jest": "^28.1.6",
6152
"@types/node": "^16",
6253
"@typescript-eslint/eslint-plugin": "^5.43.0",
@@ -74,9 +65,9 @@
7465
"jest-it-up": "^2.0.2",
7566
"prettier": "^2.7.1",
7667
"prettier-plugin-packagejson": "^2.3.0",
77-
"rimraf": "^3.0.2",
7868
"ts-jest": "^28.0.7",
7969
"ts-node": "^10.7.0",
70+
"tsup": "^7.2.0",
8071
"typedoc": "^0.23.15",
8172
"typescript": "~4.8.4"
8273
},
@@ -91,7 +82,7 @@
9182
"lavamoat": {
9283
"allowScripts": {
9384
"@lavamoat/preinstall-always-fail": false,
94-
"@swc/core": true
85+
"tsup>esbuild": true
9586
}
9687
}
9788
}

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)