diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-02-25 04:11:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-25 04:11:21 +0800 |
commit | cf0951f72635f898ef9dc98ced3c73a1c488cc17 (patch) | |
tree | 897ca02e307df414ecce5c56958dba4a1e741536 /bin/uglifyjs | |
parent | 852f78491a212ba20841847436dab93abb0a8181 (diff) | |
download | tracifyjs-cf0951f72635f898ef9dc98ced3c73a1c488cc17.tar.gz tracifyjs-cf0951f72635f898ef9dc98ced3c73a1c488cc17.zip |
allow --in-source-map inline (#1490)
- limited to one input file (or `stdin`)
- only works with built-in parser
fixes #520
Diffstat (limited to 'bin/uglifyjs')
-rwxr-xr-x | bin/uglifyjs | 71 |
1 files changed, 46 insertions, 25 deletions
diff --git a/bin/uglifyjs b/bin/uglifyjs index 27717fb6..d0c3abb2 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -282,21 +282,29 @@ if (ARGS.self) { var ORIG_MAP = ARGS.in_source_map; -if (ORIG_MAP) { +if (ORIG_MAP && ORIG_MAP != "inline") { ORIG_MAP = JSON.parse(fs.readFileSync(ORIG_MAP)); if (files.length == 0) { print_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 = [ "-" ]; } +if (ORIG_MAP == "inline") { + if (files.length > 1) { + print_error("ERROR: Inline source map only works with singular input"); + process.exit(1); + } + if (ARGS.acorn || ARGS.spidermonkey) { + print_error("ERROR: Inline source map only works with built-in parser"); + process.exit(1); + } +} + if (files.indexOf("-") >= 0 && ARGS.source_map) { print_error("ERROR: Source map doesn't work with input from STDIN"); process.exit(1); @@ -308,37 +316,19 @@ if (files.filter(function(el){ return el == "-" }).length > 1) { } var STATS = {}; -var OUTPUT_FILE = ARGS.o; var TOPLEVEL = null; var P_RELATIVE = ARGS.p && ARGS.p == "relative"; var SOURCES_CONTENT = {}; -var SOURCE_MAP = (ARGS.source_map || ARGS.source_map_inline) ? UglifyJS.SourceMap({ - file: P_RELATIVE ? path.relative(path.dirname(ARGS.source_map), OUTPUT_FILE) : OUTPUT_FILE, - root: ARGS.source_map_root, - orig: ORIG_MAP, -}) : null; - -OUTPUT_OPTIONS.source_map = SOURCE_MAP; - -try { - var output = UglifyJS.OutputStream(OUTPUT_OPTIONS); - var compressor = COMPRESS && UglifyJS.Compressor(COMPRESS); -} catch(ex) { - if (ex instanceof UglifyJS.DefaultsError) { - print_error(ex.msg); - print_error("Supported options:"); - print_error(sys.inspect(ex.defs)); - process.exit(1); - } -} - async.eachLimit(files, 1, function (file, cb) { read_whole_file(file, function (err, code) { if (err) { print_error("ERROR: can't read file: " + file); process.exit(1); } + if (ORIG_MAP == "inline") { + ORIG_MAP = read_source_map(code); + } if (ARGS.p != null) { if (P_RELATIVE) { file = path.relative(path.dirname(ARGS.source_map), file).replace(/\\/g, '/'); @@ -385,6 +375,28 @@ async.eachLimit(files, 1, function (file, cb) { cb(); }); }, function () { + var OUTPUT_FILE = ARGS.o; + + var SOURCE_MAP = (ARGS.source_map || ARGS.source_map_inline) ? UglifyJS.SourceMap({ + file: P_RELATIVE ? path.relative(path.dirname(ARGS.source_map), OUTPUT_FILE) : OUTPUT_FILE, + root: ARGS.source_map_root || ORIG_MAP && ORIG_MAP.sourceRoot, + orig: ORIG_MAP, + }) : null; + + OUTPUT_OPTIONS.source_map = SOURCE_MAP; + + try { + var output = UglifyJS.OutputStream(OUTPUT_OPTIONS); + var compressor = COMPRESS && UglifyJS.Compressor(COMPRESS); + } catch(ex) { + if (ex instanceof UglifyJS.DefaultsError) { + print_error(ex.msg); + print_error("Supported options:"); + print_error(sys.inspect(ex.defs)); + process.exit(1); + } + } + if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){ TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL); }); @@ -576,6 +588,15 @@ function read_whole_file(filename, cb) { } } +function read_source_map(code) { + var match = /\n\/\/# sourceMappingURL=data:application\/json(;.*?)?;base64,(.*)/.exec(code); + if (!match) { + print_error("WARN: inline source map not found"); + return null; + } + return JSON.parse(new Buffer(match[2], "base64")); +} + function time_it(name, cont) { var t1 = new Date().getTime(); var ret = cont(); |