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 {
@@ -289,7 +291,7 @@ async function buildSingle(
289291 }
290292
291293 const configs : BuildOptions [ ] = [ buildOptions ]
292- if ( format === 'cjs' && dts ) {
294+ if ( format === 'cjs' && dts && ( ! isDualFormat || ! dts . cjsReexport ) ) {
293295 configs . push (
294296 await getBuildOptions (
295297 config ,
@@ -308,6 +310,11 @@ async function buildSingle(
308310 async function postBuild ( ) {
309311 await copy ( config )
310312 await buildExe ( config , chunks )
313+
314+ if ( format === 'cjs' && dts && dts . cjsReexport && isDualFormat ) {
315+ await writeCjsDtsReexports ( chunks , outDir , config . write !== false )
316+ }
317+
311318 if ( ! hasBuilt ) {
312319 await done ( bundle )
313320 }
@@ -319,3 +326,27 @@ async function buildSingle(
319326 ab = executeOnSuccess ( config )
320327 }
321328}
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 ( / ^ ( .* ) \. ( c j s | j s ) $ / )
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+ }
0 commit comments