Compare commits

...

55 Commits
v2.2.3 ... v2.3

Author SHA1 Message Date
Mihai Bazon
5c22a1bdf5 v2.3 2013-05-01 13:14:07 +03:00
Mihai Bazon
9794ebf88c Workaround for missing prefix in UnaryExpression generated by Esprima
See #193
2013-04-29 15:03:52 +03:00
Mihai Bazon
68394eed93 Make compress/mangle disabled by default, as before 5af144522a 2013-04-21 11:35:50 +03:00
Mihai Bazon
753b4b6cc8 Merge pull request #191 from michaelficarra/use-es5-member-access-with-screw-ie
use dotted member access when --screw-ie8 option given
2013-04-21 01:30:02 -07:00
Mihai Bazon
a9c1b9f138 Merge pull request #190 from michaelficarra/patch-1
unbalanced parentheses in readme
2013-04-21 01:28:45 -07:00
Michael Ficarra
5af144522a fixes #189: use dotted member access when --screw-ie8 option given 2013-04-20 15:11:05 -05:00
Michael Ficarra
483e0cadfb unbalanced parentheses in readme 2013-04-20 14:05:52 -05:00
Roman Bataev
4b818056cf Fix typeof evaluation for regex and function 2013-04-03 22:34:38 -04:00
Roman Bataev
b956e5f1d9 Add tests for typeof evaluation 2013-04-03 22:34:19 -04:00
Vladimir Zhuravlev
37d7cb8565 Quote objects with numeric keys 2013-03-31 19:52:28 +03:00
Mihai Bazon
2b8e206fec fix package.json 2013-03-31 13:38:02 +03:00
Mihai Bazon
a869b854fa Don't use \xYY for identifiers
Fix #173
2013-03-31 13:36:22 +03:00
Andreas Lind Petersen
81f5efe39a Output, to_ascii: Escape non-ascii chars with \xnn instead of \unnnn whenever possible. 2013-03-31 13:36:22 +03:00
Andreas Lind Petersen
69dde0462b uglifyjs binary: Make read_whole_file async and don't attempt to read STDIN synchronously. 2013-03-31 13:36:22 +03:00
Mihai Bazon
7628bcac01 Merge pull request #163 from mzgol/screw-oldie
renamed --screw-ie to --screw-oldie, documented it in README.md, indicat...
2013-03-25 09:05:44 -07:00
Michał Gołębiowski
75f0bbe6e8 renamed --screw-ie to --screw-ie8, documented it in README.md, indicated it doesn't break IE9+ 2013-03-25 17:03:21 +01:00
Jake Harding
478bf4dbdd Add support for enclose option. Closes #139. 2013-03-24 11:11:23 +02:00
Mihai Bazon
e0f67baf2d Don't print the warning on parse error, just throw a JS_Parse_Error.
Fix #159
2013-03-24 00:57:35 +02:00
Mihai Bazon
b14d3df3d2 Keep legit code working even when --screw-ie is not passed.
Previously:

    Without `--screw-ie`, UglifyJS would always leak names of function
    expressions into the containing scope, as if they were function
    declarations.  That was to emulate IE<9 behavior.  Code relying on this
    IE bug would continue to work properly after mangling, although it would
    only work in IE (since other engines don't share the bug).  Sometimes
    this broke legitimage code (see #153 and #155).

    With `--screw-ie` the names would not be leaked into the current scope,
    working properly in legit cases; but still it broke legit code when
    running in IE<9 (see #24).

Currently:

    Regardless of the `--screw-ie` setting, the names will not be leaked.
    Code relying on the IE bug will not work properly after mangling.
    <evil laughter here>

    Without `--screw-ie`: a hack has been added to the mangler to avoid
    using the same name for a function expression and some other variable in
    the same scope.  This keeps legit code working, at the (negligible,
    indeed) cost of one more identifier.

    With `--screw-ie` you allow the mangler to name function expressions
    with the same identifier as another variable in scope.  After mangling
    code might break in IE<9.

Oh man, the commit message is longer than the patch.

Fix #153, #155
2013-03-22 18:04:46 +02:00
Mihai Bazon
24e58ee70c Merge pull request #125 from devongovett/master
Allow inSourceMap option to be a generated JSON source map
2013-03-13 01:36:55 -07:00
Mihai Bazon
9b1a40dfc3 Support mangling toplevel names
Close #127
2013-03-13 09:44:06 +02:00
Mihai Bazon
e4b078cff7 Disable unsafe by default
Close #147
2013-03-11 00:04:31 +02:00
Mihai Bazon
3bd7ca9961 Merge pull request #146 from mbostock/read-all-stdin
Read the entire STDIN.
2013-03-05 22:17:09 -08:00
Mike Bostock
f83aca65b7 Read the entire STDIN.
The problem with reading synchronously from /dev/stdin is that you can get a
spurious EOF when the input buffer is empty, even if more content is coming. Now
STDIN is read from a loop, and only stops polling when all input has been read.
This fixes #70 #85 and other errors related to parsing large files on STDIN.
2013-03-05 20:35:49 -08:00
Mihai Bazon
aebafad41e Fix reordering comparisons
Close #143
2013-03-04 10:06:01 +02:00
Mihai Bazon
26746ce316 Add --screw-ie option
For now the implication is that UglifyJS will not leak a function
expression's name in the surrounding scope (IE < 9 does that).

(ref. mishoo/UglifyJS#485)
2013-03-02 14:28:34 +02:00
Mihai Bazon
dac6efb43d Drop last default: if it's the last branch and empty
Close #141
2013-03-01 13:12:03 +02:00
Mihai Bazon
8880f4824c Compress boolean constants after evaluation
Close #137
2013-03-01 10:26:06 +02:00
Mihai Bazon
cb0c576bdd Add license
Close #131
2013-02-22 13:58:16 +02:00
Mihai Bazon
3a591c43fe Fix compressing do {...} while (false)
It's not safe to transform it to {...} because the body might contain
`break`.  The solution could be more elaborate (detect if body contains
`break`) but I don't think it's worth the trouble.

Close #129
2013-02-19 18:12:19 +02:00
Mihai Bazon
db66eca958 v2.2.5 2013-02-14 12:51:13 +02:00
Devon Govett
f2767452e6 Allow inSourceMap to be a generated JSON source map instead of just a file name 2013-02-10 10:06:13 -08:00
Mihai Bazon
916faf0a48 Force space after literal regexp when used in "instanceof" or "in"
Close #118
2013-02-06 11:57:59 +02:00
Mihai Bazon
f36e4e9a78 Give up evaluating (unary-prefix '-' 0)
Close #117

------

    JS, WHY YOU SUCK SO BADLY? ;-(
2013-02-06 11:51:09 +02:00
Mihai Bazon
fdf8b5eb71 Fix parens for NaN
Close #116
2013-02-06 11:38:29 +02:00
Mihai Bazon
de7ec7f1b7 Fix parens for negative numbers
Close #115
2013-02-06 11:36:04 +02:00
Mihai Bazon
3c8a0bdff4 Fix parens for AST_New
Close #114
2013-02-06 11:28:49 +02:00
Mihai Bazon
9e8ba27dcd Fix handling of constants
Close #113
2013-02-06 11:15:31 +02:00
Mihai Bazon
719a8fd102 Ugly hack to print comments before return/throw statements
Close #112
2013-02-05 19:11:39 +02:00
Mihai Bazon
3a22e917de Merge pull request #111 from mattrobenolt/safer-sourcemap
Wraps sourceMappingURL in a multiline comment. Fixes #108
2013-02-03 23:44:31 -08:00
Matt Robenolt
a9af2c9e62 Wraps sourceMappingURL in a multiline comment. Fixes #108 2013-02-03 16:01:01 -08:00
Mihai Bazon
31e99cebe7 v2.2.4 2013-02-01 13:32:15 +02:00
Mihai Bazon
a5b209470c Fix end token for Assign nodes 2013-02-01 13:32:15 +02:00
Mihai Bazon
e9a571b2a1 Merge pull request #94 from paulmillr/patch-1
Add better fromstring docs.
2013-01-31 23:50:59 -08:00
Mihai Bazon
8bf83f42ea Merge pull request #106 from gibson042/105
Fix #105: property comparison to undefined is not always safe
2013-01-24 05:51:33 -08:00
Richard Gibson
522566ea80 Fix #105: property comparison to undefined is not always safe 2013-01-23 23:52:04 -05:00
Mihai Bazon
297af47c89 Add --source-map-url option
Fix #100
Fix #47
2013-01-20 12:32:07 +02:00
Mihai Bazon
faa354f5ca [AST_Hole] the print function can be a no-op. 2013-01-17 11:36:10 +02:00
David Glasser
1529ab965a Fix output for arrays containing undefined values.
1b6bcca7 was a first attempt at this. That commit made Uglify stop replacing
holes with undefined, but instead it started replacing undefined with
holes. This is slightly problematic, because there is a difference between a
hole and an undefined value. More problematically, it changed [1,undefined] to
[1,] which generally doesn't even parse as a hole (just as a trailing comma), so
it didn't even preserve the length of the array!

Instead, parse holes as their own special AST node which prints invisibly.
2013-01-17 11:36:10 +02:00
Mihai Bazon
605f330e69 Merge pull request #98 from ForbesLindesay/patch-1
Update installation instructions
2013-01-17 01:08:59 -08:00
Mihai Bazon
f0909bdc8f Handle String() with no arguments.
Fix #91
2013-01-17 11:01:38 +02:00
Forbes Lindesay
c13e7e621d Update installation instructions re #4 2013-01-17 00:13:42 +00:00
Paul Miller
ad071f8017 Add better fromstring docs. 2013-01-13 18:45:43 +02:00
Mihai Bazon
c058d8b9cd Merge pull request #90 from jakearchibald/patch-1
Compressor options use underscores rather than hyphens
2013-01-08 14:21:25 -08:00
Jake Archibald
1d8871a092 Compressor options use underscores rather than hyphens 2013-01-08 12:33:58 -08:00
16 changed files with 471 additions and 185 deletions

29
LICENSE Normal file
View File

@@ -0,0 +1,29 @@
UglifyJS is released under the BSD license:
Copyright 2012-2013 (c) Mihai Bazon <mihai.bazon@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

View File

@@ -12,7 +12,14 @@ Chrome and probably Safari).
Install
-------
From NPM:
First make sure you have installed the latest version of [node.js](http://nodejs.org/)
(You may need to restart your computer after this step).
From NPM for use as a command line app:
npm install uglify-js -g
From NPM for programmatic use:
npm install uglify-js
@@ -42,8 +49,14 @@ The available options are:
[string]
--source-map-root The path to the original source to be included in the
source map. [string]
--source-map-url The path to the source map to be added in //@
sourceMappingURL. Defaults to the value passed with
--source-map. [string]
--in-source-map Input source map, useful if you're compressing JS that was
generated from some other original code.
--screw-ie8 Pass this flag if you don't care about full compliance with
Internet Explorer 6-8 quirks (by default UglifyJS will try
to be IE-proof).
-p, --prefix Skip prefix for original filenames that appear in source
maps. For example -p 3 will drop 3 directories from file
names and ensure they are relative paths.
@@ -78,8 +91,9 @@ The available options are:
[string]
--export-all Only used when --wrap, this tells UglifyJS to add code to
automatically export all globals. [boolean]
--lint Display some scope warnings [boolean]
-v, --verbose Verbose [boolean]
-V, --version Print version number and exits. [boolean]
-V, --version Print version number and exit. [boolean]
Specify `--output` (`-o`) to declare the output file. Otherwise the output
goes to STDOUT.
@@ -130,12 +144,19 @@ input files from the command line.
## Mangler options
To enable the mangler you need to pass `--mangle` (`-m`). Optionally you
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
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
enabled by default.
To enable the mangler you need to pass `--mangle` (`-m`). The following
(comma-separated) options are supported:
- `sort` — 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 (and seems to happen for other libraries I tried it
on) therefore it's not enabled by default.
- `toplevel` — mangle names declared in the toplevel scope (disabled by
default).
- `eval` — mangle names visible in scopes where `eval` or `when` are used
(disabled by default).
When mangling is enabled but you want to prevent certain names from being
mangled, you can declare those names with `--reserved` (`-r`) — pass a
@@ -152,15 +173,12 @@ you can pass a comma-separated list of options. Options are in the form
`foo=bar`, or just `foo` (the latter implies a boolean option that you want
to set `true`; it's effectively a shortcut for `foo=true`).
The defaults should be tuned for maximum compression on most code. Here are
the available options (all are `true` by default, except `hoist_vars`):
- `sequences` -- join consecutive simple statements using the comma operator
- `properties` -- rewrite property access using the dot notation, for
example `foo["bar"] → foo.bar`
- `dead-code` -- remove unreachable code
- `drop-debugger` -- remove `debugger;` statements
- `unsafe` -- apply "unsafe" transformations (discussion below)
- `dead_code` -- remove unreachable code
- `drop_debugger` -- remove `debugger;` statements
- `unsafe` (default: false) -- apply "unsafe" transformations (discussion below)
- `conditionals` -- apply optimizations for `if`-s and conditional
expressions
- `comparisons` -- apply certain optimizations to binary nodes, for example:
@@ -172,16 +190,32 @@ the available options (all are `true` by default, except `hoist_vars`):
- `loops` -- optimizations for `do`, `while` and `for` loops when we can
statically determine the condition
- `unused` -- drop unreferenced functions and variables
- `hoist-funs` -- hoist function declarations
- `hoist-vars` -- hoist `var` declarations (this is `false` by default
because it seems to increase the size of the output in general)
- `if-return` -- optimizations for if/return and if/continue
- `join-vars` -- join consecutive `var` statements
- `hoist_funs` -- hoist function declarations
- `hoist_vars` (default: false) -- hoist `var` declarations (this is `false`
by default because it seems to increase the size of the output in general)
- `if_return` -- optimizations for if/return and if/continue
- `join_vars` -- join consecutive `var` statements
- `cascade` -- small optimization for sequences, transform `x, x` into `x`
and `x = something(), x` into `x = something()`
- `warnings` -- display warnings when dropping unreachable code or unused
declarations etc.
### The `unsafe` option
It enables some transformations that *might* break code logic in certain
contrived cases, but should be fine for most code. You might want to try it
on your own code, it should reduce the minified size. Here's what happens
when this flag is on:
- `new Array(1, 2, 3)` or `Array(1, 2, 3)` → `[1, 2, 3 ]`
- `new Object()` → `{}`
- `String(exp)` or `exp.toString()` → `"" + exp`
- `new Object/RegExp/Function/Error/Array (...)` → we discard the `new`
- `typeof foo == "undefined"` → `foo === void 0`
- `void 0` → `"undefined"` (if there is a variable named "undefined" in
scope; we do it because the variable name will be mangled, typically
reduced to a single character).
### Conditional compilation
You can use the `--define` (`-d`) switch in order to declare global
@@ -324,9 +358,10 @@ There's a single toplevel function which combines all the steps. If you
don't need additional customization, you might want to go with `minify`.
Example:
// see "fromString" below if you need to pass code instead of file name
var result = UglifyJS.minify("/path/to/file.js");
console.log(result.code); // minified output
// if you need to pass code instead of file name
var result = UglifyJS.minify("var b = function () {};", {fromString: true});
You can also compress multiple files:

View File

@@ -7,6 +7,7 @@ var UglifyJS = require("../tools/node");
var sys = require("util");
var optimist = require("optimist");
var fs = require("fs");
var async = require("async");
var acorn;
var ARGS = optimist
.usage("$0 input1.js [input2.js ...] [options]\n\
@@ -19,7 +20,9 @@ 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.")
.describe("source-map-url", "The path to the source map to be added in //@ sourceMappingURL. Defaults to the value passed with --source-map.")
.describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.")
.describe("screw-ie8", "Pass this flag if you don't care about full compliance with Internet Explorer 6-8 quirks (by default UglifyJS will try to be IE-proof).")
.describe("p", "Skip prefix for original filenames that appear in source maps. \
For example -p 3 will drop 3 directories from file names and ensure they are relative paths.")
.describe("o", "Output file (default STDOUT).")
@@ -30,6 +33,7 @@ For example -p 3 will drop 3 directories from file names and ensure they are rel
Pass options like -c hoist_vars=false,if_return=false. \
Use -c with no argument to use the default compression options.")
.describe("d", "Global definitions")
.describe("e", "Embed everything in a big function, with a configurable parameter/argument list.")
.describe("comments", "Preserve copyright comments in the output. \
By default this works like Google Closure, keeping JSDoc-style comments that contain \"@license\" or \"@preserve\". \
@@ -60,15 +64,19 @@ You need to pass an argument to this option to specify the name that your module
.alias("d", "define")
.alias("r", "reserved")
.alias("V", "version")
.alias("e", "enclose")
.string("source-map")
.string("source-map-root")
.string("source-map-url")
.string("b")
.string("m")
.string("c")
.string("d")
.string("e")
.string("comments")
.string("wrap")
.boolean("screw-ie8")
.boolean("export-all")
.boolean("self")
.boolean("v")
@@ -110,12 +118,17 @@ var COMPRESS = getOptions("c", true);
var MANGLE = getOptions("m", true);
var BEAUTIFY = getOptions("b", true);
if (COMPRESS && ARGS.d) {
COMPRESS.global_defs = getOptions("d");
if (ARGS.d) {
if (COMPRESS) COMPRESS.global_defs = getOptions("d");
}
if (MANGLE && ARGS.r) {
MANGLE.except = ARGS.r.replace(/^\s+|\s+$/g).split(/\s*,+\s*/);
if (ARGS.screw_ie8) {
if (COMPRESS) COMPRESS.screw_ie8 = true;
if (MANGLE) MANGLE.screw_ie8 = true;
}
if (ARGS.r) {
if (MANGLE) MANGLE.except = ARGS.r.replace(/^\s+|\s+$/g).split(/\s*,+\s*/);
}
var OUTPUT_OPTIONS = {
@@ -204,100 +217,116 @@ try {
}
}
files.forEach(function(file) {
var code = read_whole_file(file);
if (ARGS.p != null) {
file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
async.eachLimit(files, 1, function (file, cb) {
read_whole_file(file, function (err, code) {
if (err) {
sys.error("ERROR: can't read file: " + filename);
process.exit(1);
}
if (ARGS.p != null) {
file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
}
time_it("parse", function(){
if (ARGS.spidermonkey) {
var program = JSON.parse(code);
if (!TOPLEVEL) TOPLEVEL = program;
else TOPLEVEL.body = TOPLEVEL.body.concat(program.body);
}
else if (ARGS.acorn) {
TOPLEVEL = acorn.parse(code, {
locations : true,
trackComments : true,
sourceFile : file,
program : TOPLEVEL
});
}
else {
TOPLEVEL = UglifyJS.parse(code, {
filename: file,
toplevel: TOPLEVEL
});
};
});
cb();
});
}, function () {
if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
});
if (ARGS.wrap) {
TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
}
time_it("parse", function(){
if (ARGS.spidermonkey) {
var program = JSON.parse(code);
if (!TOPLEVEL) TOPLEVEL = program;
else TOPLEVEL.body = TOPLEVEL.body.concat(program.body);
if (ARGS.enclose) {
var arg_parameter_list = ARGS.enclose;
if (!(arg_parameter_list instanceof Array)) {
arg_parameter_list = [arg_parameter_list];
}
else if (ARGS.acorn) {
TOPLEVEL = acorn.parse(code, {
locations : true,
trackComments : true,
sourceFile : file,
program : TOPLEVEL
});
}
else {
TOPLEVEL = UglifyJS.parse(code, {
filename: file,
toplevel: TOPLEVEL
});
};
TOPLEVEL = TOPLEVEL.wrap_enclose(arg_parameter_list);
}
var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
if (SCOPE_IS_NEEDED) {
time_it("scope", function(){
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8 });
if (ARGS.lint) {
TOPLEVEL.scope_warnings();
}
});
}
if (COMPRESS) {
time_it("squeeze", function(){
TOPLEVEL = TOPLEVEL.transform(compressor);
});
}
if (SCOPE_IS_NEEDED) {
time_it("scope", function(){
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8 });
if (MANGLE) {
TOPLEVEL.compute_char_frequency(MANGLE);
}
});
}
if (MANGLE) time_it("mangle", function(){
TOPLEVEL.mangle_names(MANGLE);
});
});
if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
});
if (ARGS.wrap) {
TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
}
var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
if (SCOPE_IS_NEEDED) {
time_it("scope", function(){
TOPLEVEL.figure_out_scope();
if (ARGS.lint) {
TOPLEVEL.scope_warnings();
}
time_it("generate", function(){
TOPLEVEL.print(output);
});
}
if (COMPRESS) {
time_it("squeeze", function(){
TOPLEVEL = TOPLEVEL.transform(compressor);
});
}
output = output.get();
if (SCOPE_IS_NEEDED) {
time_it("scope", function(){
TOPLEVEL.figure_out_scope();
if (MANGLE) {
TOPLEVEL.compute_char_frequency(MANGLE);
}
});
}
if (SOURCE_MAP) {
fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
output += "\n/*\n//@ sourceMappingURL=" + (ARGS.source_map_url || ARGS.source_map) + "\n*/";
}
if (MANGLE) time_it("mangle", function(){
TOPLEVEL.mangle_names(MANGLE);
});
time_it("generate", function(){
TOPLEVEL.print(output);
});
if (OUTPUT_FILE) {
fs.writeFileSync(OUTPUT_FILE, output, "utf8");
} else {
sys.print(output);
sys.error("\n");
}
output = output.get();
if (SOURCE_MAP) {
fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
output += "\n//@ sourceMappingURL=" + ARGS.source_map;
}
if (OUTPUT_FILE) {
fs.writeFileSync(OUTPUT_FILE, output, "utf8");
} else {
sys.print(output);
sys.error("\n");
}
if (ARGS.stats) {
sys.error(UglifyJS.string_template("Timing information (compressed {count} files):", {
count: files.length
}));
for (var i in STATS) if (STATS.hasOwnProperty(i)) {
sys.error(UglifyJS.string_template("- {name}: {time}s", {
name: i,
time: (STATS[i] / 1000).toFixed(3)
if (ARGS.stats) {
sys.error(UglifyJS.string_template("Timing information (compressed {count} files):", {
count: files.length
}));
for (var i in STATS) if (STATS.hasOwnProperty(i)) {
sys.error(UglifyJS.string_template("- {name}: {time}s", {
name: i,
time: (STATS[i] / 1000).toFixed(3)
}));
}
}
}
});
/* -----[ functions ]----- */
@@ -342,17 +371,18 @@ function getOptions(x, constants) {
return ret;
}
function read_whole_file(filename) {
function read_whole_file(filename, cb) {
if (filename == "-") {
// XXX: this sucks. How does one read the whole STDIN
// synchronously?
filename = "/dev/stdin";
}
try {
return fs.readFileSync(filename, "utf8");
} catch(ex) {
sys.error("ERROR: can't read file: " + filename);
process.exit(1);
var chunks = [];
process.stdin.setEncoding('utf-8');
process.stdin.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
cb(null, chunks.join(""));
});
process.openStdin();
} else {
fs.readFile(filename, "utf-8", cb);
}
}

View File

@@ -285,6 +285,27 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
$propdoc: {
globals: "[Object/S] a map of name -> SymbolDef for all undeclared names",
},
wrap_enclose: function(arg_parameter_pairs) {
var self = this;
var args = [];
var parameters = [];
arg_parameter_pairs.forEach(function(pair) {
var split = pair.split(":");
args.push(split[0]);
parameters.push(split[1]);
});
var wrapped_tl = "(function(" + parameters.join(",") + "){ '$ORIG'; })(" + args.join(",") + ")";
wrapped_tl = parse(wrapped_tl);
wrapped_tl = wrapped_tl.transform(new TreeTransformer(function before(node){
if (node instanceof AST_Directive && node.value == "$ORIG") {
return MAP.splice(self.body);
}
}));
return wrapped_tl;
},
wrap_commonjs: function(name, export_all) {
var self = this;
var to_export = [];
@@ -863,6 +884,11 @@ var AST_Undefined = DEFNODE("Undefined", null, {
value: (function(){}())
}, AST_Atom);
var AST_Hole = DEFNODE("Hole", null, {
$documentation: "A hole in an array",
value: (function(){}())
}, AST_Atom);
var AST_Infinity = DEFNODE("Infinity", null, {
$documentation: "The `Infinity` value",
value: 1/0

View File

@@ -52,7 +52,7 @@ function Compressor(options, false_by_default) {
properties : !false_by_default,
dead_code : !false_by_default,
drop_debugger : !false_by_default,
unsafe : !false_by_default,
unsafe : false,
unsafe_comps : false,
conditionals : !false_by_default,
comparisons : !false_by_default,
@@ -66,6 +66,7 @@ function Compressor(options, false_by_default) {
join_vars : !false_by_default,
cascade : !false_by_default,
side_effects : !false_by_default,
screw_ie8 : false,
warnings : true,
global_defs : {}
@@ -156,7 +157,7 @@ merge(Compressor.prototype, {
value: val
}).optimize(compressor);
case "boolean":
return make_node(val ? AST_True : AST_False, orig);
return make_node(val ? AST_True : AST_False, orig).optimize(compressor);
case "undefined":
return make_node(AST_Undefined, orig).optimize(compressor);
default:
@@ -625,10 +626,24 @@ merge(Compressor.prototype, {
var e = this.expression;
switch (this.operator) {
case "!": return !ev(e);
case "typeof": return typeof ev(e);
case "typeof":
// Function would be evaluated to an array and so typeof would
// incorrectly return 'object'. Hence making is a special case.
if (e instanceof AST_Function) return typeof function(){};
e = ev(e);
// typeof <RegExp> returns "object" or "function" on different platforms
// so cannot evaluate reliably
if (e instanceof RegExp) throw def;
return typeof e;
case "void": return void ev(e);
case "~": return ~ev(e);
case "-": return -ev(e);
case "-":
e = ev(e);
if (e === 0) throw def;
return -e;
case "+": return +ev(e);
}
throw def;
@@ -669,12 +684,7 @@ merge(Compressor.prototype, {
});
def(AST_SymbolRef, function(){
var d = this.definition();
if (d && d.constant) {
var orig = d.orig[0];
if (orig) orig = orig.init[0];
orig = orig && orig.value;
if (orig) return ev(orig);
}
if (d && d.constant && d.init) return ev(d.init);
throw def;
});
})(function(node, func){
@@ -1199,8 +1209,6 @@ merge(Compressor.prototype, {
extract_declarations_from_unreachable_code(compressor, self.body, a);
return make_node(AST_BlockStatement, self, { body: a });
}
} else {
return self.body;
}
}
return self;
@@ -1418,11 +1426,18 @@ merge(Compressor.prototype, {
body: self.expression
}).transform(compressor);
}
var last_branch = self.body[self.body.length - 1];
if (last_branch) {
var stat = last_branch.body[last_branch.body.length - 1]; // last statement
if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self)
last_branch.body.pop();
for(;;) {
var last_branch = self.body[self.body.length - 1];
if (last_branch) {
var stat = last_branch.body[last_branch.body.length - 1]; // last statement
if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self)
last_branch.body.pop();
if (last_branch instanceof AST_Default && last_branch.body.length == 0) {
self.body.pop();
continue;
}
}
break;
}
var exp = self.expression.evaluate(compressor);
out: if (exp.length == 2) try {
@@ -1566,6 +1581,9 @@ merge(Compressor.prototype, {
}
break;
case "String":
if (self.args.length == 0) return make_node(AST_String, self, {
value: ""
});
return make_node(AST_Binary, self, {
left: self.args[0],
operator: "+",
@@ -1709,7 +1727,7 @@ merge(Compressor.prototype, {
OPT(AST_Binary, function(self, compressor){
function reverse(op) {
if (!(self.left.has_side_effects() && self.right.has_side_effects())) {
if (!(self.left.has_side_effects() || self.right.has_side_effects())) {
if (op) self.operator = op;
var tmp = self.left;
self.left = self.right;
@@ -1736,7 +1754,8 @@ merge(Compressor.prototype, {
if (self.left instanceof AST_String
&& self.left.value == "undefined"
&& self.right instanceof AST_UnaryPrefix
&& self.right.operator == "typeof") {
&& self.right.operator == "typeof"
&& compressor.option("unsafe")) {
if (!(self.right.expression instanceof AST_SymbolRef)
|| !self.right.expression.undeclared()) {
self.left = self.right.expression;
@@ -1943,7 +1962,7 @@ merge(Compressor.prototype, {
var prop = self.property;
if (prop instanceof AST_String && compressor.option("properties")) {
prop = prop.getValue();
if (is_identifier(prop)) {
if (is_identifier(prop) || compressor.option("screw_ie8")) {
return make_node(AST_Dot, self, {
expression : self.expression,
property : prop

View File

@@ -148,12 +148,14 @@
};
function From_Moz_Unary(M) {
return new (M.prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
var prefix = "prefix" in M ? M.prefix
: M.type == "UnaryExpression" ? true : false;
return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
start : my_start_token(M),
end : my_end_token(M),
operator : M.operator,
expression : from_moz(M.argument)
})
});
};
var ME_TO_MOZ = {};

View File

@@ -69,11 +69,16 @@ function OutputStream(options) {
var current_pos = 0;
var OUTPUT = "";
function to_ascii(str) {
function to_ascii(str, identifier) {
return str.replace(/[\u0080-\uffff]/g, function(ch) {
var code = ch.charCodeAt(0).toString(16);
while (code.length < 4) code = "0" + code;
return "\\u" + code;
if (code.length <= 2 && !identifier) {
while (code.length < 2) code = "0" + code;
return "\\x" + code;
} else {
while (code.length < 4) code = "0" + code;
return "\\u" + code;
}
});
};
@@ -109,7 +114,7 @@ function OutputStream(options) {
function make_name(name) {
name = name.toString();
if (options.ascii_only)
name = to_ascii(name);
name = to_ascii(name, true);
return name;
};
@@ -375,6 +380,16 @@ function OutputStream(options) {
if (start && !start._comments_dumped) {
start._comments_dumped = true;
var comments = start.comments_before;
// XXX: ugly fix for https://github.com/mishoo/UglifyJS2/issues/112
// if this node is `return` or `throw`, we cannot allow comments before
// the returned or thrown value.
if (self instanceof AST_Exit &&
self.value && self.value.start.comments_before.length > 0) {
comments = (comments || []).concat(self.value.start.comments_before);
self.value.start.comments_before = [];
}
if (c.test) {
comments = comments.filter(function(comment){
return c.test(comment.value);
@@ -499,11 +514,23 @@ function OutputStream(options) {
PARENS(AST_New, function(output){
var p = output.parent();
if (no_constructor_parens(this, output)
&& (p instanceof AST_Dot // (new Date).getTime()
&& (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]()
|| p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
return true;
});
PARENS(AST_Number, function(output){
var p = output.parent();
if (this.getValue() < 0 && p instanceof AST_PropAccess && p.expression === this)
return true;
});
PARENS(AST_NaN, function(output){
var p = output.parent();
if (p instanceof AST_PropAccess && p.expression === this)
return true;
});
function assign_and_conditional_paren_rules(output) {
var p = output.parent();
// !(a = false) → true
@@ -932,7 +959,7 @@ function OutputStream(options) {
DEFPRINT(AST_Dot, function(self, output){
var expr = self.expression;
expr.print(output);
if (expr instanceof AST_Number) {
if (expr instanceof AST_Number && expr.getValue() >= 0) {
if (!/[xa-f.]/i.test(output.last())) {
output.print(".");
}
@@ -984,8 +1011,7 @@ function OutputStream(options) {
if (len > 0) output.space();
a.forEach(function(exp, i){
if (i) output.comma();
if (!(exp instanceof AST_Undefined))
exp.print(output);
exp.print(output);
});
if (len > 0) output.space();
});
@@ -1007,7 +1033,7 @@ function OutputStream(options) {
DEFPRINT(AST_ObjectKeyVal, function(self, output){
var key = self.key;
if (output.option("quote_keys")) {
output.print_string(key);
output.print_string(key + "");
} else if ((typeof key == "number"
|| !output.option("beautify")
&& +key + "" == key)
@@ -1036,6 +1062,7 @@ function OutputStream(options) {
DEFPRINT(AST_Undefined, function(self, output){
output.print("void 0");
});
DEFPRINT(AST_Hole, noop);
DEFPRINT(AST_Infinity, function(self, output){
output.print("1/0");
});
@@ -1059,6 +1086,9 @@ function OutputStream(options) {
if (output.option("ascii_only"))
str = output.to_ascii(str);
output.print(str);
var p = output.parent();
if (p instanceof AST_Binary && /^in/.test(p.operator) && p.left === self)
output.print(" ");
});
function force_statement(stat, output) {

View File

@@ -149,7 +149,7 @@ function is_unicode_connector_punctuation(ch) {
};
function is_identifier(name) {
return /^[a-z_$][a-z0-9_$]*$/i.test(name) && !RESERVED_WORDS(name);
return !RESERVED_WORDS(name) && /^[a-z_$][a-z0-9_$]*$/i.test(name);
};
function is_identifier_start(code) {
@@ -190,12 +190,6 @@ JS_Parse_Error.prototype.toString = function() {
};
function js_error(message, filename, line, col, pos) {
AST_Node.warn("ERROR: {message} [{file}:{line},{col}]", {
message: message,
file: filename,
line: line,
col: col
});
throw new JS_Parse_Error(message, line, col, pos);
};
@@ -1131,7 +1125,7 @@ function parse($TEXT, options) {
if (first) first = false; else expect(",");
if (allow_trailing_comma && is("punc", closing)) break;
if (is("punc", ",") && allow_empty) {
a.push(new AST_Undefined({ start: S.token, end: S.token }));
a.push(new AST_Hole({ start: S.token, end: S.token }));
} else {
a.push(expression(false));
}
@@ -1358,7 +1352,7 @@ function parse($TEXT, options) {
left : left,
operator : val,
right : maybe_assign(no_in),
end : peek()
end : prev()
});
}
croak("Invalid assignment");

View File

@@ -57,13 +57,17 @@ function SymbolDef(scope, index, orig) {
SymbolDef.prototype = {
unmangleable: function(options) {
return this.global
return (this.global && !(options && options.toplevel))
|| this.undeclared
|| (!(options && options.eval) && (this.scope.uses_eval || this.scope.uses_with));
},
mangle: function(options) {
if (!this.mangled_name && !this.unmangleable(options))
this.mangled_name = this.scope.next_mangled(options);
if (!this.mangled_name && !this.unmangleable(options)) {
var s = this.scope;
if (this.orig[0] instanceof AST_SymbolLambda && !options.screw_ie8)
s = s.parent_scope;
this.mangled_name = s.next_mangled(options);
}
}
};
@@ -121,13 +125,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
node.init_scope_vars();
}
if (node instanceof AST_SymbolLambda) {
//scope.def_function(node);
//
// https://github.com/mishoo/UglifyJS2/issues/24 — MSIE
// leaks function expression names into the containing
// scope. Don't like this fix but seems we can't do any
// better. IE: please die. Please!
(node.scope = scope.parent_scope).def_function(node);
scope.def_function(node);
}
else if (node instanceof AST_SymbolDefun) {
// Careful here, the scope where this should be defined is
@@ -141,7 +139,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
|| node instanceof AST_SymbolConst) {
var def = scope.def_variable(node);
def.constant = node instanceof AST_SymbolConst;
def = tw.parent();
def.init = tw.parent().value;
}
else if (node instanceof AST_SymbolCatch) {
// XXX: this is wrong according to ECMA-262 (12.4). the
@@ -279,14 +277,14 @@ AST_Scope.DEFMETHOD("def_variable", function(symbol){
});
AST_Scope.DEFMETHOD("next_mangled", function(options){
var ext = this.enclosed, n = ext.length;
var ext = this.enclosed;
out: while (true) {
var m = base54(++this.cname);
if (!is_identifier(m)) continue; // skip over "do"
// we must ensure that the mangled name does not shadow a name
// from some parent scope that is referenced in this or in
// inner scopes.
for (var i = n; --i >= 0;) {
for (var i = ext.length; --i >= 0;) {
var sym = ext[i];
var name = sym.mangled_name || (sym.unmangleable(options) && sym.name);
if (m == name) continue out;
@@ -341,9 +339,11 @@ AST_Symbol.DEFMETHOD("global", function(){
AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
return defaults(options, {
except : [],
eval : false,
sort : false
except : [],
eval : false,
sort : false,
toplevel : false,
screw_ie8 : false
});
});

View File

@@ -3,7 +3,7 @@
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
"homepage": "http://lisperator.net/uglifyjs",
"main": "tools/node.js",
"version": "2.2.3",
"version": "2.3",
"engines": { "node" : ">=0.4.0" },
"maintainers": [{
"name": "Mihai Bazon",
@@ -15,6 +15,7 @@
"url": "https://github.com/mishoo/UglifyJS2.git"
}],
"dependencies": {
"async" : "~0.2.6",
"source-map" : "~0.1.7",
"optimist" : "~0.3.5"
},

12
test/compress/arrays.js Normal file
View File

@@ -0,0 +1,12 @@
holes_and_undefined: {
input: {
x = [1, 2, undefined];
y = [1, , 2, ];
z = [1, undefined, 3];
}
expect: {
x=[1,2,void 0];
y=[1,,2];
z=[1,void 0,3];
}
}

View File

@@ -0,0 +1,17 @@
typeof_eq_undefined: {
options = {
comparisons: true,
unsafe: false
};
input: { a = typeof b.c != "undefined" }
expect: { a = "undefined" != typeof b.c }
}
typeof_eq_undefined_unsafe: {
options = {
comparisons: true,
unsafe: true
};
input: { a = typeof b.c != "undefined" }
expect: { a = b.c !== void 0 }
}

View File

@@ -23,3 +23,18 @@ dot_properties: {
a["if"] = "if";
}
}
dot_properties_es5: {
options = {
properties: true,
screw_ie8: true
};
input: {
a["foo"] = "bar";
a["if"] = "if";
}
expect: {
a.foo = "bar";
a.if = "if";
}
}

View File

@@ -208,3 +208,53 @@ constant_switch_9: {
}
}
}
drop_default_1: {
options = { dead_code: true };
input: {
switch (foo) {
case 'bar': baz();
default:
}
}
expect: {
switch (foo) {
case 'bar': baz();
}
}
}
drop_default_2: {
options = { dead_code: true };
input: {
switch (foo) {
case 'bar': baz(); break;
default:
break;
}
}
expect: {
switch (foo) {
case 'bar': baz();
}
}
}
keep_default: {
options = { dead_code: true };
input: {
switch (foo) {
case 'bar': baz();
default:
something();
break;
}
}
expect: {
switch (foo) {
case 'bar': baz();
default:
something();
}
}
}

25
test/compress/typeof.js Normal file
View File

@@ -0,0 +1,25 @@
typeof_evaluation: {
options = {
evaluate: true
};
input: {
a = typeof 1;
b = typeof 'test';
c = typeof [];
d = typeof {};
e = typeof /./;
f = typeof false;
g = typeof function(){};
h = typeof undefined;
}
expect: {
a='number';
b='string';
c=typeof[];
d=typeof{};
e=typeof/./;
f='boolean';
g='function';
h='undefined';
}
}

View File

@@ -92,17 +92,18 @@ exports.minify = function(files, options) {
}
// 4. output
var map = null;
var inMap = null;
if (options.inSourceMap) {
var inMap = options.inSourceMap;
var output = {};
if (typeof options.inSourceMap == "string") {
inMap = fs.readFileSync(options.inSourceMap, "utf8");
}
if (options.outSourceMap) map = UglifyJS.SourceMap({
file: options.outSourceMap,
orig: inMap,
root: options.sourceRoot
});
var output = { source_map: map };
if (options.outSourceMap) {
output.source_map = UglifyJS.SourceMap({
file: options.outSourceMap,
orig: inMap,
root: options.sourceRoot
});
}
if (options.output) {
UglifyJS.merge(output, options.output);
}
@@ -110,7 +111,7 @@ exports.minify = function(files, options) {
toplevel.print(stream);
return {
code : stream + "",
map : map + ""
map : output.source_map + ""
};
};