## CLI compress options
-You need to pass `--compress` (`-c`) to enable the compressor. Optionally
+You need to pass `--compress` (`-c`) to enable the compressor. Optionally
you can pass a comma-separated list of [compress options](#compress-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
+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`).
Example:
`--mangle-props`. It will mangle all properties that are seen in some
object literal, or that are assigned to. For example:
-```js
+```javascript
var x = {
- foo: 1
+ foo: 1
};
x.bar = 2;
and UglifyJS will maintain these mappings in a file which can then be reused.
It should be initially empty. Example:
-```
-rm -f /tmp/cache.json # start fresh
-uglifyjs file1.js file2.js --mangle-props --name-cache /tmp/cache.json -o part1.js
-uglifyjs file3.js file4.js --mangle-props --name-cache /tmp/cache.json -o part2.js
+```bash
+$ rm -f /tmp/cache.json # start fresh
+$ uglifyjs file1.js file2.js --mangle-props --name-cache /tmp/cache.json -o part1.js
+$ uglifyjs file3.js file4.js --mangle-props --name-cache /tmp/cache.json -o part2.js
```
Now, `part1.js` and `part2.js` will be consistent with each other in terms
so that it is not mangled throughout the entire script even when used in an
unquoted style (`o.foo`). Example:
-```
+```bash
$ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs --mangle-props keep_quoted -mc
var o={foo:1,a:3};o.foo+=o.a,console.log(o.foo);
```
You can also compress multiple files:
```javascript
var result = UglifyJS.minify({
- "file1.js": "var a = function() {};",
- "file2.js": "var b = function() {};"
+ "file1.js": "var a = function() {};",
+ "file2.js": "var b = function() {};"
});
console.log(result.code);
```
- `mangle` (default `true`) — pass `false` to skip mangling names, or pass
an object to specify [mangle options](#mangle-options) (see below).
-
- - `mangle.properties` (default `false`) — a subcategory of the mangle option.
+
+ - `mangle.properties` (default `false`) — a subcategory of the mangle option.
Pass an object to specify custom [mangle property options](#mangle-properties-options).
- `output` (default `null`) — pass an object if you wish to specify
- `sourceMap` (default `false`) - pass an object if you wish to specify
[source map options](#source-map-options).
-
+
- `toplevel` (default `false`) - set to `true` if you wish to enable top level
variable and function name mangling and to drop unused variables and functions.
-
+
- `ie8` (default `false`) - set to `true` to support IE8.
## Minify option structure
-```
+```javascript
{
warnings: false,
parse: {
},
mangle: {
// mangle options
-
- properties: {
- // mangle property options
- }
+
+ properties: {
+ // mangle property options
+ }
},
output: {
// output options
To generate a source map:
```javascript
var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, {
- sourceMap: {
- filename: "out.js",
- url: "out.js.map"
- }
+ sourceMap: {
+ filename: "out.js",
+ url: "out.js.map"
+ }
});
console.log(result.code); // minified output
console.log(result.map); // source map
You can also specify sourceRoot property to be included in source map:
```javascript
var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, {
- sourceMap: {
- root: "http://example.com/src",
- url: "out.js.map"
- }
+ sourceMap: {
+ root: "http://example.com/src",
+ url: "out.js.map"
+ }
});
```
can use `sourceMap.content`:
```javascript
var result = UglifyJS.minify({"compiled.js": "compiled code"}, {
- sourceMap: {
- content: "content from compiled.js.map",
- url: "minified.js.map"
- }
+ sourceMap: {
+ content: "content from compiled.js.map",
+ url: "minified.js.map"
+ }
});
// same as before, it returns `code` and `map`
```
```javascript
// test.js
var globalVar;
-function funcName(firstLongName, anotherLongName)
-{
+function funcName(firstLongName, anotherLongName) {
var myVariable = firstLongName + anotherLongName;
}
```
example:
```javascript
function f() {
- /** @preserve Foo Bar */
- function g() {
- // this function is never called
- }
- return something();
+ /** @preserve Foo Bar */
+ function g() {
+ // this function is never called
+ }
+ return something();
}
```
dead code removal UglifyJS will discard the following from the output:
```javascript
if (DEBUG) {
- console.log("debug stuff");
+ console.log("debug stuff");
}
```
You can also use conditional compilation via the programmatic API. With the difference that the
property name is `global_defs` and is a compressor property:
-```js
+```javascript
var result = uglifyJS.minify(fs.readFileSync("input.js", "utf8"), {
compress: {
dead_code: true,
```
### Using native Uglify AST with `minify()`
-```
+```javascript
// example: parse only, produce native Uglify AST
var result = UglifyJS.minify(code, {
// result.ast contains native Uglify AST
```
-```
+```javascript
// example: accept native Uglify AST input and then compress and mangle
// to produce both code and native AST.