Skip to content

Commit 8b91ec6

Browse files
committed
fix: skip emit dts for js files if dts already exists
closes rolldown/tsdown#936
1 parent c76f8ba commit 8b91ec6

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

src/generate.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fork, type ChildProcess } from 'node:child_process'
22
import { existsSync } from 'node:fs'
3-
import { readFile } from 'node:fs/promises'
3+
import { access, readFile } from 'node:fs/promises'
44
import path from 'node:path'
55
import { parse } from '@babel/parser'
66
import { createDebug } from 'obug'
@@ -42,6 +42,7 @@ export interface TsModule {
4242
/** `.ts` file name */
4343
id: string
4444
isEntry: boolean
45+
jsFile: boolean
4546
}
4647
/** dts filename -> ts module */
4748
export type DtsMap = Map<string, TsModule>
@@ -190,15 +191,15 @@ export function createGeneratePlugin({
190191
},
191192
},
192193
handler(code, id) {
193-
const shouldEmit = !RE_JS.test(id) || emitJs
194+
const jsFile = RE_JS.test(id)
194195

195-
if (shouldEmit) {
196+
if (!jsFile || emitJs) {
196197
const mod = this.getModuleInfo(id)
197198
const isEntry = entryMatcher
198199
? entryMatcher(path.relative(cwd, id))
199200
: !!mod?.isEntry
200201
const dtsId = filename_to_dts(id)
201-
dtsMap.set(dtsId, { code, id, isEntry })
202+
dtsMap.set(dtsId, { code, id, isEntry, jsFile })
202203
debug('register dts source: %s', id)
203204

204205
if (isEntry) {
@@ -226,9 +227,20 @@ export function createGeneratePlugin({
226227
},
227228
},
228229
async handler(dtsId) {
229-
if (!dtsMap.has(dtsId)) return
230+
const module = dtsMap.get(dtsId)
231+
if (!module) return
232+
233+
const { code, id, jsFile } = module
234+
if (
235+
jsFile &&
236+
(await access(dtsId)
237+
.then(() => true)
238+
.catch(() => false))
239+
) {
240+
debug('dts file already exists for %s, skipping generation', id)
241+
return
242+
}
230243

231-
const { code, id } = dtsMap.get(dtsId)!
232244
let dtsCode: string | undefined
233245
let map: SourceMapInput | undefined
234246
debug('generate dts %s from %s', dtsId, id)

0 commit comments

Comments
 (0)