Platform-specific native binary for SWC TypeScript/JavaScript compiler on macOS ARM64 architecture
89
Create a JavaScript code minifier that optimizes source code by removing dead code (unreachable statements) and applying minification. The minifier should process JavaScript code and produce smaller, optimized output.
Your task is to implement a code minifier that:
Removes unreachable code such as code after return statements, unused variables, and unreachable branches
Applies minification to reduce code size by removing whitespace, shortening variable names, and optimizing expressions
Supports tree shaking to eliminate unused code when analyzing module exports
Provides optimization statistics including the size reduction achieved
Create a module minifier.js that exports two functions:
async function minifyCode(sourceCode, options)
async function analyzeDeadCode(sourceCode)minifyCode ParameterssourceCode: String containing JavaScript source code to minifyoptions: Object with the following properties:
removeDeadCode (boolean): Whether to eliminate dead code (default: true)compress (boolean): Whether to apply compression optimizations (default: true)mangle (boolean): Whether to shorten variable names (default: false)minifyCode Return ValueReturns an object with:
code: The minified JavaScript code as a stringoriginalSize: Size of original code in bytesminifiedSize: Size of minified code in bytesreduction: Percentage reduction (as a number, e.g., 23.5 for 23.5%)analyzeDeadCode ParameterssourceCode: String containing JavaScript source code to analyzeanalyzeDeadCode Return ValueReturns an object with:
hasDeadCode: Boolean indicating if dead code was detecteddeadCodeLocations: Array of line numbers where dead code was foundInput code:
function calculate(x) {
if (x > 10) {
return x * 2;
} else {
return x + 5;
}
console.log("This is unreachable");
return x;
}
function unusedFunction() {
return "never called";
}
const result = calculate(15);Expected behavior:
minifyCode(sourceCode, { removeDeadCode: true, compress: true, mangle: false })reduction should be greater than 0Input code:
function calculateSum(numbers) {
let total = 0;
for (let i = 0; i < numbers.length; i++) {
total = total + numbers[i];
}
return total;
}
const myNumbers = [1, 2, 3, 4, 5];
const sum = calculateSum(myNumbers);Expected behavior:
minifyCode(sourceCode, { removeDeadCode: true, compress: true, mangle: true })calculateSum → a, numbers → b, etc.)minifiedSize should be significantly smaller than originalSizeInput code:
function process() {
return 42;
console.log("unreachable");
let x = 10;
}
if (false) {
console.log("never executed");
}Expected behavior:
analyzeDeadCode(sourceCode)hasDeadCode should be truedeadCodeLocations should identify lines with unreachable codeProvides fast JavaScript/TypeScript compilation and optimization capabilities.
minifier.js: Main implementation file with minifyCode and analyzeDeadCode functionsminifier.test.js: Test file containing the test cases aboveInstall with Tessl CLI
npx tessl i tessl/npm-swc--core-darwin-arm64docs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10