使用 gulp 复制文件

2022-01-12 00:00:00 javascript gulp

我有一个应用程序.我的应用源代码结构如下:

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

我正在尝试使用 Gulp 复制两个文件:./img/bg.png 和 ./data/list.json.我想将这两个文件复制到部署目录的根目录.也就是说,任务的结果应该是:

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

如何编写 Gulp 任务来进行这种类型的复制?让我感到困惑的是,我希望我的任务复制两个单独的文件,而不是适合某个模式的文件.我知道如果我有一个模式,我可以这样做:

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.

谢谢

推荐答案

您可以为每个目标目录创建单独的任务,然后使用通用的复制资源"任务将它们组合起来.

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']);

相关文章