Created
February 7, 2013 15:32
-
-
Save jgable/4731656 to your computer and use it in GitHub Desktop.
Grunt gzip task
This file contains hidden or 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
var fs = require("fs"), | |
path = require("path"); | |
var common = require("./lib/common"); | |
module.exports = function(grunt) { | |
var Ss = grunt.config("Ss"), | |
_ = grunt.util._, | |
async = grunt.util.async; | |
grunt.registerMultiTask("gzip", function() { | |
var done = this.async(), | |
// Get the options for the destination directory | |
opts = this.options({batchLimit: 10}), | |
// Get the list of files to gzip and copy | |
files = this.filesSrc; | |
// Build up a list of source -> dest mappings | |
var mappings = _.map(files, function(file) { | |
// Destination is the destination directory + whatever directory the file is in (css or js) + the file name. | |
var destDir = path.basename(path.dirname(file)), | |
destFile = path.basename(file), | |
dest = path.join(opts.dest, destDir, destFile); | |
return { | |
src: file, | |
dest: dest | |
}; | |
}); | |
// A little wrapper for calling common.compressFile with the proper this context. | |
var gzipFile = function(mapping, cb) { | |
common.compressFile(mapping.src, mapping.dest, cb); | |
}; | |
// Run the common.compressFileMapping for each mapping, with a limit of 10 by default. | |
async.forEachLimit(mappings, opts.batchLimit, gzipFile, function(err, destFilePaths) { | |
if(err) { | |
grunt.fatal("Error during gzip: " + err.message); | |
return done(false); | |
} | |
done(true); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment