Skip to content

Commit

Permalink
v1.0.6 - fix cjs require statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Helveg committed Dec 8, 2024
1 parent 5a5a125 commit 1a3da2a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-inject-transformer",
"version": "1.0.5",
"version": "1.0.6",
"description": "Amp up your NestJS and `class-transformer` stack with dependency injected transforms.",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.mjs",
Expand Down
16 changes: 15 additions & 1 deletion scripts/rename-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,24 @@ function renameFiles(dir) {
if (stats.isDirectory()) {
renameFiles(filePath);
}
// If it's a .js file, rename it to .mjs
// If it's a .mjs file, rename it to .js
else if (file.endsWith(".mjs")) {
const newFilePath = filePath.slice(0, -4) + ".js";
fs.renameSync(filePath, newFilePath);

// Now, read the file and replace require statements
const fileContent = fs.readFileSync(newFilePath, "utf-8");
const updatedContent = fileContent.replace(
/require\(["']([^"']+)\.mjs["']\)/g,
(match, p1) => {
return `require("${p1}.js")`;
}
);

// If the content was modified, write it back to the file
if (updatedContent !== fileContent) {
fs.writeFileSync(newFilePath, updatedContent, "utf-8");
}
}
});
}
Expand Down

0 comments on commit 1a3da2a

Please sign in to comment.