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

How to get Grunt-Contrib-Copy to copy files/directories relative to given source path

本文关键字:路径 复制 文件 目录 Grunt-Contrib-Copy 何获得 相对于      更新时间:2023-09-26

第一次使用这个任务,我想实现的是:

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

这是我当前的grunt任务

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

感谢您的帮助

我已经建立了一个示例项目,像这样的树:

.
├── 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做了你需要的。也许您将cwd设置为src/js/bower_components时将src留在src/js/bower_components/* ?在这种情况下,src应该读成类似于**/*.js,但这取决于您真正需要的内容。