Grunt.js模板:在任务中使用目标的名称?

时间:2013-07-19 00:53:31

标签: javascript gruntjs

我正在尝试在模板中使用目标的名称。应该够简单吧?

我的情况是:

copy:
{
  all: {
    src: 'commonFiles/**', dest: 'build/<%= grunt.???? =>/common'
  },
  apple: { ... },
  orange:{ ... },
  banana:{ ... },
  ...
}

grunt.registerTask('default', ['apple', 'orange', 'banana']);

grunt.registerTask('apple' ,  'copy:all copy:apples ... ... ...');
grunt.registerTask('orange',  'copy:all copy:orange ... ... ...');
grunt.registerTask('banana',  'copy:all copy:banana ... ... ...');
grimt.registerTask(...);
...
many, many more fruit

我已经搜索了文档,我已经调试了。grunt但是没有发现任何字符串是父任务。我发现的最接近的是grunt.task.current.name但最终为copy:all

目标是为我的所有水果获得这样的目录结构:

build/apple/common/...
build/orange/common/...
build/banana/common/...
build/.../common/...
...
commonFiles/...

我正在向任何可以解决这个问题的人发送水果。

1 个答案:

答案 0 :(得分:2)

动态别名任务可能更适合此用例。 见http://gruntjs.com/frequently-asked-questions#dynamic-alias-tasks

grunt.initConfig({
  buildDir: 'all',
  copy: {
    all: {
      src: 'commonFiles/**',
      dest: 'build/<%= buildDir =>/common',
    },
    apple: { ... },
    orange:{ ... },
    banana:{ ... },
  },
});

grunt.registerTask('build', function(target) {
  if (target == null) {
    return grunt.warn('Build target must be specified, like build:apple.');
  }
  grunt.config('buildDir', target);
  grunt.task.run('copy:' + target);
});