Support output, mangle and compress options to UglifyJS.minify.

Close #57
Close #86
Close #33
This commit is contained in:
Mihai Bazon
2013-01-04 11:24:29 +02:00
parent 47c9895d59
commit 130c623be7
2 changed files with 33 additions and 9 deletions

View File

@@ -214,6 +214,7 @@ will evaluate references to them to the value itself and drop unreachable
code as usual. The possible downside of this approach is that the build code as usual. The possible downside of this approach is that the build
will contain the `const` declarations. will contain the `const` declarations.
<a name="codegen-options"></a>
## Beautifier options ## Beautifier options
The code generator tries to output shortest code possible by default. In The code generator tries to output shortest code possible by default. In
@@ -367,9 +368,19 @@ no sense otherwise).
Other options: Other options:
- `warnings` (default `false`) — pass `true` to display compressor warnings. - `warnings` (default `false`) — pass `true` to display compressor warnings.
- `fromString` (default `false`) — if you pass `true` then you can pass - `fromString` (default `false`) — if you pass `true` then you can pass
JavaScript source code, rather than file names. JavaScript source code, rather than file names.
- `mangle` — pass `false` to skip mangling names.
- `output` (default `null`) — pass an object if you wish to specify
additional [output options][codegen]. The defaults are optimized
for best compression.
- `compress` (default `{}`) — pass `false` to skip compressing entirely.
Pass an object to specify custom [compressor options][compressor].
We could add more options to `UglifyJS.minify` — if you need additional We could add more options to `UglifyJS.minify` — if you need additional
functionality please suggest! functionality please suggest!
@@ -517,3 +528,5 @@ The `source_map_options` (optional) can contain the following properties:
[acorn]: https://github.com/marijnh/acorn [acorn]: https://github.com/marijnh/acorn
[source-map]: https://github.com/mozilla/source-map [source-map]: https://github.com/mozilla/source-map
[sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit [sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
[codegen]: http://lisperator.net/uglifyjs/codegen
[compressor]: http://lisperator.net/uglifyjs/compress

View File

@@ -56,6 +56,9 @@ exports.minify = function(files, options) {
inSourceMap : null, inSourceMap : null,
fromString : false, fromString : false,
warnings : false, warnings : false,
mangle : {},
output : null,
compress : {}
}); });
if (typeof files == "string") if (typeof files == "string")
files = [ files ]; files = [ files ];
@@ -73,16 +76,20 @@ exports.minify = function(files, options) {
}); });
// 2. compress // 2. compress
toplevel.figure_out_scope(); if (options.compress) {
var sq = UglifyJS.Compressor({ var compress = { warnings: options.warnings };
warnings: options.warnings, UglifyJS.merge(compress, options.compress);
}); toplevel.figure_out_scope();
toplevel = toplevel.transform(sq); var sq = UglifyJS.Compressor(compress);
toplevel = toplevel.transform(sq);
}
// 3. mangle // 3. mangle
toplevel.figure_out_scope(); if (options.mangle) {
toplevel.compute_char_frequency(); toplevel.figure_out_scope();
toplevel.mangle_names(); toplevel.compute_char_frequency();
toplevel.mangle_names(options.mangle);
}
// 4. output // 4. output
var map = null; var map = null;
@@ -95,7 +102,11 @@ exports.minify = function(files, options) {
orig: inMap, orig: inMap,
root: options.sourceRoot root: options.sourceRoot
}); });
var stream = UglifyJS.OutputStream({ source_map: map }); var output = { source_map: map };
if (options.output) {
UglifyJS.merge(output, options.output);
}
var stream = UglifyJS.OutputStream(output);
toplevel.print(stream); toplevel.print(stream);
return { return {
code : stream + "", code : stream + "",