Support input source map
This is useful while compressing generated code; for example compressing JS
compiled by CoffeeScript (assuming you got a source map):
uglifyjs2 --in-source-map generated.js.map \
--source-map uglified.js.map \
-o uglified.js
The above assumes you have a "generated.js.map" file which is the source
mapping between your CoffeeScript and the generated.js (compiled output from
CoffeeScript). The name of the input file is not present in this example;
it will be fetched from the source map (but it can be passed manually too).
The output will be in "uglified.js" and the output map "uglified.js.map"
will actually map to the original CoffeeScript code, rather than to
generated.js.
This commit is contained in:
@@ -12,6 +12,7 @@ Use a single dash to read input from the standard input.\
|
|||||||
")
|
")
|
||||||
.describe("source-map", "Specify an output file where to generate source map.")
|
.describe("source-map", "Specify an output file where to generate source map.")
|
||||||
.describe("source-map-root", "The path to the original source to be included in the source map.")
|
.describe("source-map-root", "The path to the original source to be included in the source map.")
|
||||||
|
.describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.")
|
||||||
.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.")
|
.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.")
|
||||||
.describe("o", "Output file (default STDOUT)")
|
.describe("o", "Output file (default STDOUT)")
|
||||||
.describe("b", "Beautify output")
|
.describe("b", "Beautify output")
|
||||||
@@ -60,12 +61,27 @@ if (ARGS.c && ARGS.c !== true) {
|
|||||||
var a = opt.split(/\s*=\s*/);
|
var a = opt.split(/\s*=\s*/);
|
||||||
COMPRESSOR_OPTIONS[a[0]] = new Function("return(" + a[1] + ")")();
|
COMPRESSOR_OPTIONS[a[0]] = new Function("return(" + a[1] + ")")();
|
||||||
});
|
});
|
||||||
|
normalize(COMPRESSOR_OPTIONS);
|
||||||
}
|
}
|
||||||
|
|
||||||
var files = ARGS._.slice();
|
var files = ARGS._.slice();
|
||||||
|
|
||||||
if (files.length == 0)
|
var ORIG_MAP = ARGS.in_source_map;
|
||||||
|
|
||||||
|
if (ORIG_MAP) {
|
||||||
|
ORIG_MAP = JSON.parse(fs.readFileSync(ORIG_MAP));
|
||||||
|
if (files.length == 0) {
|
||||||
|
sys.error("INFO: Using file from the input source map: " + ORIG_MAP.file);
|
||||||
|
files = [ ORIG_MAP.file ];
|
||||||
|
}
|
||||||
|
if (ARGS.source_map_root == null) {
|
||||||
|
ARGS.source_map_root = ORIG_MAP.sourceRoot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (files.length == 0) {
|
||||||
files = [ "-" ];
|
files = [ "-" ];
|
||||||
|
}
|
||||||
|
|
||||||
if (files.indexOf("-") >= 0 && ARGS.source_map) {
|
if (files.indexOf("-") >= 0 && ARGS.source_map) {
|
||||||
sys.error("ERROR: Source map doesn't work with input from STDIN");
|
sys.error("ERROR: Source map doesn't work with input from STDIN");
|
||||||
@@ -83,7 +99,8 @@ var TOPLEVEL = null;
|
|||||||
|
|
||||||
var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({
|
var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({
|
||||||
file: OUTPUT_FILE,
|
file: OUTPUT_FILE,
|
||||||
root: ARGS.source_map_root
|
root: ARGS.source_map_root,
|
||||||
|
orig: ORIG_MAP,
|
||||||
}) : null;
|
}) : null;
|
||||||
|
|
||||||
var output = UglifyJS.OutputStream({
|
var output = UglifyJS.OutputStream({
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ function OutputStream(options) {
|
|||||||
ie_proof : true,
|
ie_proof : true,
|
||||||
beautify : true,
|
beautify : true,
|
||||||
source_map : null,
|
source_map : null,
|
||||||
in_source_map : null
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var indentation = 0;
|
var indentation = 0;
|
||||||
|
|||||||
@@ -45,16 +45,30 @@
|
|||||||
function SourceMap(options) {
|
function SourceMap(options) {
|
||||||
options = defaults(options, {
|
options = defaults(options, {
|
||||||
file : null,
|
file : null,
|
||||||
root : null
|
root : null,
|
||||||
|
orig : null,
|
||||||
});
|
});
|
||||||
var generator = new MOZ_SourceMap.SourceMapGenerator({
|
var generator = new MOZ_SourceMap.SourceMapGenerator({
|
||||||
file : options.file,
|
file : options.file,
|
||||||
sourceRoot : options.root
|
sourceRoot : options.root
|
||||||
});
|
});
|
||||||
|
var orig_map = options.orig && new MOZ_SourceMap.SourceMapConsumer(options.orig);
|
||||||
function add(source, gen_line, gen_col, orig_line, orig_col, name) {
|
function add(source, gen_line, gen_col, orig_line, orig_col, name) {
|
||||||
|
orig_line++;
|
||||||
|
gen_line++;
|
||||||
|
if (orig_map) {
|
||||||
|
var info = orig_map.originalPositionFor({
|
||||||
|
line: orig_line,
|
||||||
|
column: orig_col
|
||||||
|
});
|
||||||
|
source = info.source;
|
||||||
|
orig_line = info.line;
|
||||||
|
orig_col = info.column;
|
||||||
|
name = info.name;
|
||||||
|
}
|
||||||
generator.addMapping({
|
generator.addMapping({
|
||||||
generated : { line: gen_line + 1, column: gen_col },
|
generated : { line: gen_line, column: gen_col },
|
||||||
original : { line: orig_line + 1, column: orig_col },
|
original : { line: orig_line, column: orig_col },
|
||||||
source : source,
|
source : source,
|
||||||
name : name
|
name : name
|
||||||
});
|
});
|
||||||
@@ -65,15 +79,3 @@ function SourceMap(options) {
|
|||||||
toString : function() { return generator.toString() }
|
toString : function() { return generator.toString() }
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function SourceMapInput(map) {
|
|
||||||
map = new MOZ_SourceMap.SourceMapConsumer(map);
|
|
||||||
return {
|
|
||||||
info: function(line, col) {
|
|
||||||
return map.originalPositionFor({
|
|
||||||
line: line,
|
|
||||||
column: col
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user