简体   繁体   English

Grunt手表不工作

[英]Grunt watch not working

I tried to run the watch task by grunt in node.js but it doesn't work for me (this is what I got): 我尝试在node.js中通过grunt运行监视任务,但它对我不起作用(这是我得到的):

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

This is the part of the watch task in the Gruntfile.js: 这是Gruntfile.js中watch任务的一部分:

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 is correct; u_mulder是对的; simply remove the unnecessary grunt.registerTask('watch', ['watch']) line from your code and you should be good to go. 只需从代码中删除不必要的grunt.registerTask('watch', ['watch'])行,你就应该好了。

Edit: This happens because you are registering a new task that calls itself. 编辑:发生这种情况是因为您正在注册调用自身的新任务。 Adding a line like grunt.registerTask('watch', ['watch']); 添加像grunt.registerTask('watch', ['watch']);这样的grunt.registerTask('watch', ['watch']); doesn't make sense because it is already defined for you. 没有意义,因为它已经为你定义了。 If this wasn't the case you would have to call grunt.registerTask for each and every task in your Gruntfile config. 如果不是这种情况,则必须为Gruntfile配置中的每个任务调用grunt.registerTask

In some cases it might make sense to alias the task with a different name. 在某些情况下,使用不同的名称对任务进行别名可能是有意义的。 It would be called with the exact same configuration that you have specified, but aliasing it could save typing. 它将使用您指定的完全相同的配置进行调用,但是别名可以节省输入。 For instance I like to register my available tasks plugin with the 'tasks' alias, so instead of typing grunt availabletasks I can type grunt tasks and that saves me some typing. 例如,我喜欢使用'tasks'别名注册我的可用任务插件 ,因此我可以输入grunt tasks而不是输入grunt availabletasks grunt tasks ,这可以节省一些输入。 In this instance you could do something like: 在这种情况下,您可以执行以下操作:

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

And you can then use grunt w as a shortcut for grunt watch . 然后你可以使用grunt w作为grunt watch的快捷方式。

Actually, deleting grunt.registerTask('watch', ['watch']) will sort you out. 实际上,删除grunt.registerTask('watch', ['watch'])会将你排除在外。 But let me help you to understand what's happening under the hood. 但是,让我帮助您了解幕后发生的事情。

With grunt.registerTask('watch', ['watch']) , watch is calling itself, which generates an infinite loop. 使用grunt.registerTask('watch', ['watch'])watch正在调用自身,它会生成无限循环。 When you delete it, it still works, cause watch is the default task of the package, which I guess is called at the very beggining of your file with grunt.loadNpmTasks('grunt-contrib-watch'); 当你删除它时,它仍然有效,因为watch是包的默认任务,我想这是在你的文件的grunt.loadNpmTasks('grunt-contrib-watch'); . You can go further on doc here 你可以在这里进一步了解doc

However, it would be really handy to get your customization of the watch task to work as you want it to do. 但是,将手表任务的自定义设置为您希望的工作方式非常方便。 For this to happen, it would be probably better to do something like grunt.registerTask('watchfiles', ['watch']) . 要做到这一点,最好做一些像grunt.registerTask('watchfiles', ['watch'])这样的事情。 With this you avoid the infinite loop and make your customization work. 通过这种方式,您可以避免无限循环并使您的自定义工作。

And you would run the task like this $ grunt watchfiles and it would perform well. 你会像这个$ grunt watchfiles一样运行任务,它会表现良好。

Note that all the paths must be the right ones, otherwise, if a task has a wrong path specified, it will just not run. 请注意,所有路径必须是正确的路径,否则,如果任务指定了错误的路径,则它将不会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM