Skip to content

Instantly share code, notes, and snippets.

@antixrist
Forked from Kukunin/myTransformation.js
Created December 14, 2017 08:52
Show Gist options
  • Save antixrist/e7072a235a082a1aaef06b9bf66867bc to your computer and use it in GitHub Desktop.
Save antixrist/e7072a235a082a1aaef06b9bf66867bc to your computer and use it in GitHub Desktop.
A template for a typical transformation for gulp
const through = require('through2'),
PluginError = require('gulp-util').PluginError;
module.exports = function myTransformation(options) {
if(!options) {
options = {};
}
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
// return as is
cb(null, file);
} else if (file.isBuffer()) {
try {
const content = file.contents.toString('utf8');
// do any transformation
file.contents = new Buffer(content, 'utf8');
cb(null, file);
}
catch (err) {
throw new PluginError('my-transformation', err);
}
} else if (file.isStream()) {
throw new PluginError('my-transformation', 'Streams are not supported!');
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment