This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·188 lines (155 loc) · 6.08 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*jslint node: true, nomen: true, vars: true, unparam: true */
"use strict";
var path = require("path");
var isPlainObject = require("lodash.isplainobject");
var R = require("ramda");
var Rx = require("rx");
var stream = require("readable-stream");
var concatDeps = require("./lib/concatdeps");
var gce = require("./lib/gce");
//-----------------------------------------------
function depsStream(deps) {
var s = new stream.PassThrough({ objectMode: true });
Rx.Node.writeToStream(deps, s);
return function () {
return s;
};
}
//-----------------------------------------------
var cleanOpts = R.omit(["browserify", "threshold"]);
//-----------------------------------------------
function buildEntryCompiler(name, deps, opts) {
var ourDeps = deps
.filter(R.compose(R.eq(name), R.last, R.prop("level")))
.distinct(R.prop("id"));
return ourDeps.aggregate({ externals: [], files: [] }, function (res, row) {
if (row.external) {
res.externals.push(row.id);
} else if (row.entry) {
res.files.push(row.id);
}
return res;
}).map(function (res) {
var compiler = opts.browserify(R.mixin(cleanOpts(opts), { deps: depsStream(ourDeps) }));
R.each(compiler.external.bind(compiler), res.externals);
R.each(compiler.add.bind(compiler), res.files);
return { name: name, compiler: compiler };
}).single();
}
//-----------------------------------------------
function buildCommonCompiler(name, deps, opts) {
return deps.map(function (res) {
var compiler = opts.browserify(R.mixin(cleanOpts(opts), {
deps: depsStream(Rx.Observable.fromArray([].concat(res.externals, res.requires)).distinct(R.prop("id")))
}));
R.each(compiler.external.bind(compiler), R.uniq(R.pluck("id", res.externals)));
R.each(compiler.require.bind(compiler), R.uniq(R.pluck("id", res.requires)));
return { name: name, compiler: compiler };
}).single();
}
//-----------------------------------------------
function getChildDeps(commonDeps) {
return commonDeps.flatMap(function (res) {
return Rx.Observable.fromArray([].concat(
res.externals,
R.map(R.mixin({ external: true }), res.requires),
res.childDeps
));
});
}
//-----------------------------------------------
function getCommonDeps(name, deps, opts) {
function exceedsThreshold(ctx, row) {
return (ctx.rows[row.id].length > opts.threshold && gce(R.pluck("level", ctx.rows[row.id])) === name) || ctx.ensureCommon[row.id];
}
return deps.filter(R.compose(R.contains(name), R.prop("level"))).aggregate({ rows: {}, ensureCommon: {} }, function (ctx, row) {
if (!ctx.rows[row.id]) {
ctx.rows[row.id] = [];
}
ctx.rows[row.id].push(row);
if (exceedsThreshold(ctx, row)) {
R.each(function (id) { ctx.ensureCommon[id] = true; }, R.values(row.deps));
}
return ctx;
}).map(function (ctx) {
var rowValues = R.flatten(R.values(ctx.rows));
var externals = R.filter(R.prop("external"), rowValues);
var requires = R.filter(R.curry(exceedsThreshold)(ctx), R.reject(R.prop("external"), rowValues));
var childDeps = R.filter(function (row) {
return !R.containsWith(R.eqProps("id"), row, externals) && !R.containsWith(R.eqProps("id"), row, requires);
}, rowValues);
return { externals: externals, requires: requires, childDeps: childDeps };
}).single();
}
//-----------------------------------------------
function buildCompilers(config, deps, opts) {
return Rx.Observable.concat(R.map(function (name) {
var entry = config[name];
if (isPlainObject(entry)) {
var commonDeps = getCommonDeps(name, deps, opts);
return Rx.Observable.concat(
buildCommonCompiler(name, commonDeps, opts),
buildCompilers(entry, getChildDeps(commonDeps), opts)
);
}
return buildEntryCompiler(name, deps, opts);
}, R.keys(config)));
}
//-----------------------------------------------
function bundleCompilers(compilers, opts) {
if (!opts) { opts = {}; }
var transforms = [].concat(opts.pipeTo || []);
var s = stream.PassThrough({ objectMode: !!opts.objectMode });
Rx.Node.writeToStream(
compilers.flatMap(function (res) {
var bundle = res.compiler.bundle(R.omit(["objectMode", "pipeTo"], opts));
return Rx.Node.fromStream(
R.reduce(function (b, t) {
return b.pipe(t(res.name, res.compiler));
}, bundle, transforms)
);
}),
s
);
return s;
}
//-----------------------------------------------
function streamCompilers(compilers) {
var s = stream.PassThrough({ objectMode: true });
Rx.Node.writeToStream(compilers, s);
return s;
}
//-----------------------------------------------
module.exports = function createMultiBundle(entryConfig, opts) {
var config = {};
if (typeof entryConfig === "string") {
config[path.basename(entryConfig, path.extname(entryConfig))] = entryConfig;
} else if (Array.isArray(entryConfig)) {
config.bundle = R.clone(entryConfig);
} else if (isPlainObject(entryConfig)) {
config = entryConfig;
} else {
throw new Error("multi-bundle: expected 'entryConfig' to be a string, array of strings, or an object.");
}
if (!opts) {
opts = {};
}
if (!opts.threshold || opts.threshold < 1) {
opts.threshold = 1;
}
if (!opts.browserify) {
try {
opts.browserify = require("browserify");
} catch (e) {
throw new Error("multi-bundle: expected either 'opts.browserify' to be set or an npm dependency on 'browserify'.");
}
}
opts.packageCache = {};
opts.cache = {};
var deps = concatDeps(config, opts);
var compilers = buildCompilers(config, deps, opts);
return {
bundle: bundleCompilers.bind(null, compilers),
stream: streamCompilers.bind(null, compilers)
};
};