add fromString argument to UglifyJS.minify (allows to pass the source

code, instead of file names, as first argument).

close #17
This commit is contained in:
Mihai Bazon
2012-10-18 15:49:15 +03:00
parent afb7faa6fa
commit 6f45928a73
2 changed files with 12 additions and 2 deletions

View File

@@ -322,6 +322,7 @@ There's a single toplevel function which combines all the steps. If you
don't need additional customization, you might want to go with `minify`. don't need additional customization, you might want to go with `minify`.
Example: Example:
// see "fromString" below if you need to pass code instead of file name
var result = UglifyJS.minify("/path/to/file.js"); var result = UglifyJS.minify("/path/to/file.js");
console.log(result.code); // minified output console.log(result.code); // minified output
@@ -354,6 +355,12 @@ can use the `inSourceMap` argument:
The `inSourceMap` is only used if you also request `outSourceMap` (it makes The `inSourceMap` is only used if you also request `outSourceMap` (it makes
no sense otherwise). no sense otherwise).
Other options:
- `warnings` (default `false`) — pass `true` to display compressor warnings.
- `fromString` (default `false`) — if you pass `true` then you can pass
JavaScript source code, rather than file names.
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!

View File

@@ -66,6 +66,7 @@ exports.minify = function(files, options) {
options = UglifyJS.defaults(options, { options = UglifyJS.defaults(options, {
outSourceMap : null, outSourceMap : null,
inSourceMap : null, inSourceMap : null,
fromString : false,
warnings : false, warnings : false,
}); });
if (typeof files == "string") if (typeof files == "string")
@@ -74,9 +75,11 @@ exports.minify = function(files, options) {
// 1. parse // 1. parse
var toplevel = null; var toplevel = null;
files.forEach(function(file){ files.forEach(function(file){
var code = fs.readFileSync(file, "utf8"); var code = options.fromString
? file
: fs.readFileSync(file, "utf8");
toplevel = UglifyJS.parse(code, { toplevel = UglifyJS.parse(code, {
filename: file, filename: options.fromString ? "?" : file,
toplevel: toplevel toplevel: toplevel
}); });
}); });