License detector for UglifyJS that identifies and preserves license comments during minification
Overall
score
98%
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.
The tool should accept JavaScript source code as input and process it to identify and preserve license comment blocks.
The tool must identify license-related comments using these detection criteria:
Comments matching license patterns including:
MIT, GPL, BSD, MPL, Apache, ISCCopyright, (c), ©License, LicensedSpecial comment formats:
! (e.g., /*! ... */)Position-based detection:
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:
//): Comments on adjacent line numbers/* */): Comments on adjacent line numbers after the block endsThe tool should produce minified JavaScript code where:
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;
}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
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
The regular comment "This is just a regular comment" inside the add function should be removed from output. @test
When given an empty string as input, the minifier should return an empty string. @test
@generates
/**
* 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 };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-licensedocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10