-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfix-esm-imports.js
More file actions
32 lines (27 loc) · 918 Bytes
/
fix-esm-imports.js
File metadata and controls
32 lines (27 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import fs from 'fs';
import path from 'path';
const baseDir = 'dist/esm';
function fixImportsInFile(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const fixed = content.replace(
/from\s+(['"])(\.\/[^'"]+?)(?<!\.js)\1/g,
(match, quote, pathPart) => `from ${quote}${pathPart}.js${quote}`
);
if (content !== fixed) {
fs.writeFileSync(filePath, fixed, 'utf-8');
console.log(`✔ Fixed: ${filePath}`);
}
}
function processDir(dir) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
processDir(fullPath);
} else if (entry.isFile() && fullPath.endsWith('.js')) {
fixImportsInFile(fullPath);
}
}
}
processDir(baseDir);
console.log('✔ All .js extensions processed');