vinyl-transformer

Create Transformers for Vinyl File Streams

Version Downloads Build Status

Create a Transformer

Create by Extending the Base Class

import {src, dest} from "vinyl-fs";
import {Transformer} from "vinyl-transformer";
import {compile} from "...";

class MyTransformer extends Transformer {
  
  construct(compileFn) {
    this._compileFn = compileFn;
  }
  
  transform(file) {
    file.contents = this._compileFn(file.contents);
  }
}

let stream = (src("src/**/*.js")
  .pipe(new MyTransformer(compile))
  .pipe(dest("dist"))
);

Create via a Function

import {src, dest} from "vinyl-fs";
import {createTransformer} from "vinyl-transformer";
import {compile} from "...";

let MyTransformer = createTransformer((file) => {
  file.contents = compile(file.contents);
});

let stream = (src("src/**/*.js")
  .pipe(new MyTransformer())
  .pipe(dest("dist"))
);

Create via an Object

import {src, dest} from "vinyl-fs";
import {createTransformer} from "vinyl-transformer";
import {compile} from "...";

let MyTransformer = createTransformer({
  initialize(compileFn) {
    this._compileFn = compileFn;
  },
	transform(file) {
  	file.contents = this._compileFn(file.contents);
  }
});

let stream = (src("src/**/*.js")
  .pipe(new MyTransformer(compile))
  .pipe(dest("dist"))
);

Create a Transformer Function

import {src, dest} from "vinyl-fs";
import {createTransformerFn} from "vinyl-transformer";
import {compile} from "...";

let transform = createTransformerFn({
  initialize(compileFn) {
    this._compileFn = compileFn;
  },
	transform(file) {
  	file.contents = this._compileFn(file.contents);
  }
});

let stream = (src("src/**/*.js")
  .pipe(transform(compile))
  .pipe(dest("dist"))
);

Create an Immediate Transformer Instance

import {src, dest} from "vinyl-fs";
import {transformer} from "vinyl-transformer";
import {compile} from "...";

let stream = (src("src/**/*.js")
  .pipe(transformer((file) => {
		file.contents = compile(file.contents);
	}))
  .pipe(dest("dist"))
);

Examples

List Files in the Pipeline

import {src, dest} from "vinyl-fs";
import {createTransformerFn} from "vinyl-transformer";

let filelist = createTransformerFn({
  initialize() {
    this._fileCount = 0;
  },
  transform(file) {
    console.log("File:", file.path);
    this._fileCount++;
  },
  flush() {
    console.log("---- Files:", this._fileCount);
  },
});

let stream = (src("src/**/*.js")
  .pipe(filelist())
  .pipe(dest("dist"))
);

Add a Header to all Files in the Pipeline

import {src, dest} from "vinyl-fs";
import {createTransformerFn} from "vinyl-transformer";

let header = createTransformerFn({
  initialize(headerStr) {
    this._header = new Buffer(headerStr);
  },
  transform(file) {
    file.contents = Buffer.concat([
      this._header, file.contents,
    ]);
  },
});

let stream = (src("src/**/*.js")
  .pipe(header("/* HEADER */\n"))
  .pipe(dest("dist"))
);