node-uglifier-es
Version:
Fully auto merging and uglifying a whole NodeJs project into one file with external files option. Recompiled from Zsolt Istvan Szabo's work with uglify-es instead of uglify-js-harmony.
349 lines (317 loc) • 13.4 kB
JavaScript
// Generated by CoffeeScript 2.3.1
(function() {
var IS_RE_CREATE_TEST_FILES, NodeUglifier, SEPARATOR, domain, fs, fsExtra, packageUtils, path, relativeToDir, testCycleDetection, testMerge;
fs = require('fs');
fsExtra = require('fs-extra');
NodeUglifier = require("../NodeUglifier");
packageUtils = require('../libs/packageUtils');
path = require('path');
domain = require('domain');
IS_RE_CREATE_TEST_FILES = true;
SEPARATOR = path.sep;
//exports.testExpressWithModulesPackedToo=(test)->
// testFile="lib_compiled/test/resultFiles/express.js"
// nodeUglifier=new NodeUglifier("lib_compiled/test/express/server.js",{packNodeModules:true})
// mergedSource=nodeUglifier.merge().uglify().toString()
// try
// eval(mergedSource)
// catch me
// test.fail(me.toString(),"expected no error thrown from combined project")
// if IS_RE_CREATE_TEST_FILES then nodeUglifier.exportToFile(testFile)
// else
// test.equals(packageUtils.readFile(testFile).toString(),mergedSource)
// test.done()
Function.prototype.withDomain = function(withStack) {
var fn;
fn = this;
return function(test) {
var d;
d = domain.create();
d.on('error', function(e) {
test.fail('test failed with ' + e.message);
if (withStack) {
console.error(e.stack);
}
return test.done();
});
return d.run(fn.bind(this, test));
};
};
testCycleDetection = function(test) {
var expectedFirstLine, got, me, mergedSource, nodeUglifier, testFile;
testFile = "lib_compiled/test/resultFiles/testCycleDetection.js";
try {
nodeUglifier = new NodeUglifier("lib_compiled/test/testCycleDetection/main.js", {
rngSeed: "hello"
});
mergedSource = nodeUglifier.merge().uglify().toString();
eval(mergedSource);
} catch (error) {
me = error;
expectedFirstLine = 'There has been 2 cycles in the dependency tree:';
got = me.message.slice(0, +(expectedFirstLine.length - 1) + 1 || 9e9);
if (got !== expectedFirstLine) {
throw me;
test.fail(me.toString(), "expected no error thrown from combined project");
}
}
if (IS_RE_CREATE_TEST_FILES) {
nodeUglifier.exportToFile(testFile);
} else {
test.equals(packageUtils.readFile(testFile).toString(), mergedSource);
}
return test.done();
};
exports.testCycleDetection = testCycleDetection;
exports.testExpress = function(test) {
var me, mergedSource, nodeUglifier, testFile;
testFile = "lib_compiled/test/resultFiles/express.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/express/server.js", {
rngSeed: "hello"
});
mergedSource = nodeUglifier.merge().uglify().toString();
try {
eval(mergedSource);
} catch (error) {
me = error;
test.fail(me.toString(), "expected no error thrown from combined project");
}
if (IS_RE_CREATE_TEST_FILES) {
nodeUglifier.exportToFile(testFile);
} else {
test.equals(packageUtils.readFile(testFile).toString(), mergedSource);
}
return test.done();
};
exports.directoryImportTest = function(test) {
var me, mergedSource, nodeUglifier, testFile;
testFile = "lib_compiled/test/resultFiles/directoryImportTest.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/testDirectoryImport/main.js", {
rngSeed: "hello"
});
mergedSource = nodeUglifier.merge().toString();
try {
eval(mergedSource);
} catch (error) {
me = error;
test.fail(me.toString(), "expected no error thrown from combined project");
}
if (IS_RE_CREATE_TEST_FILES) {
nodeUglifier.exportToFile(testFile);
} else {
test.equals(packageUtils.readFile(testFile).toString(), mergedSource);
}
return test.done();
};
exports.testMergeWithBothExportFilterTypes = function(test) {
var main, me, mergedSource, nodeUglifier, testFile;
testFile = "lib_compiled/test/resultFiles/simpleMergeWithBothExportFilterTypes.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/testproject/main.js", {
rngSeed: "hello",
mergeFileFilterWithExport: ["./lib_static/test/", "./depa/constants.js"],
mergeFileFilter: ["./depDynamic/filename_used_in_dynamic_require.js"]
});
mergedSource = nodeUglifier.merge().toString();
nodeUglifier.exportToFile(testFile);
try {
//dont test equality, heavily coffeescript version dependent due to comments not removed
// test.equals(packageUtils.readFile(testFile).toString(),mergedSource)
GLOBAL._loadDynamic = true;
//if main runs without error OK
main = require(path.resolve(testFile));
} catch (error) {
me = error;
test.fail("result file should run without throwing errors");
}
GLOBAL._loadDynamic = false;
//if main runs without error OK
main = require(path.resolve(testFile));
return test.done();
};
exports.testJsonImport = function(test) {
var me, mergedSource, nodeUglifier, testFile;
testFile = "lib_compiled/test/resultFiles/testJsonImport.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/testJsonImport/main.js", {
rngSeed: "hello"
});
mergedSource = nodeUglifier.merge().toString();
try {
eval(mergedSource);
} catch (error) {
me = error;
test.fail(me.toString(), "expected no error thrown from combined project");
}
if (IS_RE_CREATE_TEST_FILES) {
nodeUglifier.exportToFile(testFile);
} else {
test.equals(packageUtils.readFile(testFile).toString(), mergedSource);
}
return test.done();
};
exports.testEs6 = function(test) {
var me, mergedSource, nodeUglifier, testFile;
testFile = "lib_compiled/test/resultFiles/es6proj.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/es6proj/main.js", {
rngSeed: "hello"
});
mergedSource = nodeUglifier.merge().uglify().toString();
try {
eval(mergedSource);
} catch (error) {
me = error;
test.fail(me.toString(), "expected no error thrown from combined project");
}
if (IS_RE_CREATE_TEST_FILES) {
nodeUglifier.exportToFile(testFile);
} else {
test.equals(packageUtils.readFile(testFile).toString(), mergedSource);
}
return test.done();
};
exports.testStuff = function(test) {
var t0, t0_2, t1;
t0 = "./test/test2";
t0_2 = "onderscore";
t1 = packageUtils.hexifyString(t0_2);
console.log(t1);
console.log("\n");
return test.done();
};
relativeToDir = function(dir) {
return path.relative(__dirname, dir);
};
exports.testPackageUtils = function(test) {
var shouldBeResult1, shouldBeResult2;
test.deepEqual(packageUtils.getMatchingFiles("lib_compiled/test/testproject/main.js", []), []);
shouldBeResult1 = [`testproject${SEPARATOR}depa${SEPARATOR}constants.js`, `testproject${SEPARATOR}depa${SEPARATOR}constants.js.map`];
// console.log(packageUtils.getMatchingFiles("lib_compiled/test/testproject/main.js",["./depa/"])) #["main","./depa/","./depb/cryptoLoc.js","./depb/depDeep/deepModule"]
test.deepEqual(packageUtils.getMatchingFiles("lib_compiled/test/testproject/", ["./depa/"]).map(relativeToDir), shouldBeResult1);
test.deepEqual(packageUtils.getMatchingFiles("lib_compiled/test/testproject", ["./depa/"]).map(relativeToDir), shouldBeResult1);
test.deepEqual(packageUtils.getMatchingFiles("lib_compiled/test/testproject/main.js", ["./depa/"]).map(relativeToDir), shouldBeResult1);
shouldBeResult2 = [`testproject${SEPARATOR}main${SEPARATOR}main.js`, `testproject${SEPARATOR}main${SEPARATOR}main.js.map`, `testproject${SEPARATOR}depb${SEPARATOR}cryptoLoc.js`, `testproject${SEPARATOR}depb${SEPARATOR}depDeep${SEPARATOR}deepModule${SEPARATOR}deepModule.js`, `testproject${SEPARATOR}depb${SEPARATOR}depDeep${SEPARATOR}deepModule${SEPARATOR}deepModule.js.map`];
// console.log(packageUtils.getMatchingFiles("lib_compiled/test/testproject/main.js",["main","./depb/cryptoLoc.js","./depb/depDeep/deepModule"]))
test.deepEqual(packageUtils.getMatchingFiles("lib_compiled/test/testproject/main.js", ["main", "./depb/cryptoLoc.js", "./depb/depDeep/deepModule"]).map(relativeToDir), shouldBeResult2);
return test.done();
};
exports.testDependenciesExport = function(test) {
var constantsAfterSeparation, exportDir, me, nodeUglifier;
exportDir = "lib_test_project_export/";
nodeUglifier = new NodeUglifier("lib_compiled/test/testproject/main.js", {
rngSeed: "hello"
});
nodeUglifier.exportDependencies(exportDir, {
"coffee": {
"src": "lib_compiled"
}
});
test.ok(fsExtra.existsSync(path.resolve(exportDir)));
test.ok(fsExtra.existsSync(path.resolve(exportDir + "/src")));
test.ok(fsExtra.existsSync(path.resolve(exportDir + "/lib_compiled")));
try {
constantsAfterSeparation = require(path.resolve(exportDir) + "/lib_compiled/test/testproject/depa/constants.js");
} catch (error) {
me = error;
test.fail("the new constants file should be proper requireable js");
}
return test.done();
};
testMerge = function(test) {
var me, mergedSource, nodeUglifier, testFile;
testFile = "lib_compiled/test/resultFiles/simpleMerge.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/testproject/main.js", {
rngSeed: "hello"
});
mergedSource = nodeUglifier.merge().toString();
try {
eval(mergedSource);
} catch (error) {
me = error;
test.fail(me.toString(), "expected no error thrown from combined project");
}
nodeUglifier.exportToFile(testFile);
//dont test equality, heavily coffeescript version dependent due to comments not removed
// test.equals(packageUtils.readFile(testFile).toString(),mergedSource)
return test.done();
};
exports.testMergeWithExportFilter = function(test) {
var main, me, mergedSource, nodeUglifier, testFile;
testFile = "lib_compiled/test/resultFiles/simpleMergeWithFilter.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/testproject/main.js", {
rngSeed: "hello",
mergeFileFilterWithExport: ["./lib_static/test/", "./depa/constants.js"]
});
mergedSource = nodeUglifier.merge().toString();
nodeUglifier.exportToFile(testFile);
try {
//dont test equality, heavily coffeescript version dependent due to comments not removed
// test.equals(packageUtils.readFile(testFile).toString(),mergedSource)
GLOBAL._loadDynamic = false;
//if main runs without error OK
main = require(path.resolve(testFile));
} catch (error) {
me = error;
test.fail("result file should run without throwing errors");
}
GLOBAL._loadDynamic = false;
//if main runs without error OK
main = require(path.resolve(testFile));
return test.done();
};
exports.testMergeWithFilterAndUglify = function(test) {
var main, me, mergedSource, nodeUglifier, testFile, uglifySourceMap;
testFile = "lib_compiled/test/resultFiles/simpleMergeWithFilterAndUglify.js";
uglifySourceMap = "lib_compiled/test/resultFiles/sourcemaps/simpleMergeWithFilterAndUglify.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/testproject/main.js", {
rngSeed: "hello",
mergeFileFilterWithExport: ["./lib_static/test/", "./depa/constants.js"]
});
mergedSource = nodeUglifier.merge().uglify().toString();
if (IS_RE_CREATE_TEST_FILES) {
nodeUglifier.exportToFile(testFile);
} else {
test.equals(packageUtils.readFile(testFile).toString(), mergedSource);
}
nodeUglifier.exportSourceMaps(uglifySourceMap);
try {
GLOBAL._loadDynamic = false;
//if main runs without error OK
main = require(path.resolve(testFile));
} catch (error) {
me = error;
test.fail("result file should run without throwing errors");
}
return test.done();
};
exports.testMergeWithFilterAndUglifyAndStrProtection = function(test) {
var main, me, mergedSource, nodeUglifier, testFile, uglifySourceMap;
testFile = "lib_compiled/test/resultFiles/simpleMergeWithFilterAndUglifyAndStrProtection.js";
uglifySourceMap = "lib_compiled/test/resultFiles/sourcemaps/simpleMergeWithFilterAndUglifyAndStrProtection.js";
nodeUglifier = new NodeUglifier("lib_compiled/test/testproject/main.js", {
rngSeed: "hello",
mergeFileFilterWithExport: ["./lib_static/test/", "./depa/constants.js"]
});
mergedSource = nodeUglifier.merge().uglify({
strProtectionLvl: 1
}).toString();
if (IS_RE_CREATE_TEST_FILES) {
nodeUglifier.exportToFile(testFile);
} else {
nodeUglifier.exportSourceMaps(uglifySourceMap);
}
try {
GLOBAL._loadDynamic = false;
//if main runs without error OK
main = require(path.resolve(testFile));
} catch (error) {
me = error;
test.fail("result file should run without throwing errors");
}
// try
// eval(mergedSource)
// catch me
// test.fail(me.toString(),"expected no error thrown from combined project")
test.equals(packageUtils.readFile(testFile).toString(), mergedSource);
return test.done();
};
}).call(this);
//# sourceMappingURL=unitTest.js.map