Skip to content

Instantly share code, notes, and snippets.

@matthewhudson
Last active December 12, 2015 03:59
Show Gist options
  • Save matthewhudson/4711219 to your computer and use it in GitHub Desktop.
Save matthewhudson/4711219 to your computer and use it in GitHub Desktop.
A pre-commit hook to minify JavaScript files and add them to staging.
#!/usr/bin/env node
var fs = require('fs');
var uglify = require('uglify-js');
var path = require('path');
var exec = require('child_process').exec;
var in_file = path.normalize(__dirname + '../../../infile.js');
var out_file = path.normalize(__dirname + '../../../outfile.min.js');
var result = uglify.minify(in_file);
fs.writeFile(out_file, result.code, function (err) {
if (err) return console.warn(err);
console.log('Successfully created ' + out_file);
});
exec('git add ' + out_file, function (err, stdout, stderr) {
if (err) return console.warn(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment