Created
March 9, 2012 20:21
-
-
Save efeminella/2008471 to your computer and use it in GitHub Desktop.
Simple boilerplate Node Minification with uglify-js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Simple boilerplate Node Minification build for uglify-js | |
* | |
* Install: npm install uglify-js; cd to the this script's dir | |
* $ node <script-name>.js | |
*/ | |
var parser = require( 'uglify-js' ).parser | |
, uglify = require( 'uglify-js' ).uglify | |
, path = require('path') | |
, fs = require( 'fs' ) | |
, src = 'path/to/your/src-file' | |
, built = 'path/to/create/your/src-file-min.js'; | |
var _read = function() { | |
console.log('Loading ' + src); | |
fs.readFile( src, 'utf8', function (error, data) { | |
if (error) { | |
return console.log('Error' + error); | |
} | |
_minify(data); | |
}); | |
}; | |
var _minify = function( source ) { | |
var ast = uglify.ast_squeeze( uglify.ast_mangle( parser.parse( source ) ) ); | |
_write( uglify.gen_code( ast ) ); | |
}; | |
var _write = function(min) { | |
fs.writeFile(built, min, function(error) { | |
if (error) { | |
return console.log( 'Error' + error); | |
} | |
console.log('Minified ' + built + '\n' + min); | |
}); | |
}; | |
_read(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment