How to copy files without the full path with grunt

2019-04-03 06:01发布

问题:

I want to copy the content of /pckg to /dist with grunt.js. Here is the structure:

  |-- folder1
  |     |
  |     |-- folder2
  |           |
  |           |-- pckg
  |                 |
  |                 |--myfolder
  |                 |    |
  |                 |    |-- myfiles
  |                 |
  |                 |--myfiles
  |
  |
  |-- dist
        |
        |--myfolder
        |   |
        |   |-- myfiles
        |
        |--myfiles

Here's my Gruntfile.js

module.exports = function (grunt) {

  // Package configuration
  grunt.initConfig({

    // Metadata
    pkg: grunt.file.readJSON('package.json'),

    //Copy files
    copy: {
      main: {
        expand: true,
        src: 'folder1/folder2/pckg/**',
        dest: 'dest/'
      }
    }

  });

  // Load the plugin that provides the "copy" task.
  grunt.loadNpmTasks('grunt-contrib-copy');

  // Default task(s).
  grunt.registerTask('default', ['copy']);
};

When I run Grunt, it keep the path. It copy everything in dit/folder1/folder2/pckg. What is wrong ?

Thanks for your help !

回答1:

Here's what I've used:

copy: {
  main: {
    expand: true,
    cwd: 'folder1/folder2/pckg/',
    src: ['**'],
    dest: 'dist/'
  }
}


回答2:

use flatten:true

copy: {
    main: {
        files: [ 
            {expand: true, src: ['components/xxx/*'], dest: 'dist/', flatten: true}
        ]
    }
}