寻找方法,以复制文件的吞和重命名为基础的父目录

对于每个模块,我都有一些需要复制到构建目录的文件,我正在寻找一种方法来减少重复代码:

gulp.src('./client/src/modules/signup/index.js')
.pipe(gulp.dest('./build/public/js/signup'));


gulp.src('./client/src/modules/admin/index.js')
.pipe(gulp.dest('./build/public/js/admin'));

像这样的东西:

gulp.src('./client/src/modules/(.*)/index.js')
.pipe(gulp.dest('./build/public/js/$1'));

显然上面的方法不起作用,那么有没有一种方法可以做到这一点,或者一个已经做到这一点的 npm?

谢谢

83791 次浏览
return gulp.src('./client/src/modules/(.*)/index.js')
.pipe(gulp.dest('./build/public/js/$1'));

Worked for me !

The best way is to configure your base when sourcing files, like so:

gulp.src('./client/src/modules/**/index.js', {base: './client/src/modules'})
.pipe(gulp.dest('./build/public/js/'));

This tells gulp to use the modules directory as the starting point for determining relative paths.

(Also, you can use /**/*.js if you want to include all JS files...)

Not the answer, but applicable to this question's appearance in search results.

To copy files/folders in gulp

gulp.task('copy', () => gulp
.src('index.js')
.pipe(gulp.dest('dist'))
);

Use for preserve input directory tree will be preserved.

.pipe(gulp.dest(function(file) {
var src = path.resolve(SRC_FOLDER);
var final_dist = file.base.replace(src, '');
return DIST_FOLDER + final_dist;
}))

Using this, you can put in the src: .src(SRC_FOLDER + '/**/*.js').

The others answers not worked for me (like using base: on src()}, because some plugins flatten the directory tree.

copy files in parallel

gulp.task('copy', gulp.parallel(
() =>  gulp.src('*.json').pipe(gulp.dest('build/')),
() =>  gulp.src('*.ico').pipe(gulp.dest('build/')),
() =>  gulp.src('img/**/*').pipe(gulp.dest('build/img/')),
)
);