|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const LIB_DIR = path.resolve(__dirname, '..', 'lib'); |
| 5 | +const ALIAS_PREFIX = '@/'; |
| 6 | + |
| 7 | +// Recursively find all files with given extensions |
| 8 | +function findFiles(dir, extensions) { |
| 9 | + const results = []; |
| 10 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 11 | + const fullPath = path.join(dir, entry.name); |
| 12 | + if (entry.isDirectory()) { |
| 13 | + results.push(...findFiles(fullPath, extensions)); |
| 14 | + } else if (extensions.some((ext) => entry.name.endsWith(ext))) { |
| 15 | + results.push(fullPath); |
| 16 | + } |
| 17 | + } |
| 18 | + return results; |
| 19 | +} |
| 20 | + |
| 21 | +// Given a file path inside lib/<target>/..., compute the relative path |
| 22 | +// from the file's directory to the target root (e.g. lib/commonjs/) |
| 23 | +function resolveAlias(filePath, targetRoot, importPath) { |
| 24 | + if (!importPath.startsWith(ALIAS_PREFIX)) { |
| 25 | + return importPath; |
| 26 | + } |
| 27 | + |
| 28 | + const stripped = importPath.slice(ALIAS_PREFIX.length); |
| 29 | + const fileDir = path.dirname(filePath); |
| 30 | + let relativePath = path.relative(fileDir, path.join(targetRoot, stripped)); |
| 31 | + |
| 32 | + // Ensure it starts with ./ or ../ |
| 33 | + if (!relativePath.startsWith('.')) { |
| 34 | + relativePath = `./${relativePath}`; |
| 35 | + } |
| 36 | + |
| 37 | + return relativePath; |
| 38 | +} |
| 39 | + |
| 40 | +// Patterns that match import/require statements with @/ aliases |
| 41 | +// Handles: require("@/..."), from '@/...', from "@/...", import("@/...") |
| 42 | +const IMPORT_PATTERN = |
| 43 | + /(require\(["']|from\s+["']|import\(["'])(@\/[^"']+)(["'])/g; |
| 44 | + |
| 45 | +function processFile(filePath, targetRoot) { |
| 46 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 47 | + const updated = content.replace( |
| 48 | + IMPORT_PATTERN, |
| 49 | + (_match, prefix, importPath, suffix) => { |
| 50 | + const resolved = resolveAlias(filePath, targetRoot, importPath); |
| 51 | + return prefix + resolved + suffix; |
| 52 | + } |
| 53 | + ); |
| 54 | + |
| 55 | + if (content !== updated) { |
| 56 | + fs.writeFileSync(filePath, updated); |
| 57 | + return true; |
| 58 | + } |
| 59 | + return false; |
| 60 | +} |
| 61 | + |
| 62 | +// Process each build target directory |
| 63 | +const targets = [ |
| 64 | + { dir: 'commonjs', extensions: ['.js', '.js.map'] }, |
| 65 | + { dir: 'module', extensions: ['.js', '.js.map'] }, |
| 66 | + { dir: 'typescript', extensions: ['.d.ts', '.d.ts.map'] }, |
| 67 | +]; |
| 68 | + |
| 69 | +let totalResolved = 0; |
| 70 | + |
| 71 | +for (const target of targets) { |
| 72 | + const targetDir = path.join(LIB_DIR, target.dir); |
| 73 | + if (!fs.existsSync(targetDir)) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + const files = findFiles(targetDir, target.extensions); |
| 78 | + let count = 0; |
| 79 | + |
| 80 | + for (const file of files) { |
| 81 | + if (processFile(file, targetDir)) { |
| 82 | + count++; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (count > 0) { |
| 87 | + console.log(`Resolved aliases in ${count} files in lib/${target.dir}/`); |
| 88 | + totalResolved += count; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +if (totalResolved > 0) { |
| 93 | + console.log(`Done — resolved @/ aliases in ${totalResolved} files total.`); |
| 94 | +} else { |
| 95 | + console.log('No @/ aliases found in build output.'); |
| 96 | +} |
0 commit comments