Skip to content

Commit 9513c65

Browse files
gingerbenwCopilot
andcommitted
update rollup config
Co-authored-by: Copilot <copilot@github.com>
1 parent ed9bf49 commit 9513c65

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

.rollup/index.mjs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = /^export (?!default)/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+
/^export default (class|function)\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 = /^declare (class|function)\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*export\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-
/^export default /gm,
79-
'export = '
115+
/^export default (?!class|function)(\w+);?$/gm,
116+
'export = $1;'
80117
)
81118

82119
fs.writeFileSync(filePath, content, 'utf8')

0 commit comments

Comments
 (0)