Javascript 如何使用 Gulp 复制多个文件并保持文件夹结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30067942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:36:27  来源:igfitidea点击:

How to copy multiple files and keep the folder structure with Gulp

javascriptcssgulp

提问by NullPoiиteя

I am trying to copy files from one folder to another folder using Gulp:

我正在尝试使用 Gulp 将文件从一个文件夹复制到另一个文件夹:

gulp.task('move-css',function(){
  return gulp.src([
    './source/css/one.css',
    './source/other/css/two.css'

    ]).pipe(gulp.dest('./public/assets/css/'));
});

The above code is copying one.css& two.cssto the public/assets/cssfolder.

上面的代码是将one.css&复制two.csspublic/assets/css文件夹中。

And if I use gulp.src('./source/css/*.css')it will copy all CSS files to the public/assets/cssfolder which is not what I want.

如果我使用gulp.src('./source/css/*.css')它会将所有 CSS 文件复制到public/assets/css我不想要的文件夹中。

How do I select multiple files and keep the folder structure?

如何选择多个文件并保持文件夹结构?

回答by Anulal S

To achieve this please specify base.

要实现这一点,请指定base

? base- Specify the folder relative to the cwd. Default is where the glob begins. This is used to determine the file names when saving in .dest()

? base- 指定相对于 cwd 的文件夹。默认是 glob 开始的地方。这用于确定保存时的文件名.dest()



In your case it would be:

在您的情况下,它将是:

gulp.task('move-css',function(){
  return gulp.src([
      './source/css/one.css',
      './source/other/css/two.css'
  ],  {base: './source/'}) 
  .pipe(gulp.dest('./public/assets/'));
});

Folder structure:

文件夹结构:

.
├── gulpfile.js
├── source
│   ├── css
│   └── other
│       └── css
└── public
    └── assets

回答by AlejandroJSR7

I use gulp-flatten and use this configuration:

我使用 gulp-flatten 并使用以下配置:

var gulp = require('gulp'),
    gulpFlatten = require('gulp-flatten');

var routeSources = {
  dist:  './public/',
  app: './app/',
  html_views: {
    path: 'app/views/**/*.*',
    dist: 'public/views/'
  }
};

gulp.task('copy-html-views', task_Copy_html_views);
function task_Copy_html_views() {
  return gulp.src([routeSources.html_views.path])
      .pipe(gulpFlatten({ includeParents: 1 }))
      .pipe(gulp.dest(routeSources.html_views.dist));
}

And there you can see the documentation about gulp-flatten: Link

在那里你可以看到关于 gulp-flatten 的文档:链接