Skip to content

Commit

Permalink
Merge pull request #3 from lnolte/master
Browse files Browse the repository at this point in the history
Automatically concat files
  • Loading branch information
srod committed May 2, 2012
2 parents b182829 + 94b5a56 commit 6bbacb9
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 67 deletions.
24 changes: 23 additions & 1 deletion Readme.md
@@ -1,4 +1,3 @@

# Node-minify
A very light minifier NodeJS module.
Expand Down Expand Up @@ -54,6 +53,29 @@
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', ...]


## 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

Expand Down
9 changes: 9 additions & 0 deletions 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.";
26 changes: 4 additions & 22 deletions examples/server.js
Expand Up @@ -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);
}
Expand Down
117 changes: 73 additions & 44 deletions lib/node-minify.js
@@ -1,53 +1,82 @@
var exec = require('child_process').exec;
var minify = (function(undefined) {

var minify = function(options) {
this.type = options.type;
this.fileIn = options.fileIn;
this.fileOut = options.fileOut;
this.options = options.options || [];
if (typeof options.callback !== 'undefined') {
this.callback = options.callback;
}
var exec = require('child_process').exec,
_fs = require('fs');

this.compress();
};

minify.prototype = {
type: null,
fileIn: null,
fileOut: null,
callback: null
};

minify.prototype.compress = function() {
var self = this, command;
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' && options.fileIn instanceof Array) {
var out = options.fileIn.map(function(path) {
return _fs.readFileSync(path, 'utf8');
});

_fs.writeFileSync(this.tempFile, out.join('\n'), 'utf8');

this.fileIn = this.tempFile;
}

this.fileOut = options.fileOut;
this.options = options.options || [];
this.buffer = options.buffer || 1000 * 1024;
if (typeof options.callback !== 'undefined') {
this.callback = options.callback;
}

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;
}
this.compress();
};

exec(command, function (err, stdout, stderr) {
//console.log(err);
//console.log(stdout);
//console.log(stderr);
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;

if(self.callback){
if (err) {
self.callback(err);
} else {
self.callback(null);

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 === self.tempFile) {
// remove the temp concat file here
_fs.unlinkSync(self.tempFile);
}

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;

0 comments on commit 6bbacb9

Please sign in to comment.