Gulp - 将多个文件复制到单独的路径

JQu*_*ile 4 node.js gulp

我正在使用Node.js的Web应用程序.在这个应用程序中,我有一个Gulp文件.我正在使用Gulp 4.在构建过程中,我试图一次将多个文件复制到目录.我的目录结构如下所示:

./
  dest/
  source/
    child/
      index.js
      index.bak
    file.js
    README.md
Run Code Online (Sandbox Code Playgroud)

我的真实目录结构更复杂.不过,我试图复制./source/file.js./dest/file.js./source/child/index.js./dest/child/index.js.请注意,我不想复制README.md或复制index.bak./dest directory.为了做到这一点,我有以下功能:

function copy() {
  let files = [
    'source/file.js',
    'source/child/**/*.*'
  ];

  return gulp
    .src(files)
    .pipe(gulp.dest('dest'))
  ;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,一切都被复制到dest目录.目录结构不会被保留.如果我能弄清楚如何在单个任务中将文件复制到不同的目录,那就没问题了.我尝试了以下方法:

function copy() {
    return gulp
        .src('source/child/index.js')
        .pipe(gulp.dest('dest/child'))
        .src('source/file.js')
        .pipe(gulp.dest('dest'))
    ;            
}
Run Code Online (Sandbox Code Playgroud)

但是,这种方法只会产生一个错误:

TypeError: gulp.src(...).pipe(...).src is not a function
Run Code Online (Sandbox Code Playgroud)

所以,我被困住了.我不确定如何从单个gulp任务将多个文件复制到多个目录.

Tus*_*ora 6

你需要使用这里提到的基本选项ref.它将确保您的目录按原样复制.

function copy() {
  let files = [
    'source/file.js',
    'source/child/**/*.*'
  ];

  return gulp
    .src(files, {base: 'source/'})
    .pipe(gulp.dest('dest'));
}
Run Code Online (Sandbox Code Playgroud)