Skip to content

Commit e264b62

Browse files
committed
refactor(gulp): add Buffer version
1 parent 06f9cb7 commit e264b62

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

src/gulp/gulpfile.babel.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,45 @@
22
"use strict";
33
import gulp from "gulp";
44
import {Transform} from "stream";
5-
let transformStream = new Transform({
6-
writableObjectMode: true,
7-
readableObjectMode: true,
8-
transform: function (chunk, encoding, next) {
9-
let str = Buffer.concat([Buffer("prefix"), chunk]);
10-
this.push(str);
11-
next();
12-
}
13-
});
145

15-
let gulpTransform = new Transform({
16-
writableObjectMode: true,
17-
readableObjectMode: true,
18-
transform: function (file, encoding, next) {
19-
if (file.isStream()) {
20-
file.contents = file.contents.pipe(transformStream);
6+
let prefixBuffer = function (buffer, prefix) {
7+
return Buffer.concat([Buffer(prefix), buffer]);
8+
};
9+
10+
let prefixStream = function (prefix) {
11+
return new Transform({
12+
transform: function (chunk, encoding, next) {
13+
let buffer = prefixBuffer(chunk, prefix);
14+
this.push(buffer);
15+
next();
2116
}
22-
this.push(file);
23-
next();
24-
}
25-
});
17+
});
18+
};
19+
20+
let gulpTransform = function (prefix) {
21+
// enable `objectMode` of the stream for vinyl File objects.
22+
return new Transform({
23+
// Takes in vinyl File objects
24+
writableObjectMode: true,
25+
// Outputs vinyl File objects
26+
readableObjectMode: true,
27+
transform: function (file, encoding, next) {
28+
if (file.isBuffer()) {
29+
file.contents = prefixBuffer(file.contents, prefix);
30+
}
31+
32+
if (file.isStream()) {
33+
file.contents = file.contents.pipe(prefixStream(prefix));
34+
}
35+
this.push(file);
36+
next();
37+
}
38+
});
39+
};
2640
gulp.task("default", function () {
27-
return gulp.src("./*.js", {buffer: false})
28-
.pipe(gulpTransform)
29-
.pipe(gulp.dest("modified-files"))
41+
return gulp.src("./*.*")
42+
.pipe(gulpTransform("prefix text"))
43+
.pipe(gulp.dest("build"))
3044
.on("error", (error) => {
3145
console.error(error);
3246
});

0 commit comments

Comments
 (0)