-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
36 lines (29 loc) · 1.12 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
'use strict';
var Transform = require('readable-stream/transform');
var rs = require('replacestream');
var jsExtensionRegex = /\.js/;
var windowsBackwardSlashRegex = /\\/g;
module.exports = function () {
return new Transform({
objectMode: true,
transform: function (file, encoding, callback) {
if (file.isNull() || file.isDirectory()) {
return callback(null, file);
}
if (!file.isStream() && !file.isBuffer()) {
return callback(Error('Only streams and buffers are supported.'), file);
}
var name = file.relative
.replace(jsExtensionRegex, '')
.replace(windowsBackwardSlashRegex, '/');
var search = /^System\.register\(\[/;
var replace = "System.register('" + name + "', [";
if (file.isStream()) {
file.contents = file.contents.pipe(rs(search, replace));
} else {
file.contents = new Buffer(String(file.contents).replace(search, replace));
}
return callback(null, file);
}
})
};