improve usability of `includeSources` (#3057)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 6 Apr 2018 05:32:26 +0000 (13:32 +0800)
committerGitHub <noreply@github.com>
Fri, 6 Apr 2018 05:32:26 +0000 (13:32 +0800)
Exclude source contents from input source map if `includeSources=false`

fixes #3041

lib/minify.js
test/mocha/cli.js
test/mocha/input-sourcemaps.js
test/mocha/sourcemaps.js

index 8dc275c..0e117ed 100644 (file)
@@ -182,6 +182,8 @@ function minify(files, options) {
                     } else for (var name in files) if (HOP(files, name)) {
                         options.output.source_map.get().setSourceContent(name, files[name]);
                     }
+                } else {
+                    options.output.source_map.get()._sourcesContents = null;
                 }
             }
             delete options.output.ast;
index 7385e47..a64cb21 100644 (file)
@@ -121,7 +121,8 @@ describe("bin/uglifyjs", function () {
         var command = [
             uglifyjscmd,
             "--source-map", "content=" + mapFile,
-            "--source-map", "url=inline"
+            "--source-map", "includeSources=true",
+            "--source-map", "url=inline",
         ].join(" ");
 
         var child = exec(command, function(err, stdout) {
@@ -216,7 +217,14 @@ describe("bin/uglifyjs", function () {
         });
     });
     it("Should process inline source map", function(done) {
-        var command = uglifyjscmd + " test/input/issue-520/input.js -mc toplevel --source-map content=inline,url=inline";
+        var command = [
+            uglifyjscmd,
+            "test/input/issue-520/input.js",
+            "-mc", "toplevel",
+            "--source-map", "content=inline",
+            "--source-map", "includeSources=true",
+            "--source-map", "url=inline",
+        ].join(" ");
 
         exec(command, function (err, stdout) {
             if (err) throw err;
index bda6e1a..22c4a23 100644 (file)
@@ -1,66 +1,68 @@
-var Uglify = require('../../');
 var assert = require("assert");
+var Uglify = require("../../");
 var SourceMapConsumer = require("source-map").SourceMapConsumer;
 
-describe("input sourcemaps", function() {
-    var transpilemap, map;
-
-    function getMap() {
-      return {
-          "version": 3,
-          "sources": ["index.js"],
-          "names": [],
-          "mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ",
-          "file": "bundle.js",
-          "sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"]
-      };
-    }
-
-    function prepareMap(sourceMap) {
-        var transpiled = '"use strict";\n\n' +
-            'var foo = function foo(x) {\n  return "foo " + x;\n};\n' +
-            'console.log(foo("bar"));\n\n' +
-            '//# sourceMappingURL=bundle.js.map';
-
-        transpilemap = sourceMap || getMap();
-
-        var result = Uglify.minify(transpiled, {
-            sourceMap: {
-                content: transpilemap
-            }
-        });
+function getMap() {
+    return {
+        "version": 3,
+        "sources": ["index.js"],
+        "names": [],
+        "mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ",
+        "file": "bundle.js",
+        "sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"]
+    };
+}
 
-        map = new SourceMapConsumer(result.map);
-    }
-
-    beforeEach(function () {
-      prepareMap();
+function prepareMap(sourceMap) {
+    var code = [
+        '"use strict";',
+        "",
+        "var foo = function foo(x) {",
+        '  return "foo " + x;',
+        "};",
+        'console.log(foo("bar"));',
+        "",
+        "//# sourceMappingURL=bundle.js.map",
+    ].join("\n");
+    var result = Uglify.minify(code, {
+        sourceMap: {
+            content: sourceMap,
+            includeSources: true,
+        }
     });
+    if (result.error) throw result.error;
+    return new SourceMapConsumer(result.map);
+}
 
+describe("input sourcemaps", function() {
     it("Should copy over original sourcesContent", function() {
-        assert.equal(map.sourceContentFor("index.js"), transpilemap.sourcesContent[0]);
+        var orig = getMap();
+        var map = prepareMap(orig);
+        assert.equal(map.sourceContentFor("index.js"), orig.sourcesContent[0]);
     });
 
-    it("Should copy sourcesContent if sources are relative", function () {
+    it("Should copy sourcesContent if sources are relative", function() {
         var relativeMap = getMap();
         relativeMap.sources = ['./index.js'];
-
-        prepareMap(relativeMap);
+        var map = prepareMap(relativeMap);
         assert.notEqual(map.sourcesContent, null);
         assert.equal(map.sourcesContent.length, 1);
-        assert.equal(map.sourceContentFor("index.js"), transpilemap.sourcesContent[0]);
+        assert.equal(map.sourceContentFor("index.js"), relativeMap.sourcesContent[0]);
     });
 
-    it("Final sourcemap should not have invalid mappings from inputSourceMap (issue #882)", function() {
+    it("Should not have invalid mappings from inputSourceMap (issue #882)", function() {
+        var map = prepareMap(getMap());
         // The original source has only 2 lines, check that mappings don't have more lines
-
         var msg = "Mapping should not have higher line number than the original file had";
         map.eachMapping(function(mapping) {
-            assert.ok(mapping.originalLine <= 2, msg)
+            assert.ok(mapping.originalLine <= 2, msg);
         });
-
-        map.allGeneratedPositionsFor({source: "index.js", line: 1, column: 1}).forEach(function(pos) {
+        map.allGeneratedPositionsFor({
+            source: "index.js",
+            line: 1,
+            column: 1
+        }).forEach(function(pos) {
             assert.ok(pos.line <= 2, msg);
-        })
+        });
     });
 });
index a1a8e61..8868c5a 100644 (file)
@@ -70,6 +70,7 @@ describe("sourcemaps", function() {
                 compress: { toplevel: true },
                 sourceMap: {
                     content: "inline",
+                    includeSources: true,
                     url: "inline"
                 }
             }).code + "\n";
@@ -109,6 +110,29 @@ describe("sourcemaps", function() {
             assert.ok(err instanceof Error);
             assert.strictEqual(err.stack.split(/\n/)[0], "Error: inline source map only works with singular input");
         });
+        it("Should drop source contents for includeSources=false", function() {
+            var result = Uglify.minify(read("./test/input/issue-520/input.js"), {
+                compress: false,
+                mangle: false,
+                sourceMap: {
+                    content: "inline",
+                    includeSources: true,
+                },
+            });
+            if (result.error) throw result.error;
+            var map = JSON.parse(result.map);
+            assert.strictEqual(map.sourcesContent.length, 1);
+            result = Uglify.minify(result.code, {
+                compress: false,
+                mangle: false,
+                sourceMap: {
+                    content: result.map,
+                },
+            });
+            if (result.error) throw result.error;
+            map = JSON.parse(result.map);
+            assert.ok(!("sourcesContent" in map));
+        });
     });
 
     describe("sourceMapInline", function() {