-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
137 lines (113 loc) · 2.36 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Module Dependencies
*/
var type = require('component-type');
var path = require('path');
var extname = path.extname;
/**
* Regexps
*/
var rsupport = /^(js|css)$/;
/**
* Export `coerce`
*/
module.exports = coerce;
/**
* Coerce the "main"'s from a manifest's JSON
*
* @param {Object} json
* @return {Array}
* @api private
*/
function coerce(json, type) {
return type
? dependency(json, type)
: entry(json);
};
/**
* Get the mains from an entry
*
* @param {Object} json
* @return {Array}
* @api private
*/
function entry(json) {
var script = (json.scripts || [])[0];
var style = (json.styles || [])[0];
var main = json.main;
var entries = [];
switch(type(main)) {
case 'object': entries = entries.concat(values(main)); break;
case 'array': entries = entries.concat(main); break;
case 'string': entries.push(main); break;
}
if (!main || compat(json)) {
switch (extension(main)) {
case 'css': script && entries.push(script); break;
case 'js': style && entries.push(style); break;
case '':
script && entries.push(script);
style && entries.push(style);
break;
}
}
return entries;
}
/**
* Get the main from a dependency
*
* @param {Object} json
* @param {String} ext
* @return {String|Boolean}
*/
function dependency(json, ext) {
var script = (json.scripts || [])[0];
var style = (json.styles || [])[0];
var main = json.main;
if (!main || compat(json)) {
if (ext == extension(json.main)) return main;
else if (ext == extension(style)) return style;
else if (ext == extension(script)) return script;
else return false;
}
switch(type(main)) {
case 'object': return main[ext] || false;
case 'string': return main;
}
return false;
}
/**
* Compatibility
*
* @param {Object} json
* @return {Boolean}
*/
function compat(json) {
var main = json.main;
var ext = extension(main);
return rsupport.test(ext)
&& 'string' == type(json.main);
}
/**
* Get the values of an object
*
* @param {Object} obj
* @return {Array}
* @api private
*/
function values(obj) {
return Object.keys(obj).map(function(k) {
return obj[k];
});
}
/**
* Get the extension
*
* @param {String} path
* @return {String}
* @api private
*/
function extension(path) {
if (typeof path !== 'string') return '';
return extname(path).slice(1);
}