forked from documentationjs/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency.js
83 lines (79 loc) · 2.75 KB
/
dependency.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const mdeps = require('module-deps-sortable');
const path = require('path');
const babelify = require('babelify');
const concat = require('concat-stream');
const moduleFilters = require('../module_filters');
const { standardBabelParserPlugins } = require('../parsers/parse_to_ast');
const smartGlob = require('../smart_glob.js');
const STANDARD_BABEL_CONFIG = {
compact: false,
parserOpts: { plugins: [...standardBabelParserPlugins, 'flow', 'jsx'] }
};
/**
* Returns a readable stream of dependencies, given an array of entry
* points and an object of options to provide to module-deps.
*
* This stream requires filesystem access, and thus isn't suitable
* for a browser environment.
*
* @param indexes paths to entry files as strings
* @param config optional options passed
* @returns results
*/
function dependencyStream(indexes, config) {
const babelConfig = config.babel
? { configFile: path.resolve(__dirname, '../../../../', config.babel) }
: STANDARD_BABEL_CONFIG;
const md = mdeps({
/**
* Determine whether a module should be included in documentation
* @param {string} id path to a module
* @returns {boolean} true if the module should be included.
*/
filter: id => !!config.external || moduleFilters.internalOnly(id),
extensions: []
.concat(config.requireExtension || [])
.map(ext => '.' + ext.replace(/^\./, ''))
.concat(['.mjs', '.js', '.json', '.es6', '.jsx']),
transform: [babelify.configure(babelConfig)],
postFilter: moduleFilters.externals(indexes, config),
resolve:
config.resolve === 'node' &&
((id, opts, cb) => {
const r = require('resolve');
opts.basedir = path.dirname(opts.filename);
r(id, opts, cb);
})
});
smartGlob(indexes, config.parseExtension).forEach(index => {
md.write(path.resolve(index));
});
md.end();
return new Promise((resolve, reject) => {
md.once('error', reject);
md.pipe(
concat(function (inputs) {
resolve(
inputs
.filter(
input =>
// At this point, we may have allowed a JSON file to be caught by
// module-deps, or anything else allowed by requireExtension.
// otherwise module-deps would complain about
// it not being found. But Babel can't parse JSON, so we filter non-JavaScript
// files away.
config.parseExtension.indexOf(
path.extname(input.file).replace(/^\./, '')
) > -1
)
.map(input => {
// remove source file, since it's transformed anyway
delete input.source;
return input;
})
);
})
);
});
}
module.exports = dependencyStream;