Permalink
Browse files

Adding a way to pass options to uglify

  • Loading branch information...
1 parent d98ec8a commit 5984f67fc7be122161139f39d179d1266ae0afd6 @oliverjohnstone oliverjohnstone committed Jun 19, 2012
Showing with 51 additions and 13 deletions.
  1. +40 −13 lib/compact.js
  2. +5 −0 test/assets/a.jade
  3. +6 −0 test/compact.test.js
View
@@ -7,12 +7,10 @@ var path = require('path')
, uglifyer = require('uglify-js').uglify
, crypto = require('crypto');
-module.exports.createCompact = function(options) {
-
+module.exports.createCompact = function(options, globalUglifyOptions) {
options = _.extend({
webPath: '',
debug: false,
- safeCompression: false // Added By Oliver TODO: Remove comment before push
}, options);
if (!path.existsSync(options.srcPath)) {
@@ -56,7 +54,7 @@ module.exports.createCompact = function(options) {
});
var namespace = namespaces[name];
- function addJs(filePath, safe) {
+ function addJs(filePath) {
if (!namespace.javascriptFiles) {
namespace.javascriptFiles = [];
}
@@ -78,7 +76,6 @@ module.exports.createCompact = function(options) {
if (jsPath === undefined) {
throw new Error('Unable to find \'' + filePath + '\'');
}
-
namespace.javascriptFiles.push(jsPath);
return namespace;
@@ -105,12 +102,28 @@ module.exports.createCompact = function(options) {
if (!namespaces[namespace]) {
throw new Error('Unknown namespace \'' + namespace + '\'. Ensure you provide a namespace that has been defined with \'addNamespace()\'');
}
+ /*namespaces[namespace].javascriptFiles.forEach(function(obj) {
+ files.push(obj.path);
+ });*/
files = files.concat(namespaces[namespace].javascriptFiles);
});
return _.uniq(files);
}
+ /*function getJavaScriptFileObjectsFromNamespaces(targetNamespaces) {
+ var objs = [];
+ targetNamespaces.forEach(function(namespace) {
+ if(!namespaces[namespace]) {
+ throw new Error('Unknown namespace \'' + namespace + '\'. Ensure you provide a namespace that has been defined with \'addNamespace()\'');
+ }
+ namespaces[namespace].javascriptFiles.forEach(function(obj) {
+ objs.push(obj);
+ });
+ });
+ return objs;
+ }*/
+
function copyJavaScript(targetNamespaces, callback) {
var files = [];
try {
@@ -123,14 +136,15 @@ module.exports.createCompact = function(options) {
});
}
- function compressAndWriteJavascript(targetNamespaces, callback) {
+ function compressAndWriteJavascript(targetNamespaces, callback, uglifyOptions) {
var compressedData = ''
, files
, compactFilename = targetNamespaces.map(function(namespace) {
return namespace;
}).join('-') + '.js'
, outputFilename = options.destPath + '/' + compactFilename
- , compactedWebPath = path.normalize(options.webPath + '/' + compactFilename);
+ , compactedWebPath = path.normalize(options.webPath + '/' + compactFilename)
+ , objs = [];
// Only compress and write 'compactFilename' once
if (compressOperationCache[compactFilename]) {
@@ -149,7 +163,7 @@ module.exports.createCompact = function(options) {
return callback(error);
}
- fs.writeFile(outputFilename, compress(contents.join(';\n')), 'utf-8', function(error) {
+ fs.writeFile(outputFilename, compress(contents.join(';\n'), uglifyOptions), 'utf-8', function(error) {
if (error) {
return callback(error);
}
@@ -160,11 +174,23 @@ module.exports.createCompact = function(options) {
});
}
- function compress(data) {
+ function compress(data, options) {
var ast = parser.parse(data);
- ast = uglifyer.ast_mangle(ast);
- ast = uglifyer.ast_squeeze(ast);
- return uglifyer.gen_code(ast);
+ if (typeof options !== 'undefined') {
+ ast = uglifyer.ast_mangle(ast, options);
+ ast = uglifyer.ast_squeeze(ast, options);
+ return uglifyer.gen_code(ast, options);
+ } else {
+ if (this.globalUglifyOptions !== 'undefined') {
+ ast = uglifyer.ast_mangle(ast, this.globalUglifyOptions);
+ ast = uglifyer.ast_squeeze(ast, this.globalUglifyOptions);
+ return uglifyer.gen_code(ast, this.globalUglifyOptions);
+ } else {
+ ast = uglifyer.ast_mangle(ast);
+ ast = uglifyer.ast_squeeze(ast);
+ return uglifyer.gen_code(ast);
+ }
+ }
}
function processNamespaceGroups(namespaceGroups, callback) {
@@ -223,6 +249,7 @@ module.exports.createCompact = function(options) {
return {
addNamespace: addNamespace,
js: compactJavascript,
- ns: namespaces
+ ns: namespaces,
+ globalUglifyOptions: globalUglifyOptions
};
};
View
@@ -0,0 +1,5 @@
+!!!5
+head
+ title=hello world
+ body
+ h1 hello world
@@ -86,6 +86,12 @@ describe('compact.js', function() {
});
});
+ describe('#addJadeWithSafeCompression', function() {
+ it('should succeed with in && out files === same data', function() {
+ namespace.addJs('a.jade', { mangle: false, no_mangle_functions: true });
+ });
+ });
+
describe('#addNamespace()', function () {
it('should not allow a namespace to be added more than once', function () {
(function () {

0 comments on commit 5984f67

Please sign in to comment.