Permalink
Browse files

Merge pull request #15 from oliverjohnstone/master

Added way of passing options to uglify.
  • Loading branch information...
2 parents c0ca3cd + 5984f67 commit 0446f9ae7abec9128aa456f0f6a4fd2945500c19 @serby committed Jun 26, 2012
Showing with 88 additions and 53 deletions.
  1. +40 −12 lib/compact.js
  2. +5 −0 test/assets/a.jade
  3. +43 −41 test/compact.test.js
View
@@ -7,11 +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
+ debug: false,
}, options);
if (!path.existsSync(options.srcPath)) {
@@ -77,7 +76,6 @@ module.exports.createCompact = function(options) {
if (jsPath === undefined) {
throw new Error('Unable to find \'' + filePath + '\'');
}
-
namespace.javascriptFiles.push(jsPath);
return namespace;
@@ -104,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 {
@@ -122,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]) {
@@ -148,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);
}
@@ -159,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) {
@@ -222,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
View
@@ -42,46 +42,6 @@ describe('compact.js', function() {
});
});
- describe('#addNamespace()', function() {
- var compact;
-
- beforeEach(function() {
- compact = require('../../compact').createCompact({ srcPath: srcPath, destPath: destPath });
- });
-
- it('should fail on null', function() {
- (function() {
- compact.addNamespace(null);
- }).should.throw('Invalid namespace');
- });
-
- it('should succeed with valid namespace', function() {
- compact.addNamespace('global').addJs.should.be.a('function');
- });
-
- it('should add a source path to the lookup chain when given', function () {
-
- compact.addNamespace('alternative', altPath);
-
- // Lookup item in added path
- (function () {
- compact.ns.alternative.addJs('d.js');
- }).should.not.throw();
-
- // Lookup item in default path
- (function () {
- compact.ns.alternative.addJs('a.js');
- }).should.not.throw();
-
- // Lookup item that doesn't exist in either path
- (function () {
- compact.ns.alternative.addJs('xyz.js');
- }).should.throw('Unable to find \'xyz.js\'');
-
- });
-
- });
-
describe('Namespace', function() {
var namespace
@@ -126,15 +86,57 @@ 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 () {
compact.addNamespace('foo');
compact.addNamespace('foo');
}).should.throw('The namespace \'foo\' has already been added');
});
- });
+
+ var compact;
+
+ beforeEach(function() {
+ compact = require('../../compact').createCompact({ srcPath: srcPath, destPath: destPath });
+ });
+
+ it('should fail on null', function() {
+ (function() {
+ compact.addNamespace(null);
+ }).should.throw('Invalid namespace');
+ });
+
+ it('should succeed with valid namespace', function() {
+ compact.addNamespace('global').addJs.should.be.a('function');
+ });
+ it('should add a source path to the lookup chain when given', function () {
+
+ compact.addNamespace('alternative', altPath);
+
+ // Lookup item in added path
+ (function () {
+ compact.ns.alternative.addJs('d.js');
+ }).should.not.throw();
+
+ // Lookup item in default path
+ (function () {
+ compact.ns.alternative.addJs('a.js');
+ }).should.not.throw();
+
+ // Lookup item that doesn't exist in either path
+ (function () {
+ compact.ns.alternative.addJs('xyz.js');
+ }).should.throw('Unable to find \'xyz.js\'');
+
+ });
+ });
});
describe('#js()', function() {

0 comments on commit 0446f9a

Please sign in to comment.