javascript 咕噜手表不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19197303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:44:08  来源:igfitidea点击:

Grunt watch not working

javascriptnode.jsgruntjsgrunt-contrib-watch

提问by elkebirmed

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 运行 watch 任务,但它对我不起作用(这就是我得到的):

$ 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']);

回答by Ben

u_mulderis correct; simply remove the unnecessary grunt.registerTask('watch', ['watch'])line from your code and you should be good to go.

u_mulder是正确的;只需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']);doesn't make sense because it is already defined for you. If this wasn't the case you would have to call grunt.registerTaskfor each and every task in your Gruntfile config.

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

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 pluginwith the 'tasks' alias, so instead of typing grunt availabletasksI can type grunt tasksand that saves me some typing. In this instance you could do something like:

在某些情况下,使用不同的名称为任务添加别名可能是有意义的。它将使用您指定的完全相同的配置调用,但别名可以节省输入。例如,我喜欢用“tasks”别名注册我的可用任务插件,这样grunt availabletasks我就可以输入而不是输入,这样可以grunt tasks节省一些输入。在这种情况下,您可以执行以下操作:

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

And you can then use grunt was a shortcut for grunt watch.

然后您可以将grunt w其用作grunt watch.

回答by 2682562

Actually, deleting grunt.registerTask('watch', ['watch'])will sort you out. But let me help you to understand what's happening under the hood.

其实,删除grunt.registerTask('watch', ['watch'])会让你分心。但让我帮助您了解幕后发生的事情。

With grunt.registerTask('watch', ['watch']), watchis calling itself, which generates an infinite loop. When you delete it, it still works, cause watchis the default task of the package, which I guess is called at the very beggining of your file with grunt.loadNpmTasks('grunt-contrib-watch');. You can go further on doc here

随着grunt.registerTask('watch', ['watch']),watch正在调用自身,这会产生一个无限循环。当你删除它时,它仍然有效,原因watch是包的默认任务,我猜它是在你的文件开始时调用的grunt.loadNpmTasks('grunt-contrib-watch');。您可以在此处进一步了解 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']). With this you avoid the infinite loop and make your customization work.

但是,让您对监视任务的自定义按照您的意愿工作会非常方便。为此,最好执行类似grunt.registerTask('watchfiles', ['watch']). 有了这个,您可以避免无限循环并使您的自定义工作。

And you would run the task like this $ grunt watchfilesand 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.

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