0

我正在尝试使用 grunt-contrib-copy 从父文件夹复制文件。我的文件夹结构是:

-libs
  -html5shiv
  -respond
  -jquery
-apps
  -exampleApp1
    -GruntFile.js
    -build
  -exampleApp2
    -GruntFile.js
    -build

当我在 exampleApp1 中运行 GruntFile.js 时,我试图将 libs 文件夹中的所有 JavaScript 文件复制到 apps/exampleApp1/build 中。我在 GruntFile.js 中有以下设置:

build_dir: 'build';

lib_files: {
  js: [
    '../../libs/html5shiv/dist/html5shiv.js',
    '../../libs/respond/dest/respond.min.js',
    '../../libs/jquery/dist/jquery.min.js'
  ]
};

copy: {
  build_libjs: {
    files: [
      {
        src: [ '<%= lib_files.js %>' ],
        dest: '<%= build_dir %>/',
        cwd: '.',
        expand: true            
      }
    ]
  },

},

目前它将所有文件复制到应用程序/库,因为每个文件在 lib_files.js 数组中都有“../../”。我怎样才能确保文件最终像这样在构建文件夹中?:

-apps
  -exampleApp1
    -GruntFile.js
    -build
      -libs
        -html5shiv/dist/html5shiv.js
        -libs/respond/dest/respond.min.js
        -jquery/dist/jquery.min.js
4

2 回答 2

2

The answer is quite simple actually. There is a rename function which can be used in combination with grunt-contrib-copy.

"You just need to attach one parameter to my configuration, which overrides the standard rename function of the Grunt file utilities.".

from:

http://fettblog.eu/blog/2014/05/27/undocumented-features-rename/

I combined this with a simple regular expression and problem solved:

  build_libjs: {
    files: [
      {
        src: [ '<%= lib_files.js %>' ],
        dest: '<%= build_dir %>/',
        cwd: '.',
        expand: true,
        rename: function(dest, src) {
          //Replace '../../' with an empty string
          return dest + ( src.replace(/^..\/..\// ,"") );
        }           
      }
    ]
  },
于 2015-02-02T15:03:37.610 回答
0

尝试使用cwd属性更改源目录

lib_files: {


js: [
    'html5shiv/dist/html5shiv.js',
    'respond/dest/respond.min.js',
    'jquery/dist/jquery.min.js'
  ]
};

copy: {
  build_libjs: {
    files: [
      {
        src: [ '<%= lib_files.js %>' ],
        dest: '<%= build_dir %>/',
        cwd: '../libs',
        expand: true            
      }
    ]
  },
于 2015-05-04T14:29:34.443 回答