From f7b614dd0ec9be3e6d9ec1ed5b4113217b92408e Mon Sep 17 00:00:00 2001 From: Lucas Nolte Date: Fri, 27 Apr 2012 14:58:56 +0200 Subject: [PATCH 1/3] Added function to automatically concatenate files --- Readme.md | 6 ++++++ examples/public/js/base2.js | 9 +++++++++ examples/server.js | 26 ++++---------------------- lib/node-minify.js | 28 ++++++++++++++++++++++++---- 4 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 examples/public/js/base2.js diff --git a/Readme.md b/Readme.md index 9636946bd..39630a004 100644 --- a/Readme.md +++ b/Readme.md @@ -54,6 +54,12 @@ console.log(err); } }); + +## Cocatenate Files + +In order to concatenate files, simply pass in an array with the file paths to fileIn. + + fileIn: ['public/js/base.js', 'public/js/base2.js', ...] ## YUI Compressor diff --git a/examples/public/js/base2.js b/examples/public/js/base2.js new file mode 100644 index 000000000..bbf4e378f --- /dev/null +++ b/examples/public/js/base2.js @@ -0,0 +1,9 @@ +/* + Lots of comments + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ut lorem sem. + Vestibulum vehicula dolor ac elit dictum convallis. Vivamus rhoncus, neque id euismod tempor, justo nulla pellentesque nibh, + nec placerat mauris massa vel massa. Etiam cursus rutrum faucibus. + Mauris sem turpis, lacinia eget faucibus vel, vulputate et ante. + */ + +someOtherNiftyCode="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; \ No newline at end of file diff --git a/examples/server.js b/examples/server.js index a09cccbc8..f368df414 100644 --- a/examples/server.js +++ b/examples/server.js @@ -9,34 +9,16 @@ http.createServer(function (req, res) { new compressor.minify({ type: 'gcc', fileIn: 'public/js/base.js', - fileOut: 'public/js/base-min-gcc.js', + fileOut: 'public/js/base-onefile-gcc.js', callback: function(err){ console.log(err); } }); new compressor.minify({ - type: 'yui', - fileIn: './public/js/base.js', - fileOut: './public/js/base-min-yui.js', - callback: function(err){ - console.log(err); - } -}); - -new compressor.minify({ - type: 'uglifyjs', - fileIn: './public/js/base.js', - fileOut: './public/js/base-min-uglifyjs.js', - callback: function(err){ - console.log(err); - } -}); - -new compressor.minify({ - type: 'yui', - fileIn: './public/css/base.css', - fileOut: './public/css/base-min.css', + type: 'gcc', + fileIn: ['public/js/base.js', 'public/js/base2.js'], + fileOut: 'public/js/base-concat-gcc.js', callback: function(err){ console.log(err); } diff --git a/lib/node-minify.js b/lib/node-minify.js index e6e165654..b945a4190 100644 --- a/lib/node-minify.js +++ b/lib/node-minify.js @@ -1,8 +1,23 @@ -var exec = require('child_process').exec; +var exec = require('child_process').exec, + _fs = require('fs'); var minify = function(options) { this.type = options.type; - this.fileIn = options.fileIn; + + if(typeof options.fileIn === 'string') { + this.fileIn = options.fileIn; + } + + if(typeof options.fileIn === 'object') { + var out = options.fileIn.map(function(path) { + return _fs.readFileSync(path, 'utf8'); + }); + + _fs.writeFileSync('concatFile', out.join('\n'), 'utf8'); + + this.fileIn = 'concatFile'; + } + this.fileOut = options.fileOut; this.options = options.options || []; if (typeof options.callback !== 'undefined') { @@ -33,12 +48,17 @@ minify.prototype.compress = function() { command = __dirname + '/../node_modules/uglify-js/bin/uglifyjs --output ' + this.fileOut + ' --no-copyright ' + this.fileIn + ' ' + this.options.join(' '); break; } - + exec(command, function (err, stdout, stderr) { //console.log(err); //console.log(stdout); //console.log(stderr); - + + if(self.fileIn === 'concatFile') { + // remove the temp concat file here + _fs.unlinkSync('concatFile'); + } + if(self.callback){ if (err) { self.callback(err); From ca9551dc916f13f99702ffb60b3387e8fcf65a1c Mon Sep 17 00:00:00 2001 From: Lucas Nolte Date: Mon, 30 Apr 2012 20:12:55 +0200 Subject: [PATCH 2/3] Added Buffer option --- lib/node-minify.js | 127 ++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 59 deletions(-) diff --git a/lib/node-minify.js b/lib/node-minify.js index b945a4190..011605d07 100644 --- a/lib/node-minify.js +++ b/lib/node-minify.js @@ -1,73 +1,82 @@ -var exec = require('child_process').exec, - _fs = require('fs'); +var minify = (function(undefined) { -var minify = function(options) { - this.type = options.type; - - if(typeof options.fileIn === 'string') { - this.fileIn = options.fileIn; - } + var exec = require('child_process').exec, + _fs = require('fs'); + + var minify = function(options) { + this.type = options.type; + this.tempFile = new Date().getTime().toString(); + + if(typeof options.fileIn === 'string') { + this.fileIn = options.fileIn; + } - if(typeof options.fileIn === 'object') { - var out = options.fileIn.map(function(path) { - return _fs.readFileSync(path, 'utf8'); - }); + if(typeof options.fileIn === 'object' && options.fileIn instanceof Array) { + var out = options.fileIn.map(function(path) { + return _fs.readFileSync(path, 'utf8'); + }); - _fs.writeFileSync('concatFile', out.join('\n'), 'utf8'); + _fs.writeFileSync(this.tempFile, out.join('\n'), 'utf8'); - this.fileIn = 'concatFile'; - } + this.fileIn = this.tempFile; + } - this.fileOut = options.fileOut; - this.options = options.options || []; - if (typeof options.callback !== 'undefined') { - this.callback = options.callback; - } - - this.compress(); -}; + this.fileOut = options.fileOut; + this.options = options.options || []; + this.buffer = options.buffer || 1000 * 1024; + if (typeof options.callback !== 'undefined') { + this.callback = options.callback; + } -minify.prototype = { - type: null, - fileIn: null, - fileOut: null, - callback: null -}; + this.compress(); + }; -minify.prototype.compress = function() { - var self = this, command; + minify.prototype = minify.fn = { + type: null, + fileIn: null, + fileOut: null, + callback: null, + buffer: null, // with larger files you will need a bigger buffer for closure compiler + compress: function() { + var self = this, command; - switch (this.type) { - case 'yui': - command = 'java -jar -Xss2048k ' + __dirname + '/yuicompressor-2.4.7.jar ' + this.fileIn + ' -o ' + this.fileOut + ' ' + this.options.join(' '); - break; - case 'gcc': - command = 'java -jar ' + __dirname + '/google_closure_compiler-r1810.jar --js=' + this.fileIn + ' --js_output_file=' + this.fileOut + ' ' + this.options.join(' '); - break; - case 'uglifyjs': - command = __dirname + '/../node_modules/uglify-js/bin/uglifyjs --output ' + this.fileOut + ' --no-copyright ' + this.fileIn + ' ' + this.options.join(' '); - break; - } - exec(command, function (err, stdout, stderr) { - //console.log(err); - //console.log(stdout); - //console.log(stderr); + switch (this.type) { + case 'yui': + command = 'java -jar -Xss2048k ' + __dirname + '/yuicompressor-2.4.7.jar ' + this.fileIn + ' -o ' + this.fileOut + ' --type css ' + this.options.join(' '); + break; + case 'gcc': + command = 'java -jar ' + __dirname + '/google_closure_compiler-r1810.jar --js=' + this.fileIn + ' --js_output_file=' + this.fileOut + ' ' + this.options.join(' '); + break; + case 'uglifyjs': + command = __dirname + '/../node_modules/uglify-js/bin/uglifyjs --output ' + this.fileOut + ' --no-copyright ' + this.fileIn + ' ' + this.options.join(' '); + break; + } + + exec(command, { maxBuffer: this.buffer }, function (err, stdout, stderr) { + //console.log(err); + //console.log(stdout); + //console.log(stderr); - if(self.fileIn === 'concatFile') { - // remove the temp concat file here - _fs.unlinkSync('concatFile'); - } + if(self.fileIn === self.tempFile) { + // remove the temp concat file here + _fs.unlinkSync(self.tempFile); + } - if(self.callback){ - if (err) { - self.callback(err); - } else { - self.callback(null); - } + if(self.callback){ + if (err) { + self.callback(err); + } else { + self.callback(null); + } + } + }); } - }); -}; + }; + + return minify; + +})(); exports.version = '0.3.6'; -exports.minify = minify; +exports.minify = minify; \ No newline at end of file From 94b5a56b9cbbf7ceeec847eee4c1c1b7dd551d79 Mon Sep 17 00:00:00 2001 From: Lucas Nolte Date: Mon, 30 Apr 2012 21:19:24 +0300 Subject: [PATCH 3/3] Update Readme.md --- Readme.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 39630a004..179a7744b 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,3 @@ - # Node-minify A very light minifier NodeJS module. @@ -60,6 +59,23 @@ In order to concatenate files, simply pass in an array with the file paths to fileIn. fileIn: ['public/js/base.js', 'public/js/base2.js', ...] + + +## Max Buffer Size + +In some cases you might need a bigger max buffer size (for example when minifying really large files). +By default the buffer is `1000 * 1024` which should be enough. If you however need more buffer, you can simply pass in the +desired buffer size as an argument to compressor.minify like so: + + new compressor.minify({ + type: 'uglifyjs', + fileIn: './public/css/base.css', + fileOut: './public/css/base-min-uglifyjs.css', + buffer: 1000 * 1024, + callback: function(err){ + console.log(err); + } + }); ## YUI Compressor