0
# uglify-save-license
1
2
uglify-save-license is a license detector that identifies and preserves license comments during UglifyJS minification. It analyzes JavaScript comment tokens to intelligently determine which comments contain license information that should be preserved during code compression.
3
4
## Package Information
5
6
- **Package Name**: uglify-save-license
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install uglify-save-license`
10
11
## Core Imports
12
13
```javascript
14
const saveLicense = require('uglify-save-license');
15
```
16
17
ES modules (if supported by your environment):
18
19
```javascript
20
import saveLicense from 'uglify-save-license';
21
```
22
23
## Basic Usage
24
25
```javascript
26
const UglifyJS = require('uglify-js');
27
const saveLicense = require('uglify-save-license');
28
29
// Use with UglifyJS2
30
const result = UglifyJS.minify('file.js', {
31
output: {
32
comments: saveLicense
33
}
34
});
35
36
// Use with grunt-contrib-uglify
37
grunt.initConfig({
38
uglify: {
39
my_target: {
40
options: {
41
preserveComments: saveLicense
42
},
43
src: ['src/app.js'],
44
dest: 'dest/app.min.js'
45
}
46
}
47
});
48
```
49
50
## Capabilities
51
52
### License Detection Function
53
54
The main export function that determines whether a comment should be preserved during minification based on license detection heuristics.
55
56
```javascript { .api }
57
/**
58
* Determines whether a comment should be preserved as a license comment
59
* @param {Object} node - UglifyJS AST node (currently unused but part of callback signature)
60
* @param {Object} comment - UglifyJS comment token with the following properties:
61
* @param {string} comment.file - Name of the file being processed
62
* @param {string} comment.value - Content of the comment
63
* @param {string} comment.type - Comment type ('comment1' for //, 'comment2' for /* */)
64
* @param {number} comment.line - Line number where comment appears
65
* @returns {boolean} - Returns true if comment should be preserved, false otherwise
66
*/
67
function saveLicense(node, comment)
68
```
69
70
The function identifies license comments using these detection criteria:
71
72
1. **Pattern Matching**: Comments matching the license detection regex `/@preserve|@cc_on|\bMIT\b|\bMPL\b|\bGPL\b|\bBSD\b|\bISCL\b|\(c\)|License|Copyright/mi`:
73
- Preservation directives: `@preserve`, `@cc_on`
74
- License type keywords: `MIT`, `MPL`, `GPL`, `BSD`, `ISCL`
75
- Copyright indicators: `(c)`, `Copyright`, `License`
76
77
2. **Special Comment Formats**:
78
- Block comments starting with `!` (e.g., `/*! ... */`)
79
80
3. **Position-Based Detection**:
81
- Comments on the first line of a file
82
- Comments immediately following another preserved license comment (consecutive license blocks)
83
84
**Note on Stateful Behavior**: The function maintains internal state to track the line number of the last preserved comment (`prevCommentLine`) and the current file being processed (`prevFile`). This allows it to identify consecutive license comment blocks that should be preserved together, even if only the first comment in the block matches the license patterns.
85
86
**Usage Examples:**
87
88
```javascript
89
const UglifyJS = require('uglify-js');
90
const saveLicense = require('uglify-save-license');
91
92
// CLI tool example
93
const minified = UglifyJS.minify(process.argv[2], {
94
output: {
95
comments: saveLicense
96
}
97
}).code;
98
99
console.log(minified);
100
```
101
102
**Detection Examples:**
103
104
Given this input file:
105
```javascript
106
// First line comment - preserved (position-based: line 1)
107
// (c) 2024 Company Name - preserved (pattern match: contains "(c)")
108
// This line is also preserved (consecutive: follows preserved comment)
109
110
// This comment won't be preserved (no match, not consecutive)
111
function myFunction() {
112
/*! Important license notice */ // preserved (special format: starts with !)
113
return 'Hello World';
114
}
115
```
116
117
The function will preserve the first three comments and the `/*!` comment while removing the regular comment. The consecutive behavior ensures that multi-line license headers stay together even if only the first line contains recognizable license patterns.