Skip to content

Commit

Permalink
Adding a way to pass options to uglify
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Johnstone committed Jun 19, 2012
1 parent d98ec8a commit 5984f67
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
53 changes: 40 additions & 13 deletions lib/compact.js
Expand Up @@ -7,12 +7,10 @@ var path = require('path')
, uglifyer = require('uglify-js').uglify , uglifyer = require('uglify-js').uglify
, crypto = require('crypto'); , crypto = require('crypto');


module.exports.createCompact = function(options) { module.exports.createCompact = function(options, globalUglifyOptions) {

options = _.extend({ options = _.extend({
webPath: '', webPath: '',
debug: false, debug: false,
safeCompression: false // Added By Oliver TODO: Remove comment before push
}, options); }, options);


if (!path.existsSync(options.srcPath)) { if (!path.existsSync(options.srcPath)) {
Expand Down Expand Up @@ -56,7 +54,7 @@ module.exports.createCompact = function(options) {
}); });
var namespace = namespaces[name]; var namespace = namespaces[name];


function addJs(filePath, safe) { function addJs(filePath) {
if (!namespace.javascriptFiles) { if (!namespace.javascriptFiles) {
namespace.javascriptFiles = []; namespace.javascriptFiles = [];
} }
Expand All @@ -78,7 +76,6 @@ module.exports.createCompact = function(options) {
if (jsPath === undefined) { if (jsPath === undefined) {
throw new Error('Unable to find \'' + filePath + '\''); throw new Error('Unable to find \'' + filePath + '\'');
} }

namespace.javascriptFiles.push(jsPath); namespace.javascriptFiles.push(jsPath);


return namespace; return namespace;
Expand All @@ -105,12 +102,28 @@ module.exports.createCompact = function(options) {
if (!namespaces[namespace]) { if (!namespaces[namespace]) {
throw new Error('Unknown namespace \'' + namespace + '\'. Ensure you provide a namespace that has been defined with \'addNamespace()\''); 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); files = files.concat(namespaces[namespace].javascriptFiles);
}); });


return _.uniq(files); 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) { function copyJavaScript(targetNamespaces, callback) {
var files = []; var files = [];
try { try {
Expand All @@ -123,14 +136,15 @@ module.exports.createCompact = function(options) {
}); });
} }


function compressAndWriteJavascript(targetNamespaces, callback) { function compressAndWriteJavascript(targetNamespaces, callback, uglifyOptions) {
var compressedData = '' var compressedData = ''
, files , files
, compactFilename = targetNamespaces.map(function(namespace) { , compactFilename = targetNamespaces.map(function(namespace) {
return namespace; return namespace;
}).join('-') + '.js' }).join('-') + '.js'
, outputFilename = options.destPath + '/' + compactFilename , outputFilename = options.destPath + '/' + compactFilename
, compactedWebPath = path.normalize(options.webPath + '/' + compactFilename); , compactedWebPath = path.normalize(options.webPath + '/' + compactFilename)
, objs = [];


// Only compress and write 'compactFilename' once // Only compress and write 'compactFilename' once
if (compressOperationCache[compactFilename]) { if (compressOperationCache[compactFilename]) {
Expand All @@ -149,7 +163,7 @@ module.exports.createCompact = function(options) {
return callback(error); 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) { if (error) {
return callback(error); return callback(error);
} }
Expand All @@ -160,11 +174,23 @@ module.exports.createCompact = function(options) {
}); });
} }


function compress(data) { function compress(data, options) {
var ast = parser.parse(data); var ast = parser.parse(data);
ast = uglifyer.ast_mangle(ast); if (typeof options !== 'undefined') {
ast = uglifyer.ast_squeeze(ast); ast = uglifyer.ast_mangle(ast, options);
return uglifyer.gen_code(ast); 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) { function processNamespaceGroups(namespaceGroups, callback) {
Expand Down Expand Up @@ -223,6 +249,7 @@ module.exports.createCompact = function(options) {
return { return {
addNamespace: addNamespace, addNamespace: addNamespace,
js: compactJavascript, js: compactJavascript,
ns: namespaces ns: namespaces,
globalUglifyOptions: globalUglifyOptions
}; };
}; };
5 changes: 5 additions & 0 deletions test/assets/a.jade
@@ -0,0 +1,5 @@
!!!5
head
title=hello world
body
h1 hello world
6 changes: 6 additions & 0 deletions test/compact.test.js
Expand Up @@ -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 () { describe('#addNamespace()', function () {
it('should not allow a namespace to be added more than once', function () { it('should not allow a namespace to be added more than once', function () {
(function () { (function () {
Expand Down

0 comments on commit 5984f67

Please sign in to comment.