docs/writing-a-plugin/guidelines.md
While these guidelines are totally optional, we HIGHLY recommend that everyone follows them. Nobody wants to use a bad plugin. These guidelines will actually help make your life easier by giving you assurance that your plugin fits well within gulp.
Writing a Plugin > Guidelines
gulpplugin as a keyword in your package.json so you show up on our searchgulp-replace: Cannot do regexp replace on a streamfile.contents should always be the same going out as it was when it came in
file object downstream until you are done with itfile.clone() when cloning a file or creating a new one based on a file.gulp as a dependency or peerDependency in your plugin
gulp aims to be simple for users. By providing strict guidelines we are able to provide a consistent and high-quality ecosystem for everyone. While this does add a little more work and thought for plugin authors, it removes a lot of problems later down the road.
npm is open for everyone, and you are free to make whatever you want but these guidelines were prescribed for a reason. There are acceptance tests coming soon that will be integrated into the plugin search. If you fail to adhere to the plugin guidelines it will be publicly visible/sortable via a scoring system. People will always prefer to use plugins that match "the gulp way".
// through2 is a thin wrapper around node transform streams
var through = require('through2');
var PluginError = require('plugin-error');
// Consts
const PLUGIN_NAME = 'gulp-prefixer';
function prefixStream(prefixText) {
var stream = through();
stream.write(prefixText);
return stream;
}
// 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
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
// return empty file
return cb(null, file);
}
if (file.isBuffer()) {
file.contents = Buffer.concat([prefixText, file.contents]);
}
if (file.isStream()) {
file.contents = file.contents.pipe(prefixStream(prefixText));
}
cb(null, file);
});
}
// Exporting the plugin main function
module.exports = gulpPrefixer;