CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-swc--core-darwin-arm64

Platform-specific native binary for SWC TypeScript/JavaScript compiler on macOS ARM64 architecture

89

1.15x
Overview
Eval results
Files

task.mdevals/scenario-8/

Code Minifier with Dead Code Elimination

Overview

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.

Requirements

Your task is to implement a code minifier that:

  1. Removes unreachable code such as code after return statements, unused variables, and unreachable branches

  2. Applies minification to reduce code size by removing whitespace, shortening variable names, and optimizing expressions

  3. Supports tree shaking to eliminate unused code when analyzing module exports

  4. Provides optimization statistics including the size reduction achieved

Implementation Details

Create a module minifier.js that exports two functions:

async function minifyCode(sourceCode, options)
async function analyzeDeadCode(sourceCode)

minifyCode Parameters

  • sourceCode: String containing JavaScript source code to minify
  • options: 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 Value

Returns an object with:

  • code: The minified JavaScript code as a string
  • originalSize: Size of original code in bytes
  • minifiedSize: Size of minified code in bytes
  • reduction: Percentage reduction (as a number, e.g., 23.5 for 23.5%)

analyzeDeadCode Parameters

  • sourceCode: String containing JavaScript source code to analyze

analyzeDeadCode Return Value

Returns an object with:

  • hasDeadCode: Boolean indicating if dead code was detected
  • deadCodeLocations: Array of line numbers where dead code was found

Test Cases

Test Case 1: Basic Dead Code Elimination { @test }

Input 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:

  • Call minifyCode(sourceCode, { removeDeadCode: true, compress: true, mangle: false })
  • The unreachable code after the if/else block should be removed
  • The output code size should be smaller than the input
  • reduction should be greater than 0

Test Case 2: Minification with Variable Mangling { @test }

Input 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:

  • Call minifyCode(sourceCode, { removeDeadCode: true, compress: true, mangle: true })
  • Variable names should be shortened (e.g., calculateSuma, numbersb, etc.)
  • Whitespace should be removed
  • minifiedSize should be significantly smaller than originalSize

Test Case 3: Dead Code Detection { @test }

Input code:

function process() {
  return 42;
  console.log("unreachable");
  let x = 10;
}

if (false) {
  console.log("never executed");
}

Expected behavior:

  • Call analyzeDeadCode(sourceCode)
  • hasDeadCode should be true
  • deadCodeLocations should identify lines with unreachable code

Dependencies { .dependencies }

@swc/core { .dependency }

Provides fast JavaScript/TypeScript compilation and optimization capabilities.

Constraints

  • Use only Node.js built-in modules and the specified dependencies
  • The optimizer should work with ES6 module syntax
  • Handle both named and default exports
  • Ensure the optimized code remains functionally equivalent to the original

Deliverables

  • minifier.js: Main implementation file with minifyCode and analyzeDeadCode functions
  • minifier.test.js: Test file containing the test cases above

Install with Tessl CLI

npx tessl i tessl/npm-swc--core-darwin-arm64

tile.json