Skip to content

Commit 5c78ae6

Browse files
committed
Switch build to tsdown
1 parent 6ada87e commit 5c78ae6

10 files changed

Lines changed: 795 additions & 1264 deletions

config/plugins/esbuild/graphQLImportPlugin.ts

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

config/plugins/esbuild/resolveCoreImportsPlugin.ts

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

config/plugins/esbuild/copyWorkerPlugin.ts renamed to config/plugins/rolldown/copyWorkerPlugin.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import path from 'node:path'
33
import crypto from 'crypto'
44
import minify from 'babel-minify'
55
import { invariant } from 'outvariant'
6-
import type { Plugin } from 'esbuild'
7-
import copyServiceWorker from '../../copyServiceWorker.js'
6+
import type { TsdownPlugin } from 'tsdown'
7+
import copyServiceWorker from '../../copyServiceWorker.ts'
88

99
const SERVICE_WORKER_ENTRY_PATH = path.resolve(
1010
process.cwd(),
@@ -27,15 +27,16 @@ export function getWorkerChecksum(): string {
2727
return getChecksum(workerContents)
2828
}
2929

30-
export function copyWorkerPlugin(checksum: string): Plugin {
30+
export function copyWorkerPlugin(checksum: string): TsdownPlugin {
3131
return {
3232
name: 'copyWorkerPlugin',
33-
async setup(build) {
33+
async buildStart() {
3434
invariant(
3535
SERVICE_WORKER_ENTRY_PATH,
3636
'Failed to locate the worker script source file',
3737
)
38-
38+
},
39+
async writeBundle() {
3940
if (fs.existsSync(SERVICE_WORKER_OUTPUT_PATH)) {
4041
console.warn(
4142
'Skipped copying the worker script to "%s": already exists',
@@ -44,31 +45,14 @@ export function copyWorkerPlugin(checksum: string): Plugin {
4445
return
4546
}
4647

47-
// Generate the checksum from the worker script's contents.
48-
// const workerContents = await fs.readFile(workerSourcePath, 'utf8')
49-
// const checksum = getChecksum(workerContents)
50-
51-
build.onLoad({ filter: /mockServiceWorker\.js$/ }, async () => {
52-
return {
53-
// Prevent the worker script from being transpiled.
54-
// But, generally, the worker script is not in the entrypoints.
55-
contents: '',
56-
}
57-
})
48+
// eslint-disable-next-line no-console
49+
console.log('worker script checksum:', checksum)
5850

59-
build.onEnd(() => {
60-
// eslint-disable-next-line no-console
61-
console.log('worker script checksum:', checksum)
62-
63-
// Copy the worker script on the next tick.
64-
process.nextTick(async () => {
65-
await copyServiceWorker(
66-
SERVICE_WORKER_ENTRY_PATH,
67-
SERVICE_WORKER_OUTPUT_PATH,
68-
checksum,
69-
)
70-
})
71-
})
51+
await copyServiceWorker(
52+
SERVICE_WORKER_ENTRY_PATH,
53+
SERVICE_WORKER_OUTPUT_PATH,
54+
checksum,
55+
)
7256
},
7357
}
7458
}

config/plugins/esbuild/forceEsmExtensionsPlugin.ts renamed to config/plugins/rolldown/forceFileExtensionsPlugin.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1-
import { type Plugin } from 'esbuild'
1+
import type { TsdownPlugin } from 'tsdown'
22

33
export const ESM_EXTENSION = '.mjs'
44
export const CJS_EXTENSION = '.js'
55

6-
export function forceEsmExtensionsPlugin(): Plugin {
6+
export function forceFileExtensionsPlugin(): TsdownPlugin {
77
return {
8-
name: 'forceEsmExtensionsPlugin',
9-
setup(build) {
10-
const isEsm = build.initialOptions.format === 'esm'
8+
name: 'forceFileExtensionsPlugin',
9+
renderChunk(code, chunk, outputOptions) {
10+
const isEsm = outputOptions.format === 'es'
1111

12-
build.onEnd(async (result) => {
13-
if (result.errors.length > 0) {
14-
return
15-
}
16-
17-
for (const outputFile of result.outputFiles || []) {
18-
// Only target CJS/ESM files.
19-
// This ignores additional files emitted, like sourcemaps ("*.js.map").
20-
if (
21-
!(
22-
outputFile.path.endsWith(ESM_EXTENSION) ||
23-
outputFile.path.endsWith('.mjs')
24-
)
25-
) {
26-
continue
27-
}
28-
29-
const fileContents = outputFile.text
30-
const nextFileContents = modifyRelativeImports(fileContents, isEsm)
12+
if (!(chunk.fileName.endsWith(ESM_EXTENSION) || isEsm)) {
13+
return
14+
}
3115

32-
outputFile.contents = Buffer.from(nextFileContents)
33-
}
34-
})
16+
return {
17+
code: modifyRelativeImports(code, isEsm),
18+
map: null,
19+
}
3520
},
3621
}
3722
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { TsdownPlugin } from 'tsdown'
2+
3+
/**
4+
* A plugin to replace `require('graphql')` statements with `await import('graphql')`
5+
* only for ESM bundles. This makes the GraphQL module to be imported lazily
6+
* while maintaining the CommonJS compatibility.
7+
* @see https://github.com/mswjs/msw/issues/2254
8+
*/
9+
export function graphqlImportPlugin(): TsdownPlugin {
10+
return {
11+
name: 'graphql-import-plugin',
12+
transform(code, id) {
13+
if (!id.endsWith('.ts') || !code.includes(`require('graphql')`)) {
14+
return
15+
}
16+
17+
return {
18+
code: code.replace(
19+
/require\(['"]graphql['"]\)/g,
20+
`await import('graphql').catch((error) => {console.error('[MSW] Failed to parse a GraphQL query: cannot import the "graphql" module. Please make sure you install it if you wish to intercept GraphQL requests. See the original import error below.'); throw error})`,
21+
),
22+
map: null,
23+
}
24+
},
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import path from 'node:path'
2+
import type { TsdownPlugin } from 'tsdown'
3+
import { replaceCoreImports } from '../../replaceCoreImports.js'
4+
import { ESM_EXTENSION } from './forceFileExtensionsPlugin.ts'
5+
6+
export function resolveCoreImportsPlugin(): TsdownPlugin {
7+
return {
8+
name: 'resolveCoreImportsPlugin',
9+
renderChunk(code, chunk, outputOptions) {
10+
const isEsm = chunk.fileName.endsWith(ESM_EXTENSION)
11+
const moduleFilePath = path.resolve(outputOptions.dir!, chunk.fileName)
12+
13+
return {
14+
code: replaceCoreImports(moduleFilePath, code, isEsm),
15+
map: null,
16+
}
17+
},
18+
}
19+
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@
177177
"node": ">=22"
178178
},
179179
"scripts": {
180-
"start": "tsup --watch",
180+
"start": "tsdown --watch",
181181
"clean": "rimraf ./lib",
182182
"lint": "oxlint ./cli ./src ./test",
183-
"build": "pnpm clean && cross-env NODE_ENV=production tsup && pnpm patch:dts",
183+
"build": "pnpm clean && cross-env NODE_ENV=production tsdown && pnpm patch:dts",
184184
"patch:dts": "node \"./config/scripts/patch-ts.js\"",
185185
"publint": "publint",
186186
"test": "pnpm test:unit && pnpm test:node && pnpm test:browser && pnpm test:native && pnpm test:memory",
@@ -303,7 +303,7 @@
303303
"regenerator-runtime": "^0.14.1",
304304
"rimraf": "^6.1.3",
305305
"simple-git-hooks": "^2.13.1",
306-
"tsup": "^8.5.1",
306+
"tsdown": "^0.22.3",
307307
"typescript": "^5.9.3",
308308
"undici": "^8.1.0",
309309
"url-loader": "^4.1.1",

0 commit comments

Comments
 (0)