Skip to content

Commit

Permalink
refactored regex
Browse files Browse the repository at this point in the history
  • Loading branch information
heyAyushh committed Sep 24, 2024
1 parent 9bbba20 commit ecf2a55
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions post-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ async function processEsmFiles(dir) {
} else if (entry.isFile() && entry.name.endsWith('.js')) {
let content = await fs.readFile(fullPath, 'utf8');

// Add .js extension to import statements
content = content.replace(/from\s+['"](\.[^'"]+)['"]/g, "from '$1.js'");

// Add .js extension to export statements
content = content.replace(/export\s+\*\s+from\s+['"](\.[^'"]+)['"]/g, "export * from '$1.js'");

// Replace relative imports and exports with .js extension
// import or export, followed by optional whitespace, optional from, then optional whitespace
// skip .js if already present
const regex = /(import|export)(\s+[\s\S]*?\s+(?:from)?\s+['"])(\.\/[^'"]+)(['"])/g;
content = content.replace(regex, (match, keyword, beforePath, path, quote) => {
return `${keyword}${beforePath}${path}${path.endsWith('.js') ? '' : '.js'}${quote}`;
});

await fs.writeFile(fullPath, content);
console.log(`Processed ESM file: ${fullPath}`);
}
Expand Down

0 comments on commit ecf2a55

Please sign in to comment.