Created
June 11, 2013 01:19
-
-
Save taywils/5753879 to your computer and use it in GitHub Desktop.
A GruntFile.js for minifying CSS using Yahoo!'s yuicompressor and minifying JS using Google's Closure compiler. Note that the Javascript gets compiled to a single file while the minified css files remain separate.
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
module.exports = function(grunt) { | |
grunt.registerTask('compile-js', 'Runs GoogleClosure compiler on all javascript files', function() { | |
var exec = require('child_process').exec; | |
var done = grunt.task.current.async(); | |
var compilationLevel = "--compilation_level SIMPLE_OPTIMIZATIONS "; | |
var cmd = "java -jar ./build/google-closure-compiler.jar " + compilationLevel; | |
var jsDir = "./javascript/"; | |
var addJs = "--js " + jsDir; | |
var output = " --js_output_file ./build/deploy/javascript-compiled.js"; | |
// Javascript files must be in the same order as loaded in the html file | |
var jsFiles = [ | |
addJs + 'jquery-2.0.2.js' // The first line needs the addJs prefix | |
// Add other javascript files to this array | |
].join(" " + addJs); | |
var run = cmd + jsFiles + output; | |
grunt.log.writeln(run); | |
exec(run, function(err, stdout, stderr) { | |
if(stdout) console.log(stdout); | |
if(stderr) console.log("ERROR:" + stderr); | |
done(err); | |
}); | |
}); | |
grunt.registerTask('min-css', 'Minifies css using yuicompressor', function(){ | |
var exec = require('child_process').exec; | |
var done = grunt.task.current.async(); | |
var subCmd = "java -jar ./build/yuicompressor-2.4.7.jar "; | |
var output = " -o ./build/deploy/compiled."; | |
var minCss = ".min.css"; | |
var cssDir = "./css/"; | |
// Exclude the file extension from the name: property | |
var cssFiles = [ | |
{ | |
dir: cssDir, | |
name: "flatui" //Add the names of the CSS files without the .css extentsion | |
} | |
// Add an object for each css file to this array | |
]; | |
var cmd = ""; | |
cssFiles.forEach(function(val, idx, arr) { | |
cmd += subCmd + val.dir + val.name + ".css" + output + val.name + minCss + ";"; | |
}); | |
grunt.log.writeln(cmd); | |
exec(cmd, function (error, stdout, stderr) { | |
if(stdout) console.log(stdout); | |
if(stderr) console.log("ERROR:" + stderr); | |
done(error); | |
}); | |
}); | |
}; |
Oh and you need Java installed as well.... OpenJDK is fine if you hate Oracle that much lol
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forgot to mention you need to download both the YuiCompressor and Google Closure compiler executable jar files before using this GruntFile