Update JavaScript Minify operation to support ES6.

This commit is contained in:
Jarrod Connolly 2019-10-31 23:18:54 -07:00
parent 734962ac22
commit 462f619f43
3 changed files with 17 additions and 146 deletions

View file

@ -4,10 +4,9 @@
* @license Apache-2.0
*/
import OperationError from "../errors/OperationError.mjs";
import Operation from "../Operation.mjs";
import * as esprima from "esprima";
import escodegen from "escodegen";
import esmangle from "esmangle";
import Terser from "terser";
/**
* JavaScript Minify operation
@ -34,22 +33,11 @@ class JavaScriptMinify extends Operation {
* @returns {string}
*/
run(input, args) {
let result = "";
const AST = esprima.parseScript(input),
optimisedAST = esmangle.optimize(AST, null),
mangledAST = esmangle.mangle(optimisedAST);
result = escodegen.generate(mangledAST, {
format: {
renumber: true,
hexadecimal: true,
escapeless: true,
compact: true,
semicolons: false,
parentheses: false
}
});
return result;
const result = Terser.minify(input);
if (result.error) {
throw new OperationError(`Error minifying JavaScript. (${result.error})`);
}
return result.code;
}
}