Code coverage report for lib/module_filters.js

Statements: 100% (20 / 20)      Branches: 100% (13 / 13)      Functions: 100% (5 / 5)      Lines: 100% (20 / 20)      Ignored: 1 branch     

All files » lib/ » module_filters.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63  25 25     25         25                               68 68 2   2 2   8 2         6 2   4     6 6         68 137 137     8            
'use strict';
var path = require('path');
var micromatch = require('micromatch');
 
// Skip external modules. Based on http://git.io/pzPO.
var internalModuleRegexp = process.platform === 'win32' ?
  /* istanbul ignore next */
  /^(\.|\w:)/ :
  /^[\/.]/;
 
module.exports = {
  internalOnly: internalModuleRegexp.test.bind(internalModuleRegexp),
 
  /**
   * Create a filter function for use with module-deps, allowing the specified
   * external modules through.
   *
   * @param {Array<string>} indexes - the list of entry points that will be
   * used by module-deps
   * @param {Object} options - An options object with `external` being a
   * micromatch-compaitible glob. *NOTE:* the glob will be matched relative to
   * the top-level node_modules directory for each entry point.
   * @return {function} - A function for use as the module-deps `postFilter`
   * options.
   */
  externals: function externalModuleFilter(indexes, options) {
    var externalFilters = false;
    if (options.external) {
      externalFilters = indexes.map(function (index) {
        // grab the path of the top-level node_modules directory.
        var topNodeModules = path.join(path.dirname(index), 'node_modules');
        return function matchGlob(file, pkg) {
          // if a module is not found, don't include it.
          if (!file || !pkg) {
            return false;
          }
          // if package.json specifies a 'main' script, strip that path off
          // the file to get the module's directory.
          // otherwise, just use the dirname of the file.
          if (pkg.main) {
            file = file.slice(0, -path.normalize(pkg.main).length);
          } else {
            file = path.dirname(file);
          }
          // test the path relative to the top node_modules dir.
          var p = path.relative(topNodeModules, file);
          return micromatch.any(p, options.external);
        };
      });
    }
 
    return function (id, file, pkg) {
      var internal = internalModuleRegexp.test(id);
      return internal || (externalFilters &&
        externalFilters
        .some(function (f) {
          return f(file, pkg);
        }));
    };
  }
};