Compare commits

...

14 Commits

Author SHA1 Message Date
Mihai Bazon
448a8d3845 v2.4.13 2014-03-11 15:22:37 +02:00
Mihai Bazon
f5c09d0bbf Merge pull request #439 from Arnavion/null-source-in-sourcemap
Handle the case when SourceMapConsumer.originalPositionFor returns null source.
2014-03-03 09:19:39 +02:00
Arnavion
014f655c5f Handle the case when SourceMapConsumer.originalPositionFor returns null source.
This happens when SourceMapConsumer does not have a valid position to map the input line and column. This is a change in mozilla/source-map starting from version 0.1.33

Fixes #436
2014-03-02 19:20:19 -08:00
Mihai Bazon
bf30dc3038 Mangle name of exception when --screw-ie8. Fix #430.
The effect of not mangling it was visible only with --screw-ie8 (otherwise
the names would be mangled exactly because they leaked into the parent
scope).
2014-02-14 13:58:14 +02:00
Mihai Bazon
ef2ef07cbd Add option keep_fargs.
By default it's `false`.  Pass `true` if you need to keep unused function
arguments.

Close #188.
2014-02-08 12:33:56 +02:00
Mihai Bazon
1a4440080d Merge pull request #424 from mattbasta/simplify_conditionals
Simplify nested conditionals if possible
2014-02-07 11:31:11 +02:00
Matt Basta
ac0086a745 Simplify nested conditionals if possible 2014-02-06 12:39:13 -08:00
Mihai Bazon
2494daaf68 Merge pull request #422 from mourner/patch-1
Fix readme typo (when -> with)
2014-02-06 18:13:10 +02:00
Vladimir Agafonkin
9b404f9de6 fix readme typo (when -> with) 2014-02-06 18:11:33 +02:00
Mihai Bazon
5344b7dab8 Fix if_return dropping the alternative. Close #413 2014-01-31 10:44:13 +02:00
Mihai Bazon
0007a53b9c Update source-map 2014-01-26 10:18:20 +02:00
Mihai Bazon
1dd05f44eb Merge branch 'sourcesContent' of https://github.com/arty-name/UglifyJS2 into arty-name-sourcesContent 2014-01-26 10:15:24 +02:00
Artemy Tregubenko
f6203bd5a8 added hasOwnProperty check to avoid warnings 2014-01-09 15:20:05 +01:00
Artemy Tregubenko
03cf94ebe8 Added support for sourcesContent property of source map 2014-01-09 15:12:00 +01:00
8 changed files with 93 additions and 16 deletions

View File

@@ -54,6 +54,10 @@ The available options are:
--source-map-url The path to the source map to be added in //#
sourceMappingURL. Defaults to the value passed with
--source-map. [string]
--source-map-include-sources
Pass this flag if you want to include the content of
source files in the source map as sourcesContent
property. [boolean]
--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
@@ -169,7 +173,7 @@ To enable the mangler you need to pass `--mangle` (`-m`). The following
- `toplevel` — mangle names declared in the toplevel scope (disabled by
default).
- `eval` — mangle names visible in scopes where `eval` or `when` are used
- `eval` — mangle names visible in scopes where `eval` or `with` are used
(disabled by default).
When mangling is enabled but you want to prevent certain names from being

View File

@@ -22,6 +22,7 @@ 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("source-map-include-sources", "Pass this flag if you want to include the content of source files in the source map as sourcesContent property.")
.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("expr", "Parse a single expression, rather than a program (for parsing JSON)")
@@ -88,6 +89,7 @@ You need to pass an argument to this option to specify the name that your module
.string("p")
.boolean("expr")
.boolean("source-map-include-sources")
.boolean("screw-ie8")
.boolean("export-all")
.boolean("self")
@@ -218,6 +220,7 @@ var STATS = {};
var OUTPUT_FILE = ARGS.o;
var TOPLEVEL = null;
var P_RELATIVE = ARGS.p && ARGS.p == "relative";
var SOURCES_CONTENT = {};
var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({
file: P_RELATIVE ? path.relative(path.dirname(ARGS.source_map), OUTPUT_FILE) : OUTPUT_FILE,
@@ -255,6 +258,7 @@ async.eachLimit(files, 1, function (file, cb) {
}
}
}
SOURCES_CONTENT[file] = code;
time_it("parse", function(){
if (ARGS.spidermonkey) {
var program = JSON.parse(code);
@@ -337,6 +341,15 @@ async.eachLimit(files, 1, function (file, cb) {
if (MANGLE) time_it("mangle", function(){
TOPLEVEL.mangle_names(MANGLE);
});
if (ARGS.source_map_include_sources) {
for (var file in SOURCES_CONTENT) {
if (SOURCES_CONTENT.hasOwnProperty(file)) {
SOURCE_MAP.get().setSourceContent(file, SOURCES_CONTENT[file]);
}
}
}
time_it("generate", function(){
TOPLEVEL.print(output);
});

View File

@@ -61,6 +61,7 @@ function Compressor(options, false_by_default) {
loops : !false_by_default,
unused : !false_by_default,
hoist_funs : !false_by_default,
keep_fargs : false,
hoist_vars : false,
if_return : !false_by_default,
join_vars : !false_by_default,
@@ -376,7 +377,7 @@ merge(Compressor.prototype, {
stat = stat.clone();
stat.condition = stat.condition.negate(compressor);
stat.body = make_node(AST_BlockStatement, stat, {
body: ret
body: as_statement_array(stat.alternative).concat(ret)
});
stat.alternative = make_node(AST_BlockStatement, stat, {
body: body
@@ -1044,18 +1045,20 @@ merge(Compressor.prototype, {
var tt = new TreeTransformer(
function before(node, descend, in_list) {
if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) {
for (var a = node.argnames, i = a.length; --i >= 0;) {
var sym = a[i];
if (sym.unreferenced()) {
a.pop();
compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {
name : sym.name,
file : sym.start.file,
line : sym.start.line,
col : sym.start.col
});
if (!compressor.option("keep_fargs")) {
for (var a = node.argnames, i = a.length; --i >= 0;) {
var sym = a[i];
if (sym.unreferenced()) {
a.pop();
compressor.warn("Dropping unused function argument {name} [{file}:{line},{col}]", {
name : sym.name,
file : sym.start.file,
line : sym.start.line,
col : sym.start.col
});
}
else break;
}
else break;
}
}
if (node instanceof AST_Defun && node !== self) {
@@ -2296,6 +2299,19 @@ merge(Compressor.prototype, {
return consequent;
}
}
// x?y?z:a:a --> x&&y?z:a
if (consequent instanceof AST_Conditional
&& consequent.alternative.equivalent_to(alternative)) {
return make_node(AST_Conditional, self, {
condition: make_node(AST_Binary, self, {
left: self.condition,
operator: "&&",
right: consequent.condition
}),
consequent: consequent.consequent,
alternative: alternative
});
}
return self;
});

View File

@@ -365,6 +365,10 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
node.mangled_name = name;
return true;
}
if (options.screw_ie8 && node instanceof AST_SymbolCatch) {
to_mangle.push(node.definition());
return;
}
});
this.walk(tw);
to_mangle.forEach(function(def){ def.mangle(options) });

View File

@@ -64,6 +64,9 @@ function SourceMap(options) {
line: orig_line,
column: orig_col
});
if (info.source === null) {
return;
}
source = info.source;
orig_line = info.line;
orig_col = info.column;

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.4.12",
"version": "2.4.13",
"engines": { "node" : ">=0.4.0" },
"maintainers": [{
"name": "Mihai Bazon",
@@ -16,7 +16,7 @@
},
"dependencies": {
"async" : "~0.2.6",
"source-map" : "~0.1.7",
"source-map" : "~0.1.33",
"optimist" : "~0.3.5",
"uglify-to-browserify": "~1.0.0"
},

View File

@@ -205,3 +205,30 @@ cond_4: {
some_condition(), do_something();
}
}
cond_5: {
options = {
conditionals: true
};
input: {
if (some_condition()) {
if (some_other_condition()) {
do_something();
} else {
alternate();
}
} else {
alternate();
}
if (some_condition()) {
if (some_other_condition()) {
do_something();
}
}
}
expect: {
some_condition() && some_other_condition() ? do_something() : alternate();
some_condition() && some_other_condition() && do_something();
}
}

View File

@@ -64,7 +64,8 @@ exports.minify = function(files, options) {
UglifyJS.base54.reset();
// 1. parse
var toplevel = null;
var toplevel = null,
sourcesContent = {};
if (options.spidermonkey) {
toplevel = UglifyJS.AST_Node.from_mozilla_ast(files);
@@ -75,6 +76,7 @@ exports.minify = function(files, options) {
var code = options.fromString
? file
: fs.readFileSync(file, "utf8");
sourcesContent[file] = code;
toplevel = UglifyJS.parse(code, {
filename: options.fromString ? "?" : file,
toplevel: toplevel
@@ -110,6 +112,14 @@ exports.minify = function(files, options) {
orig: inMap,
root: options.sourceRoot
});
if (options.sourceMapIncludeSources) {
for (var file in sourcesContent) {
if (sourcesContent.hasOwnProperty(file)) {
options.source_map.get().setSourceContent(file, sourcesContent[file]);
}
}
}
}
if (options.output) {
UglifyJS.merge(output, options.output);