I started today getting used to grunt. I wanted to create a grunt package? script? module? task? which optimizes all images that will be uploaded to a certain folder (uploads/ … it’s a wordpress installation).
After some fiddling, I’ve create this small Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
imagemin: {
dynamic: {
files: [{
expand: true,
cwd: '../uploads',
src: ['**/*.{png,jpg,gif}'],
dest: '../uploads'
}]
}
},
watch: {
options: { nospawn: true},
scripts: {
files: ['../uploads/**/*.{png,jpg,gif}'],
tasks: ['newer:imagemin']
}
}
});
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-newer');
grunt.registerTask('default', ['newer:imagemin','watch']);
}
It seems to work fine, the task first tries to optimize all existing images (if they are not already optimized), after this watch ensures that the optimize task runs whenever a picture in the uploads folder changes, newer ensures that only new files will be optimized.
Because I’m just started with grunt, is there anything I missed? What would happen if the upload takes a long time, will watch already start the task on a unfinished file and how could I prevent it?