I've been working with the Grunt cssmin plugin. I had a block in my Gruntfile which looks approximately like this:
cssmin: {
target: {
files: {
'<%= config.target %>/mytarget.css': [
'bower_components/normalize.css/*.css',
'bower_components/html5-boilerplate/css/main.css',
'<%= config.src %>/css/*.css'
]
}
}
}
For a while this was working fine; but I moved this to another machine and didn't set up my bower components correctly - the html5-boilerplate/css/main.css
file was missing - and yet the task still completed successfully. In fact, I can put completely fake paths in that array and the minification still completes.
Is there a way, in general, to cause Grunt to fail and quit if files are missing from an array like this? (I'm not sure if the files array is a general Grunt concept or one provided by cssmin). If not, is there a way to cause this specific plugin to fail?
(By the way, I am aware that HTML5 Boilerplate is probably somewhat old-fashioned these days, but I'm in the process of migrating an old site. I've also fixed my Bower install process so that it's run before this step automatically now. I'd still like to understand a more general solution to the problem of missing files, though).
In the files array format you can use nonull: true
to raise a warning if a file doesn't exist:
files: [
{
src: [
'a.js',
'b.js',
],
dest: 'c.js',
nonull: true
}
]
Now force Grunt to stop on a warning by wrapping grunt.log.warn in your own function:
var gruntLogWarn = grunt.log.warn;
grunt.log.warn = function(error) {
gruntLogWarn(error); // The original warning.
grunt.fail.warn('Stop! Hammer time.'); // Forced stop.
};
Above will stop on any warning. But we want to stop if source files are missing. Filter Grunt warnings on 'Source file ... not found.':
var gruntLogWarn = grunt.log.warn;
grunt.log.warn = function(error) {
var pattern = new RegExp("^Source file (.*) not found.$");
if (pattern.test(error)) {
grunt.fail.warn(error);
} else {
gruntLogWarn(error);
}
};
You could create a task that goes before cssmin
.
grunt.task.registerTask('checkIfFilesExist','',function(){
// Check for those files here. Throw if something's wrong.
});
grunt.task.registerTask('checkBeforeDoingStuff',[
'checkIfFilesExist',
'cssmin'
]);
And iirc, you could reuse the same params in the cssmin task by just referring them via <% %>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With