vinyl-sources

1.0.1 • Public • Published

vinyl-sources

Record and read sources used to transform a vinyl file.

NPM Version Build Status Coveralls Status Dependency Status

Installation

npm install vinyl-sources

Usage

var sources = require('vinyl-sources');
sources.record(vinylFile, sourcePaths);

Example (gulp plugin)

var through = require('through2');
var sources = require('vinyl-sources');
var myTransform = require('myTransform');
 
module.exports = function(options) {
 
  return through.obj(function transform(file, encoding, callback) {
 
    var result = myTransform(file.contents, options);
    file.contents = new Buffer(result.code);
 
    // record transformation sources to the vinyl file
    sources.record(file, result.sources);
 
    callback(null, file);
 
  });
 
};

Example (iterating sources)

var through = require('through2');
var sources = require('vinyl-sources');
 
module.exports = function(options) {
 
  return through.obj(function transform(file, encoding, callback) {
  
    sources.iterateSources(file, function(source, sourcePath) {
    
      // `source` is a vinyl file representing a single source file
      // `sourcePath` is an array of vinyl files representing the in-order ancestry of the current `source`
      // i.e., when a source file also has sources of it's own, this callback will be called with each child 
      // source and the parent source will be `sourcePath[0]`.
    
    });
 
    callback(null, file);
 
  });
 
};

Example (getting all sources)

var through = require('through2');
var sources = require('vinyl-sources');
 
module.exports = function(options) {
 
  return through.obj(function transform(file, encoding, callback) {
  
    var fileSources = sources.getSources(file);
    
    // `fileSources` is an array of all sources (direct sources of `file` and sources of sources)
 
    callback(null, file);
 
  });
 
};

Package Sidebar

Install

npm i vinyl-sources

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • meandmycode