Skip to content

Commit

Permalink
allow --in-source-map inline
Browse files Browse the repository at this point in the history
fixes #520
  • Loading branch information
alexlamsl committed Feb 15, 2017
1 parent e96673d commit f930a9f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
24 changes: 19 additions & 5 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -281,21 +281,23 @@ 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 (files.length > 1 && ORIG_MAP == "inline") {
print_error("ERROR: Inline source map only works with singular input");
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);
Expand All @@ -317,6 +319,9 @@ async.eachLimit(files, 1, function (file, cb) {
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, '/');
Expand Down Expand Up @@ -367,7 +372,7 @@ async.eachLimit(files, 1, function (file, cb) {

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,
root: ARGS.source_map_root || ORIG_MAP && ORIG_MAP.sourceRoot,
orig: ORIG_MAP,
}) : null;

Expand Down Expand Up @@ -576,6 +581,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();
Expand Down
3 changes: 3 additions & 0 deletions test/input/issue-520/input.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/input/issue-520/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions test/mocha/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var assert = require("assert");
var exec = require("child_process").exec;
var readFileSync = require("fs").readFileSync;

describe("bin/uglifyjs", function () {
var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
Expand Down Expand Up @@ -100,4 +101,33 @@ describe("bin/uglifyjs", function () {
done();
});
});
it("Should process inline source map", function(done) {
var command = uglifyjscmd + ' test/input/issue-520/input.js -cm toplevel --in-source-map inline --source-map-inline';

exec(command, function (err, stdout) {
if (err) throw err;

assert.strictEqual(stdout, readFileSync("test/input/issue-520/output.js", "utf8"));
done();
});
});
it("Should warn for missing inline source map", function(done) {
var command = uglifyjscmd + ' test/input/issue-1323/sample.js --in-source-map inline';

exec(command, function (err, stdout, stderr) {
if (err) throw err;

assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n");
assert.strictEqual(stderr, "WARN: inline source map not found\n");
done();
});
});
it("Should fail for multiple input and inline source map", function(done) {
var command = uglifyjscmd + ' foo.js bar.js --in-source-map inline --source-map-inline';

exec(command, function (err) {
assert.ok(err);
done();
});
});
});

0 comments on commit f930a9f

Please sign in to comment.