Skip to content

Instantly share code, notes, and snippets.

@itsjustcon
Created May 15, 2013 22:24
Show Gist options
  • Save itsjustcon/5587923 to your computer and use it in GitHub Desktop.
Save itsjustcon/5587923 to your computer and use it in GitHub Desktop.
Packages UglifyJS into a nice, lightweight command-line tool for minifying JavaScript files.
// CREATED BY:
// Connor Grady on May 15, 2013
// http://connorgrady.com/
// USAGE:
// node minify myfile.js
// node minify file1.js file2.js file3.js
// OUTPUT:
// file1.js --> file1.min.js
var UglifyJS = require('uglify-js'),
fs = require('fs'),
path = require('path'),
Files = process.argv.splice(2);
Files.forEach(function (arg) {
// Check for .js
if (path.extname(arg) !== '.js')
throw 'ERROR: "'+arg+'" is not a .js file!';
// Get absolute path for input & output files
var iF = path.join(__dirname, arg),
oF = path.join(__dirname, arg.substring(0,arg.length-3) + '.min.js' ),
min = UglifyJS.minify(iF);
fs.writeFile(oF,min.code,function (err){
if (err) throw err;
console.log('MINIFIED: "'+path.basename(iF)+'" --> "'+path.basename(oF)+'"');
});
});
{
"name": "UglifyJS-Command-Line",
"description": "Packages UglifyJS into a nice, lightweight command-line tool for minifying JavaScript files.",
"version": "1.0",
"dependencies": {
"uglify-js": "2.2.*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment