@@ -11,6 +11,7 @@ import {
1111 type Plugin ,
1212 type RolldownPluginOption ,
1313} from 'rolldown'
14+ import { filename_js_to_dts , RE_JS } from 'rolldown-plugin-dts/internal'
1415import { importGlobPlugin } from 'rolldown/experimental'
1516import pkg from '../../package.json' with { type : 'json' }
1617import { mergeUserOptions } from '../config/options.ts'
@@ -117,9 +118,10 @@ async function resolveInputOptions(
117118
118119 if ( dts ) {
119120 const { dts : dtsPlugin } = await import ( 'rolldown-plugin-dts' )
121+ const { cjsReexport : _ , ...dtsPluginOptions } = dts
120122 const options : DtsOptions = {
121123 tsconfig,
122- ...dts ,
124+ ...dtsPluginOptions ,
123125 }
124126
125127 if ( format === 'es' ) {
@@ -132,6 +134,8 @@ async function resolveInputOptions(
132134 cjsDefault,
133135 } ) ,
134136 )
137+ } else if ( dts . cjsReexport && isDualFormat ) {
138+ plugins . push ( CjsDtsReexportPlugin ( ) )
135139 }
136140 }
137141 let cssPostPlugins : Plugin [ ] | undefined
@@ -339,6 +343,31 @@ function handlePluginInspect(plugins: RolldownPluginOption) {
339343 }
340344}
341345
346+ export function CjsDtsReexportPlugin ( ) : Plugin {
347+ return {
348+ name : 'tsdown:cjs-dts-reexport' ,
349+ generateBundle ( _options , bundle ) {
350+ for ( const chunk of Object . values ( bundle ) ) {
351+ if ( chunk . type !== 'chunk' ) continue
352+
353+ if ( ! chunk . fileName . endsWith ( '.cjs' ) && ! chunk . fileName . endsWith ( '.js' ) )
354+ continue
355+
356+ const dMtsBasename = path . basename (
357+ chunk . fileName . replace ( RE_JS , '.d.mts' ) ,
358+ )
359+ const content = `export * from './${ dMtsBasename } '\n`
360+
361+ this . emitFile ( {
362+ type : 'prebuilt-chunk' ,
363+ fileName : filename_js_to_dts ( chunk . fileName ) ,
364+ code : content ,
365+ } )
366+ }
367+ } ,
368+ }
369+ }
370+
342371export function CssGuardPlugin ( ) : Plugin {
343372 return {
344373 name : 'tsdown:css-guard' ,
0 commit comments