For each module I have some files that need to be copied over to the build directory, and am looking for a way to minimize the repeated code from this:
gulp
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'))
);
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/')),
)
);
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...)
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.
return gulp.src('./client/src/modules/(.*)/index.js')
.pipe(gulp.dest('./build/public/js/$1'));
Worked for me !