How do you watch multiple files, but only run task on changed file, in Grunt.js?

前端 未结 7 1848
盖世英雄少女心
盖世英雄少女心 2020-12-01 00:12

In learning how to use grunt, I am trying to make a simple coffee-script watcher/compiler. The problem is, if I tell the watch task to watch several files, and

7条回答
  •  萌比男神i
    2020-12-01 00:29

    In this issue, Kyle Robinson suggests using the watchevent. It's very important to set watch task nospawn property to true to make it work. I modified his solution to selectively run the tasks:

    grunt.event.on('watch', function(action, filepath) {
        if (minimatch(filepath, grunt.config('watch.stylesheets.files'))) {
            grunt.config('compass.dist.options.specify', [filepath]);
        }
    
        if (minimatch(filepath, grunt.config('watch.scripts.files'))) {
            var uglifySrc = filepath.replace(grunt.config('uglify.dist.cwd'), '');
            grunt.config('jshint.dist.src', [filepath]);
            grunt.config('uglify.dist.src', [uglifySrc]);
        }
    });
    

    Here is the complete solution: https://gist.github.com/luissquall/5408257

提交回复
热议问题