@@ -55,13 +55,17 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
5555 entryFileNames = path . join ( options . extraOutdir , entryFileNames )
5656 }
5757
58- for ( const [ filename , source ] of Object . entries ( outputFiles ) ) {
58+ for ( let [ outname , source ] of Object . entries ( outputFiles ) ) {
59+ const fileName = entryFileNames . replace (
60+ '[name]' ,
61+ path . relative ( outBase , outname ) ,
62+ )
63+ if ( options . patchCjsDefaultExport && fileName . endsWith ( '.d.cts' ) ) {
64+ source = patchCjsDefaultExport ( source )
65+ }
5966 this . emitFile ( {
6067 type : 'asset' ,
61- fileName : entryFileNames . replace (
62- '[name]' ,
63- path . relative ( outBase , filename ) ,
64- ) ,
68+ fileName,
6569 source,
6670 } )
6771 }
@@ -246,13 +250,16 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
246250 }
247251
248252 const textEncoder = new TextEncoder ( )
249- for ( const [ filename , source ] of Object . entries ( outputFiles ) ) {
253+ for ( let [ filename , source ] of Object . entries ( outputFiles ) ) {
250254 const outDir = build . initialOptions . outdir
251255 let outFile = `${ path . relative ( outBase , filename ) } .d.${ outExt } `
252256 if ( options . extraOutdir ) {
253257 outFile = path . join ( options . extraOutdir , outFile )
254258 }
255259 const filePath = outDir ? path . resolve ( outDir , outFile ) : outFile
260+ if ( options . patchCjsDefaultExport && filePath . endsWith ( '.d.cts' ) ) {
261+ source = patchCjsDefaultExport ( source )
262+ }
256263 if ( write ) {
257264 await mkdir ( path . dirname ( filePath ) , { recursive : true } )
258265 await writeFile ( filePath , source )
@@ -292,6 +299,13 @@ function stripExt(filename: string) {
292299 return filename . replace ( / \. ( .? ) [ j t ] s $ / , '' )
293300}
294301
302+ function patchCjsDefaultExport ( source : string ) {
303+ return source . replace (
304+ / (?< = (?: [ ; } ] | ^ ) \s * e x p o r t \s * ) (?: \{ \s * ( [ \w $ ] + ) \s * a s \s + d e f a u l t \s * \} | d e f a u l t \s + ( [ \w $ ] + ) ) / ,
305+ ( _ , s1 , s2 ) => `= ${ s1 || s2 } ` ,
306+ )
307+ }
308+
295309export function lowestCommonAncestor ( ...filepaths : string [ ] ) : string {
296310 if ( filepaths . length === 0 ) return ''
297311 if ( filepaths . length === 1 ) return path . dirname ( filepaths [ 0 ] )
0 commit comments