Last active
December 25, 2015 05:59
-
-
Save mach3/6928970 to your computer and use it in GitHub Desktop.
更新時刻を保持するpngquantタスク
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
/** | |
* Grunt task for pngquant | |
* ----------------------- | |
*/ | |
// @example: | |
// | |
// pngquant: { | |
// options: { | |
// preserve_mtime: true | |
// }, | |
// dist: { | |
// src: [ | |
// "the/path/to/*.png", | |
// "the/path/to/**/*.png" | |
// ] | |
// } | |
// } | |
module.exports = function(grunt){ | |
grunt.registerMultiTask("pngquant", "", function(){ | |
var my = {}, fs, cp, done; | |
fs = require("fs"), | |
cp = require("child_process"); | |
done = this.async(); | |
/** | |
* Options: | |
* - preserve_mtime:Boolean ... Preserve modified time or not | |
* - pngquant:String ... Template string for pngquant command | |
* - touch:String ... Tempalte string for touch command | |
* - verbose:Boolean ... Verbose mode | |
* - separate:Number ... Separate the process by count | |
*/ | |
my.options = this.options({ | |
preserve_mtime: true, | |
pngquant: 'pngquant <%=file %> --iebug --ext .png --force', | |
touch: 'touch -d "<%=date %>" <%=file %>', | |
verbose: true, | |
separate: 8 | |
}); | |
/** | |
* Get file list | |
*/ | |
my.count = 0; | |
my.files = (function(files){ | |
var list = [], res = [], i = 0, tmp; | |
files.forEach(function(o){ | |
list = list.concat(o.src); | |
}); | |
my.count = list.length; | |
while(i<=list.length){ | |
tmp = list.slice(i, i+my.options.separate); | |
if(tmp.length){ | |
res.push(tmp); | |
} | |
// res.push(list.slice(i, i+my.options.separate)); | |
i+=my.options.separate; | |
} | |
return res; | |
}(this.files)); | |
my.length = my.files.length; | |
/** | |
* Get modified time of file | |
* - If `preserve_mtime` is false, return null | |
* @param String file | |
*/ | |
my.getMTime= function(file){ | |
if(! this.options.preserve_mtime){ return null; } | |
var date = new Date(fs.statSync(file).mtime); | |
return grunt.template.date(date, "yyyy/mm/dd HH:MM:ss"); | |
}; | |
/** | |
* Optimize image file by pngquant | |
* @param {String} file | |
* @param {Function} callback | |
*/ | |
my.optimize = function(file, callback){ | |
var mtime, pngquant, touch; | |
mtime = my.getMTime(file); | |
pngquant = grunt.template.process(my.options.pngquant, { | |
data: { file: file } | |
}); | |
touch = mtime ? grunt.template.process(my.options.touch, { | |
data: { | |
file: file, | |
date: mtime | |
} | |
}) : ""; | |
cp.exec(pngquant, function(e, out, error){ | |
if(e){ | |
return grunt.log.error(error); | |
} | |
mtime && cp.exec(touch); | |
callback(); | |
}); | |
}; | |
/** | |
* Run process | |
*/ | |
my.process = function(){ | |
var files, count; | |
if(! my.files.length){ | |
grunt.log.writeln("All images optimized."); | |
return done(); | |
} | |
files = my.files.pop(); | |
count = files.length; | |
files.forEach(function(file){ | |
my.optimize(file, function(){ | |
count -= 1; | |
! count && my.process(); | |
}); | |
}); | |
if(my.options.verbose){ | |
var percent = 100 - parseInt(my.files.length / my.length * 100, 10); | |
grunt.log.writeln( | |
grunt.template.process( | |
"<%=percent %>% of <%=total %> files", | |
{data: {percent: percent, total: my.count}} | |
) | |
); | |
} | |
}; | |
my.process(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment