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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22697919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:36:23  来源:igfitidea点击:

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

javascriptcopygruntjsgrunt-contrib-copy

提问by micahblu

First time using this task and what I'm trying to achieve is the following:

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

copy all directories/files from src/js/bower_components/*to build/assets/js/vendor/

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

I've tried using cwdproperty but it doesn't work at all when I use it.. I've set it to: src/js/bower_components/

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

From src

从源

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

I currently get:

我目前得到:

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

What I'd like

我想要什么

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

Here's my current grunt task

这是我目前的 grunt 任务

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

Thanks for any help

谢谢你的帮助

回答by Kosmotaur

I've set up an example project with tree like this:

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

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

Using the below Gruntfile:

使用以下 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');
  });

};

This gave me this structure:

这给了我这个结构:

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

When I had changed cwdso that the Gruntfile read:

当我更改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');
  });

};

I got this dir structure:

我得到了这个目录结构:

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

So it seems like cwddoes what you need. Maybe you left srcat src/js/bower_components/*when setting cwdto src/js/bower_components? In that case, srcshould read something like **/*.js, but depending on what you really need.

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