From 9489aa34a7368372e046dc96101b5f2b9b1bec66 Mon Sep 17 00:00:00 2001 From: Steve Lacy Date: Sat, 19 Jul 2014 08:32:15 -0700 Subject: [PATCH 1/2] through2 0.5.1 file callback --- docs/writing-a-plugin/guidelines.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/docs/writing-a-plugin/guidelines.md b/docs/writing-a-plugin/guidelines.md index 2cc7b8a5f..b7e03500d 100644 --- a/docs/writing-a-plugin/guidelines.md +++ b/docs/writing-a-plugin/guidelines.md @@ -74,7 +74,7 @@ function gulpPrefixer(prefixText) { prefixText = new Buffer(prefixText); // allocate ahead of time // Creating a stream through which each file will pass - var stream = through.obj(function(file, enc, callback) { + return through.obj(function(file, enc, callback) { if (file.isNull()) { // Do nothing if no contents } @@ -86,13 +86,11 @@ function gulpPrefixer(prefixText) { file.contents = file.contents.pipe(prefixStream(prefixText)); } - this.push(file); - return callback(); + callback(null, file); }); - // returning the file stream - return stream; + }; // Exporting the plugin main function From f19e11210b6191765754bf0e403100417d670682 Mon Sep 17 00:00:00 2001 From: Steve Lacy Date: Mon, 15 Sep 2014 19:34:41 -0700 Subject: [PATCH 2/2] merge --- docs/writing-a-plugin/guidelines.md | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/docs/writing-a-plugin/guidelines.md b/docs/writing-a-plugin/guidelines.md index 49ef5dbe7..1073162fa 100644 --- a/docs/writing-a-plugin/guidelines.md +++ b/docs/writing-a-plugin/guidelines.md @@ -56,7 +56,7 @@ var through = require('through2'); var gutil = require('gulp-util'); var PluginError = gutil.PluginError; -// consts +// Consts const PLUGIN_NAME = 'gulp-prefixer'; function prefixStream(prefixText) { @@ -65,37 +65,33 @@ function prefixStream(prefixText) { return stream; } -// plugin level function (dealing with files) +// Plugin level function(dealing with files) function gulpPrefixer(prefixText) { + if (!prefixText) { throw new PluginError(PLUGIN_NAME, 'Missing prefix text!'); } - prefixText = new Buffer(prefixText); // allocate ahead of time - // creating a stream through which each file will pass - var stream = through.obj(function(file, enc, cb) { + // Creating a stream through which each file will pass + return through.obj(function(file, enc, callback) { if (file.isNull()) { - // do nothing if no contents + // return empty file + return callback(null, file); } - if (file.isBuffer()) { - file.contents = Buffer.concat([prefixText, file.contents]); + file.contents = Buffer.concat([prefixText, file.contents]); } - if (file.isStream()) { - file.contents = file.contents.pipe(prefixStream(prefixText)); + file.contents = file.contents.pipe(prefixStream(prefixText)); } - this.push(file); + return callback(null, file); - return cb(); }); - // returning the file stream - return stream; }; -// exporting the plugin main function +// Exporting the plugin main function module.exports = gulpPrefixer; ```