|
| 1 | +import { readFile } from 'fs/promises'; |
| 2 | + |
| 3 | +const processedFiles = new Map(); |
| 4 | + |
| 5 | +function escapeRegExp(string) { |
| 6 | + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 7 | +} |
| 8 | + |
| 9 | +function findMatchingBrace(contents, openBraceIndex) { |
| 10 | + let braceCount = 1; |
| 11 | + let currentIndex = openBraceIndex + 1; |
| 12 | + let inString = false; |
| 13 | + let stringChar = ''; |
| 14 | + let inComment = false; |
| 15 | + let commentType = ''; |
| 16 | + |
| 17 | + while (currentIndex < contents.length && braceCount > 0) { |
| 18 | + const char = contents[currentIndex]; |
| 19 | + const nextChar = contents[currentIndex + 1]; |
| 20 | + |
| 21 | + if (!inComment && (char === '"' || char === "'" || char === '`')) { |
| 22 | + if (!inString) { |
| 23 | + inString = true; |
| 24 | + stringChar = char; |
| 25 | + } else if (char === stringChar && contents[currentIndex - 1] !== '\\') { |
| 26 | + inString = false; |
| 27 | + stringChar = ''; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + if (!inString && !inComment) { |
| 32 | + if (char === '/' && nextChar === '/') { |
| 33 | + inComment = true; |
| 34 | + commentType = 'line'; |
| 35 | + currentIndex++; |
| 36 | + } else if (char === '/' && nextChar === '*') { |
| 37 | + inComment = true; |
| 38 | + commentType = 'block'; |
| 39 | + currentIndex++; |
| 40 | + } |
| 41 | + } else if (inComment) { |
| 42 | + if (commentType === 'line' && char === '\n') { |
| 43 | + inComment = false; |
| 44 | + commentType = ''; |
| 45 | + } else if (commentType === 'block' && char === '*' && nextChar === '/') { |
| 46 | + inComment = false; |
| 47 | + commentType = ''; |
| 48 | + currentIndex++; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (!inString && !inComment) { |
| 53 | + if (char === '{') { |
| 54 | + braceCount++; |
| 55 | + } else if (char === '}') { |
| 56 | + braceCount--; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + currentIndex++; |
| 61 | + } |
| 62 | + |
| 63 | + return braceCount === 0 ? currentIndex - 1 : -1; |
| 64 | +} |
| 65 | + |
| 66 | +function replaceFunctionByName(contents, functionName, replacement, patterns) { |
| 67 | + let result = contents; |
| 68 | + |
| 69 | + for (const pattern of patterns) { |
| 70 | + pattern.lastIndex = 0; |
| 71 | + |
| 72 | + let match; |
| 73 | + while ((match = pattern.exec(result)) !== null) { |
| 74 | + const startIndex = match.index; |
| 75 | + const matchEnd = match.index + match[0].length; |
| 76 | + |
| 77 | + const closingBraceIndex = findMatchingBrace(result, matchEnd - 1); |
| 78 | + |
| 79 | + if (closingBraceIndex !== -1) { |
| 80 | + result = result.slice(0, startIndex) + replacement + result.slice(closingBraceIndex + 1); |
| 81 | + |
| 82 | + pattern.lastIndex = 0; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return result; |
| 89 | +} |
| 90 | +export const replaceFunction = (replacements) => { |
| 91 | + if (!replacements || typeof replacements !== 'object') { |
| 92 | + throw new Error('replaceFunction: replacements must be an object'); |
| 93 | + } |
| 94 | + |
| 95 | + const functionPatterns = Object.keys(replacements).map(functionName => ({ |
| 96 | + name: functionName, |
| 97 | + replacement: replacements[functionName], |
| 98 | + patterns: [ |
| 99 | + new RegExp(`function\\s+${escapeRegExp(functionName)}\\s*\\([^)]*\\)\\s*{`, 'g'), |
| 100 | + new RegExp(`const\\s+${escapeRegExp(functionName)}\\s*=\\s*function\\s*\\([^)]*\\)\\s*{`, 'g'), |
| 101 | + new RegExp(`let\\s+${escapeRegExp(functionName)}\\s*=\\s*function\\s*\\([^)]*\\)\\s*{`, 'g'), |
| 102 | + new RegExp(`var\\s+${escapeRegExp(functionName)}\\s*=\\s*function\\s*\\([^)]*\\)\\s*{`, 'g'), |
| 103 | + new RegExp(`${escapeRegExp(functionName)}\\s*:\\s*function\\s*\\([^)]*\\)\\s*{`, 'g'), |
| 104 | + new RegExp(`${escapeRegExp(functionName)}\\s*=\\s*\\([^)]*\\)\\s*=>\\s*{`, 'g'), |
| 105 | + new RegExp(`const\\s+${escapeRegExp(functionName)}\\s*=\\s*\\([^)]*\\)\\s*=>\\s*{`, 'g'), |
| 106 | + new RegExp(`let\\s+${escapeRegExp(functionName)}\\s*=\\s*\\([^)]*\\)\\s*=>\\s*{`, 'g'), |
| 107 | + new RegExp(`var\\s+${escapeRegExp(functionName)}\\s*=\\s*\\([^)]*\\)\\s*=>\\s*{`, 'g') |
| 108 | + ] |
| 109 | + })); |
| 110 | + |
| 111 | + return { |
| 112 | + name: 'replace-function', |
| 113 | + setup(build) { |
| 114 | + build.onLoad({ filter: /\.js$/ }, async (args) => { |
| 115 | + try { |
| 116 | + const cacheKey = `${args.path}:${JSON.stringify(replacements)}`; |
| 117 | + if (processedFiles.has(cacheKey)) { |
| 118 | + return processedFiles.get(cacheKey); |
| 119 | + } |
| 120 | + |
| 121 | + let contents = await readFile(args.path, 'utf8'); |
| 122 | + let modified = false; |
| 123 | + |
| 124 | + const hasTargetFunction = functionPatterns.some(({ name }) => |
| 125 | + contents.includes(name) |
| 126 | + ); |
| 127 | + |
| 128 | + if (!hasTargetFunction) { |
| 129 | + const result = { contents, loader: 'js' }; |
| 130 | + processedFiles.set(cacheKey, result); |
| 131 | + return result; |
| 132 | + } |
| 133 | + |
| 134 | + for (const { name, replacement, patterns } of functionPatterns) { |
| 135 | + const result = replaceFunctionByName(contents, name, replacement, patterns); |
| 136 | + if (result !== contents) { |
| 137 | + contents = result; |
| 138 | + modified = true; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + const result = { contents, loader: 'js' }; |
| 143 | + |
| 144 | + processedFiles.set(cacheKey, result); |
| 145 | + |
| 146 | + return result; |
| 147 | + } catch (error) { |
| 148 | + console.warn(`[replace-function] Failed to process ${args.path}:`, error.message); |
| 149 | + return null; |
| 150 | + } |
| 151 | + }); |
| 152 | + }, |
| 153 | + }; |
| 154 | +}; |
| 155 | + |
| 156 | +export function clearCache() { |
| 157 | + processedFiles.clear(); |
| 158 | +} |
0 commit comments