aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md5
-rwxr-xr-xbin/uglifyjs23
-rw-r--r--test/input/issue-1323/sample.js7
-rw-r--r--test/mocha/cli.js21
-rw-r--r--test/mocha/minify.js19
-rw-r--r--tools/node.js14
6 files changed, 75 insertions, 14 deletions
diff --git a/README.md b/README.md
index ed2630f1..87d828a6 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ The available options are:
--source-map-include-sources Pass this flag if you want to include the
content of source files in the source map as
sourcesContent property.
+ --source-map-inline Write base64-encoded source map to the end of js output.
--in-source-map Input source map, useful if you're compressing
JS that was generated from some other original
code.
@@ -641,7 +642,9 @@ var result = UglifyJS.minify({"file1.js": "var a = function () {};"}, {
Note that the source map is not saved in a file, it's just returned in
`result.map`. The value passed for `outSourceMap` is only used to set the
-`file` attribute in the source map (see [the spec][sm-spec]).
+`file` attribute in the source map (see [the spec][sm-spec]). You can set
+option `sourceMapInline` to be `true` and source map will be appended to
+code.
You can also specify sourceRoot property to be included in source map:
```javascript
diff --git a/bin/uglifyjs b/bin/uglifyjs
index da9e0f10..76438961 100755
--- a/bin/uglifyjs
+++ b/bin/uglifyjs
@@ -23,6 +23,7 @@ mangling you need to use `-c` and `-m`.\
.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-url", "The path to the source map to be added in //# sourceMappingURL. Defaults to the value passed with --source-map.")
+ .describe("source-map-inline", "Write base64-encoded source map to the end of js output. Disabled by default")
.describe("source-map-include-sources", "Pass this flag if you want to include the content of source files in the source map as sourcesContent property.")
.describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.")
.describe("screw-ie8", "Do not support Internet Explorer 6-8 quirks. This flag is enabled by default.")
@@ -113,6 +114,7 @@ You need to pass an argument to this option to specify the name that your module
.array("pure-funcs")
.boolean("expr")
+ .boolean("source-map-inline")
.boolean("source-map-include-sources")
.boolean("screw-ie8")
.boolean("support-ie8")
@@ -309,7 +311,7 @@ var TOPLEVEL = null;
var P_RELATIVE = ARGS.p && ARGS.p == "relative";
var SOURCES_CONTENT = {};
-var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({
+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,
@@ -474,13 +476,18 @@ async.eachLimit(files, 1, function (file, cb) {
output = output.get();
if (SOURCE_MAP) {
- fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
- 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 (ARGS.source_map_inline) {
+ var base64_string = new Buffer(SOURCE_MAP.toString()).toString('base64');
+ output += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + base64_string;
+ } else {
+ fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
+ 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) {
diff --git a/test/input/issue-1323/sample.js b/test/input/issue-1323/sample.js
new file mode 100644
index 00000000..ff56acc3
--- /dev/null
+++ b/test/input/issue-1323/sample.js
@@ -0,0 +1,7 @@
+var bar = (function () {
+ function foo (bar) {
+ return bar;
+ }
+
+ return foo;
+})(); \ No newline at end of file
diff --git a/test/mocha/cli.js b/test/mocha/cli.js
index 495b0076..bebd4d9d 100644
--- a/test/mocha/cli.js
+++ b/test/mocha/cli.js
@@ -49,4 +49,25 @@ describe("bin/uglifyjs", function () {
done();
});
});
+ it("Should append source map to output when using --source-map-inline", function (done) {
+ var command = uglifyjscmd + ' test/input/issue-1323/sample.js --source-map-inline';
+
+ exec(command, function (err, stdout) {
+ if (err) throw err;
+
+ assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n" +
+ "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxHQUFJQSxLQUFNLFdBQ04sUUFBU0MsS0FBS0QsS0FDVixNQUFPQSxLQUdYLE1BQU9DIn0=\n");
+ done();
+ });
+ });
+ it("should not append source map to output when not using --source-map-inline", function (done) {
+ var command = uglifyjscmd + ' test/input/issue-1323/sample.js';
+
+ exec(command, function (err, stdout) {
+ if (err) throw err;
+
+ assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n");
+ done();
+ });
+ });
});
diff --git a/test/mocha/minify.js b/test/mocha/minify.js
index 2adbadcb..ce5e8497 100644
--- a/test/mocha/minify.js
+++ b/test/mocha/minify.js
@@ -75,4 +75,23 @@ describe("minify", function() {
'let foo = x => "foo " + x;\nconsole.log(foo("bar"));');
});
});
+
+ describe("sourceMapInline", function() {
+ it("should append source map to output js when sourceMapInline is enabled", function() {
+ var result = Uglify.minify('var a = function(foo) { return foo; };', {
+ fromString: true,
+ sourceMapInline: true
+ });
+ var code = result.code;
+ assert.strictEqual(code, "var a=function(n){return n};\n" +
+ "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIj8iXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsR0FBSUEsR0FBSSxTQUFTQyxHQUFPLE1BQU9BIn0=");
+ });
+ it("should not append source map to output js when sourceMapInline is not enabled", function() {
+ var result = Uglify.minify('var a = function(foo) { return foo; };', {
+ fromString: true
+ });
+ var code = result.code;
+ assert.strictEqual(code, "var a=function(n){return n};");
+ });
+ });
});
diff --git a/tools/node.js b/tools/node.js
index 6712ccf6..a16169b1 100644
--- a/tools/node.js
+++ b/tools/node.js
@@ -44,6 +44,7 @@ exports.minify = function(files, options) {
sourceRoot : null,
inSourceMap : null,
sourceMapUrl : null,
+ sourceMapInline : false,
fromString : false,
warnings : false,
mangle : {},
@@ -117,7 +118,7 @@ exports.minify = function(files, options) {
if (typeof options.inSourceMap == "string") {
inMap = JSON.parse(fs.readFileSync(options.inSourceMap, "utf8"));
}
- if (options.outSourceMap) {
+ if (options.outSourceMap || options.sourceMapInline) {
output.source_map = UglifyJS.SourceMap({
file: options.outSourceMap,
orig: inMap,
@@ -138,16 +139,19 @@ exports.minify = function(files, options) {
var stream = UglifyJS.OutputStream(output);
toplevel.print(stream);
- var mappingUrlPrefix = "\n//# sourceMappingURL=";
- if (options.outSourceMap && typeof options.outSourceMap === "string" && options.sourceMapUrl !== false) {
- stream += mappingUrlPrefix + (typeof options.sourceMapUrl === "string" ? options.sourceMapUrl : options.outSourceMap);
- }
var source_map = output.source_map;
if (source_map) {
source_map = source_map + "";
}
+ var mappingUrlPrefix = "\n//# sourceMappingURL=";
+ if (options.sourceMapInline) {
+ stream += mappingUrlPrefix + "data:application/json;charset=utf-8;base64," + new Buffer(source_map).toString("base64");
+ } else if (options.outSourceMap && typeof options.outSourceMap === "string" && options.sourceMapUrl !== false) {
+ stream += mappingUrlPrefix + (typeof options.sourceMapUrl === "string" ? options.sourceMapUrl : options.outSourceMap);
+ }
+
return {
code : stream + "",
map : source_map