我刚开始使用 Grunt,想运行 grunt-contrib-watch
[GitHub page]每次修改文件时检查我的 JavaScript(使用 grunt-contrib-jshint
[GitHub page] )并运行 grunt-nodemon
[GitHub page]同样,同时使用 grunt-concurrent
[GitHub page] .
据我了解(我显然不了解)我的 Gruntfile 应该:
- 默认运行
并发
并发
运行watch
watch
每次修改文件时运行jshint
Gruntfile.js
module.exports = function (grunt) {
grunt.initConfig({
concurrent: {
dev: [
'watch'
],
options: {
logConcurrentOutput: true
}
},
jshint: {
server: [
'**/*.js',
'!node_modules/**/*.js'
],
options: {
node: true
}
},
watch: {
all: [
'**/*/.js',
'!node_modules/**/*.js'
],
tasks: [
'jshint'
]
}
});
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', [
'concurrent:dev'/*,
'jshint',
'watch'*/
]);
};
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', [
'concurrent:dev'
]);
};
注意我还没有将 grunt-nodemon
添加到组合中。
看起来 concurrent
正在运行 watch
但当我修改文件时,它似乎 jshint
没有运行。我当然没有在终端中得到任何输出(我认为 logConcurrentOutput: true
会这样做)。
这是我在终端中得到的输出:
Running "concurrent:dev" (concurrent) task
Running "watch" task
Waiting...
Done, without errors.
我还想在第一次运行 default
任务(以及修改文件时)时运行 jshint
。
任何人都可以阐明我哪里出错了吗?
谢谢!
最佳答案
如果没有找到要“监视”的文件,则监视
任务退出;根据 this issue .
为了轻松地告诉 watch
与 jshint
任务监视相同的文件,我使用 Grunt 的模板引擎来引用相同的 Array
文件作为jshint
任务。
然后我将 jshint
添加到要运行的并发任务列表中,这样它会在最初运行并在我修改文件时运行(使用 watch
)。
这是我的工作 Gruntfile
:
module.exports = function (grunt) {
grunt.initConfig({
concurrent: {
dev: [
'jshint',
'watch'
],
options: {
logConcurrentOutput: true
}
},
jshint: {
server: [
'**/*.js',
'!node_modules/**/*.js'
],
options: {
node: true
}
},
watch: {
files: '<%= jshint.server %>',
tasks: [
'jshint'
]
}
});
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', [
'concurrent'
]);
};
关于node.js - 与 Grunt 同时运行 `watch` 和 `nodemon`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479322/