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

前端 未结 1 777
耶瑟儿~
耶瑟儿~ 2021-02-07 07:37

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/asset

相关标签:
1条回答
  • 2021-02-07 08:01

    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.

    0 讨论(0)
提交回复
热议问题