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-8/

JavaScript Minifier with License Header Preservation

Summary

Build a JavaScript minification utility that processes a source file and produces minified output while automatically preserving any comment that appears on the first line of the file. This is useful for maintaining license headers during code minification.

Dependencies { .dependencies }

uglify-save-license { .dependency }

Provides license comment detection and preservation during minification.

uglify-js { .dependency }

JavaScript parser and minifier.

Requirements

Core Functionality

  1. File Processing: Create a function that accepts input JavaScript source code as a string and returns minified JavaScript as a string.

  2. First-Line Comment Preservation: Any comment appearing on line 1 of the source code must be preserved in the minified output, regardless of comment type (line comment // or block comment /* */) or content.

  3. Standard Minification: Apply JavaScript minification including whitespace removal and code compression.

  4. Comment Filtering: Comments not on the first line should be removed during minification (standard minification behavior).

Behavior Specification

  • If a source file has a comment on line 1, that comment must appear in the minified output
  • If a source file has no comment on line 1, perform standard minification with no comment preservation
  • Both line comments (// ...) and block comments (/* ... */) on line 1 must be handled
  • The preserved first-line comment should appear at the start of the minified output

Test Cases

@test Source with first-line comment using line comment syntax

Input:

// Copyright 2024 Example Corp. MIT License.
function add(x, y) {
    return x + y;
}
console.log(add(5, 10));

Expected output (example):

// Copyright 2024 Example Corp. MIT License.
function add(n,o){return n+o}console.log(add(5,10));

The first-line comment is preserved while the code is minified.

@test Source with no first-line comment

Input:

function multiply(a, b) {
    return a * b;
}

Expected output (example):

function multiply(n,o){return n*o}

No comments are preserved; standard minification is applied.

@test Source with first-line block comment

Input:

/* Copyright 2024. All rights reserved. */
const greet = (name) => {
    return "Hello, " + name;
};

Expected output (example):

/* Copyright 2024. All rights reserved. */
const greet=n=>"Hello, "+n;

The first-line block comment is preserved while the code is minified.

Implementation Files

minify.js { .implementation }

Main module that exports a minification function:

  • minify(sourceCode) - Accepts JavaScript source code as a string and returns minified JavaScript as a string, preserving any first-line comments.

minify.test.js { .test }

Test suite validating the behavior described in the test cases above.

Install with Tessl CLI

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

tile.json