From 5b584b4e7d02b4e7d128b25ae1ff5bf7bdd9a58d Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Sun, 5 Jul 2026 14:37:49 +0800 Subject: [PATCH] refactor: replace birpc with minimal worker IPC Co-Authored-By: Claude Fable 5 --- package.json | 1 - pnpm-lock.yaml | 6 ---- pnpm-workspace.yaml | 1 - src/generate.ts | 73 ++++++++++++++++++++++++++++++++++----------- src/tsc/worker.ts | 29 +++++++++++++----- 5 files changed, 78 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index decc52e..a3c496c 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,6 @@ } }, "dependencies": { - "birpc": "catalog:prod", "dts-resolver": "catalog:prod", "get-tsconfig": "catalog:prod", "obug": "catalog:prod", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b793f69..d33f96e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -73,9 +73,6 @@ catalogs: specifier: ^4.4.3 version: 4.4.3 prod: - birpc: - specifier: ^4.0.0 - version: 4.0.0 dts-resolver: specifier: ^3.0.0 version: 3.0.0 @@ -105,9 +102,6 @@ importers: '@ts-macro/tsc': specifier: ^0.3.6 version: 0.3.7(rollup@4.62.2) - birpc: - specifier: catalog:prod - version: 4.0.0 dts-resolver: specifier: catalog:prod version: 3.0.0 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 0e3a2e8..47f0570 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -28,7 +28,6 @@ catalog: catalogs: prod: - birpc: ^4.0.0 dts-resolver: ^3.0.0 get-tsconfig: 5.0.0-beta.5 obug: ^2.1.3 diff --git a/src/generate.ts b/src/generate.ts index b5d427e..3eaa56c 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -1,4 +1,4 @@ -import { fork, type ChildProcess } from 'node:child_process' +import { fork } from 'node:child_process' import { existsSync } from 'node:fs' import { access, readFile } from 'node:fs/promises' import path from 'node:path' @@ -28,8 +28,7 @@ import { import { runTsgo, type TsgoContext } from './tsgo.ts' import type { OptionsResolved } from './options.ts' import type { TscOptions, TscResult } from './tsc/index.ts' -import type { TscFunctions } from './tsc/worker.ts' -import type { BirpcReturn } from 'birpc' +import type { WorkerRequest, WorkerResponse } from './tsc/worker.ts' import type { Plugin, SourceMapInput } from 'rolldown' const debug = createDebug('rolldown-plugin-dts:generate') @@ -103,8 +102,7 @@ export function createGeneratePlugin({ */ const inputAliasMap = new Map() - let childProcess: ChildProcess | undefined - let rpc: BirpcReturn | undefined + let tscWorker: TscWorker | undefined let tscModule: typeof import('./tsc/index.ts') let tscContext: TscContext | undefined let tsgoContext: TsgoContext | undefined @@ -119,16 +117,7 @@ export function createGeneratePlugin({ } else if (!oxc) { // tsc if (parallel) { - childProcess = fork(new URL(WORKER_URL, import.meta.url), { - stdio: 'inherit', - }) - rpc = (await import('birpc')).createBirpc( - {}, - { - post: (data) => childProcess!.send(data), - on: (fn) => childProcess!.on('message', fn), - }, - ) + tscWorker = createTscWorker() } else { tscModule = await import('./tsc/index.ts') if (newContext) { @@ -304,7 +293,7 @@ export function createGeneratePlugin({ } let result: TscResult if (parallel) { - result = await rpc!.tscEmit(options) + result = await tscWorker!.emit(options) } else { result = tscModule.tscEmit(options) } @@ -370,7 +359,8 @@ export { __json_default_export as default }` : undefined, async buildEnd() { - childProcess?.kill() + tscWorker?.kill() + tscWorker = undefined await tsgoContext?.dispose() tsgoContext = undefined if (newContext) { @@ -386,6 +376,55 @@ export { __json_default_export as default }` } } +interface TscWorker { + emit: (options: TscOptions) => Promise + kill: () => void +} + +function createTscWorker(): TscWorker { + const childProcess = fork(new URL(WORKER_URL, import.meta.url), { + stdio: 'inherit', + serialization: 'advanced', + }) + + const pending = new Map< + number, + { + resolve: (result: TscResult) => void + reject: (error: unknown) => void + } + >() + let nextId = 0 + + childProcess.on('message', (response: WorkerResponse) => { + const handler = pending.get(response.id) + if (!handler) return + pending.delete(response.id) + if (response.error) { + handler.reject(response.error) + } else { + handler.resolve(response.result!) + } + }) + + childProcess.on('exit', (code) => { + for (const handler of pending.values()) { + handler.reject(new Error(`tsc worker exited with code ${code}`)) + } + pending.clear() + }) + + return { + emit: (options) => + new Promise((resolve, reject) => { + const id = nextId++ + pending.set(id, { resolve, reject }) + childProcess.send({ id, options } satisfies WorkerRequest) + }), + kill: () => childProcess.kill(), + } +} + function collectJsonExportMap(code: string): Map { const exportMap = new Map() const { program } = parse(code, { sourceType: 'module', lang: 'dts' }) diff --git a/src/tsc/worker.ts b/src/tsc/worker.ts index 302113f..1ac54ad 100644 --- a/src/tsc/worker.ts +++ b/src/tsc/worker.ts @@ -1,11 +1,26 @@ import process from 'node:process' -import { createBirpc } from 'birpc' -import { tscEmit } from './index.ts' +import { tscEmit, type TscOptions, type TscResult } from './index.ts' -const functions: { tscEmit: typeof tscEmit } = { tscEmit } -export type TscFunctions = typeof functions +export interface WorkerRequest { + id: number + options: TscOptions +} -createBirpc(functions, { - post: (data) => process.send!(data), - on: (fn) => process.on('message', fn), +export interface WorkerResponse { + id: number + result?: TscResult + error?: unknown +} + +process.on('message', (request: WorkerRequest) => { + let response: WorkerResponse + try { + response = { id: request.id, result: tscEmit(request.options) } + } catch (error) { + response = { + id: request.id, + error, + } + } + process.send!(response) }) -- knotmirror 0.0.1