support for --comments option to keep comments containing @license or @preserve

This commit is contained in:
Mihai Bazon
2012-10-02 16:40:42 +03:00
parent cb2e811191
commit 815abcfe18
2 changed files with 37 additions and 3 deletions

View File

@@ -8,8 +8,13 @@ var sys = require("util");
var optimist = require("optimist");
var fs = require("fs");
var ARGS = optimist
.usage("$0 [options] input1.js [input2.js ...]\n\
.usage("$0 input1.js [input2.js ...] [options]\n\
Use a single dash to read input from the standard input.\
\n\n\
NOTE: by default there is no mangling/compression.\n\
Without [options] it will simply parse input files and dump the AST\n\
with whitespace and comments discarded. To achieve compression and\n\
mangling you need to use `-c` and `-m`.\
")
.describe("source-map", "Specify an output file where to generate source map.")
.describe("source-map-root", "The path to the original source to be included in the source map.")
@@ -25,6 +30,15 @@ Pass options like -c hoist_vars=false,if_return=false. \
Use -c with no argument if you want to disable the squeezer entirely.")
.describe("d", "Global definitions")
.describe("comments", "Preserve copyright comments in the output. \
By default this works like Google Closure, keeping JSDoc-style comments that contain \"@license\" or \"@preserve\". \
You can optionally pass one of the following arguments to this flag:\n\
- \"all\" to keep all comments\n\
- a valid JS regexp (needs to start with a slash) to keep only comments that match.\n\
\
Note that currently not *all* comments can be kept when compression is on, \
because of dead code removal or cascading statements into sequences.")
.describe("stats", "Display operations run time on STDERR.")
.describe("v", "Verbose")
@@ -35,12 +49,13 @@ Use -c with no argument if you want to disable the squeezer entirely.")
.alias("m", "mangle")
.alias("c", "compress")
.alias("d", "define")
.alias("r", "reserved-names")
.alias("r", "reserved")
.string("b")
.string("m")
.string("c")
.string("d")
.string("comments")
.boolean("v")
.boolean("stats")
@@ -92,9 +107,28 @@ if (MANGLE && ARGS.r) {
var OUTPUT_OPTIONS = {
beautify: BEAUTIFY ? true : false
};
if (BEAUTIFY)
UglifyJS.merge(OUTPUT_OPTIONS, BEAUTIFY);
if (ARGS.comments) {
if (/^\//.test(ARGS.comments)) {
OUTPUT_OPTIONS.comments = new Function("return(" + ARGS.comments + ")")();
} else if (ARGS.comments == "all") {
OUTPUT_OPTIONS.comments = true;
} else {
OUTPUT_OPTIONS.comments = function(node, comment) {
var text = comment.value;
var type = comment.type;
if (type == "comment2") {
// multiline comment
return text.indexOf("@preserve") >= 0
|| text.indexOf("@license") >= 0;
}
}
}
}
var files = ARGS._.slice();
var ORIG_MAP = ARGS.in_source_map;

View File

@@ -344,7 +344,7 @@ function OutputStream(options) {
});
} else if (typeof c == "function") {
comments = comments.filter(function(comment){
return c(self, comment.value, comment.type);
return c(self, comment);
});
}
comments.forEach(function(c){