Skip to content

Commit 56dd503

Browse files
committed
refactor(dts): implement cjsReexport stub via Rolldown plugin
1 parent 95d727d commit 56dd503

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

src/build.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { writeFile } from 'node:fs/promises'
2-
import path from 'node:path'
31
import { bold, green } from 'ansis'
42
import { clearRequireCache } from 'import-without-cache'
53
import {
@@ -311,10 +309,6 @@ async function buildSingle(
311309
await copy(config)
312310
await buildExe(config, chunks)
313311

314-
if (format === 'cjs' && dts && dts.cjsReexport && isDualFormat) {
315-
await writeCjsDtsReexports(chunks, outDir, config.write !== false)
316-
}
317-
318312
if (!hasBuilt) {
319313
await done(bundle)
320314
}
@@ -326,27 +320,3 @@ async function buildSingle(
326320
ab = executeOnSuccess(config)
327321
}
328322
}
329-
330-
async function writeCjsDtsReexports(
331-
chunks: RolldownChunk[],
332-
outDir: string,
333-
write: boolean,
334-
): Promise<void> {
335-
for (const chunk of chunks) {
336-
if (chunk.type !== 'chunk') continue
337-
338-
// Match CJS JS output files: .cjs (fixed extension) or .js (non-fixed)
339-
const match = chunk.fileName.match(/^(.*)\.(cjs|js)$/)
340-
if (!match) continue
341-
342-
const baseName = match[1]
343-
const dCtsName =
344-
match[2] === 'cjs' ? `${baseName}.d.cts` : `${baseName}.d.ts`
345-
const dMtsBasename = path.basename(`${baseName}.d.mts`)
346-
const content = `export * from './${dMtsBasename}'\n`
347-
348-
if (write) {
349-
await writeFile(path.join(outDir, dCtsName), content)
350-
}
351-
}
352-
}

src/features/rolldown.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ async function resolveInputOptions(
133133
cjsDefault,
134134
}),
135135
)
136+
} else if (dts.cjsReexport && isDualFormat) {
137+
plugins.push(CjsDtsReexportPlugin())
136138
}
137139
}
138140
let cssPostPlugins: Plugin[] | undefined
@@ -340,6 +342,28 @@ function handlePluginInspect(plugins: RolldownPluginOption) {
340342
}
341343
}
342344

345+
export function CjsDtsReexportPlugin(): Plugin {
346+
return {
347+
name: 'tsdown:cjs-dts-reexport',
348+
generateBundle(_options, bundle) {
349+
for (const chunk of Object.values(bundle)) {
350+
if (chunk.type !== 'chunk') continue
351+
352+
const match = chunk.fileName.match(/^(.*)\.(cjs|js)$/)
353+
if (!match) continue
354+
355+
const baseName = match[1]
356+
const dCtsName =
357+
match[2] === 'cjs' ? `${baseName}.d.cts` : `${baseName}.d.ts`
358+
const dMtsBasename = path.basename(`${baseName}.d.mts`)
359+
const content = `export * from './${dMtsBasename}'\n`
360+
361+
this.emitFile({ type: 'asset', fileName: dCtsName, source: content })
362+
}
363+
},
364+
}
365+
}
366+
343367
export function CssGuardPlugin(): Plugin {
344368
return {
345369
name: 'tsdown:css-guard',

0 commit comments

Comments
 (0)