document minify() option toplevel (#1979)

This commit is contained in:
kzc
2017-05-20 10:09:21 -04:00
committed by Alex Lam S.L
parent 58fae7dc07
commit 22aedef849

View File

@@ -301,32 +301,45 @@ like this:
var UglifyJS = require("uglify-js"); var UglifyJS = require("uglify-js");
``` ```
There is a single high level minification function, `minify(files, options)`, which will There is a single high level minification function, `minify(code, options)`, which will
performs all the steps in a configurable manner. performs all the steps in a configurable manner.
Example: Example:
```javascript ```javascript
var result = UglifyJS.minify("var b = function() {};"); var code = "function add(first, second) { return first + second; }";
console.log(result.code); // minified output var result = UglifyJS.minify(code);
console.log(result.error); // runtime error, if present console.log(result.error); // runtime error, or `undefined` if no error
console.log(result.code); // minified output: function add(n,d){return n+d}
``` ```
You can also compress multiple files: You can also compress multiple files:
```javascript ```javascript
var result = UglifyJS.minify({ var code = {
"file1.js": "var a = function() {};", "file1.js": "function add(first, second) { return first + second; }",
"file2.js": "var b = function() {};" "file2.js": "console.log(add(1 + 2, 3 + 4));"
}); };
console.log(result.code); var result = UglifyJS.minify(code);
console.log(result.code); // function add(d,n){return d+n}console.log(add(3,7));
```
The `toplevel` option:
```javascript
var code = {
"file1.js": "function add(first, second) { return first + second; }",
"file2.js": "console.log(add(1 + 2, 3 + 4));"
};
var options = { toplevel: true };
var result = UglifyJS.minify(code, options);
console.log(result.code); // console.log(function(n,o){return n+o}(3,7));
``` ```
To produce warnings: To produce warnings:
```javascript ```javascript
var result = UglifyJS.minify("function f(){ var u; return 5; }", { var code = "function f(){ var u; return 2 + 3; }";
warnings: true var options = { warnings: true };
}); var result = UglifyJS.minify(code, options);
console.log(result.code); // function f(){return 5} console.log(result.error); // runtime error, `undefined` in this case
console.log(result.warnings); // [ 'Dropping unused variable u [0:1,18]' ] console.log(result.warnings); // [ 'Dropping unused variable u [0:1,18]' ]
console.log(result.error); // runtime error, not defined in this case console.log(result.code); // function f(){return 5}
``` ```
An error example: An error example:
@@ -338,7 +351,7 @@ console.log(JSON.stringify(result.error));
Note: unlike `uglify-js@2.x`, the `3.x` API does not throw errors. To Note: unlike `uglify-js@2.x`, the `3.x` API does not throw errors. To
achieve a similar effect one could do the following: achieve a similar effect one could do the following:
```javascript ```javascript
var result = UglifyJS.minify("if (0) else console.log(1);"); var result = UglifyJS.minify(code, options);
if (result.error) throw result.error; if (result.error) throw result.error;
``` ```
@@ -371,7 +384,7 @@ if (result.error) throw result.error;
- `ie8` (default `false`) - set to `true` to support IE8. - `ie8` (default `false`) - set to `true` to support IE8.
## Minify option structure ## Minify options structure
```javascript ```javascript
{ {
@@ -526,8 +539,8 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
- `cascade` -- small optimization for sequences, transform `x, x` into `x` - `cascade` -- small optimization for sequences, transform `x, x` into `x`
and `x = something(), x` into `x = something()` and `x = something(), x` into `x = something()`
- `collapse_vars` -- Collapse single-use `var` and `const` definitions - `collapse_vars` -- Collapse single-use non-constant variables - side
when possible. effects permitting.
- `reduce_vars` -- Improve optimization on variables assigned with and - `reduce_vars` -- Improve optimization on variables assigned with and
used as constant values. used as constant values.
@@ -749,8 +762,8 @@ Another way of doing that is to declare your globals as constants in a
separate file and include it into the build. For example you can have a separate file and include it into the build. For example you can have a
`build/defines.js` file with the following: `build/defines.js` file with the following:
```javascript ```javascript
const DEBUG = false; var DEBUG = false;
const PRODUCTION = true; var PRODUCTION = true;
// etc. // etc.
``` ```