17

第一次使用这个任务,我想要实现的是以下内容:

将所有目录/文件复制src/js/bower_components/*build/assets/js/vendor/

我尝试过使用cwd属性,但是当我使用它时它根本不起作用。我将它设置为:src/js/bower_components/

从源

.
├── Gruntfile
└── src
    └── js
        └── bower_components
            └── jquery

我目前得到:

.
├── Gruntfile
└── build
    └── assets
        └── js
            └── vendor
                src
                └── js
                    └── bower_components
                        └── jquery

我想要什么

.
├── Gruntfile
└── build
    └── assets
        └── js
            └── vendor
                └──jquery

这是我当前的任务

copy: {
  main: {
    src: 'src/js/bower_components/*',
    dest: 'build/assets/js/vendor/',
    expand: true,
  }
},

谢谢你的帮助

4

1 回答 1

21

我已经用这样的树设置了一个示例项目:

.
├── Gruntfile.js
├── package.json
└── src
    └── js
        └── foo.js

使用下面的 Gruntfile:

module.exports = function(grunt) {
  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({
    copy          : {
      foo : {
        files : [
          {
            expand : true,
            dest   : 'dist',
            cwd    : 'src',
            src    : [
              '**/*.js'
            ]
          }
        ]
      }
    }
  });

  grunt.registerTask('build', function(target) {
    grunt.task.run('copy');
  });

};

这给了我这个结构:

.
├── Gruntfile.js
├── dist
│   └── js
│       └── foo.js
├── package.json
└── src
    └── js
        └── foo.js

当我进行更改cwd以使 Gruntfile 读取时:

module.exports = function(grunt) {
  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({
    copy          : {
      foo : {
        files : [
          {
            expand : true,
            dest   : 'dist',
            cwd    : 'src/js',
            src    : [
              '**/*.js'
            ]
          }
        ]
      }
    }
  });

  grunt.registerTask('build', function(target) {
    grunt.task.run('copy');
  });

};

我得到了这个目录结构:

.
├── Gruntfile.js
├── dist
│   └── foo.js
├── package.json
└── src
    └── js
        └── foo.js

所以它似乎cwd做了你需要的。也许您srcsrc/js/bower_components/*设置为时离开cwdsrc/js/bower_components?在这种情况下,src应该阅读类似的**/*.js内容,但取决于您真正需要的内容。

于 2014-03-27T22:45:06.880 回答