Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@ function convertToCommonJs(filePath) {
fs.renameSync(filePath, commonJsPath);
}

function convertToCommonJsDeclaration(filePath) {
// update imports
const content = fs.readFileSync(filePath, 'utf-8');
const updatedContent = content.replace(/(from ['"])(.*)(\.js)(['"];?)/g, '$1$2.cjs$4');
fs.writeFileSync(filePath, updatedContent, 'utf-8');

// update file extension
const commonJsDeclPath = filePath.replace(/\.d\.ts$/, '.d.cts');
fs.renameSync(filePath, commonJsDeclPath);
}

function walk(dir) {
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
walk(fullPath);
} else if (file.endsWith('.js')) {
convertToCommonJs(fullPath);
} else if (file.endsWith('.d.ts')) {
convertToCommonJsDeclaration(fullPath);
}
});
}
Expand Down
Loading