Add an option for writing inline source map

This commit is contained in:
pengzhenqing
2016-10-09 14:15:25 +08:00
committed by Richard van Velzen
parent 6389e52305
commit e51c6ba380
6 changed files with 75 additions and 14 deletions

View File

@@ -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