寻找在gulp中复制文件并基于父目录重命名的方法

时间:2021-06-03 10:32:12

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

to something like this:

是这样的:

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

Obviously the above doesn't work, so is there a way to do this, or an npm that already does this?

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

Thanks

谢谢

5 个解决方案

#1


131  

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.

这告诉gulp使用模块目录作为确定相对路径的起点。

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

(你也可以使用/**/*。如果你想包含所有的js文件…)

#2


211  

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

#3


5  

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

Worked for me !

为我工作!

#4


3  

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').

使用这个,您可以放入src: .src(SRC_FOLDER + '/**/*.js')。

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

其他的答案对我不起作用(比如使用base: on src()},因为有些插件使目录树变平。

#5


0  

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/')),
)
);

#1


131  

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.

这告诉gulp使用模块目录作为确定相对路径的起点。

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

(你也可以使用/**/*。如果你想包含所有的js文件…)

#2


211  

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

#3


5  

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

Worked for me !

为我工作!

#4


3  

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').

使用这个,您可以放入src: .src(SRC_FOLDER + '/**/*.js')。

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

其他的答案对我不起作用(比如使用base: on src()},因为有些插件使目录树变平。

#5


0  

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/')),
)
);