3

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

我的情况是这样的:

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

我已经搜索了文档,我已经 console.log'dgrunt但没有找到作为父任务的字符串。我找到的最接近的是,grunt.task.current.name但最终是copy:all.

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

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

我正在向任何能解决这个问题的人发送一个晒太阳的水果。

4

1 回答 1

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);
});
于 2013-07-19T05:27:34.220 回答