copying files with gulp

2020-04-07 04:49发布

I have an app. My app source code is structured like this:

./
  gulpfile.js
  src
    img
      bg.png
      logo.png
    data
      list.json
    favicon.ico
    web.config
    index.html
  deploy

I am trying to use Gulp to copy two files: ./img/bg.png and ./data/list.json. I want to copy these two files to the root of the deploy directory. In other words, the result of the task should have:

./
  deploy
    imgs
      bg.png
    data
      list.json

How do I write a Gulp task to do this type of copying? The thing that is confusing me is the fact that I want my task to copy two seperate files instead of files that fit a pattern. I know if I had a pattern, I could do this:

var copy = require('gulp-copy');
gulp.task('copy-resources', function() {
  return gulp.src('./src/img/*.png')
    .pipe(gulp.dest('./deploy'))
  ;
});

Yet, I'm still not sure how to do this with two seperate files.

Thanks

2条回答
放我归山
2楼-- · 2020-04-07 05:26

You can create separate tasks for each target directory, and then combine them using a general "copy-resources" task.

gulp.task('copy-img', function() {
  return gulp.src('./src/img/*.png')
    .pipe(gulp.dest('./deploy/imgs'));
});

gulp.task('copy-data', function() {
  return gulp.src('./src/data/*.json')
    .pipe(gulp.dest('./deploy/data'));
});

gulp.task('copy-resources', ['copy-img', 'copy-data']);
查看更多
别忘想泡老子
3楼-- · 2020-04-07 05:49

You could also use merge-stream

Install dependency:

npm i -D merge-stream

Load the depedency in your gulp file and use it:

const merge = require("merge-stream");

gulp.task('copy-resources', function() {
  return merge([
      gulp.src('./src/img/*.png').pipe(gulp.dest('./deploy/imgs')),
      gulp.src('./src/data/*.json').pipe(gulp.dest('./deploy/data'))
  ]);
});
查看更多
登录 后发表回答