forked from openannotation/annotator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (38 loc) · 1.19 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
var through = require('through');
// Addd a loader script to a browserify instance which exports the annotator
// namespace and then requires any other exposed modules. Use it on a browserify
// instance to create standalone bundles for annotator extensions and plugins
// by exposing the annotator/lib/namespace module as annotator instead of
// declaring annotator as external.
function loader(b) {
var exposed = ['annotator'];
var loader = through(expose);
loader.pause();
loader.write("module.exports = require('annotator');\n");
b.add(loader);
b.transform(function (file) {
return through(expose);
});
// Browserify doesn't honor streams as entry points so we need to force it.
b._entries.push(loader);
return b;
function expose(data) {
this.queue(data);
if (this === loader) return;
var done = true;
for (var m in b._mapped) {
if (exposed.indexOf(m) == -1) {
done = false;
exposed.push(m);
loader.write("require('" + m + "');\n");
}
}
if (Object.keys(exposed).length == Object.keys(b._expose).length) {
if (!done) {
loader.resume();
loader.end();
}
}
}
}
exports.loader = loader;