Create gulp plugin from a function or object that works with string data (e.g. a converter).
Exposes function that returns a gulp plugin: gulpPluginFabric(name, run, [init]).
name: name of the plugin (will begulp-name)run: function that transforms incoming data, can be both sync or asyncinit: function that is launched beforerunwith options (optional)
The following types of functions are supported:
- Sync:
run(data, options)returns resulting string, or throws an error - Async:
run(data, options, cb)callscb(null, result)upon successful completion, orcb(err)in case of an error
Let's create a simple gulp plugin that adds a custom suffix to the input string. For example, suffix "!" would result in "str" -> "str!" transformation.
Convert function to gulp plugin:
let run = (text, options) => text + options.suffix;
let plugin = require('gulp-plugin-fabric')("my-plugin", run);Convert object to gulp plugin:
let runner = {
_suffix: "!",
run: function(text, options) {
return text + this._suffix;
},
init: function (options) {
this._suffix = options.suffix | "";
}
};
let plugin = require('gulp-plugin-fabric')("my-plugin", runner.run.bind(runner), runner.init.bind(runner));Then you can use plugin in gulp as follows:
gulp.src('.')
.pipe(plugin({suffix: "!"}))
.pipe(gulp.dest('out'));