监视grunt-contrib-watch的子任务
Monitoring sub-task for grunt-contrib-watch
我有下面的Gruntfile.coffee。我正在监视如下所示的watch任务,以查看文件更改,然后将更改后的文件编译为coffee-script。
# Watch task
watch:
coffee:
files: ['client/**/*.coffee','server/**/*/.coffee']
options:
nospawn: true
livereload: true
# Watch changed files
grunt.event.on 'watch', (action, filepath) ->
cwd = 'client/'
filepath = filepath.replace(cwd,'')
grunt.config.set('coffee',
changed:
expand: true
cwd: cwd
src: filepath
dest: 'client-dist/'
ext: '.js'
)
grunt.task.run('coffee:changed')
然而,我想添加另一个监视任务来复制非咖啡文件的文件。我该如何监控这些变化?
我想做
# Watch copy task
grunt.event.on 'watch:copy', (action,filepath) -> ...
# Watch coffee task
grunt.event.on 'watch:coffee', (action,filepath) -> ...
但这似乎不起作用。想法吗?
我的解决方案-完成工作,但不是很漂亮。我欢迎更好的答案
基本上,我匹配传入文件的路径如果是。coffee,运行coffee编译任务
如果是。*运行拷贝任务
# Watch changed files
grunt.event.on 'watch', (action, filepath) ->
# Determine server or client folder
path = if filepath.indexOf('client') isnt -1 then 'client' else 'server'
cwd = "#{path}/"
filepath = filepath.replace(cwd,'')
# Minimatch for coffee files
if minimatch filepath, '**/*.coffee'
# Compile changed file
grunt.config.set('coffee',
changed:
expand: true
cwd: cwd
src: filepath
dest: "#{path}-dist/"
ext: '.js'
)
grunt.task.run('coffee:changed')
# Minimatch for all others
if minimatch filepath, '**/*.!(coffee)'
# Copy changed file
grunt.config.set('copy',
changed:
files: [
expand: true
cwd: cwd
src: filepath
dest: "#{path}-dist/"
]
)
grunt.task.run("copy:changed")
看一下手表事件示例底部的注释:https://github.com/gruntjs/grunt-contrib-watch#using-the-watch-event
watch
事件并不打算替换Grunt API。使用tasks
代替:
watch:
options:
nospawn: true
livereload: true
coffee:
files: ['client/**/*.coffee','server/**/*/.coffee']
tasks: ['coffee']
copy:
files: ['copyfiles/*']
tasks: ['copy']
相关文章:
- 如果助手不在,如何从Grunt中的代码中调用任务
- 如何为requirejs和qunit设置grunt任务
- 不使用Grunt.initConfig()注册Grunt任务
- Grunt任务没有加载,获得无限循环
- 通过Grunt任务注入内容,具体取决于asp.net项目构建配置
- Grunt:jit Grunt:插件;量角器”;找不到任务
- 如何在Grunt.js中观察多个文件,但只在更改的文件上运行任务
- 从另一个grunt文件加载grunt任务
- 如何将项目变量配置到任务中's在Grunt中的initConfig
- Grunt任务退出,总线错误:10
- 无法创建Jasmine并发Grunt任务
- 可以't使用grunt-bower任务插件运行我的grunt-task
- 如何使用Grunt异步运行2同步任务
- Grunt Copy任务不复制父文件夹's文件保存到正确的文件夹中
- Grunt:如何只对修改后的文件运行任务
- Grunt为任务设置选项,并使用新值运行该任务
- 找不到任务和子任务
- 没有明确定义的子任务执行
- 监视grunt-contrib-watch的子任务
- 操作由Grunt任务启动的脚本中的子进程