@@ -72,11 +72,48 @@ function fixCjsDefaultExport() {
7272 const fixExportInFile = ( filePath ) => {
7373 let content = fs . readFileSync ( filePath , 'utf8' )
7474
75- // Replace 'export default' with 'export =' for CJS compatibility
76- // This matches the rollup footer: module.exports = Object.assign(exports.default || {}, exports);
75+ // Check if there are any other exports (named exports, export interface, etc.)
76+ const hasOtherExports = / ^ e x p o r t (? ! d e f a u l t ) / m. test ( content )
77+
78+ if ( hasOtherExports ) {
79+ // Don't convert to 'export =' if there are other exports
80+ // Just leave it as 'export default' for compatibility
81+ return
82+ }
83+
84+ // Handle 'export default class/function' declarations
85+ // These need to be converted to 'declare class/function' + 'export ='
86+ content = content . replace (
87+ / ^ e x p o r t d e f a u l t ( c l a s s | f u n c t i o n ) \s + ( \w + ) / gm,
88+ ( match , keyword , name ) => {
89+ return `declare ${ keyword } ${ name } `
90+ }
91+ )
92+
93+ // Now add 'export =' for any class/function that was a default export
94+ // Look for 'declare class X' or 'declare function X' and add export after the closing brace/declaration
95+ const classOrFunctionPattern = / ^ d e c l a r e ( c l a s s | f u n c t i o n ) \s + ( \w + ) [ \s \S ] * ?^ } / gm
96+ const matches = [ ...content . matchAll ( classOrFunctionPattern ) ]
97+
98+ // Process matches in reverse order to maintain correct positions
99+ for ( let i = matches . length - 1 ; i >= 0 ; i -- ) {
100+ const match = matches [ i ]
101+ const name = match [ 2 ]
102+ const endIndex = match . index + match [ 0 ] . length
103+
104+ // Check if this class/function doesn't already have an export
105+ const afterDeclaration = content . substring ( endIndex , endIndex + 100 )
106+ if ( ! afterDeclaration . match ( / ^ \s * e x p o r t \s * = / ) ) {
107+ content = content . substring ( 0 , endIndex ) +
108+ '\nexport = ' + name + ';' +
109+ content . substring ( endIndex )
110+ }
111+ }
112+
113+ // For simple default exports (like 'export default identifier'), convert directly
77114 content = content . replace (
78- / ^ e x p o r t d e f a u l t / gm,
79- 'export = '
115+ / ^ e x p o r t d e f a u l t (? ! c l a s s | f u n c t i o n ) ( \w + ) ; ? $ / gm,
116+ 'export = $1; '
80117 )
81118
82119 fs . writeFileSync ( filePath , content , 'utf8' )
0 commit comments