Jeroen
Jeroen

Reputation: 63830

How to have gulp-replace affect only certain files?

I've got this gulp task:

var gulp = require('gulp');

gulp.task('myStaticFiles', function() {
    gulp.src('staticSite/**')
        .pipe(gulp.dest('build/site'));
});

Between src and dest I want to do a gulp-replace (or similar). I currently have:

var gulp = require('gulp');
var replace = require('gulp-replace');

gulp.task('myStaticFiles', function() {
    gulp.src('staticSite/**')
        .pipe(replace(/mypattern/, "replacevalue"))
        .pipe(gulp.dest('build/site'));
});

However, this affects all files in the pipe. I want to:

How can I do so?

The gulp-replace documentation has nothing on this, so perhaps I'm just lacking some knowledge on "the gulp way" to do this kind of thing?

Upvotes: 1

Views: 814

Answers (1)

avcajaraville
avcajaraville

Reputation: 9084

Try this approach, using gulp-if:

(function() {

  'use strict';

  var gulp    = require( 'gulp' ),
      gulpif  = require( 'gulp-if' ),
      replace = require( 'gulp-replace' );

  var condition = function ( file ) {
    // TODO: add business logic
    return true;
  }

  gulp.task( 'myStaticFiles', function() {
    return gulp
      .src( 'staticSite/**' )
      .pipe( gulpif( condition, replace( /mypattern/, 'replacevalue' ) ) )
      .pipe( gulp.dest( 'build/site' ) );
  });

})();

Upvotes: 2

Related Questions