gulp-uglify-es
Version:
gulp stream to uglify with 'uglify-es' (es6 supported).
73 lines (72 loc) • 2.93 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const o_stream_1 = require("o-stream");
const gutil = require("gulp-util");
const index_1 = require("./index");
function default_1(suite) {
suite.test("When recieves a valid file, then uglify it.", (test) => __awaiter(this, void 0, void 0, function* () {
yield testText("class MyClass { }", output => output === "class MyClass{}");
}));
suite.test("When recieves a file without contents, then throws.", (test) => __awaiter(this, void 0, void 0, function* () {
yield testFile(new gutil.File({ contents: null }), (output, error) => {
if (!error) {
throw new Error("Expected an error but none was thrown.");
}
return true;
});
}));
}
exports.default = default_1;
function createBundleFile(content) {
return new gutil.File({
path: "bundle.js",
contents: new Buffer(content)
});
}
function testText(input, validation) {
return testFile(createBundleFile(input), validation);
}
function testFile(file, validation) {
return new Promise((resolve, reject) => {
let input = file.contents ? file.contents.toString() : null;
let fileStream = o_stream_1.default.fromArray([file]);
fileStream.pipe(index_1.default())
.pipe(o_stream_1.default.transform({
onEntered: args => {
let output = args.object.contents.toString();
try {
if (validation(output, undefined)) {
resolve();
}
else {
reject(`Validation returned false. Input: ${input}. Output:${output}`);
}
}
catch (error) {
reject(error);
}
},
onSourceStreamError: args => {
try {
if (validation(undefined, args.error)) {
resolve();
}
else {
reject(`Validation returned false. Error: ${args.error}.`);
}
}
catch (error) {
reject(error);
}
}
}));
});
}