Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16953c2064 | ||
|
|
6b14f7c224 | ||
|
|
130c623be7 | ||
|
|
47c9895d59 | ||
|
|
ba403331c5 | ||
|
|
e82e89d1b0 | ||
|
|
83a4ebfedc | ||
|
|
9916d0e547 | ||
|
|
31c4a37e37 | ||
|
|
08219f0cee | ||
|
|
c4993e1e5c |
16
README.md
16
README.md
@@ -79,6 +79,7 @@ The available options are:
|
|||||||
--export-all Only used when --wrap, this tells UglifyJS to add code to
|
--export-all Only used when --wrap, this tells UglifyJS to add code to
|
||||||
automatically export all globals. [boolean]
|
automatically export all globals. [boolean]
|
||||||
-v, --verbose Verbose [boolean]
|
-v, --verbose Verbose [boolean]
|
||||||
|
-V, --version Print version number and exits. [boolean]
|
||||||
|
|
||||||
Specify `--output` (`-o`) to declare the output file. Otherwise the output
|
Specify `--output` (`-o`) to declare the output file. Otherwise the output
|
||||||
goes to STDOUT.
|
goes to STDOUT.
|
||||||
@@ -130,7 +131,7 @@ input files from the command line.
|
|||||||
## Mangler options
|
## Mangler options
|
||||||
|
|
||||||
To enable the mangler you need to pass `--mangle` (`-m`). Optionally you
|
To enable the mangler you need to pass `--mangle` (`-m`). Optionally you
|
||||||
can pass `-m sort` (we'll possibly have other flags in the future) in order
|
can pass `-m sort=true` (we'll possibly have other flags in the future) in order
|
||||||
to assign shorter names to most frequently used variables. This saves a few
|
to assign shorter names to most frequently used variables. This saves a few
|
||||||
hundred bytes on jQuery before gzip, but the output is _bigger_ after gzip
|
hundred bytes on jQuery before gzip, but the output is _bigger_ after gzip
|
||||||
(and seems to happen for other libraries I tried it on) therefore it's not
|
(and seems to happen for other libraries I tried it on) therefore it's not
|
||||||
@@ -213,6 +214,7 @@ will evaluate references to them to the value itself and drop unreachable
|
|||||||
code as usual. The possible downside of this approach is that the build
|
code as usual. The possible downside of this approach is that the build
|
||||||
will contain the `const` declarations.
|
will contain the `const` declarations.
|
||||||
|
|
||||||
|
<a name="codegen-options"></a>
|
||||||
## Beautifier options
|
## Beautifier options
|
||||||
|
|
||||||
The code generator tries to output shortest code possible by default. In
|
The code generator tries to output shortest code possible by default. In
|
||||||
@@ -366,9 +368,19 @@ no sense otherwise).
|
|||||||
Other options:
|
Other options:
|
||||||
|
|
||||||
- `warnings` (default `false`) — pass `true` to display compressor warnings.
|
- `warnings` (default `false`) — pass `true` to display compressor warnings.
|
||||||
|
|
||||||
- `fromString` (default `false`) — if you pass `true` then you can pass
|
- `fromString` (default `false`) — if you pass `true` then you can pass
|
||||||
JavaScript source code, rather than file names.
|
JavaScript source code, rather than file names.
|
||||||
|
|
||||||
|
- `mangle` — pass `false` to skip mangling names.
|
||||||
|
|
||||||
|
- `output` (default `null`) — pass an object if you wish to specify
|
||||||
|
additional [output options][codegen]. The defaults are optimized
|
||||||
|
for best compression.
|
||||||
|
|
||||||
|
- `compress` (default `{}`) — pass `false` to skip compressing entirely.
|
||||||
|
Pass an object to specify custom [compressor options][compressor].
|
||||||
|
|
||||||
We could add more options to `UglifyJS.minify` — if you need additional
|
We could add more options to `UglifyJS.minify` — if you need additional
|
||||||
functionality please suggest!
|
functionality please suggest!
|
||||||
|
|
||||||
@@ -516,3 +528,5 @@ The `source_map_options` (optional) can contain the following properties:
|
|||||||
[acorn]: https://github.com/marijnh/acorn
|
[acorn]: https://github.com/marijnh/acorn
|
||||||
[source-map]: https://github.com/mozilla/source-map
|
[source-map]: https://github.com/mozilla/source-map
|
||||||
[sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
|
[sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
|
||||||
|
[codegen]: http://lisperator.net/uglifyjs/codegen
|
||||||
|
[compressor]: http://lisperator.net/uglifyjs/compress
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ You need to pass an argument to this option to specify the name that your module
|
|||||||
.describe("export-all", "Only used when --wrap, this tells UglifyJS to add code to automatically export all globals.")
|
.describe("export-all", "Only used when --wrap, this tells UglifyJS to add code to automatically export all globals.")
|
||||||
.describe("lint", "Display some scope warnings")
|
.describe("lint", "Display some scope warnings")
|
||||||
.describe("v", "Verbose")
|
.describe("v", "Verbose")
|
||||||
|
.describe("V", "Print version number and exit.")
|
||||||
|
|
||||||
.alias("p", "prefix")
|
.alias("p", "prefix")
|
||||||
.alias("o", "output")
|
.alias("o", "output")
|
||||||
@@ -58,6 +59,7 @@ You need to pass an argument to this option to specify the name that your module
|
|||||||
.alias("c", "compress")
|
.alias("c", "compress")
|
||||||
.alias("d", "define")
|
.alias("d", "define")
|
||||||
.alias("r", "reserved")
|
.alias("r", "reserved")
|
||||||
|
.alias("V", "version")
|
||||||
|
|
||||||
.string("source-map")
|
.string("source-map")
|
||||||
.string("source-map-root")
|
.string("source-map-root")
|
||||||
@@ -74,6 +76,7 @@ You need to pass an argument to this option to specify the name that your module
|
|||||||
.boolean("acorn")
|
.boolean("acorn")
|
||||||
.boolean("spidermonkey")
|
.boolean("spidermonkey")
|
||||||
.boolean("lint")
|
.boolean("lint")
|
||||||
|
.boolean("V")
|
||||||
|
|
||||||
.wrap(80)
|
.wrap(80)
|
||||||
|
|
||||||
@@ -82,6 +85,12 @@ You need to pass an argument to this option to specify the name that your module
|
|||||||
|
|
||||||
normalize(ARGS);
|
normalize(ARGS);
|
||||||
|
|
||||||
|
if (ARGS.version || ARGS.V) {
|
||||||
|
var json = require("../package.json");
|
||||||
|
sys.puts(json.name + ' ' + json.version);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
if (ARGS.ast_help) {
|
if (ARGS.ast_help) {
|
||||||
var desc = UglifyJS.describe_ast();
|
var desc = UglifyJS.describe_ast();
|
||||||
sys.puts(typeof desc == "string" ? desc : JSON.stringify(desc, null, 2));
|
sys.puts(typeof desc == "string" ? desc : JSON.stringify(desc, null, 2));
|
||||||
|
|||||||
@@ -1601,7 +1601,7 @@ merge(Compressor.prototype, {
|
|||||||
case "Function":
|
case "Function":
|
||||||
case "Error":
|
case "Error":
|
||||||
case "Array":
|
case "Array":
|
||||||
return make_node(AST_Call, self, self);
|
return make_node(AST_Call, self, self).transform(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -340,8 +340,11 @@ function OutputStream(options) {
|
|||||||
/* -----[ utils ]----- */
|
/* -----[ utils ]----- */
|
||||||
|
|
||||||
function DEFPRINT(nodetype, generator) {
|
function DEFPRINT(nodetype, generator) {
|
||||||
nodetype.DEFMETHOD("print", function(stream, force_parens){
|
nodetype.DEFMETHOD("_codegen", generator);
|
||||||
var self = this;
|
};
|
||||||
|
|
||||||
|
AST_Node.DEFMETHOD("print", function(stream, force_parens){
|
||||||
|
var self = this, generator = self._codegen;
|
||||||
stream.push_node(self);
|
stream.push_node(self);
|
||||||
if (force_parens || self.needs_parens(stream)) {
|
if (force_parens || self.needs_parens(stream)) {
|
||||||
stream.with_parens(function(){
|
stream.with_parens(function(){
|
||||||
@@ -356,7 +359,6 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
stream.pop_node();
|
stream.pop_node();
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
AST_Node.DEFMETHOD("print_to_string", function(options){
|
AST_Node.DEFMETHOD("print_to_string", function(options){
|
||||||
var s = OutputStream(options);
|
var s = OutputStream(options);
|
||||||
@@ -726,7 +728,7 @@ function OutputStream(options) {
|
|||||||
// to the inner IF). This function checks for this case and
|
// to the inner IF). This function checks for this case and
|
||||||
// adds the block brackets if needed.
|
// adds the block brackets if needed.
|
||||||
if (!self.body)
|
if (!self.body)
|
||||||
return output.semicolon();
|
return output.force_semicolon();
|
||||||
if (self.body instanceof AST_Do
|
if (self.body instanceof AST_Do
|
||||||
&& output.option("ie_proof")) {
|
&& output.option("ie_proof")) {
|
||||||
// https://github.com/mishoo/UglifyJS/issues/#issue/57 IE
|
// https://github.com/mishoo/UglifyJS/issues/#issue/57 IE
|
||||||
@@ -750,7 +752,7 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
self.body.print(output);
|
force_statement(self.body, output);
|
||||||
};
|
};
|
||||||
DEFPRINT(AST_If, function(self, output){
|
DEFPRINT(AST_If, function(self, output){
|
||||||
output.print("if");
|
output.print("if");
|
||||||
@@ -902,7 +904,7 @@ function OutputStream(options) {
|
|||||||
DEFPRINT(AST_New, function(self, output){
|
DEFPRINT(AST_New, function(self, output){
|
||||||
output.print("new");
|
output.print("new");
|
||||||
output.space();
|
output.space();
|
||||||
AST_Call.prototype.print.call(self, output);
|
AST_Call.prototype._codegen(self, output);
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Seq.DEFMETHOD("_do_print", function(output){
|
AST_Seq.DEFMETHOD("_do_print", function(output){
|
||||||
|
|||||||
@@ -881,11 +881,14 @@ function parse($TEXT, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var function_ = function(in_statement, ctor) {
|
var function_ = function(in_statement, ctor) {
|
||||||
var name = is("name") ? as_symbol(in_statement
|
var is_accessor = ctor === AST_Accessor;
|
||||||
|
var name = (is("name") ? as_symbol(in_statement
|
||||||
? AST_SymbolDefun
|
? AST_SymbolDefun
|
||||||
: ctor === AST_Accessor
|
: is_accessor
|
||||||
? AST_SymbolAccessor
|
? AST_SymbolAccessor
|
||||||
: AST_SymbolLambda) : null;
|
: AST_SymbolLambda)
|
||||||
|
: is_accessor && (is("string") || is("num")) ? as_atom_node()
|
||||||
|
: null);
|
||||||
if (in_statement && !name)
|
if (in_statement && !name)
|
||||||
unexpected();
|
unexpected();
|
||||||
expect("(");
|
expect("(");
|
||||||
|
|||||||
12
lib/scope.js
12
lib/scope.js
@@ -84,9 +84,12 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
|
|||||||
if (node instanceof AST_Scope) {
|
if (node instanceof AST_Scope) {
|
||||||
node.init_scope_vars(nesting);
|
node.init_scope_vars(nesting);
|
||||||
var save_scope = node.parent_scope = scope;
|
var save_scope = node.parent_scope = scope;
|
||||||
|
var save_labels = labels;
|
||||||
++nesting;
|
++nesting;
|
||||||
scope = node;
|
scope = node;
|
||||||
|
labels = new Dictionary();
|
||||||
descend();
|
descend();
|
||||||
|
labels = save_labels;
|
||||||
scope = save_scope;
|
scope = save_scope;
|
||||||
--nesting;
|
--nesting;
|
||||||
return true; // don't descend again in TreeWalker
|
return true; // don't descend again in TreeWalker
|
||||||
@@ -340,6 +343,7 @@ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
|
|||||||
return defaults(options, {
|
return defaults(options, {
|
||||||
except : [],
|
except : [],
|
||||||
eval : false,
|
eval : false,
|
||||||
|
sort : false
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -360,12 +364,16 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
|||||||
return true; // don't descend again in TreeWalker
|
return true; // don't descend again in TreeWalker
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Scope) {
|
if (node instanceof AST_Scope) {
|
||||||
var p = tw.parent();
|
var p = tw.parent(), a = [];
|
||||||
node.variables.each(function(symbol){
|
node.variables.each(function(symbol){
|
||||||
if (options.except.indexOf(symbol.name) < 0) {
|
if (options.except.indexOf(symbol.name) < 0) {
|
||||||
to_mangle.push(symbol);
|
a.push(symbol);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (options.sort) a.sort(function(a, b){
|
||||||
|
return b.references.length - a.references.length;
|
||||||
|
});
|
||||||
|
to_mangle.push.apply(to_mangle, a);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Label) {
|
if (node instanceof AST_Label) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
||||||
"homepage": "http://lisperator.net/uglifyjs",
|
"homepage": "http://lisperator.net/uglifyjs",
|
||||||
"main": "tools/node.js",
|
"main": "tools/node.js",
|
||||||
"version": "2.2.2",
|
"version": "2.2.3",
|
||||||
"engines": { "node" : ">=0.4.0" },
|
"engines": { "node" : ">=0.4.0" },
|
||||||
"maintainers": [{
|
"maintainers": [{
|
||||||
"name": "Mihai Bazon",
|
"name": "Mihai Bazon",
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ exports.minify = function(files, options) {
|
|||||||
inSourceMap : null,
|
inSourceMap : null,
|
||||||
fromString : false,
|
fromString : false,
|
||||||
warnings : false,
|
warnings : false,
|
||||||
|
mangle : {},
|
||||||
|
output : null,
|
||||||
|
compress : {}
|
||||||
});
|
});
|
||||||
if (typeof files == "string")
|
if (typeof files == "string")
|
||||||
files = [ files ];
|
files = [ files ];
|
||||||
@@ -73,16 +76,20 @@ exports.minify = function(files, options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 2. compress
|
// 2. compress
|
||||||
|
if (options.compress) {
|
||||||
|
var compress = { warnings: options.warnings };
|
||||||
|
UglifyJS.merge(compress, options.compress);
|
||||||
toplevel.figure_out_scope();
|
toplevel.figure_out_scope();
|
||||||
var sq = UglifyJS.Compressor({
|
var sq = UglifyJS.Compressor(compress);
|
||||||
warnings: options.warnings,
|
|
||||||
});
|
|
||||||
toplevel = toplevel.transform(sq);
|
toplevel = toplevel.transform(sq);
|
||||||
|
}
|
||||||
|
|
||||||
// 3. mangle
|
// 3. mangle
|
||||||
|
if (options.mangle) {
|
||||||
toplevel.figure_out_scope();
|
toplevel.figure_out_scope();
|
||||||
toplevel.compute_char_frequency();
|
toplevel.compute_char_frequency();
|
||||||
toplevel.mangle_names();
|
toplevel.mangle_names(options.mangle);
|
||||||
|
}
|
||||||
|
|
||||||
// 4. output
|
// 4. output
|
||||||
var map = null;
|
var map = null;
|
||||||
@@ -95,7 +102,11 @@ exports.minify = function(files, options) {
|
|||||||
orig: inMap,
|
orig: inMap,
|
||||||
root: options.sourceRoot
|
root: options.sourceRoot
|
||||||
});
|
});
|
||||||
var stream = UglifyJS.OutputStream({ source_map: map });
|
var output = { source_map: map };
|
||||||
|
if (options.output) {
|
||||||
|
UglifyJS.merge(output, options.output);
|
||||||
|
}
|
||||||
|
var stream = UglifyJS.OutputStream(output);
|
||||||
toplevel.print(stream);
|
toplevel.print(stream);
|
||||||
return {
|
return {
|
||||||
code : stream + "",
|
code : stream + "",
|
||||||
|
|||||||
Reference in New Issue
Block a user