Skip to content
Atlas Cove edited this page Feb 15, 2020 · 7 revisions

Example 1. Have a look into what's being passed through the pipeline

Tap into the Gulp pipeline and console.log the string of what's being passed through

    const tap = require('gulp-tap')
    const log = require('fancy-log')

    .pipe(tap(function (file, t) {
      log(file.contents.toString())
    }))

Example 2. Run your own functions on a pipeline contents, as string

Tap into Gulp pipeline, convert Buffer to string, perform operations on it and replace the stream with the new results.

    const tap = require('gulp-tap')

    .pipe(tap(function (file) {
      file.contents = Buffer.from(yourFunction(file.contents.toString()))
    }))

A yourFunction could be any function that reads string and outputs string. For example it could replace all occurrences of string zzz with yyy:

    function yourFunction (input) {
      return input.replace(/zzz/g, 'yyy')
    }

Example 3. Save files to different locations based on file extension.

var destinations = {
  "scss": "sass",
  "js": "scripts",
  "img": "assets/images"
};

var files = ["loongpath/s.scss", "another/js.js", "verylongpath/img.png"]

gulp.src(files).pipe(tap(where))

function where(file, t) {
  var destPath, match
  match = function(p) {
    var ext
    ext = path.extname(p).substr(1); // remove leading "."
    if ((ext === "jpg" || ext === "png" || ext === "svg" || ext === "gif")) {
      ext = "img"
    }
    return destinations[ext] || false
  }
  destPath = match(file.path)
  if (destPath) {
    return t.through(gulp.dest, [destPath])
  }
}