如何复制多个文件,并保持与 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/'));
});

以上代码将 one.csstwo.css复制到 public/assets/css文件夹。

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

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

41230 次浏览

To achieve this please specify 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()


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

I use gulp-flatten and use this configuration:

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.task('move-css',function(){
return gulp
.src([ 'source/**'], { base: './' })
.pipe(gulp.dest('./public/assets/css/'));
});

Your own code didn't include the entire dir tree of source 'source/**' and the base {base:'./'} when calling to gulp.src which caused the function to fail. The other parts where fine.

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


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