CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-uglify-save-license

License detector for UglifyJS that identifies and preserves license comments during minification

Overall
score

98%

Overview
Eval results
Files

task.mdevals/scenario-6/

License Comment Preserving Minifier

Build a JavaScript code minifier that preserves consecutive license comment blocks during minification. When a license comment is detected, all immediately following consecutive comments should also be preserved, even if they don't explicitly contain license keywords.

Requirements

Input Processing

The tool should accept JavaScript source code as input and process it to identify and preserve license comment blocks.

License Detection

The tool must identify license-related comments using these detection criteria:

  1. Comments matching license patterns including:

    • License keywords: MIT, GPL, BSD, MPL, Apache, ISC
    • Copyright indicators: Copyright, (c), ©
    • License-related terms: License, Licensed
  2. Special comment formats:

    • Block comments starting with ! (e.g., /*! ... */)
  3. Position-based detection:

    • Any comment on the first line of the source code

Consecutive Comment Preservation

Core Requirement: When a comment is identified as a license comment, all immediately consecutive comments (both line and block comments) must also be preserved, even if they don't match license patterns themselves.

"Consecutive" means:

  • For line comments (//): Comments on adjacent line numbers
  • For block comments (/* */): Comments on adjacent line numbers after the block ends
  • Mixed types: A line comment following a block comment on the next line, or vice versa

Output

The tool should produce minified JavaScript code where:

  • All license comment blocks are preserved
  • Non-license comments are removed
  • The code is minified (whitespace removed, identifiers shortened, etc.)

Test Cases

Consider this input JavaScript:

// Copyright 2024 Example Corp
// All rights reserved
// This software is licensed under the MIT license

function add(a, b) {
  // This is just a regular comment
  return a + b;
}

/*! BSD License */
// Redistribution permitted
// See full license text

function multiply(x, y) {
  return x * y;
}

Test 1: Consecutive Line Comments @test

When processing the three consecutive line comments at the top (lines 1-3), all three should be preserved even though only the first contains "Copyright". @test

Test 2: Mixed Comment Block @test

When processing the block comment /*! BSD License */ (line 11) followed by two line comments (lines 12-13), all three should be preserved as one consecutive block. @test

Test 3: Non-License Comment @test

The regular comment "This is just a regular comment" inside the add function should be removed from output. @test

Test 4: Empty Input @test

When given an empty string as input, the minifier should return an empty string. @test

Implementation

@generates

API

/**
 * Minifies JavaScript source code while preserving license comment blocks
 * @param {string} sourceCode - The JavaScript source code to minify
 * @returns {string} The minified code with license comments preserved
 */
function minify(sourceCode);

module.exports = { minify };

Dependencies { .dependencies }

uglify-save-license { .dependency }

Provides license detection and preservation logic for identifying which comments should be kept during minification.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-uglify-save-license

tile.json