add --source-map base
update documentation
This commit is contained in:
parent
3d9e9a164d
commit
2af73232c7
48
README.md
48
README.md
|
|
@ -118,6 +118,7 @@ The available options are:
|
||||||
code relying on Function.prototype.name.
|
code relying on Function.prototype.name.
|
||||||
--self Build UglifyJS2 as a library (implies --wrap UglifyJS)
|
--self Build UglifyJS2 as a library (implies --wrap UglifyJS)
|
||||||
--source-map [options] Enable source map/specify source map options:
|
--source-map [options] Enable source map/specify source map options:
|
||||||
|
`base` Path to compute relative paths from input files.
|
||||||
`content` Input source map, useful if you're compressing
|
`content` Input source map, useful if you're compressing
|
||||||
JS that was generated from some other original
|
JS that was generated from some other original
|
||||||
code. Specify "inline" if the source map is
|
code. Specify "inline" if the source map is
|
||||||
|
|
@ -146,17 +147,6 @@ TODOs:
|
||||||
--reserved-file File containing reserved names
|
--reserved-file File containing reserved names
|
||||||
--reserve-domprops Make (most?) DOM properties reserved for
|
--reserve-domprops Make (most?) DOM properties reserved for
|
||||||
--mangle-props
|
--mangle-props
|
||||||
--mangle-props Mangle property names (default `0`). Set to
|
|
||||||
`true` or `1` to mangle all property names. Set
|
|
||||||
to `unquoted` or `2` to only mangle unquoted
|
|
||||||
property names. Mode `2` also enables the
|
|
||||||
`keep_quoted_props` beautifier option to
|
|
||||||
preserve the quotes around property names and
|
|
||||||
disables the `properties` compressor option to
|
|
||||||
prevent rewriting quoted properties with dot
|
|
||||||
notation. You can override these by setting
|
|
||||||
them explicitly on the command line.
|
|
||||||
--mangle-regex Only mangle property names matching the regex
|
|
||||||
--name-cache File to hold mangled names mappings
|
--name-cache File to hold mangled names mappings
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -167,23 +157,19 @@ goes to STDOUT.
|
||||||
|
|
||||||
UglifyJS2 can generate a source map file, which is highly useful for
|
UglifyJS2 can generate a source map file, which is highly useful for
|
||||||
debugging your compressed JavaScript. To get a source map, pass
|
debugging your compressed JavaScript. To get a source map, pass
|
||||||
`--source-map output.js.map` (full path to the file where you want the
|
`--source-map --output output.js` (source map will be written out to
|
||||||
source map dumped).
|
`output.js.map`).
|
||||||
|
|
||||||
Additionally you might need `--source-map-root` to pass the URL where the
|
Additionally you might need `--source-map root=<URL>` to pass the URL where
|
||||||
original files can be found. In case you are passing full paths to input
|
the original files can be found. Use `--source-map url=<URL>` to specify
|
||||||
files to UglifyJS, you can use `--prefix` (`-p`) to specify the number of
|
the URL where the source map can be found.
|
||||||
directories to drop from the path prefix when declaring files in the source
|
|
||||||
map.
|
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
uglifyjs /home/doe/work/foo/src/js/file1.js \
|
uglifyjs /home/doe/work/foo/src/js/file1.js \
|
||||||
/home/doe/work/foo/src/js/file2.js \
|
/home/doe/work/foo/src/js/file2.js \
|
||||||
-o foo.min.js \
|
-o foo.min.js -c -m \
|
||||||
--source-map foo.min.js.map \
|
--source-map base="/home/doe/work/foo/src",root="http://foo.com/src"
|
||||||
--source-map-root http://foo.com/src \
|
|
||||||
-p 5 -c -m
|
|
||||||
|
|
||||||
The above will compress and mangle `file1.js` and `file2.js`, will drop the
|
The above will compress and mangle `file1.js` and `file2.js`, will drop the
|
||||||
output in `foo.min.js` and the source map in `foo.min.js.map`. The source
|
output in `foo.min.js` and the source map in `foo.min.js.map`. The source
|
||||||
|
|
@ -273,8 +259,8 @@ cover most standard JS and DOM properties defined in various browsers. Pass
|
||||||
`--reserve-domprops` to read that in.
|
`--reserve-domprops` to read that in.
|
||||||
|
|
||||||
You can also use a regular expression to define which property names should be
|
You can also use a regular expression to define which property names should be
|
||||||
mangled. For example, `--mangle-regex="/^_/"` will only mangle property names
|
mangled. For example, `--mangle-props regex=/^_/` will only mangle property
|
||||||
that start with an underscore.
|
names that start with an underscore.
|
||||||
|
|
||||||
When you compress multiple files using this option, in order for them to
|
When you compress multiple files using this option, in order for them to
|
||||||
work together in the end we need to ensure somehow that one property gets
|
work together in the end we need to ensure somehow that one property gets
|
||||||
|
|
@ -294,26 +280,26 @@ of mangled property names.
|
||||||
Using the name cache is not necessary if you compress all your files in a
|
Using the name cache is not necessary if you compress all your files in a
|
||||||
single call to UglifyJS.
|
single call to UglifyJS.
|
||||||
|
|
||||||
#### Mangling unquoted names (`--mangle-props=unquoted` or `--mangle-props=2`)
|
#### Mangling unquoted names (`--mangle-props ignore_quoted`)
|
||||||
|
|
||||||
Using quoted property name (`o["foo"]`) reserves the property name (`foo`)
|
Using quoted property name (`o["foo"]`) reserves the property name (`foo`)
|
||||||
so that it is not mangled throughout the entire script even when used in an
|
so that it is not mangled throughout the entire script even when used in an
|
||||||
unquoted style (`o.foo`). Example:
|
unquoted style (`o.foo`). Example:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs --mangle-props=2 -mc
|
$ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs --mangle-props ignore_quoted -mc
|
||||||
var o={"foo":1,a:3};o.foo+=o.a,console.log(o.foo);
|
var o={foo:1,a:3};o.foo+=o.a,console.log(o.foo);
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Debugging property name mangling
|
#### Debugging property name mangling
|
||||||
|
|
||||||
You can also pass `--mangle-props-debug` in order to mangle property names
|
You can also pass `--mangle-props debug` in order to mangle property names
|
||||||
without completely obscuring them. For example the property `o.foo`
|
without completely obscuring them. For example the property `o.foo`
|
||||||
would mangle to `o._$foo$_` with this option. This allows property mangling
|
would mangle to `o._$foo$_` with this option. This allows property mangling
|
||||||
of a large codebase while still being able to debug the code and identify
|
of a large codebase while still being able to debug the code and identify
|
||||||
where mangling is breaking things.
|
where mangling is breaking things.
|
||||||
|
|
||||||
You can also pass a custom suffix using `--mangle-props-debug=XYZ`. This would then
|
You can also pass a custom suffix using `--mangle-props debug=XYZ`. This would then
|
||||||
mangle `o.foo` to `o._$foo$XYZ_`. You can change this each time you compile a
|
mangle `o.foo` to `o._$foo$XYZ_`. You can change this each time you compile a
|
||||||
script to identify how a property got mangled. One technique is to pass a
|
script to identify how a property got mangled. One technique is to pass a
|
||||||
random number on every compile to simulate mangling changing with different
|
random number on every compile to simulate mangling changing with different
|
||||||
|
|
@ -674,7 +660,7 @@ Example:
|
||||||
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
|
||||||
// if you need to pass code instead of file name
|
// if you need to pass code instead of file name
|
||||||
var result = UglifyJS.minify("var b = function () {};", {fromString: true});
|
var result = UglifyJS.minify("var b = function() {};", {fromString: true});
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also compress multiple files:
|
You can also compress multiple files:
|
||||||
|
|
@ -694,7 +680,7 @@ console.log(result.map);
|
||||||
|
|
||||||
To generate a source map with the fromString option, you can also use an object:
|
To generate a source map with the fromString option, you can also use an object:
|
||||||
```javascript
|
```javascript
|
||||||
var result = UglifyJS.minify({"file1.js": "var a = function () {};"}, {
|
var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, {
|
||||||
outSourceMap: "out.js.map",
|
outSourceMap: "out.js.map",
|
||||||
outFileName: "out.js",
|
outFileName: "out.js",
|
||||||
fromString: true
|
fromString: true
|
||||||
|
|
|
||||||
24
bin/uglifyjs
24
bin/uglifyjs
|
|
@ -85,6 +85,18 @@ if (program.parse) {
|
||||||
options.parse = program.parse;
|
options.parse = program.parse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var convert_path = function(name) {
|
||||||
|
return name;
|
||||||
|
};
|
||||||
|
if (program.sourceMap && "base" in program.sourceMap) {
|
||||||
|
convert_path = function() {
|
||||||
|
var base = program.sourceMap.base;
|
||||||
|
delete options.sourceMap.base;
|
||||||
|
return function(name) {
|
||||||
|
return path.relative(base, name);
|
||||||
|
};
|
||||||
|
}();
|
||||||
|
}
|
||||||
if (program.verbose && options.compress) {
|
if (program.verbose && options.compress) {
|
||||||
if (typeof options.compress != "object") options.compress = {};
|
if (typeof options.compress != "object") options.compress = {};
|
||||||
options.compress.warnings = "verbose";
|
options.compress.warnings = "verbose";
|
||||||
|
|
@ -94,21 +106,21 @@ if (program.self) {
|
||||||
console.error("WARN: Ignoring input files since --self was passed");
|
console.error("WARN: Ignoring input files since --self was passed");
|
||||||
}
|
}
|
||||||
if (!options.wrap) options.wrap = "UglifyJS";
|
if (!options.wrap) options.wrap = "UglifyJS";
|
||||||
simple_glob(UglifyJS.FILES).forEach(function(path) {
|
simple_glob(UglifyJS.FILES).forEach(function(name) {
|
||||||
files[path] = readFile(path);
|
files[convert_path(name)] = readFile(name);
|
||||||
});
|
});
|
||||||
run();
|
run();
|
||||||
} else if (program.args.length) {
|
} else if (program.args.length) {
|
||||||
simple_glob(program.args).forEach(function(path) {
|
simple_glob(program.args).forEach(function(name) {
|
||||||
files[path] = readFile(path);
|
files[convert_path(name)] = readFile(name);
|
||||||
});
|
});
|
||||||
run();
|
run();
|
||||||
} else {
|
} else {
|
||||||
var chunks = [];
|
var chunks = [];
|
||||||
process.stdin.setEncoding("utf8");
|
process.stdin.setEncoding("utf8");
|
||||||
process.stdin.on("data", function (chunk) {
|
process.stdin.on("data", function(chunk) {
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
}).on("end", function () {
|
}).on("end", function() {
|
||||||
files = [ chunks.join("") ];
|
files = [ chunks.join("") ];
|
||||||
run();
|
run();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -880,7 +880,7 @@ TreeWalker.prototype = {
|
||||||
parent: function(n) {
|
parent: function(n) {
|
||||||
return this.stack[this.stack.length - 2 - (n || 0)];
|
return this.stack[this.stack.length - 2 - (n || 0)];
|
||||||
},
|
},
|
||||||
push: function (node) {
|
push: function(node) {
|
||||||
if (node instanceof AST_Lambda) {
|
if (node instanceof AST_Lambda) {
|
||||||
this.directives = Object.create(this.directives);
|
this.directives = Object.create(this.directives);
|
||||||
} else if (node instanceof AST_Directive && !this.directives[node.value]) {
|
} else if (node instanceof AST_Directive && !this.directives[node.value]) {
|
||||||
|
|
|
||||||
|
|
@ -1216,7 +1216,7 @@ merge(Compressor.prototype, {
|
||||||
/* -----[ boolean/negation helpers ]----- */
|
/* -----[ boolean/negation helpers ]----- */
|
||||||
|
|
||||||
// methods to determine whether an expression has a boolean result type
|
// methods to determine whether an expression has a boolean result type
|
||||||
(function (def){
|
(function(def){
|
||||||
var unary_bool = [ "!", "delete" ];
|
var unary_bool = [ "!", "delete" ];
|
||||||
var binary_bool = [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ];
|
var binary_bool = [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ];
|
||||||
def(AST_Node, return_false);
|
def(AST_Node, return_false);
|
||||||
|
|
@ -1244,7 +1244,7 @@ merge(Compressor.prototype, {
|
||||||
});
|
});
|
||||||
|
|
||||||
// methods to determine if an expression has a numeric result type
|
// methods to determine if an expression has a numeric result type
|
||||||
(function (def){
|
(function(def){
|
||||||
def(AST_Node, return_false);
|
def(AST_Node, return_false);
|
||||||
def(AST_Number, return_true);
|
def(AST_Number, return_true);
|
||||||
var unary = makePredicate("+ - ~ ++ --");
|
var unary = makePredicate("+ - ~ ++ --");
|
||||||
|
|
@ -1272,7 +1272,7 @@ merge(Compressor.prototype, {
|
||||||
});
|
});
|
||||||
|
|
||||||
// methods to determine if an expression has a string result type
|
// methods to determine if an expression has a string result type
|
||||||
(function (def){
|
(function(def){
|
||||||
def(AST_Node, return_false);
|
def(AST_Node, return_false);
|
||||||
def(AST_String, return_true);
|
def(AST_String, return_true);
|
||||||
def(AST_UnaryPrefix, function(){
|
def(AST_UnaryPrefix, function(){
|
||||||
|
|
@ -1302,7 +1302,7 @@ merge(Compressor.prototype, {
|
||||||
if (parent instanceof AST_Assign && parent.left === node) return node;
|
if (parent instanceof AST_Assign && parent.left === node) return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
(function (def){
|
(function(def){
|
||||||
AST_Node.DEFMETHOD("resolve_defines", function(compressor) {
|
AST_Node.DEFMETHOD("resolve_defines", function(compressor) {
|
||||||
if (!compressor.option("global_defs")) return;
|
if (!compressor.option("global_defs")) return;
|
||||||
var def = this._find_defs(compressor, "");
|
var def = this._find_defs(compressor, "");
|
||||||
|
|
@ -1383,7 +1383,7 @@ merge(Compressor.prototype, {
|
||||||
}
|
}
|
||||||
|
|
||||||
// methods to evaluate a constant expression
|
// methods to evaluate a constant expression
|
||||||
(function (def){
|
(function(def){
|
||||||
// If the node has been successfully reduced to a constant,
|
// If the node has been successfully reduced to a constant,
|
||||||
// then its value is returned; otherwise the element itself
|
// then its value is returned; otherwise the element itself
|
||||||
// is returned.
|
// is returned.
|
||||||
|
|
|
||||||
|
|
@ -681,7 +681,7 @@ function OutputStream(options) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
PARENS([ AST_Assign, AST_Conditional ], function (output){
|
PARENS([ AST_Assign, AST_Conditional ], function(output){
|
||||||
var p = output.parent();
|
var p = output.parent();
|
||||||
// !(a = false) → true
|
// !(a = false) → true
|
||||||
if (p instanceof AST_Unary)
|
if (p instanceof AST_Unary)
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,14 @@ var exec = require("child_process").exec;
|
||||||
var uglify = require("../../");
|
var uglify = require("../../");
|
||||||
|
|
||||||
describe("spidermonkey export/import sanity test", function() {
|
describe("spidermonkey export/import sanity test", function() {
|
||||||
it("should produce a functional build when using --self with spidermonkey", function (done) {
|
it("should produce a functional build when using --self with spidermonkey", function(done) {
|
||||||
this.timeout(20000);
|
this.timeout(20000);
|
||||||
|
|
||||||
var uglifyjs = '"' + process.argv[0] + '" bin/uglifyjs';
|
var uglifyjs = '"' + process.argv[0] + '" bin/uglifyjs';
|
||||||
var command = uglifyjs + " --self -cm --wrap SpiderUglify -o spidermonkey | " +
|
var command = uglifyjs + " --self -cm --wrap SpiderUglify -o spidermonkey | " +
|
||||||
uglifyjs + " -p spidermonkey -cm";
|
uglifyjs + " -p spidermonkey -cm";
|
||||||
|
|
||||||
exec(command, function (err, stdout) {
|
exec(command, function(err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
eval(stdout);
|
eval(stdout);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user