node.js 寻找在 gulp 中复制文件并根据父目录重命名的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21224252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:42:47  来源:igfitidea点击:

Looking for way to copy files in gulp and rename based on parent directory

node.jsgulp

提问by chris

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

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

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

Thanks

谢谢

回答by Kirk Strobeck

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

不是答案,但适用于此问题在搜索结果中的出现。

To copy files/folders in gulp

在 gulp 中复制文件/文件夹

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

回答by OverZealous

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

最好的方法是base在采购文件时进行配置,如下所示:

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

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

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

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

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

回答by user2977367

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

Worked for me !

对我来说有效!

回答by brnmonteiro

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:src()} 上使用,因为某些插件会使目录树变平。

回答by Dan Alboteanu

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