|
2 | 2 | "use strict";
|
3 | 3 | import gulp from "gulp";
|
4 | 4 | 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 |
| -}); |
14 | 5 |
|
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(); |
21 | 16 | }
|
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 | +}; |
26 | 40 | 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")) |
30 | 44 | .on("error", (error) => {
|
31 | 45 | console.error(error);
|
32 | 46 | });
|
|
0 commit comments