|
| 1 | +'use strict'; |
| 2 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 3 | +// Find every instance of inline start and end |
| 4 | +const pattern = /@inlineStart(.*?)@inlineEnd/gs; |
| 5 | +// Variable to capture matches |
| 6 | +let match; |
| 7 | +// Running only once doesn't find all values, so we re-run the function this many times |
| 8 | +const maxLoop = 200; |
| 9 | +// Store list of function names for logging |
| 10 | +const changedFunctions = new Set(); |
| 11 | +function inlineFunction(file) { |
| 12 | + const functionsToInline = {}; |
| 13 | + while ((match = pattern.exec(file.code)) !== null) { |
| 14 | + const betweenComments = match[1]; |
| 15 | + // Check for local function X() |
| 16 | + const localFunctionMatch = betweenComments.match( |
| 17 | + /function\s+(\w+)\s*\([^)]*\)/, |
| 18 | + ); |
| 19 | + if (localFunctionMatch) { |
| 20 | + const functionName = localFunctionMatch[1]; |
| 21 | + // console.log(`Inlining local function: ${functionName}`); |
| 22 | + const start = betweenComments.indexOf(')') + 1; |
| 23 | + const end = betweenComments.lastIndexOf('end'); |
| 24 | + let content = betweenComments.substring(start, end).trim(); |
| 25 | + // Check for @removeReturn |
| 26 | + if (betweenComments.includes('@removeReturn')) { |
| 27 | + content = content.replace(/\breturn\b/g, ''); // Remove "return" keywords |
| 28 | + } |
| 29 | + // console.log(`Captured content: ${content}`); |
| 30 | + changedFunctions.add(functionName); |
| 31 | + // Store result for later |
| 32 | + if (functionsToInline[functionName] === undefined) |
| 33 | + functionsToInline[functionName] = content; |
| 34 | + } |
| 35 | + // Check for X = function() |
| 36 | + const functionAssignmentMatch = betweenComments.match( |
| 37 | + /(\w+)\s*=\s*function\s*\([^)]*\)/, |
| 38 | + ); |
| 39 | + if (functionAssignmentMatch) { |
| 40 | + const functionName = functionAssignmentMatch[1]; |
| 41 | + // console.log(`Inlining function: ${functionName}`); |
| 42 | + const start = betweenComments.indexOf(')') + 1; |
| 43 | + const end = betweenComments.lastIndexOf('end'); |
| 44 | + let content = betweenComments.substring(start, end).trim(); |
| 45 | + // Check for @removeReturn |
| 46 | + if (betweenComments.includes('@removeReturn')) { |
| 47 | + content = content.replace(/\breturn\b/g, ''); // Remove "return" keywords |
| 48 | + } |
| 49 | + // console.log(`Captured content: ${content}`); |
| 50 | + changedFunctions.add(functionName); |
| 51 | + // Store result for later |
| 52 | + if (functionsToInline[functionName] === undefined) |
| 53 | + functionsToInline[functionName] = content; |
| 54 | + } |
| 55 | + } |
| 56 | + // Iterate through stored functions and replace their occurrences |
| 57 | + Object.entries(functionsToInline).forEach(([functionName, content]) => { |
| 58 | + const functionPattern = new RegExp( |
| 59 | + `\\b(?<!function\\s)${functionName}\\s*\\([^)]*\\)`, |
| 60 | + 'g', |
| 61 | + ); |
| 62 | + file.code = file.code.replace(functionPattern, content); |
| 63 | + }); |
| 64 | +} |
| 65 | +function removeInlinedFunction(file) { |
| 66 | + while ((match = pattern.exec(file.code)) !== null) { |
| 67 | + const betweenComments = match[1]; |
| 68 | + // Check for local function X() |
| 69 | + const localFunctionMatch = betweenComments.match( |
| 70 | + /function\s+(\w+)\s*\([^)]*\)/, |
| 71 | + ); |
| 72 | + // Check for X = function() |
| 73 | + const functionAssignmentMatch = betweenComments.match( |
| 74 | + /(\w+)\s*=\s*function\s*\([^)]*\)/, |
| 75 | + ); |
| 76 | + if (functionAssignmentMatch || localFunctionMatch) { |
| 77 | + // Remove function that will be inlined |
| 78 | + file.code = file.code.replace(betweenComments, ''); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +const plugin = { |
| 83 | + afterEmit: (_program, _options, emitHost, result) => { |
| 84 | + for (const file of result) { |
| 85 | + for (let index = 0; index < maxLoop; index++) { |
| 86 | + inlineFunction(file); |
| 87 | + } |
| 88 | + for (let index = 0; index < maxLoop; index++) { |
| 89 | + removeInlinedFunction(file); |
| 90 | + } |
| 91 | + // Write the changed code |
| 92 | + emitHost.writeFile(file.outputPath, file.code, false); |
| 93 | + } |
| 94 | + // Display log of function names |
| 95 | + changedFunctions.forEach((changedFunctionName) => { |
| 96 | + // @ts-expect-error Missing definition for `console` |
| 97 | + console.log(`Inlining function ${changedFunctionName}`); |
| 98 | + }); |
| 99 | + }, |
| 100 | +}; |
| 101 | +exports.default = plugin; |
0 commit comments