-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
122 lines (100 loc) · 2.78 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var getModuleType = require('module-definition');
var debug = require('debug')('precinct');
var Walker = require('node-source-walk');
var detectiveCjs = require('detective-cjs');
var detectiveAmd = require('detective-amd');
var detectiveEs6 = require('detective-es6');
var detectiveSass = require('detective-sass');
var detectiveStylus = require('detective-stylus');
var fs = require('fs');
var path = require('path');
var natives = process.binding('natives');
/**
* Finds the list of dependencies for the given file
*
* @param {String|Object} content - File's content or AST
* @param {String} [type] - The type of content being passed in. Useful if you want to use a non-js detective
* @return {String[]}
*/
function precinct(content, type) {
var dependencies = [];
var ast;
// We assume we're dealing with a JS file
if (!type && typeof content !== 'object') {
var walker = new Walker();
try {
// Parse once and distribute the AST to all detectives
ast = walker.parse(content, walker.options);
precinct.ast = ast;
} catch (e) {
// In case a previous call had it populated
precinct.ast = null;
debug('could not parse content');
return dependencies;
}
// SASS files shouldn't be parsed by Acorn
} else {
ast = content;
if (typeof content === 'object') {
precinct.ast = content;
}
}
type = type || getModuleType.fromSource(ast);
var theDetective;
switch (type) {
case 'commonjs':
theDetective = detectiveCjs;
break;
case 'amd':
theDetective = detectiveAmd;
break;
case 'es6':
theDetective = detectiveEs6;
break;
case 'sass':
theDetective = detectiveSass;
break;
case 'stylus':
theDetective = detectiveStylus;
break;
}
if (theDetective) {
dependencies = theDetective(ast);
}
// For non-JS files that we don't parse
if (theDetective && theDetective.ast) {
precinct.ast = theDetective.ast;
}
return dependencies;
};
/**
* Returns the dependencies for the given file path
*
* @param {String} filename
* @param {Object} [options]
* @param {Boolean} [options.includeCore=true] - Whether or not to include core modules in the dependency list
* @return {String[]}
*/
precinct.paperwork = function(filename, options) {
options = options || {
includeCore: true
};
var content = fs.readFileSync(filename, 'utf8');
var ext = path.extname(filename);
var type;
if (ext === '.scss' || ext === '.sass') {
type = 'sass';
} else if (ext === '.styl') {
type = 'stylus';
} else if (ext === '.less') {
type = 'less';
}
var deps = precinct(content, type);
if (!options.includeCore) {
return deps.filter(function(d) {
return !natives[d];
});
}
return deps;
};
module.exports = precinct;