【问题标题】:How to copy images having them renamed with a prefix?如何复制使用前缀重命名的图像?
【发布时间】:2014-10-19 10:43:53
【问题描述】:

我在 Gulp 中有以下数组:

var imageFiles = {
    'bootstrap-fileinput': 'bower/bootstrap-fileinput/dist/img/*', 
    'some-other-thing':    'bower/some-other-thing/dist/img/*'
};

如果需要,我可以重组,只要该前缀有前缀和路径即可。

如何让 gulp 将我的图像复制到我想要的单个文件夹中,但在每个文件夹前面加上数组中的前缀?意思是:

bower/bootstrap-fileinput/dist/img/arrow.jpg => /images/bootstrap-fileinput_arrow.jpg

凉亭/其他东西/dist/img/arrow.jpg => /images/some-other-thing_arrow.jpg

27.08.14: 我是否理解正确,我描述的任务不能仅使用一根管道来完成?正如我所见,在@Doodlebot 提供的解决方案中,每个文件都是独立且按顺序传输的。

【问题讨论】:

    标签: gulp gulp-rename


    【解决方案1】:

    我可以使用 gulp-rename 插件来做到这一点:

    var gulp = require('gulp');
    var rename = require('gulp-rename');
    
    gulp.task('default', function() {
        var imageFiles = {
            'bootstrap-fileinput': 'bower/bootstrap-fileinput/dist/img/*',
            'some-other-thing': 'bower/some-other-thing/dist/img/*'
        }
    
        Object.keys(imageFiles).forEach(function(key) {
            gulp.src(imageFiles[key])
            .pipe(rename({
                prefix: key
            }))
            .pipe(gulp.dest('images/'));
        });
    });
    

    【讨论】:

    • 仅供参考:在 Node(和现代浏览器)中,您可以使用 Object.keys(imageFiles).forEach(function(key) { … }) 来遍历键,而不必担心 hasOwnPropertyMore info here.
    • 我是否理解正确,我描述的任务不能仅使用一个管道来完成?如我所见,在您提供的解决方案中,每个文件都是独立且按顺序传输的。
    • 是的,因为每个 src 都有不同的重命名前缀。您可能会在重命名管道后合并流并使用单个 dest 管道,但我不确定增加的复杂性是否会带来任何好处。
    猜你喜欢
    • 2010-09-17
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2012-08-15
    • 2013-10-18
    相关资源
    最近更新 更多