uglify-js
Advanced tools
Comparing version
@@ -170,2 +170,10 @@ /*********************************************************************** | ||
function is_identifier_string(str){ | ||
for (var i = str.length; --i >= 0;) { | ||
if (!is_identifier_char(str.charAt(i))) | ||
return false; | ||
} | ||
return true; | ||
}; | ||
function parse_js_number(num) { | ||
@@ -172,0 +180,0 @@ if (RE_HEX_NUMBER.test(num)) { |
@@ -6,3 +6,3 @@ { | ||
"main": "tools/node.js", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"engines": { "node" : ">=0.4.0" }, | ||
@@ -9,0 +9,0 @@ "maintainers": [{ |
176
README.md
@@ -83,3 +83,3 @@ UglifyJS 2 | ||
--acorn Use Acorn for parsing. [boolean] | ||
--spidermonkey Assume input fles are SpiderMonkey AST format (as JSON). | ||
--spidermonkey Assume input files are SpiderMonkey AST format (as JSON). | ||
[boolean] | ||
@@ -226,7 +226,8 @@ --self Build itself (UglifyJS2) as a library (implies | ||
dead code removal UglifyJS will discard the following from the output: | ||
```javascript | ||
if (DEBUG) { | ||
console.log("debug stuff"); | ||
} | ||
``` | ||
if (DEBUG) { | ||
console.log("debug stuff"); | ||
} | ||
UglifyJS will warn about the condition being always false and about dropping | ||
@@ -239,7 +240,8 @@ unreachable code; for now there is no option to turn off only this specific | ||
`build/defines.js` file with the following: | ||
```javascript | ||
const DEBUG = false; | ||
const PRODUCTION = true; | ||
// etc. | ||
``` | ||
const DEBUG = false; | ||
const PRODUCTION = true; | ||
// etc. | ||
and build your code like this: | ||
@@ -302,11 +304,12 @@ | ||
example: | ||
```javascript | ||
function f() { | ||
/** @preserve Foo Bar */ | ||
function g() { | ||
// this function is never called | ||
} | ||
return something(); | ||
} | ||
``` | ||
function f() { | ||
/** @preserve Foo Bar */ | ||
function g() { | ||
// this function is never called | ||
} | ||
return something(); | ||
} | ||
Even though it has "@preserve", the comment will be lost because the inner | ||
@@ -352,5 +355,6 @@ function `g` (which is the AST node to which the comment is attached to) is | ||
like this: | ||
```javascript | ||
var UglifyJS = require("uglify-js"); | ||
``` | ||
var UglifyJS = require("uglify-js"); | ||
It exports a lot of names, but I'll discuss here the basics that are needed | ||
@@ -365,21 +369,24 @@ for parsing, mangling and compressing a piece of code. The sequence is (1) | ||
Example: | ||
```javascript | ||
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}); | ||
``` | ||
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: | ||
```javascript | ||
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ]); | ||
console.log(result.code); | ||
``` | ||
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ]); | ||
console.log(result.code); | ||
To generate a source map: | ||
```javascript | ||
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ], { | ||
outSourceMap: "out.js.map" | ||
}); | ||
console.log(result.code); // minified output | ||
console.log(result.map); | ||
``` | ||
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ], { | ||
outSourceMap: "out.js.map" | ||
}); | ||
console.log(result.code); // minified output | ||
console.log(result.map); | ||
Note that the source map is not saved in a file, it's just returned in | ||
@@ -390,18 +397,19 @@ `result.map`. The value passed for `outSourceMap` is only used to set the | ||
You can also specify sourceRoot property to be included in source map: | ||
```javascript | ||
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ], { | ||
outSourceMap: "out.js.map", | ||
sourceRoot: "http://example.com/src" | ||
}); | ||
``` | ||
var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ], { | ||
outSourceMap: "out.js.map", | ||
sourceRoot: "http://example.com/src" | ||
}); | ||
If you're compressing compiled JavaScript and have a source map for it, you | ||
can use the `inSourceMap` argument: | ||
```javascript | ||
var result = UglifyJS.minify("compiled.js", { | ||
inSourceMap: "compiled.js.map", | ||
outSourceMap: "minified.js.map" | ||
}); | ||
// same as before, it returns `code` and `map` | ||
``` | ||
var result = UglifyJS.minify("compiled.js", { | ||
inSourceMap: "compiled.js.map", | ||
outSourceMap: "minified.js.map" | ||
}); | ||
// same as before, it returns `code` and `map` | ||
The `inSourceMap` is only used if you also request `outSourceMap` (it makes | ||
@@ -435,5 +443,6 @@ no sense otherwise). | ||
#### The parser | ||
```javascript | ||
var toplevel_ast = UglifyJS.parse(code, options); | ||
``` | ||
var toplevel_ast = UglifyJS.parse(code, options); | ||
`options` is optional and if present it must be an object. The following | ||
@@ -451,12 +460,13 @@ properties are available: | ||
something like this: | ||
```javascript | ||
var toplevel = null; | ||
files.forEach(function(file){ | ||
var code = fs.readFileSync(file); | ||
toplevel = UglifyJS.parse(code, { | ||
filename: file, | ||
toplevel: toplevel | ||
}); | ||
}); | ||
``` | ||
var toplevel = null; | ||
files.forEach(function(file){ | ||
var code = fs.readFileSync(file); | ||
toplevel = UglifyJS.parse(code, { | ||
filename: file, | ||
toplevel: toplevel | ||
}); | ||
}); | ||
After this, we have in `toplevel` a big AST containing all our files, with | ||
@@ -474,12 +484,14 @@ each token having proper information about where it came from. | ||
anything with the tree: | ||
```javascript | ||
toplevel.figure_out_scope() | ||
``` | ||
toplevel.figure_out_scope() | ||
#### Compression | ||
Like this: | ||
```javascript | ||
var compressor = UglifyJS.Compressor(options); | ||
var compressed_ast = toplevel.transform(compressor); | ||
``` | ||
var compressor = UglifyJS.Compressor(options); | ||
var compressed_ast = toplevel.transform(compressor); | ||
The `options` can be missing. Available options are discussed above in | ||
@@ -499,7 +511,8 @@ “Compressor options”. Defaults should lead to best compression in most | ||
non-mangleable words). Example: | ||
```javascript | ||
compressed_ast.figure_out_scope(); | ||
compressed_ast.compute_char_frequency(); | ||
compressed_ast.mangle_names(); | ||
``` | ||
compressed_ast.figure_out_scope(); | ||
compressed_ast.compute_char_frequency(); | ||
compressed_ast.mangle_names(); | ||
#### Generating output | ||
@@ -509,11 +522,13 @@ | ||
to generate code you do this: | ||
```javascript | ||
var stream = UglifyJS.OutputStream(options); | ||
compressed_ast.print(stream); | ||
var code = stream.toString(); // this is your minified code | ||
``` | ||
var stream = UglifyJS.OutputStream(options); | ||
compressed_ast.print(stream); | ||
var code = stream.toString(); // this is your minified code | ||
or, for a shortcut you can do: | ||
```javascript | ||
var code = compressed_ast.print_to_string(options); | ||
``` | ||
var code = compressed_ast.print_to_string(options); | ||
As usual, `options` is optional. The output stream accepts a lot of otions, | ||
@@ -555,13 +570,14 @@ most of them documented above in section “Beautifier options”. The two | ||
Example: | ||
```javascript | ||
var source_map = UglifyJS.SourceMap(source_map_options); | ||
var stream = UglifyJS.OutputStream({ | ||
... | ||
source_map: source_map | ||
}); | ||
compressed_ast.print(stream); | ||
var source_map = UglifyJS.SourceMap(source_map_options); | ||
var stream = UglifyJS.OutputStream({ | ||
... | ||
source_map: source_map | ||
}); | ||
compressed_ast.print(stream); | ||
var code = stream.toString(); | ||
var map = source_map.toString(); // json output for your source map | ||
``` | ||
var code = stream.toString(); | ||
var map = source_map.toString(); // json output for your source map | ||
The `source_map_options` (optional) can contain the following properties: | ||
@@ -568,0 +584,0 @@ |
typeof_eq_undefined: { | ||
options = { | ||
comparisons: true, | ||
unsafe: false | ||
comparisons: true | ||
}; | ||
@@ -16,3 +15,12 @@ input: { a = typeof b.c != "undefined" } | ||
input: { a = typeof b.c != "undefined" } | ||
expect: { a = b.c !== void 0 } | ||
expect: { a = void 0 !== b.c } | ||
} | ||
typeof_eq_undefined_unsafe2: { | ||
options = { | ||
comparisons: true, | ||
unsafe: true | ||
}; | ||
input: { a = "undefined" != typeof b.c } | ||
expect: { a = void 0 !== b.c } | ||
} |
@@ -20,2 +20,4 @@ keep_properties: { | ||
a["if"] = "if"; | ||
a["*"] = "asterisk"; | ||
a["\u0EB3"] = "unicode"; | ||
} | ||
@@ -25,2 +27,4 @@ expect: { | ||
a["if"] = "if"; | ||
a["*"] = "asterisk"; | ||
a.\u0EB3 = "unicode"; | ||
} | ||
@@ -37,2 +41,4 @@ } | ||
a["if"] = "if"; | ||
a["*"] = "asterisk"; | ||
a["\u0EB3"] = "unicode"; | ||
} | ||
@@ -42,3 +48,5 @@ expect: { | ||
a.if = "if"; | ||
a["*"] = "asterisk"; | ||
a.\u0EB3 = "unicode"; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
332132
0.59%34
3.03%8057
0.83%584
2.82%