From 236a4e5907b22dc5ef9725c66fdf2a963fef2fa3 Mon Sep 17 00:00:00 2001 From: Kevin Bot Date: Fri, 6 Nov 2026 16:04:58 +0900 Subject: [PATCH] fix: clear fake-js state on watch rebuilds (#346) --- src/fake-js/index.ts | 38 ++++++++++++-------------------------- tests/index.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/fake-js/index.ts b/src/fake-js/index.ts index a6c3ca7..cbd1fcb 100644 --- a/src/fake-js/index.ts +++ b/src/fake-js/index.ts @@ -83,10 +83,6 @@ export function createFakeJsPlugin({ }: Pick): Plugin { let declarationIdx = 0 const declarationMap = new Map() - const moduleDeclarationsMap = new Map< - string /* filename */, - number[] /* declaration ids */ - >() const commentsMap = new Map() const fileCommentsMap = new Map() const moduleExportsMap = new Map() @@ -95,6 +91,16 @@ export function createFakeJsPlugin({ return { name: 'rolldown-plugin-dts:fake-js', + buildStart() { + // The plugin instance lives across watch rebuilds, which transform every + // module again, so the previous build's state is never used again. + // Rolldown's dev engine reuses cached transforms and is not supported. + declarationMap.clear() + moduleExportsMap.clear() + commentsMap.clear() + fileCommentsMap.clear() + }, + outputOptions(options) { if (options.format === 'cjs' || options.format === 'commonjs') { throw new Error( @@ -195,11 +201,6 @@ export function createFakeJsPlugin({ ) } - // A watch rebuild transforms changed modules again, so drop what the - // previous transform stored. Unchanged modules may keep their cached - // output with incremental builds, so their state must stay. - clearModuleState(id) - const { program } = file moduleExportsMap.set(id, await collectModuleExports(this, program.body, id)) const identifierMap: Record = Object.create(null) @@ -341,7 +342,7 @@ export function createFakeJsPlugin({ decl.comments = stmt.comments } - const declarationId = registerDeclaration(id, { + const declarationId = registerDeclaration({ decl, deps, bindings, @@ -700,27 +701,12 @@ export function createFakeJsPlugin({ } } - function registerDeclaration(id: string, info: DeclarationInfo) { + function registerDeclaration(info: DeclarationInfo) { const declarationId = declarationIdx++ declarationMap.set(declarationId, info) - - let ids = moduleDeclarationsMap.get(id) - if (!ids) moduleDeclarationsMap.set(id, (ids = [])) - ids.push(declarationId) - return declarationId } - function clearModuleState(id: string) { - for (const declarationId of moduleDeclarationsMap.get(id) || []) { - declarationMap.delete(declarationId) - } - moduleDeclarationsMap.delete(id) - moduleExportsMap.delete(id) - commentsMap.delete(id) - fileCommentsMap.delete(id) - } - function getDeclaration(declarationId: number) { return declarationMap.get(declarationId)! } diff --git a/tests/index.test.ts b/tests/index.test.ts index 2e6ab27..7ffc41b 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url' import { normalizePath, rolldownBuild } from '@sxzz/test-utils' import { describe, expect, test, vi } from 'vitest' import { dts, resolveOptions, type Generator } from '../src/index.ts' +import type { Plugin, TransformResult } from 'rolldown' const dirname = path.dirname(fileURLToPath(import.meta.url)) @@ -999,3 +1000,42 @@ test('file doc comment removed before a rebuild', async () => { await rm(dir, { recursive: true, force: true }) } }) + +test('release the previous build state on rebuild', async () => { + const dir = await mkdtemp(path.join(os.tmpdir(), 'dts-rebuild-')) + try { + const input = path.join(dir, 'index.ts') + await writeFile(input, 'export interface A { a: string }') + + // reusing the plugins mimics a watch rebuild + let reuse = false + let cached: TransformResult + const plugins = dts({ dtsOnly: true }).map((plugin): Plugin => { + if (plugin.name !== 'rolldown-plugin-dts:fake-js') return plugin + const transform = plugin.transform as Extract< + Plugin['transform'], + { handler: unknown } + > + return { + ...plugin, + transform: { + ...transform, + async handler(...args) { + if (reuse) return cached + return (cached = await transform.handler.apply(this, args)) + }, + }, + } + }) + + const first = await rolldownBuild(input, plugins) + expect(first.snapshot).toContain('interface A') + + // The output of the previous transform refers to the declarations it + // stored, which no longer exist. + reuse = true + await expect(rolldownBuild(input, plugins)).rejects.toThrow() + } finally { + await rm(dir, { recursive: true, force: true }) + } +}) -- knotmirror 0.0.1