/
index.js
59 lines (50 loc) · 1.57 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
'use strict';
var xtend = require('xtend');
var runnel = require('runnel');
var cleanLoaders = require('./lib/clean-loaders');
var genRemote = require('./lib/gen-remote');
var genIndex = require('./lib/gen-index');
/**
* Generates all remote loaders and adds them to browserify instance if it is given.
* Calls back with paths to generated loaders.
*
* @name exports
* @function
* @param bify {Object} browserify instance (optional) but recommended
* @param remote {Object} hashtable containing information about remote scripts for which to generate and add loaders
* @param cb {Function} called back with paths to generated loaders
*/
module.exports = function addRemoteScripts(bify, remote, cb) {
if (typeof remote == 'function') {
cb = remote;
remote = bify;
bify = null;
}
cleanLoaders(function (err) {
if (err) return cb(err);
if (!remote) return cb(null, []);
var keys = Object.keys(remote);
if (!keys.length) return cb(null, []);
var tasks = [ runnel.seed([]) ]
.concat(keys.map(
function (k) {
var rem = remote[k];
rem.key = k;
return function (acc, cb) {
genRemote(rem, function (err, gen) {
if (err) return cb(err);
acc.push(gen);
cb(null, acc);
});
};
})
)
.concat(genIndex)
.concat(function done (err, acc) {
if (err) return cb(err);
if (bify) acc.forEach(bify.add.bind(bify));
cb(null, acc);
});
runnel(tasks);
});
};