寻找在 gulp 中复制文件并根据父目录重命名的方法 [英] Looking for way to copy files in gulp and rename based on parent directory

查看:15
本文介绍了寻找在 gulp 中复制文件并根据父目录重命名的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

到这样的事情:

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

显然上述方法不起作用,那么有没有办法做到这一点,或者一个 npm 已经这样做了?

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

谢谢

推荐答案

最好的方法是在获取文件时配置你的 base,像这样:

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

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

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

(另外,如果你想包含所有的JS文件,你可以使用/**/*.js...)

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

这篇关于寻找在 gulp 中复制文件并根据父目录重命名的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆