Skip to content

Commit 2b70253

Browse files
committed
Document Rollup writeBundle hook integration in README
Add recommended Rollup writeBundle hook pattern with fast-glob to transform-commonjs-exports README. Move standalone script approach to alternative section. This reflects the current implementation in registry/.config/rollup.dist.config.mjs.
1 parent fc180af commit 2b70253

1 file changed

Lines changed: 53 additions & 5 deletions

File tree

registry/scripts/babel/README.md

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,54 @@ export async function transformFile(filePath, options = {}) {
193193

194194
## Integration with Build
195195

196+
### Rollup `writeBundle` Hook (Recommended)
197+
198+
Integrate transforms directly into Rollup's build pipeline using the `writeBundle` hook:
199+
196200
```javascript
197-
// package.json
198-
{
199-
"scripts": {
200-
"build": "tsgo && node scripts/fix-commonjs-exports.mjs"
201-
}
201+
// .config/rollup.dist.config.mjs
202+
import fastGlob from 'fast-glob'
203+
import {
204+
fixImports,
205+
transformFile,
206+
} from '../scripts/babel/transform-commonjs-exports.mjs'
207+
208+
export default {
209+
// ... other config
210+
plugins: [
211+
// ... other plugins
212+
{
213+
name: 'transform-commonjs-exports',
214+
async writeBundle() {
215+
const files = await fastGlob('**/*.js', {
216+
absolute: true,
217+
cwd: distPath,
218+
})
219+
220+
const fixedModules = new Set()
221+
222+
// First pass: transform exports.default to module.exports
223+
for (const file of files) {
224+
const result = await transformFile(file)
225+
if (result.modified && result.moduleName) {
226+
fixedModules.add(result.moduleName)
227+
}
228+
}
229+
230+
// Second pass: fix .default accessors in imports
231+
for (const file of files) {
232+
await fixImports(file, fixedModules)
233+
}
234+
},
235+
},
236+
],
202237
}
203238
```
204239

240+
### Standalone Script (Alternative)
241+
242+
For projects not using Rollup, run as a standalone script:
243+
205244
```javascript
206245
// scripts/fix-commonjs-exports.mjs
207246
import { transformFile, fixImports } from './babel/transform-commonjs-exports.mjs'
@@ -224,6 +263,15 @@ for (const file of files) {
224263
}
225264
```
226265

266+
```json
267+
// package.json
268+
{
269+
"scripts": {
270+
"build": "tsgo && node scripts/fix-commonjs-exports.mjs"
271+
}
272+
}
273+
```
274+
227275
## Best Practices
228276

229277
1. **Always use magic-string** - Don't use Babel's code generator for transforms

0 commit comments

Comments
 (0)