@@ -13,6 +13,11 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'
1313import replacePlugin from '@rollup/plugin-replace'
1414import fastGlob from 'fast-glob'
1515
16+ import {
17+ fixImports ,
18+ transformFile ,
19+ } from '../scripts/babel/transform-commonjs-exports.mjs'
20+
1621const __filename = fileURLToPath ( import . meta. url )
1722const __dirname = path . dirname ( __filename )
1823
@@ -120,6 +125,33 @@ export default async () => {
120125 preventAssignment : false ,
121126 values : builtinAliases ,
122127 } ) ,
128+ // Post-build transform to fix CommonJS exports compatibility.
129+ {
130+ name : 'transform-commonjs-exports' ,
131+ async writeBundle ( ) {
132+ const files = await fastGlob ( '**/*.js' , {
133+ absolute : true ,
134+ cwd : distPath ,
135+ } )
136+
137+ const fixedModules = new Set ( )
138+
139+ // First pass: transform exports.default to module.exports.
140+ for ( const file of files ) {
141+ // eslint-disable-next-line no-await-in-loop
142+ const result = await transformFile ( file )
143+ if ( result . modified && result . moduleName ) {
144+ fixedModules . add ( result . moduleName )
145+ }
146+ }
147+
148+ // Second pass: fix .default accessors in imports.
149+ for ( const file of files ) {
150+ // eslint-disable-next-line no-await-in-loop
151+ await fixImports ( file , fixedModules )
152+ }
153+ } ,
154+ } ,
123155 ] ,
124156 }
125157}
0 commit comments