documentation/lib/module_filters.js
Tom MacWright 631c6925d4 feat(core): Switch to Promises everywhere. Adopt Node v4 ES6 (#648)
* feat(core): Switch to Promises everywhere. Adopt Node v4 ES6

Big changes:

* Uses template strings where appropriate
* Config and argument parsing is unified and there is no such thing
  as formatterOptions anymore. All user-passed options go through
  mergeConfig.
* The node API surface changed (again): `buildSync` is removed,
  building operations return Promises.
* Now using Flow for internal type annotations.

More changes:

* Remove buildSync command
* feat(inference): Partially implement object shorthand support
* Refs #649
* Use Flow annotations to  enforce types
* Keep flow but switch to comment syntax
* Clarify types
* More flow improvements
* Turn server into class
* LinkerStack becomes class too
* Fix comment description type
* Run flow on lint
* Many more flow fixes
* More intense flow refactoring
* Simplify inference steps
* Update inference tests, flow errors down to 1
* Continue refining types
* Fix more flow issues
* Use 'use strict' everywhere
* Make 'ast' property configurable
* Fix many tests
* Fix more tests
* Fix more tests
* Fix augments
* Test Markdown meta support
* Improve test coverage
* Switch back from for of to for for speed
2017-01-27 16:14:19 -05:00

69 lines
2.2 KiB
JavaScript

/* @flow */
'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 filters
* @private
*/
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/*: Array<string>*/, options/*: Object*/) {
var externalFilters = false;
if (options.external) {
externalFilters = indexes.map((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/*: string*/,
file/*: string*/,
pkg/*: Object*/) {
var internal = internalModuleRegexp.test(id);
return internal || (externalFilters &&
externalFilters
.some(f => f(file, pkg)));
};
}
};