Disable unsafe by default

Close #147
This commit is contained in:
Mihai Bazon
2013-03-11 00:04:31 +02:00
parent 3bd7ca9961
commit e4b078cff7
2 changed files with 20 additions and 7 deletions

View File

@@ -163,15 +163,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 `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`). 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 - `sequences` -- join consecutive simple statements using the comma operator
- `properties` -- rewrite property access using the dot notation, for - `properties` -- rewrite property access using the dot notation, for
example `foo["bar"] → foo.bar` example `foo["bar"] → foo.bar`
- `dead_code` -- remove unreachable code - `dead_code` -- remove unreachable code
- `drop_debugger` -- remove `debugger;` statements - `drop_debugger` -- remove `debugger;` statements
- `unsafe` -- apply "unsafe" transformations (discussion below) - `unsafe` (default: false) -- apply "unsafe" transformations (discussion below)
- `conditionals` -- apply optimizations for `if`-s and conditional - `conditionals` -- apply optimizations for `if`-s and conditional
expressions expressions
- `comparisons` -- apply certain optimizations to binary nodes, for example: - `comparisons` -- apply certain optimizations to binary nodes, for example:
@@ -184,8 +181,8 @@ the available options (all are `true` by default, except `hoist_vars`):
statically determine the condition statically determine the condition
- `unused` -- drop unreferenced functions and variables - `unused` -- drop unreferenced functions and variables
- `hoist_funs` -- hoist function declarations - `hoist_funs` -- hoist function declarations
- `hoist_vars` -- hoist `var` declarations (this is `false` by default - `hoist_vars` (default: false) -- hoist `var` declarations (this is `false`
because it seems to increase the size of the output in general) by default because it seems to increase the size of the output in general)
- `if_return` -- optimizations for if/return and if/continue - `if_return` -- optimizations for if/return and if/continue
- `join_vars` -- join consecutive `var` statements - `join_vars` -- join consecutive `var` statements
- `cascade` -- small optimization for sequences, transform `x, x` into `x` - `cascade` -- small optimization for sequences, transform `x, x` into `x`
@@ -193,6 +190,22 @@ the available options (all are `true` by default, except `hoist_vars`):
- `warnings` -- display warnings when dropping unreachable code or unused - `warnings` -- display warnings when dropping unreachable code or unused
declarations etc. 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 ### Conditional compilation
You can use the `--define` (`-d`) switch in order to declare global You can use the `--define` (`-d`) switch in order to declare global

View File

@@ -52,7 +52,7 @@ function Compressor(options, false_by_default) {
properties : !false_by_default, properties : !false_by_default,
dead_code : !false_by_default, dead_code : !false_by_default,
drop_debugger : !false_by_default, drop_debugger : !false_by_default,
unsafe : !false_by_default, unsafe : false,
unsafe_comps : false, unsafe_comps : false,
conditionals : !false_by_default, conditionals : !false_by_default,
comparisons : !false_by_default, comparisons : !false_by_default,