Grunt手表不工作

我尝试在node.js中通过grunt运行watch任务,但它不适用于我(这是我得到的):

$ grunt watch warning: Maximum call stack size exceeded Use --force to continue. 

这是Gruntfile.js中的监视任务的一部分:

 watch: { less: { files: 'src/less/*.less', tasks: ['clean', 'recess', 'copy'] }, js: { files: 'src/js/*.js', tasks: ['clean', 'jshint', 'concat', 'uglify', 'copy'] }, theme: { files: 'src/theme/**', tasks: ['clean', 'recess', 'copy'] }, images: { files: 'src/images/*', tasks: ['clean', 'recess', 'copy'] } } grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('watch', ['watch']); 

u_mulder是正确的; 只需从你的代码中删除不必要的grunt.registerTask('watch', ['watch'])行,你应该很好。

编辑:这是因为你正在注册一个自己调用的新任务。 添加一行像grunt.registerTask('watch', ['watch']); 没有意义,因为它已经为你定义。 如果不是这种情况,您将不得不在gruntfileconfiguration中为每个任务调用grunt.registerTask

在某些情况下,使用不同的名称别名任务可能是有意义的。 它将被调用完全相同的configuration,你已经指定,但别名可以节省input。 例如,我喜欢用“任务”别名注册我的可用任务插件 ,所以,而不是inputgrunt availabletasks我可以inputgrunt tasks ,这节省了我一些打字。 在这种情况下,你可以做这样的事情:

 grunt.registerTask('w', ['watch']); 

然后你可以使用grunt w作为grunt watch的捷径。

其实,删除grunt.registerTask('watch', ['watch'])会把你排除在外。 但让我来帮助你了解底下发生了什么。

grunt.registerTask('watch', ['watch'])watch会调用自己,这会产生一个无限循环。 当你删除它,它仍然有效,因为watch是包的默认任务,我猜是在grunt.loadNpmTasks('grunt-contrib-watch');文件的开始处调用grunt.loadNpmTasks('grunt-contrib-watch'); 。 你可以在这里进一步的文档

但是,如果您希望将自定义的监视任务按照您的要求工作,那将非常方便。 要做到这一点,最好做一些像grunt.registerTask('watchfiles', ['watch']) 。 有了这个,你可以避免无限循环,并使你的定制工作。

而且你会像这个$ grunt watchfiles一样运行这个任务,并且性能会很好。

请注意,所有的path必须是正确的,否则,如果任务指定了错误的path,它将不会运行。