|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +const ROOT = path.resolve(__dirname, "..", "dist", "esm"); |
| 5 | + |
| 6 | +function isRelativeBare(spec) { |
| 7 | + return ( |
| 8 | + (spec.startsWith("./") || spec.startsWith("../")) && |
| 9 | + !spec.endsWith(".js") && |
| 10 | + !spec.endsWith(".mjs") && |
| 11 | + !spec.endsWith(".cjs") && |
| 12 | + !spec.endsWith(".json") |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +function resolveTarget(fileDir, spec) { |
| 17 | + const base = path.resolve(fileDir, spec); |
| 18 | + |
| 19 | + // Case 1: "./foo.js" exists |
| 20 | + if (fs.existsSync(base + ".js")) { |
| 21 | + return spec + ".js"; |
| 22 | + } |
| 23 | + |
| 24 | + // Case 2: "./foo/index.js" exists |
| 25 | + const indexPath = path.join(base, "index.js"); |
| 26 | + if (fs.existsSync(indexPath)) { |
| 27 | + // normalize to POSIX for import strings |
| 28 | + return spec.replace(/\\/g, "/") + "/index.js"; |
| 29 | + } |
| 30 | + |
| 31 | + // Could not resolve safely |
| 32 | + return null; |
| 33 | +} |
| 34 | + |
| 35 | +function processFile(filePath) { |
| 36 | + if (!filePath.endsWith(".js")) return; |
| 37 | + |
| 38 | + let code = fs.readFileSync(filePath, "utf8"); |
| 39 | + let changed = false; |
| 40 | + |
| 41 | + // matches: import ... from "spec" OR export ... from "spec" |
| 42 | + const importExportRegex = |
| 43 | + /(import\s+(?:[^"']+?\s+from\s+)?|export\s+(?:\*|{[^}]*})\s+from\s+)["']([^"']+)["']/g; |
| 44 | + |
| 45 | + code = code.replace(importExportRegex, (full, prefix, spec) => { |
| 46 | + if (!isRelativeBare(spec)) return full; |
| 47 | + |
| 48 | + const dir = path.dirname(filePath); |
| 49 | + const fixed = resolveTarget(dir, spec); |
| 50 | + if (!fixed) return full; |
| 51 | + |
| 52 | + changed = true; |
| 53 | + return `${prefix}"${fixed}"`; |
| 54 | + }); |
| 55 | + |
| 56 | + if (changed) { |
| 57 | + fs.writeFileSync(filePath, code, "utf8"); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function walk(dir) { |
| 62 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 63 | + const full = path.join(dir, entry.name); |
| 64 | + if (entry.isDirectory()) { |
| 65 | + walk(full); |
| 66 | + } else if (entry.isFile()) { |
| 67 | + processFile(full); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +if (fs.existsSync(ROOT)) { |
| 73 | + walk(ROOT); |
| 74 | +} |
| 75 | + |
0 commit comments