Skip to content

Instantly share code, notes, and snippets.

@efeminella
Created December 16, 2012 01:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efeminella/4301937 to your computer and use it in GitHub Desktop.
Save efeminella/4301937 to your computer and use it in GitHub Desktop.
Simple node program for js/css minification using yuicompressor
/*
* basic node program for js/css minification using yuicompressor
*
* Compress the file test.js:
* $ node compress.js ../js/test.js
* Created Minified File: ../js/test.min.js
*
* Compress the file test.css
* $ node compress.js ../css/test.css
* Created Minified File: ../css/test.min.css
*
*/
var min = require('yuicompressor');
var fs = require('fs');
var _minify = function(name, ext, content) {
min.compress( content, {
'charset' :'utf8'
, 'type' : ext
, 'nomunge' : true
}, function(err, data, extra) {
_writeFile(name.replace('.' + ext, '.min.' + ext), data)
});
};
var _writeFile = function(name, content) {
fs.writeFile(name, content, function (err) {
if (!err) {
console.log('Created Minified File: ' + name);
} else {
throw err;
}
});
};
var _readFile = function(options) {
fs.readFile(options.name, 'utf8', function(err, source) {
if (!err) {
_minify(options.name, options.ext, source);
} else {
throw err;
}
});
};
var arguments = function() {
var args = process.argv.splice(2);
return {
'name' : args[0]
, 'ext' : args.length > 1 ? args[1] : 'js'
}
};
_readFile(arguments());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment