Skip to content

Commit 236a4e5

Browse files
authored
fix: clear fake-js state on watch rebuilds (#346)
1 parent c997c4e commit 236a4e5

2 files changed

Lines changed: 52 additions & 26 deletions

File tree

‎src/fake-js/index.ts‎

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ export function createFakeJsPlugin({
8383
}: Pick<OptionsResolved, 'sourcemap' | 'cjsDefault' | 'sideEffects'>): Plugin {
8484
let declarationIdx = 0
8585
const declarationMap = new Map<number /* declaration id */, DeclarationInfo>()
86-
const moduleDeclarationsMap = new Map<
87-
string /* filename */,
88-
number[] /* declaration ids */
89-
>()
9086
const commentsMap = new Map<string /* filename */, t.Comment[]>()
9187
const fileCommentsMap = new Map<string /* filename */, t.Comment[]>()
9288
const moduleExportsMap = new Map<string /* filename */, ModuleExports>()
@@ -95,6 +91,16 @@ export function createFakeJsPlugin({
9591
return {
9692
name: 'rolldown-plugin-dts:fake-js',
9793

94+
buildStart() {
95+
// The plugin instance lives across watch rebuilds, which transform every
96+
// module again, so the previous build's state is never used again.
97+
// Rolldown's dev engine reuses cached transforms and is not supported.
98+
declarationMap.clear()
99+
moduleExportsMap.clear()
100+
commentsMap.clear()
101+
fileCommentsMap.clear()
102+
},
103+
98104
outputOptions(options) {
99105
if (options.format === 'cjs' || options.format === 'commonjs') {
100106
throw new Error(
@@ -195,11 +201,6 @@ export function createFakeJsPlugin({
195201
)
196202
}
197203

198-
// A watch rebuild transforms changed modules again, so drop what the
199-
// previous transform stored. Unchanged modules may keep their cached
200-
// output with incremental builds, so their state must stay.
201-
clearModuleState(id)
202-
203204
const { program } = file
204205
moduleExportsMap.set(id, await collectModuleExports(this, program.body, id))
205206
const identifierMap: Record<string, number> = Object.create(null)
@@ -341,7 +342,7 @@ export function createFakeJsPlugin({
341342
decl.comments = stmt.comments
342343
}
343344

344-
const declarationId = registerDeclaration(id, {
345+
const declarationId = registerDeclaration({
345346
decl,
346347
deps,
347348
bindings,
@@ -700,27 +701,12 @@ export function createFakeJsPlugin({
700701
}
701702
}
702703

703-
function registerDeclaration(id: string, info: DeclarationInfo) {
704+
function registerDeclaration(info: DeclarationInfo) {
704705
const declarationId = declarationIdx++
705706
declarationMap.set(declarationId, info)
706-
707-
let ids = moduleDeclarationsMap.get(id)
708-
if (!ids) moduleDeclarationsMap.set(id, (ids = []))
709-
ids.push(declarationId)
710-
711707
return declarationId
712708
}
713709

714-
function clearModuleState(id: string) {
715-
for (const declarationId of moduleDeclarationsMap.get(id) || []) {
716-
declarationMap.delete(declarationId)
717-
}
718-
moduleDeclarationsMap.delete(id)
719-
moduleExportsMap.delete(id)
720-
commentsMap.delete(id)
721-
fileCommentsMap.delete(id)
722-
}
723-
724710
function getDeclaration(declarationId: number) {
725711
return declarationMap.get(declarationId)!
726712
}

‎tests/index.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url'
55
import { normalizePath, rolldownBuild } from '@sxzz/test-utils'
66
import { describe, expect, test, vi } from 'vitest'
77
import { dts, resolveOptions, type Generator } from '../src/index.ts'
8+
import type { Plugin, TransformResult } from 'rolldown'
89

910
const dirname = path.dirname(fileURLToPath(import.meta.url))
1011

@@ -999,3 +1000,42 @@ test('file doc comment removed before a rebuild', async () => {
9991000
await rm(dir, { recursive: true, force: true })
10001001
}
10011002
})
1003+
1004+
test('release the previous build state on rebuild', async () => {
1005+
const dir = await mkdtemp(path.join(os.tmpdir(), 'dts-rebuild-'))
1006+
try {
1007+
const input = path.join(dir, 'index.ts')
1008+
await writeFile(input, 'export interface A { a: string }')
1009+
1010+
// reusing the plugins mimics a watch rebuild
1011+
let reuse = false
1012+
let cached: TransformResult
1013+
const plugins = dts({ dtsOnly: true }).map((plugin): Plugin => {
1014+
if (plugin.name !== 'rolldown-plugin-dts:fake-js') return plugin
1015+
const transform = plugin.transform as Extract<
1016+
Plugin['transform'],
1017+
{ handler: unknown }
1018+
>
1019+
return {
1020+
...plugin,
1021+
transform: {
1022+
...transform,
1023+
async handler(...args) {
1024+
if (reuse) return cached
1025+
return (cached = await transform.handler.apply(this, args))
1026+
},
1027+
},
1028+
}
1029+
})
1030+
1031+
const first = await rolldownBuild(input, plugins)
1032+
expect(first.snapshot).toContain('interface A')
1033+
1034+
// The output of the previous transform refers to the declarations it
1035+
// stored, which no longer exist.
1036+
reuse = true
1037+
await expect(rolldownBuild(input, plugins)).rejects.toThrow()
1038+
} finally {
1039+
await rm(dir, { recursive: true, force: true })
1040+
}
1041+
})

0 commit comments

Comments
 (0)