|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const distRoot = path.resolve(__dirname, '../dist'); |
| 5 | +const projectRoot = path.resolve(distRoot, '..', '..'); |
| 6 | +const projectSrcRoot = path.join(projectRoot, 'src'); |
| 7 | + |
| 8 | +const aliasPatterns = [ |
| 9 | + { match: '@features/', isPrefix: true, target: path.join(distRoot, 'src/features') }, |
| 10 | + { match: '@features', isPrefix: false, target: path.join(distRoot, 'src/features/index') }, |
| 11 | + { match: '@shared/core/', isPrefix: true, target: path.join(distRoot, 'src/shared/core') }, |
| 12 | + { match: '@shared/core', isPrefix: false, target: path.join(distRoot, 'src/shared/core/index') }, |
| 13 | +]; |
| 14 | + |
| 15 | +function resolveAlias(specifier) { |
| 16 | + for (const pattern of aliasPatterns) { |
| 17 | + if (pattern.isPrefix) { |
| 18 | + if (specifier.startsWith(pattern.match)) { |
| 19 | + return path.join(pattern.target, specifier.slice(pattern.match.length)); |
| 20 | + } |
| 21 | + } else if (specifier === pattern.match) { |
| 22 | + return pattern.target; |
| 23 | + } |
| 24 | + } |
| 25 | + return null; |
| 26 | +} |
| 27 | + |
| 28 | +function toRelative(filePath, absolutePath) { |
| 29 | + let relative = path.relative(path.dirname(filePath), absolutePath); |
| 30 | + if (!relative.startsWith('.')) { |
| 31 | + relative = `./${relative}`; |
| 32 | + } |
| 33 | + return relative.replace(/\\/g, '/'); |
| 34 | +} |
| 35 | + |
| 36 | +function rewriteFile(filePath) { |
| 37 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 38 | + const updated = content.replace(/require\((['"])([^'"]+)\1\)/g, (match, quote, specifier) => { |
| 39 | + const resolved = resolveAlias(specifier); |
| 40 | + if (!resolved) { |
| 41 | + return match; |
| 42 | + } |
| 43 | + const relativeSpecifier = toRelative(filePath, resolved); |
| 44 | + return `require(${quote}${relativeSpecifier}${quote})`; |
| 45 | + }); |
| 46 | + |
| 47 | + if (updated !== content) { |
| 48 | + fs.writeFileSync(filePath, updated, 'utf8'); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function walk(dir, callback) { |
| 53 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 54 | + for (const entry of entries) { |
| 55 | + const entryPath = path.join(dir, entry.name); |
| 56 | + if (entry.isDirectory()) { |
| 57 | + walk(entryPath, callback); |
| 58 | + } else if (entry.isFile()) { |
| 59 | + callback(entryPath); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function rewriteDistRequires() { |
| 65 | + if (!fs.existsSync(distRoot)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + walk(distRoot, filePath => { |
| 70 | + if (filePath.endsWith('.js')) { |
| 71 | + rewriteFile(filePath); |
| 72 | + } |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +function cleanupProjectArtifacts() { |
| 77 | + const targets = [ |
| 78 | + path.join(projectSrcRoot, 'features'), |
| 79 | + path.join(projectSrcRoot, 'shared') |
| 80 | + ]; |
| 81 | + |
| 82 | + for (const target of targets) { |
| 83 | + if (!fs.existsSync(target)) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + walk(target, filePath => { |
| 88 | + if (filePath.endsWith('.js')) { |
| 89 | + const tsPath = filePath.slice(0, -3) + '.ts'; |
| 90 | + if (fs.existsSync(tsPath)) { |
| 91 | + fs.rmSync(filePath); |
| 92 | + } |
| 93 | + } else if (filePath.endsWith('.js.map')) { |
| 94 | + const tsPath = filePath.slice(0, -7) + '.ts'; |
| 95 | + if (fs.existsSync(tsPath)) { |
| 96 | + fs.rmSync(filePath); |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +rewriteDistRequires(); |
| 104 | +cleanupProjectArtifacts(); |
0 commit comments