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

1

16

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/

I've tried using cwd property but it doesn't work at all when I use it.. I've set it to: 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

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

Thanks for any help

Imprescriptible answered 27/3, 2014 at 19:57 Comment(0)
N
20

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

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

Using the below 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 cwd so that the Gruntfile read:

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 cwd does what you need. Maybe you left src at src/js/bower_components/* when setting cwd to src/js/bower_components? In that case, src should read something like **/*.js, but depending on what you really need.

Nealey answered 27/3, 2014 at 22:45 Comment(3)
Its worth noting that grunt-contrib-copy copies files one at a time when using the */ pattern, which is extremely slow. You're probably better off with a different plugin for large file hierarchies.Moonshine
This gives me: Running "build" task Warning: Task "copy" not found. Use --force to continue.Basidiomycete
you can add flatten:true if you want them all to be in the same destination folderMexico

© 2022 - 2024 — McMap. All rights reserved.