-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (36 loc) · 833 Bytes
/
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 fs = require('fs');
var pipeline = require('stream-combiner');
var through = require('through');
var transforms = [];
function transform(file) {
if (transforms.length === 0) {
return through();
}
var streams = transforms.map(function(transform) {
return transform(file);
});
return pipeline.apply(null, streams);
}
function _hook(module, filename) {
var src = fs.readFileSync(filename);
var stream = transform(filename);
var buf = '';
function write(data) {
buf += data;
}
function end() {
module._compile(buf, filename);
}
stream.pipe(through(write, end));
stream.write(src);
stream.end()
}
function install(transform) {
if (transforms.length === 0) {
require.extensions['.js'] = _hook;
}
transforms.push(transform);
}
module.exports = {
install: install
};