Alan2
Alan2

Reputation: 24562

Can I rename or copy two files with one task in gulp

Here's what I am doing now. It works but if I want to rename more flies then I am going to end up with a lot of tasks:

gulp.task('rename_bundle_css', ['make_css_bundle'], function () {
    return gulp.src('content/bundles/css.min.css')
        .pipe(rename('css-' + md5File('content/bundles/css.min.css') + '.min.css'))
        .pipe(gulp.dest('content/bundles/'));
});

gulp.task('rename_bundle_css_zip', ['rename_bundle_css'], function () {
    return gulp.src('content/bundles/css.min.css.gz')
        .pipe(rename('css-' + md5File('content/bundles/css.min.css') + '.min.css.gz'))
        .pipe(gulp.dest('content/bundles/'));
});

Is there a way I can combine these two into one or some way I an rename (or copy) more than one file in one task?

Upvotes: 0

Views: 89

Answers (1)

Sven Schoenung
Sven Schoenung

Reputation: 30564

You can pass a function to gulp-rename which receives an object with the following properties for each file in the stream:

  • dirname: path to the directory that the file is in
  • basename: name of file without file extension
  • extname: file extension

Changing any of these renames the file.

Your example rewritten in this fashion could look like this:

gulp.task('rename_bundle_css', ['make_css_bundle'], function () {
  return gulp.src(['content/bundles/css.min.css',
                   'content/bundles/css.min.css.gz'])
    .pipe(rename(function(file) {
       var original = file.basename + file.extname.replace(/\.gz$/, '');
       var hash = md5File('content/bundles/' + original);
       file.basename = file.basename.replace(/^css/, 'css-' + hash);
    }))
    .pipe(gulp.dest('content/bundles/'));
});

Upvotes: 1

Related Questions