Grunt递归副本

ric*_*wil 9 gruntjs

我正在设置一个Grunt脚本,需要复制和重组图像目录从A到B.很简单.

目录结构:

组件

  • componentA
    • JS
    • IMG
      • imgfolderA
      • imgfolderB
    • CSS
  • 以componentB
    • JS
    • IMG
      • imgfolderA

每个img目录都可以包含这些目录中的其他目录和目录,以帮助组织图像.

我想使用Grunt获取所有这些图像并将它们放在一个目录下(assets/img):

资产

  • IMG
    • DIRA
      • imgfolderA
      • imgfolderB
    • DIRB
      • imgfolderA

如果我没有指定每个组件目录(它需要完全自动化),我怎么能在grunt中执行此操作?

aqm*_*aqm 20

知道它有点晚了,但是应该做这个工作,像这样使用'grunt-contrib-copy'

module.exports = function (grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    copy: {
      production: {
        files: [{
            expand: true,
            cwd: "componentA/img/imgfolderA/",
            src: ["*.*", "**/*.*"],
            dest: "./assets/img/dirA/",
          },
          {
            expand: true,
            cwd: "componentB/img/imgfolderB/",
            src: ["*.*", "**/*.*"],
            dest: "./assets/img/dirB/",
          },
        ]
      }
    }
  });

  // Production Build Tools
  grunt.loadNpmTasks('grunt-contrib-copy');

  // Default Production Build task(s).
  grunt.registerTask('default', ['copy']);
};
Run Code Online (Sandbox Code Playgroud)

ps魔法是在文件对象中,没有很好的文档记录,但文档在这里,经过一两次读取它是有道理的!

grunt-contrib-copy设置:https://github.com/gruntjs/grunt-contrib-copy(自述文件底部)

files对象设置:http://gruntjs.com/configuring-tasks#globbing-patterns

任务设置:http://gruntjs.com/configuring-tasks

  • 使用`src:['*','**']`就足够了。 (2认同)

Sim*_*ias 1

使用起来相当简单grunt.file.expand

只需传递匹配的 glob 模式(例如**/img/**),然后递归返回的匹配文件值进行复制。