Skip to content

Commit

Permalink
Append .js to export statements as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadnasriya committed Jul 9, 2024
1 parent 79db65a commit 98a6997
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class Main {
this.#_helpers.extensions.add(fullPath);
} else if (file.endsWith('.js')) {
let content = fs.readFileSync(fullPath, 'utf8');

// Regex to match import statements
content = content.replace(/(import\s+.*\s+from\s+['"])(.*)(['"];)/g, (match, p1, p2, p3) => {
if (!p2.endsWith('.js') && !p2.startsWith('.') && !p2.startsWith('/')) {
return match; // Skip non-relative imports
Expand All @@ -173,6 +175,19 @@ class Main {
}
return `${p1}${p2}.js${p3}`;
});

// Regex to match export statements
content = content.replace(/(export\s+\{\s*.*\s*}\s+from\s+['"])(.*)(['"];)/g, (match, p1, p2, p3) => {
if (!p2.endsWith('.js') && !p2.startsWith('.') && !p2.startsWith('/')) {
return match; // Skip non-relative exports
}
const lastPart = p2.split('/').pop();
if (lastPart.includes('.') && lastPart.split('.').length > 1) {
return match; // Skip if last part already has an extension
}
return `${p1}${p2}.js${p3}`;
});

fs.writeFileSync(fullPath, content, 'utf8');
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nasriya/postbuild",
"version": "1.0.4",
"version": "1.0.5",
"description": "A package that does some tasks after compilation",
"main": "main.js",
"type": "module",
Expand Down

0 comments on commit 98a6997

Please sign in to comment.