Prevent `grunt-watch` from looping when there is a syntax error in less files?

不打扰是莪最后的温柔 提交于 2019-12-11 20:23:05

问题


I'm using grunt-watch to re-build my less style sheets:

    watch: {
        less: {
            files: ['media/less/**/*.less'],
            tasks: ['less'],
            options: {
                atBegin: true,
                spawn: false
            }
        }
    }

But if there is a syntax error in any of the .less files, the task just loops, trying to re-build the .less files every second… which makes debugging fairly difficult, because the error messages scroll past very quickly.

Is there any way fix that, so grunt-watch will only re-run the task once the .less files have been changed again?

This is using:

grunt@0.4.2
grunt-contrib-less@0.8.3
grunt-contrib-watch@0.5.3

回答1:


I think the issue you're describing is this one, which was fixed in master but hasn't yet been released (as of 2013/12/17).




回答2:


Well, for debugging purposes you could do a simple envelope of the less task with a custom task:

grunt.registerTask('myless', 'my less task', function() {
  // do whatever debugging you want and stop the loop if needed.
  grunt.task.run(['less']);
});

Then use your myless task in the watch.

UPDATE:

the idea is that since any repeated call to less now goes through your code - you can do whatever needed to either provide a more concrete output or preven repeated calls if failing is "desired" outcome and should fail, but not loop.

UPDATE 2:

Something like this:

watch: {
    `less`: {
        files: ['**/*.less'],   // or whatever the extension is
        tasks: ['myless']       // your envelope task
    }
}

var flag;

grunt.registerTask('myless', 'My LESS task', function() {
    if(flag === true) {
        // if you are here - it means watch just invoked you repeatedly
        // do whatever you need to analyze the issue (includig running an additional task)

        flag = false;
        return; // you exit task without altering any less files again - 
                // that should NOT trigger watch again
    } else {
        flag = true;
        grunt.task.run(['less']);
    }
});


来源:https://stackoverflow.com/questions/20603125/prevent-grunt-watch-from-looping-when-there-is-a-syntax-error-in-less-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!