From d7eaa959492e7a10064cb0a7d153f1a98aa17e7f Mon Sep 17 00:00:00 2001 From: pengzhenqing Date: Sun, 9 Oct 2016 14:15:25 +0800 Subject: [PATCH] Add an option for writing inline source map --- README.md | 5 ++++- bin/uglifyjs | 23 +++++++++++++++-------- tools/node.js | 14 +++++++++----- 3 files changed, 28 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/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