index.js
function browserifySwap(file) {
  var env = process.env.BROWSERIFYSWAP_ENV
    , data = ''
    , swapFile;

  // no stubbing desired or we already determined that we can't find a swap config => just pipe it through
  if (!env || cachedConfig === -1) return through();

  if (cachedConfig) {
    swapFile = swap(cachedConfig, env, file);

    // early exit if we have config cached already and know we won't replace anything anyways
    return swapFile ? through(write, end) : through();
  } else {
    return through(write, end)
  }

  function write(d, enc, cb) { data += d; cb(); }
  function end(cb) {
    /*jshint validthis:true */
    var self = this;

    // if config was cached we already resolved the swapFile if we got here
    if (swapFile) {
      swapFile = swapFile.replace(/\\/g, '/');
      debug.inspect({ file: file, swapFile: swapFile });
      self.push(requireSwap(swapFile));
      return cb();
    }

    // we should only get here the very first time that this transform is invoked
    resolveSwaps(root, function (err, config) {
      if (config && config.packages) viralifyDeps(config.packages);
      var swaps = config && config.swaps;

      // signal with -1 that we already tried to resolve a swap config but didn't find any
      cachedConfig = swaps || -1;

      if (err) return cb(err);

      debug.inspect({ swaps: swaps, env: env });
      swapFile = swap(swaps, env, file);
      debug.inspect({ file: file, swapFile: swapFile });

      var src = swapFile ? requireSwap(swapFile.replace(/\\/g, '/')) : data;
      self.push(src);
      cb();
    });
  }
}
function browserifySwap(file) {
  var env = process.env.BROWSERIFYSWAP_ENV
    , data = ''
    , swapFile;

  // no stubbing desired or we already determined that we can't find a swap config => just pipe it through
  if (!env || cachedConfig === -1) return through();

  if (cachedConfig) {
    swapFile = swap(cachedConfig, env, file);

    // early exit if we have config cached already and know we won't replace anything anyways
    return swapFile ? through(write, end) : through();
  } else {
    return through(write, end)
  }

  function write(d, enc, cb) { data += d; cb(); }
  function end(cb) {
    /*jshint validthis:true */
    var self = this;

    // if config was cached we already resolved the swapFile if we got here
    if (swapFile) {
      swapFile = swapFile.replace(/\\/g, '/');
      debug.inspect({ file: file, swapFile: swapFile });
      self.push(requireSwap(swapFile));
      return cb();
    }

    // we should only get here the very first time that this transform is invoked
    resolveSwaps(root, function (err, config) {
      if (config && config.packages) viralifyDeps(config.packages);
      var swaps = config && config.swaps;

      // signal with -1 that we already tried to resolve a swap config but didn't find any
      cachedConfig = swaps || -1;

      if (err) return cb(err);

      debug.inspect({ swaps: swaps, env: env });
      swapFile = swap(swaps, env, file);
      debug.inspect({ file: file, swapFile: swapFile });

      var src = swapFile ? requireSwap(swapFile.replace(/\\/g, '/')) : data;
      self.push(src);
      cb();
    });
  }
}
function browserifySwap ( file ) { var env = process . env . BROWSERIFYSWAP_ENV , data = '' , swapFile ; // no stubbing desired or we already determined that we can't find a swap config => just pipe it through if ( ! env || cachedConfig === - 1 ) return through ( ) ; if ( cachedConfig ) { swapFile = swap ( cachedConfig , env , file ) ; // early exit if we have config cached already and know we won't replace anything anyways return swapFile ? through ( write , end ) : through ( ) ; } else { return through ( write , end ) } function write ( d , enc , cb ) { data += d ; cb ( ) ; } function end ( cb ) { /*jshint validthis:true */ var self = this ; // if config was cached we already resolved the swapFile if we got here if ( swapFile ) { swapFile = swapFile . replace ( / \\ / g , '/' ) ; debug . inspect ( { file : file , swapFile : swapFile } ) ; self . push ( requireSwap ( swapFile ) ) ; return cb ( ) ; } // we should only get here the very first time that this transform is invoked resolveSwaps ( root , function ( err , config ) { if ( config && config . packages ) viralifyDeps ( config . packages ) ; var swaps = config && config . swaps ; // signal with -1 that we already tried to resolve a swap config but didn't find any cachedConfig = swaps || - 1 ; if ( err ) return cb ( err ) ; debug . inspect ( { swaps : swaps , env : env } ) ; swapFile = swap ( swaps , env , file ) ; debug . inspect ( { file : file , swapFile : swapFile } ) ; var src = swapFile ? requireSwap ( swapFile . replace ( / \\ / g , '/' ) ) : data ; self . push ( src ) ; cb ( ) ; } ) ; } }
Looks up browserify_swap configuratios specified for the given file in the environment specified via `BROWSERIFYSWAP_ENV`.

If found the file content is replaced with a require statement to the file to swap in for the original.
Otherwise the file's content is just piped through.

@name browserifySwap
@function
@param {String} file full path to file being transformed
@return {TransformStream} transform stream into which `browserify` will pipe the original content of the file
Looks up browserify_swap configuratios specified for the given file in the environment specified via BROWSERIFYSWAP_ENV .