-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpost-build.mjs
More file actions
38 lines (31 loc) · 1.17 KB
/
post-build.mjs
File metadata and controls
38 lines (31 loc) · 1.17 KB
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
33
34
35
36
37
38
#!/usr/bin/env node
import { readdir, readFile, writeFile } from "fs/promises";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const distDir = join(__dirname, "dist");
async function fixImports(dir) {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
await fixImports(fullPath);
} else if (entry.name.endsWith(".js")) {
const content = await readFile(fullPath, "utf-8");
const fixedContent = content
.replace(/from\s+['"](\.[^'"]*)['"]/g, (match, path) => {
if (path.endsWith(".js")) return match;
return match.replace(path, path + ".js");
})
.replace(/import\s+['"](\.[^'"]*)['"]/g, (match, path) => {
if (path.endsWith(".js")) return match;
return match.replace(path, path + ".js");
});
if (content !== fixedContent) {
await writeFile(fullPath, fixedContent);
console.log(`Fixed imports in ${fullPath}`);
}
}
}
}
fixImports(distDir).catch(console.error);