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

Grunt.js templates: Use the name of the target in a task?

本文关键字:目标 模板 js 任务 Grunt      更新时间:2023-09-26

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

我的场景是:

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的grunt,但没有找到父任务的字符串。我发现最接近的是grunt.task.current.name,但最终是copy:all

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

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

我要送一个水果篮给任何能想出这个办法的人。

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

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);
});