- Rename CommonJS build output from .js to .cjs extensions - Add .js extensions to ESM imports (required by Node.js) - Add .cjs extensions to CommonJS requires - Add post-build scripts for both ESM and CommonJS builds - Update package.json exports to reference .cjs files for require() Fixes: ReferenceError: exports is not defined in ES module scope Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
30 lines
841 B
JavaScript
30 lines
841 B
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Recursively process all .cjs files
|
|
function processDirectory(dir) {
|
|
const files = fs.readdirSync(dir);
|
|
|
|
files.forEach(file => {
|
|
const filePath = path.join(dir, file);
|
|
const stat = fs.statSync(filePath);
|
|
|
|
if (stat.isDirectory()) {
|
|
processDirectory(filePath);
|
|
} else if (file.endsWith('.cjs')) {
|
|
// Read the file
|
|
let content = fs.readFileSync(filePath, 'utf8');
|
|
|
|
// Replace require statements to add .cjs extension
|
|
content = content.replace(/require\("([^"]*?)(?<!\.cjs)"\)/g, 'require("$1.cjs")');
|
|
|
|
// Write back
|
|
fs.writeFileSync(filePath, content, 'utf8');
|
|
console.log(`✓ Updated ${filePath}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
processDirectory('./dist/cjs');
|
|
console.log('✓ Post-build processing complete');
|