Skip to content

Commit 71f015e

Browse files
committed
Add CommonJS export transform to Rollup writeBundle hook
1 parent 3937bf1 commit 71f015e

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

registry/.config/rollup.dist.config.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'
1313
import replacePlugin from '@rollup/plugin-replace'
1414
import fastGlob from 'fast-glob'
1515

16+
import {
17+
fixImports,
18+
transformFile,
19+
} from '../scripts/babel/transform-commonjs-exports.mjs'
20+
1621
const __filename = fileURLToPath(import.meta.url)
1722
const __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

Comments
 (0)