-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgulp-prefixer.js
37 lines (33 loc) · 1.03 KB
/
gulp-prefixer.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
import {Transform} from "stream";
export function prefixBuffer(buffer, prefix) {
return Buffer.concat([Buffer(prefix), buffer]);
}
export function prefixStream(prefix) {
return new Transform({
transform: function (chunk, encoding, next) {
const buffer = prefixBuffer(chunk, prefix);
this.push(buffer);
next();
}
});
}
const gulpPrefixer = function (prefix) {
// enable `objectMode` of the stream for vinyl File objects.
return new Transform({
// Takes in vinyl File objects
writableObjectMode: true,
// Outputs vinyl File objects
readableObjectMode: true,
transform: function (file, encoding, next) {
if (file.isBuffer()) {
file.contents = prefixBuffer(file.contents, prefix);
}
if (file.isStream()) {
file.contents = file.contents.pipe(prefixStream(prefix));
}
this.push(file);
next();
}
});
};
export default gulpPrefixer;