Skip to content

Commit c4c79f2

Browse files
committed
refactor(gulp): gulpPrefixerにファイルを分離
1 parent e264b62 commit c4c79f2

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

Diff for: src/gulp/gulp-prefixer.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
import {Transform} from "stream";
3+
let prefixBuffer = function (buffer, prefix) {
4+
return Buffer.concat([Buffer(prefix), buffer]);
5+
};
6+
7+
let prefixStream = function (prefix) {
8+
return new Transform({
9+
transform: function (chunk, encoding, next) {
10+
let buffer = prefixBuffer(chunk, prefix);
11+
this.push(buffer);
12+
next();
13+
}
14+
});
15+
};
16+
17+
let gulpTransform = function (prefix) {
18+
// enable `objectMode` of the stream for vinyl File objects.
19+
return new Transform({
20+
// Takes in vinyl File objects
21+
writableObjectMode: true,
22+
// Outputs vinyl File objects
23+
readableObjectMode: true,
24+
transform: function (file, encoding, next) {
25+
if (file.isBuffer()) {
26+
file.contents = prefixBuffer(file.contents, prefix);
27+
}
28+
29+
if (file.isStream()) {
30+
file.contents = file.contents.pipe(prefixStream(prefix));
31+
}
32+
this.push(file);
33+
next();
34+
}
35+
});
36+
};
37+
38+
export default gulpTransform;

Diff for: src/gulp/gulpfile.babel.js

+2-37
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
11
// LICENSE : MIT
22
"use strict";
33
import gulp from "gulp";
4-
import {Transform} from "stream";
5-
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();
16-
}
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-
};
4+
import gulpPrefixer from "./gulp-prefixer";
405
gulp.task("default", function () {
416
return gulp.src("./*.*")
42-
.pipe(gulpTransform("prefix text"))
7+
.pipe(gulpPrefixer("prefix text"))
438
.pipe(gulp.dest("build"))
449
.on("error", (error) => {
4510
console.error(error);

0 commit comments

Comments
 (0)