-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
127 lines (105 loc) · 3.62 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
'use strict';
var path = require('path');
var utils = require('./utils');
var _ = require('lodash');
var _workdir = process.cwd();
module.exports = browserifyBower;
module.exports.workdir = function(workdir) {
_workdir = workdir || _workdir;
return workdir && browserifyBower || _workdir;
};
//////////////////////////////////////////////////////////////////////////////
/**
* alias can be configured at: options.alias > currConfig.alias > currConfig.include (later can override former)
* NOTE: options.alias and currConfig.alias can be: { name1: alias1, ... } or ['name1:alias1', ...]
**/
function getAliasConfigs(options, currConfig) {
var aliasConfigs = {},
optAlias = {}, confAlias = {}, incAlias = {};
if (_.isArray(options.alias)) {
_.forEach(options.alias || [], function(rawname) {
optAlias[rawname.split(':')[0]] = rawname.split(':')[1];
});
} else optAlias = options.alias;
if (_.isArray(currConfig.alias)) {
_.forEach(currConfig.alias || [], function(rawname) {
confAlias[rawname.split(':')[0]] = rawname.split(':')[1];
});
} else confAlias = currConfig.alias;
_.forEach(currConfig.include || [], function(rawname) {
if (rawname.indexOf(':') > 0) {
incAlias[rawname.split(':')[0]] = rawname.split(':')[1];
}
});
return _.extend(aliasConfigs, optAlias, confAlias, incAlias);
}
function getWorkingLists(options) {
var workinglists = {};
var mainfiles = options.mainfiles || {};
_.forEach(options, function(config, action) {
if (_(['require', 'external']).includes(action)) {
if (_(config).isArray()) config = { "include": config };
var aliasConfigs = getAliasConfigs(options, config);
var excludeFilter = function(name) {
name = name.name || name;
return !_(config.exclude).map(function(name1) {
return name1.split(':')[0];
}).includes(name.split(':')[0]);
};
var workinglist = _(config.include || utils.componentNames(_workdir))
// process '*' including
.map(function(name) {
return (name === '*') ? utils.componentNames(_workdir) : name;
})
.flatten()
// filter out excluded names
.filter(excludeFilter)
// resolve name and deps
.uniq()
.map(function(rawname) {
var name = rawname.split(':')[0];
return utils.resolve(name, _workdir, mainfiles);
})
// prepare the working list
.flatten()
// filter out excluded dependencies
.filter(excludeFilter)
.groupBy(function(item) {
return item.name;
})
.map(function(list, name) {
return {
name : name,
alias: aliasConfigs[name] || name, // merge in alias configs
path : list[0].path
};
}).value();
///
workinglists[action] = workinglist;
}
});
return workinglists;
}
function browserifyBower(browserify, options) {
options = options || {};
if (options.workdir) _workdir = options.workdir;
if (options.conf) {
var confjson = require(path.join(_workdir, options.conf));
options = options.confnode && _.get(confjson, options.confnode) || confjson;
}
// if no reqiure configs are specified, let it include all components.
options.require = options.require || utils.componentNames(_workdir);
///
var workinglists = getWorkingLists(options);
// if an item existed in both 'require' and 'external' lists, let's remove it from 'require' list
workinglists.require = _.filter(workinglists.require || [], function(item) {
return ! _.find(workinglists.external || [], { name: item.name });
});
///
_.forEach(workinglists.require || [], function(item) {
browserify.require(item.path, { expose: item.alias });
});
_.forEach(workinglists.external || [], function(item) {
browserify.external(item.alias);
});
};