Support -p relative. Fix #256

This commit is contained in:
Mihai Bazon
2013-08-07 11:43:47 +03:00
parent 2604aadb37
commit 4aa4b3e694
2 changed files with 77 additions and 53 deletions

105
README.md
View File

@@ -46,55 +46,62 @@ files.
The available options are: The available options are:
--source-map Specify an output file where to generate source map. --source-map Specify an output file where to generate source map.
[string] [string]
--source-map-root The path to the original source to be included in the --source-map-root The path to the original source to be included in the
source map. [string] source map. [string]
--source-map-url The path to the source map to be added in //@ --source-map-url The path to the source map to be added in //#
sourceMappingURL. Defaults to the value passed with sourceMappingURL. Defaults to the value passed with
--source-map. [string] --source-map. [string]
--in-source-map Input source map, useful if you're compressing JS that was --in-source-map Input source map, useful if you're compressing JS that was
generated from some other original code. generated from some other original code.
--screw-ie8 Pass this flag if you don't care about full compliance with --screw-ie8 Pass this flag if you don't care about full compliance
Internet Explorer 6-8 quirks (by default UglifyJS will try with Internet Explorer 6-8 quirks (by default UglifyJS
to be IE-proof). will try to be IE-proof). [boolean]
-p, --prefix Skip prefix for original filenames that appear in source --expr Parse a single expression, rather than a program (for
maps. For example -p 3 will drop 3 directories from file parsing JSON) [boolean]
names and ensure they are relative paths. -p, --prefix Skip prefix for original filenames that appear in source
-o, --output Output file (default STDOUT). maps. For example -p 3 will drop 3 directories from file
-b, --beautify Beautify output/specify output options. [string] names and ensure they are relative paths. You can also
-m, --mangle Mangle names/pass mangler options. [string] specify -p relative, which will make UglifyJS figure out
-r, --reserved Reserved names to exclude from mangling. itself the relative paths between original sources, the
-c, --compress Enable compressor/pass compressor options. Pass options source map and the output file. [string]
like -c hoist_vars=false,if_return=false. Use -c with no -o, --output Output file (default STDOUT).
argument to use the default compression options. [string] -b, --beautify Beautify output/specify output options. [string]
-d, --define Global definitions [string] -m, --mangle Mangle names/pass mangler options. [string]
--comments Preserve copyright comments in the output. By default this -r, --reserved Reserved names to exclude from mangling.
works like Google Closure, keeping JSDoc-style comments -c, --compress Enable compressor/pass compressor options. Pass options
that contain "@license" or "@preserve". You can optionally like -c hoist_vars=false,if_return=false. Use -c with no
pass one of the following arguments to this flag: argument to use the default compression options. [string]
- "all" to keep all comments -d, --define Global definitions [string]
- a valid JS regexp (needs to start with a slash) to keep -e, --enclose Embed everything in a big function, with a configurable
only comments that match. parameter/argument list. [string]
Note that currently not *all* comments can be kept when --comments Preserve copyright comments in the output. By default this
compression is on, because of dead code removal or works like Google Closure, keeping JSDoc-style comments
cascading statements into sequences. [string] that contain "@license" or "@preserve". You can optionally
--stats Display operations run time on STDERR. [boolean] pass one of the following arguments to this flag:
--acorn Use Acorn for parsing. [boolean] - "all" to keep all comments
--spidermonkey Assume input files are SpiderMonkey AST format (as JSON). - a valid JS regexp (needs to start with a slash) to keep
[boolean] only comments that match.
--self Build itself (UglifyJS2) as a library (implies Note that currently not *all* comments can be kept when
--wrap=UglifyJS --export-all) [boolean] compression is on, because of dead code removal or
--wrap Embed everything in a big function, making the “exports” cascading statements into sequences. [string]
and “global” variables available. You need to pass an --stats Display operations run time on STDERR. [boolean]
argument to this option to specify the name that your --acorn Use Acorn for parsing. [boolean]
module will take when included in, say, a browser. --spidermonkey Assume input files are SpiderMonkey AST format (as JSON).
[string] [boolean]
--export-all Only used when --wrap, this tells UglifyJS to add code to --self Build itself (UglifyJS2) as a library (implies
automatically export all globals. [boolean] --wrap=UglifyJS --export-all) [boolean]
--lint Display some scope warnings [boolean] --wrap Embed everything in a big function, making the “exports”
-v, --verbose Verbose [boolean] and “global” variables available. You need to pass an
-V, --version Print version number and exit. [boolean] argument to this option to specify the name that your
module will take when included in, say, a browser.
[string]
--export-all Only used when --wrap, this tells UglifyJS to add code to
automatically export all globals. [boolean]
--lint Display some scope warnings [boolean]
-v, --verbose Verbose [boolean]
-V, --version Print version number and exit. [boolean]
Specify `--output` (`-o`) to declare the output file. Otherwise the output Specify `--output` (`-o`) to declare the output file. Otherwise the output
goes to STDOUT. goes to STDOUT.

View File

@@ -7,6 +7,7 @@ var UglifyJS = require("../tools/node");
var sys = require("util"); var sys = require("util");
var optimist = require("optimist"); var optimist = require("optimist");
var fs = require("fs"); var fs = require("fs");
var path = require("path");
var async = require("async"); var async = require("async");
var acorn; var acorn;
var ARGS = optimist var ARGS = optimist
@@ -25,7 +26,9 @@ mangling you need to use `-c` and `-m`.\
.describe("screw-ie8", "Pass this flag if you don't care about full compliance with Internet Explorer 6-8 quirks (by default UglifyJS will try to be IE-proof).") .describe("screw-ie8", "Pass this flag if you don't care about full compliance with Internet Explorer 6-8 quirks (by default UglifyJS will try to be IE-proof).")
.describe("expr", "Parse a single expression, rather than a program (for parsing JSON)") .describe("expr", "Parse a single expression, rather than a program (for parsing JSON)")
.describe("p", "Skip prefix for original filenames that appear in source maps. \ .describe("p", "Skip prefix for original filenames that appear in source maps. \
For example -p 3 will drop 3 directories from file names and ensure they are relative paths.") For example -p 3 will drop 3 directories from file names and ensure they are relative paths. \
You can also specify -p relative, which will make UglifyJS figure out itself the relative paths between original sources, \
the source map and the output file.")
.describe("o", "Output file (default STDOUT).") .describe("o", "Output file (default STDOUT).")
.describe("b", "Beautify output/specify output options.") .describe("b", "Beautify output/specify output options.")
.describe("m", "Mangle names/pass mangler options.") .describe("m", "Mangle names/pass mangler options.")
@@ -77,6 +80,7 @@ You need to pass an argument to this option to specify the name that your module
.string("e") .string("e")
.string("comments") .string("comments")
.string("wrap") .string("wrap")
.string("p")
.boolean("expr") .boolean("expr")
.boolean("screw-ie8") .boolean("screw-ie8")
@@ -199,9 +203,10 @@ if (files.filter(function(el){ return el == "-" }).length > 1) {
var STATS = {}; var STATS = {};
var OUTPUT_FILE = ARGS.o; var OUTPUT_FILE = ARGS.o;
var TOPLEVEL = null; var TOPLEVEL = null;
var P_RELATIVE = ARGS.p && ARGS.p == "relative";
var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({ var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({
file: OUTPUT_FILE, file: P_RELATIVE ? path.relative(path.dirname(ARGS.source_map), OUTPUT_FILE) : OUTPUT_FILE,
root: ARGS.source_map_root, root: ARGS.source_map_root,
orig: ORIG_MAP, orig: ORIG_MAP,
}) : null; }) : null;
@@ -227,7 +232,14 @@ async.eachLimit(files, 1, function (file, cb) {
process.exit(1); process.exit(1);
} }
if (ARGS.p != null) { if (ARGS.p != null) {
file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/"); if (P_RELATIVE) {
file = path.relative(path.dirname(ARGS.source_map), file);
} else {
var p = parseInt(ARGS.p, 10);
if (!isNaN(p)) {
file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
}
}
} }
time_it("parse", function(){ time_it("parse", function(){
if (ARGS.spidermonkey) { if (ARGS.spidermonkey) {
@@ -310,7 +322,12 @@ async.eachLimit(files, 1, function (file, cb) {
if (SOURCE_MAP) { if (SOURCE_MAP) {
fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8"); fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
output += "\n//# sourceMappingURL=" + (ARGS.source_map_url || ARGS.source_map); var source_map_url = ARGS.source_map_url || (
P_RELATIVE
? path.relative(path.dirname(OUTPUT_FILE), ARGS.source_map)
: ARGS.source_map
);
output += "\n//# sourceMappingURL=" + source_map_url;
} }
if (OUTPUT_FILE) { if (OUTPUT_FILE) {