add --source-map base

update documentation
This commit is contained in:
alexlamsl 2017-04-14 18:02:33 +08:00
parent 3d9e9a164d
commit 2af73232c7
6 changed files with 44 additions and 46 deletions

View File

@ -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

View File

@ -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,13 +106,13 @@ 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 {