如何让Grunt-Contrib-Copy复制相对于给定源路径的文件/目录

时间:2014-03-27 19:57:15

标签: javascript copy gruntjs grunt-contrib-copy

首次使用此任务以及我想要实现的目标如下:

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

我尝试过使用cwd属性,但在使用它时根本不起作用..我已将其设置为:src/js/bower_components/

来自src

.
├── 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,
  }
},

感谢您的帮助

1 个答案:

答案 0 :(得分:20)

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

.
├── 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可以满足您的需求。在将src设置为src/js/bower_components/*时,您可能会在cwd离开src/js/bower_components吗?在这种情况下,src应该读取类似**/*.js的内容,但取决于您真正需要的内容。