- rename `screw_ie8` to `ie8` - rename `mangle.except` to `mangle.reserved` - rename `mangle.properties.ignore_quoted` to `mangle.properties.keep_quoted` - compact `sourceMap` options - more stringent verification on input `options` - toplevel shorthands - `ie8` - `keep_fnames` - `toplevel` - `warnings` - support arrays and unquoted string values on CLI - drop `fromString` from `minify()` - `minify()` no longer handles any `fs` operations - unify order of operations for `mangle_properties()` on CLI & API - `bin/uglifyjs` used to `mangle_properties()` before even `Compressor` - `minify()` used to `mangle_properties()` after `Compressor` but before `mangle_names()` - both will now do `Compressor`, `mangle_names()` then `mangle_properties()` - `options.parse` / `--parse` for parser options beyond `bare_returns` - add `mangle.properties.builtins` to disable built-in reserved list - disable with `--mangle-props builtins` on CLI - `warnings` now off by default - add `--warn` and `--verbose` on CLI - drop `--enclose` - drop `--export-all` - drop `--reserved-file` - use `--mangle reserved` instead - drop `--reserve-domprops` - enabled by default, disable with `--mangle-props domprops` - drop `--prefix` - use `--source-map base` instead - drop `--lint` - remove `bin/extract-props.js` - limit exposure of internal APIs - update documentations closes #96 closes #102 closes #136 closes #166 closes #243 closes #254 closes #261 closes #311 closes #700 closes #748 closes #912 closes #1072 closes #1366 fixes #101 fixes #123 fixes #124 fixes #263 fixes #379 fixes #419 fixes #423 fixes #461 fixes #465 fixes #576 fixes #737 fixes #772 fixes #958 fixes #1036 fixes #1142 fixes #1175 fixes #1220 fixes #1223 fixes #1280 fixes #1359 fixes #1368
196 lines
7.8 KiB
JavaScript
196 lines
7.8 KiB
JavaScript
var Uglify = require('../../');
|
|
var assert = require("assert");
|
|
var readFileSync = require("fs").readFileSync;
|
|
|
|
function read(path) {
|
|
return readFileSync(path, "utf8");
|
|
}
|
|
|
|
describe("minify", function() {
|
|
it("Should test basic sanity of minify with default options", function() {
|
|
var js = 'function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }';
|
|
var result = Uglify.minify(js);
|
|
assert.strictEqual(result.code, 'function foo(n){return n?3:7}');
|
|
});
|
|
|
|
describe("keep_quoted_props", function() {
|
|
it("Should preserve quotes in object literals", function() {
|
|
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
|
|
var result = Uglify.minify(js, {
|
|
output: {
|
|
keep_quoted_props: true
|
|
}});
|
|
assert.strictEqual(result.code, 'var foo={"x":1,y:2,"z":3};');
|
|
});
|
|
|
|
it("Should preserve quote styles when quote_style is 3", function() {
|
|
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
|
|
var result = Uglify.minify(js, {
|
|
output: {
|
|
keep_quoted_props: true,
|
|
quote_style: 3
|
|
}});
|
|
assert.strictEqual(result.code, 'var foo={"x":1,y:2,\'z\':3};');
|
|
});
|
|
|
|
it("Should not preserve quotes in object literals when disabled", function() {
|
|
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
|
|
var result = Uglify.minify(js, {
|
|
output: {
|
|
keep_quoted_props: false,
|
|
quote_style: 3
|
|
}});
|
|
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
|
|
});
|
|
});
|
|
|
|
describe("mangleProperties", function() {
|
|
it("Shouldn't mangle quoted properties", function() {
|
|
var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};';
|
|
var result = Uglify.minify(js, {
|
|
compress: {
|
|
properties: false
|
|
},
|
|
mangle: {
|
|
properties: {
|
|
keep_quoted: true
|
|
}
|
|
},
|
|
output: {
|
|
keep_quoted_props: true,
|
|
quote_style: 3
|
|
}
|
|
});
|
|
assert.strictEqual(result.code,
|
|
'a["foo"]="bar",a.a="red",x={"bar":10};');
|
|
});
|
|
});
|
|
|
|
describe("inSourceMap", function() {
|
|
it("Should read the given string filename correctly when sourceMapIncludeSources is enabled (#1236)", function() {
|
|
var result = Uglify.minify(read("./test/input/issue-1236/simple.js"), {
|
|
sourceMap: {
|
|
content: read("./test/input/issue-1236/simple.js.map"),
|
|
filename: "simple.min.js",
|
|
includeSources: true
|
|
}
|
|
});
|
|
|
|
var map = JSON.parse(result.map);
|
|
|
|
assert.equal(map.file, 'simple.min.js');
|
|
assert.equal(map.sourcesContent.length, 1);
|
|
assert.equal(map.sourcesContent[0],
|
|
'let foo = x => "foo " + x;\nconsole.log(foo("bar"));');
|
|
});
|
|
it("Should process inline source map", function() {
|
|
var code = Uglify.minify(read("./test/input/issue-520/input.js"), {
|
|
compress: { toplevel: true },
|
|
sourceMap: {
|
|
content: "inline",
|
|
url: "inline"
|
|
}
|
|
}).code + "\n";
|
|
assert.strictEqual(code, readFileSync("test/input/issue-520/output.js", "utf8"));
|
|
});
|
|
it("Should warn for missing inline source map", function() {
|
|
var warn_function = Uglify.AST_Node.warn_function;
|
|
var warnings = [];
|
|
Uglify.AST_Node.warn_function = function(txt) {
|
|
warnings.push(txt);
|
|
};
|
|
try {
|
|
var result = Uglify.minify(read("./test/input/issue-1323/sample.js"), {
|
|
mangle: false,
|
|
sourceMap: {
|
|
content: "inline"
|
|
}
|
|
});
|
|
assert.strictEqual(result.code, "var bar=function(){function foo(bar){return bar}return foo}();");
|
|
assert.strictEqual(warnings.length, 1);
|
|
assert.strictEqual(warnings[0], "inline source map not found");
|
|
} finally {
|
|
Uglify.AST_Node.warn_function = warn_function;
|
|
}
|
|
});
|
|
it("Should fail with multiple input and inline source map", function() {
|
|
assert.throws(function() {
|
|
Uglify.minify([
|
|
read("./test/input/issue-520/input.js"),
|
|
read("./test/input/issue-520/output.js")
|
|
], {
|
|
sourceMap: {
|
|
content: "inline",
|
|
url: "inline"
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
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; };', {
|
|
sourceMap: {
|
|
url: "inline"
|
|
}
|
|
});
|
|
var code = result.code;
|
|
assert.strictEqual(code, "var a=function(n){return n};\n" +
|
|
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsR0FBSUEsR0FBSSxTQUFTQyxHQUFPLE1BQU9BIn0=");
|
|
});
|
|
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; };');
|
|
var code = result.code;
|
|
assert.strictEqual(code, "var a=function(n){return n};");
|
|
});
|
|
});
|
|
|
|
describe("#__PURE__", function() {
|
|
it("should drop #__PURE__ hint after use", function() {
|
|
var result = Uglify.minify('//@__PURE__ comment1 #__PURE__ comment2\n foo(), bar();', {
|
|
output: {
|
|
comments: "all",
|
|
beautify: false,
|
|
}
|
|
});
|
|
var code = result.code;
|
|
assert.strictEqual(code, "// comment1 comment2\nbar();");
|
|
});
|
|
it("should not drop #__PURE__ hint if function is retained", function() {
|
|
var result = Uglify.minify("var a = /*#__PURE__*/(function(){ foo(); })();", {
|
|
output: {
|
|
comments: "all",
|
|
beautify: false,
|
|
}
|
|
});
|
|
var code = result.code;
|
|
assert.strictEqual(code, "var a=/*#__PURE__*/function(){foo()}();");
|
|
})
|
|
});
|
|
|
|
describe("JS_Parse_Error", function() {
|
|
it("should throw syntax error", function() {
|
|
assert.throws(function() {
|
|
Uglify.minify("function f(a{}");
|
|
}, function(err) {
|
|
assert.ok(err instanceof Error);
|
|
assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc «{», expected punc «,»");
|
|
assert.strictEqual(err.filename, "0");
|
|
assert.strictEqual(err.line, 1);
|
|
assert.strictEqual(err.col, 12);
|
|
return true;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Compressor", function() {
|
|
it("should be backward compatible with ast.transform(compressor)", function() {
|
|
var ast = Uglify.parse("function f(a){for(var i=0;i<a;i++)console.log(i)}");
|
|
ast.figure_out_scope();
|
|
ast = ast.transform(Uglify.Compressor());
|
|
assert.strictEqual(ast.print_to_string(), "function f(a){for(var i=0;i<a;i++)console.log(i)}");
|
|
});
|
|
})
|
|
});
|