-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.js
83 lines (73 loc) · 2.4 KB
/
bundle.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
var createDepsStreamer = require('deps-stream');
var resolver = require('./lib/resolver');
var _ = require('./lib/util');
var makeTransformer = _.makeTransformer;
var defaultTransformers = require('./lib/transformers');
var BPromise = require('bluebird');
module.exports = function(options) {
options = options || {};
var pathSettings = options.pathSettings,
transformers = [].concat(options.transformers || defaultTransformers).filter(Boolean),
resolverFns = _.makeResolverFns(pathSettings, options);
transformers = transformers.map(function(exTrans) {
return makeTransformer(resolverFns, exTrans, options);
});
var streamMaker = createDepsStreamer({
transformers: transformers,
depResolver: resolver(resolverFns)
// resolveDepsCache: resolveCache,
// contentCache: contentCache
});
return generateStream(options, streamMaker, resolverFns);
};
/**
* bundleMappings => {
* '/xxx.js': {
* m: 'mod1,xx/mod2'
* },
* '/yyy/a.js': {
* m: 'mod_a,mod_b',
* l: 'mod1, xx/mod2'
* }
* }
*/
function generateStream(options, streamMaker, resolverFns){
var paths = Object.keys(options.bundleMappings);
if(paths.length === 0){
throw new Error('[module-serv/bundle]invalid bundle mappings!');
}
return paths.reduce(function(m, path){
var stream = _makeStream(streamMaker, resolverFns.resolveDepPath, options.bundleMappings[path], path, options);
m.push(stream);
return m;
}, []);
}
function ensureLeadSlash(path){
path = _.unixfy(path.trim());
return path.charAt(0) !== '/' ? '/' + path : path;
}
function _makeStream(streamMaker, resolveDepPath, mappings, bundlePath, options){
var stream = _.generateStream(streamMaker, mappings, options, resolveDepPath);
var _streamTo = stream.streamTo.bind(stream);
stream.streamTo = function(writable){
return stream.getMeta().then(function(meta){
if(meta == null){
throw new Error('[module-serv/bundle]module files for bundle "' + bundlePath + '" are not found!');
}
return new BPromise(function(resolve, reject){
stream.onError(reject);
stream.onEnd(resolve);
if(typeof writable === 'function'){
writable = writable({
etag: meta.etag,
hash: meta.hash,
mtime: meta.mtime,
bundlePath: bundlePath
});
}
_streamTo(writable);
});
});
};
return stream;
}