1+ import { writeFile } from 'node:fs/promises'
2+ import path from 'node:path'
13import { bold , green } from 'ansis'
24import { clearRequireCache } from 'import-without-cache'
35import {
@@ -290,16 +292,18 @@ async function buildSingle(
290292
291293 const configs : BuildOptions [ ] = [ buildOptions ]
292294 if ( format === 'cjs' && dts ) {
293- configs . push (
294- await getBuildOptions (
295- config ,
296- format ,
297- configFiles ,
298- bundle ,
299- true ,
300- isDualFormat ,
301- ) ,
302- )
295+ if ( ! isDualFormat || ! dts . cjsReexport ) {
296+ configs . push (
297+ await getBuildOptions (
298+ config ,
299+ format ,
300+ configFiles ,
301+ bundle ,
302+ true ,
303+ isDualFormat ,
304+ ) ,
305+ )
306+ }
303307 }
304308
305309 return configs
@@ -308,6 +312,11 @@ async function buildSingle(
308312 async function postBuild ( ) {
309313 await copy ( config )
310314 await buildExe ( config , chunks )
315+
316+ if ( format === 'cjs' && dts && dts . cjsReexport && isDualFormat ) {
317+ await writeCjsDtsReexports ( chunks , outDir , config . write !== false )
318+ }
319+
311320 if ( ! hasBuilt ) {
312321 await done ( bundle )
313322 }
@@ -319,3 +328,27 @@ async function buildSingle(
319328 ab = executeOnSuccess ( config )
320329 }
321330}
331+
332+ async function writeCjsDtsReexports (
333+ chunks : RolldownChunk [ ] ,
334+ outDir : string ,
335+ write : boolean ,
336+ ) : Promise < void > {
337+ for ( const chunk of chunks ) {
338+ if ( chunk . type !== 'chunk' ) continue
339+
340+ // Match CJS JS output files: .cjs (fixed extension) or .js (non-fixed)
341+ const match = chunk . fileName . match ( / ^ ( .* ) \. ( c j s | j s ) $ / )
342+ if ( ! match ) continue
343+
344+ const baseName = match [ 1 ]
345+ const dCtsName =
346+ match [ 2 ] === 'cjs' ? `${ baseName } .d.cts` : `${ baseName } .d.ts`
347+ const dMtsBasename = path . basename ( `${ baseName } .d.mts` )
348+ const content = `export * from './${ dMtsBasename } '\n`
349+
350+ if ( write ) {
351+ await writeFile ( path . join ( outDir , dCtsName ) , content )
352+ }
353+ }
354+ }
0 commit comments