JQuery Mobile
JQuery Mobile

Reputation: 6301

Gulp - Copy multiple files to seperate paths

I am working on a web app that uses Node.js. In this app, I have a Gulp file. I am using Gulp 4. During my build process, I am attempting to copy multiple files to directories at once. My directory structure looks like this:

./
  dest/
  source/
    child/
      index.js
      index.bak
    file.js
    README.md

My real directory structure is more involved. However, I am trying to copy ./source/file.js to ./dest/file.js and ./source/child/index.js to ./dest/child/index.js. Notice that I do not want to copy README.md or index.bak over to the ./dest directory. In an attempt to do this, I have the following function:

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

  return gulp
    .src(files)
    .pipe(gulp.dest('dest'))
  ;
}

My problem is, everything just gets copied to the dest directory. The directory structure does not get preserved. While would be fine if I could figure out how to copy files to different directories in a single task. I tried the following:

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

However, that approach just generates an error that says:

TypeError: gulp.src(...).pipe(...).src is not a function

So, I'm stuck. I'm not sure how to copy multiple files to multiple directories from a single gulp task.

Upvotes: 4

Views: 1690

Answers (1)

Tushar Arora
Tushar Arora

Reputation: 1146

You need to use the base option as mentioned here ref. It will make sure your directory is copied as it is.

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

  return gulp
    .src(files, {base: 'source/'})
    .pipe(gulp.dest('dest'));
}

Upvotes: 6

Related Questions