var assert = require("assert"); var exec = require("child_process").exec; var readFileSync = require("fs").readFileSync; function read(path) { return readFileSync(path, "utf8"); } describe("bin/uglifyjs", function () { var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs'; it("should produce a functional build when using --self", function (done) { this.timeout(30000); var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS'; exec(command, function (err, stdout) { if (err) throw err; eval(stdout); assert.strictEqual(typeof WrappedUglifyJS, 'object'); var result = WrappedUglifyJS.minify("foo([true,,2+3]);"); assert.strictEqual(result.error, undefined); assert.strictEqual(result.code, "foo([!0,,5]);"); done(); }); }); it("Should be able to filter comments correctly with `--comments all`", function (done) { var command = uglifyjscmd + ' test/input/comments/filter.js --comments all'; exec(command, function (err, stdout) { if (err) throw err; assert.strictEqual(stdout, "// foo\n/*@preserve*/\n// bar\n\n"); done(); }); }); it("Should be able to filter comments correctly with `--comment `", function (done) { var command = uglifyjscmd + ' test/input/comments/filter.js --comments /r/'; exec(command, function (err, stdout) { if (err) throw err; assert.strictEqual(stdout, "/*@preserve*/\n// bar\n\n"); done(); }); }); it("Should be able to filter comments correctly with just `--comment`", function (done) { var command = uglifyjscmd + ' test/input/comments/filter.js --comments'; exec(command, function (err, stdout) { if (err) throw err; assert.strictEqual(stdout, "/*@preserve*/\n\n"); done(); }); }); it("Should append source map to output when using --source-map url=inline", function (done) { var command = uglifyjscmd + " test/input/issue-1323/sample.js --source-map url=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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFJQSxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxJQUdYLE9BQU9DLElBTEQifQ==\n"); done(); }); }); it("should not append source map to output when not using --source-map url=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(); }); }); it("should not consider source map file content as source map file name (issue #2082)", function (done) { var command = [ uglifyjscmd, "test/input/issue-2082/sample.js", "--source-map", "content=test/input/issue-2082/sample.js.map", "--source-map", "url=inline", ].join(" "); exec(command, function (err, stdout, stderr) { if (err) throw err; var stderrLines = stderr.split('\n'); assert.strictEqual(stderrLines[0], 'INFO: Using input source map: test/input/issue-2082/sample.js.map'); assert.notStrictEqual(stderrLines[1], 'INFO: Using input source map: {"version": 3,"sources": ["index.js"],"mappings": ";"}'); done(); }); }); it("should not load the source map before it fully receives the JS source", function(done) { var command = [ '"' + process.argv[0] + '" test/input/pr-3040/pipe.js |', uglifyjscmd, "--source-map", "content=test/input/pr-3040/input.js.map", "--source-map", "url=inline" ].join(" "); exec(command, function(err, stdout) { if (err) throw err; // Revert input file to initial state for future test validity require("fs").writeFileSync("test/input/pr-3040/input.js.map", '{"version":3,"sources":["input2.js"],"names":["require","foo","hello","x","apply","_toConsumableArray","y","z"],"mappings":"kLAAcA,QAAQ,OAAfC,aAAAA,kBACSD,QAAQ,SAAjBE,gBAAAA,MAEPD,IAAIE,EAAJC,MAAAH,IAAAI,mBAASJ,IAAIK,EAAEJ,MAAMK","sourcesContent":["const {foo} = require(\\"bar\\");\\nconst {hello} = require(\\"world\\");\\n\\nfoo.x(...foo.y(hello.z));\\n"]}'); assert.strictEqual(stdout, [ 'function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i