UNPKG

gulp-image-data-uri

Version:
59 lines (51 loc) 1.87 kB
var DataURI, PluginError, gutil, path, pluginName, through; through = require('through2'); path = require('path'); DataURI = require('datauri.template'); gutil = require('gulp-util'); PluginError = gutil.PluginError; pluginName = 'gulp-image-data-uri'; module.exports = function(options) { if (options == null) { options = {}; } return through.obj(function(file, enc, cb) { var basename, className, dataURI, dataURIArgs, fileContents, ref, ref1, variables; if (file.isNull() || !file.contents.toString().length) { cb(null, file); return; } if (file.isStream()) { cb(new PluginError(pluginName, 'Streaming not supported')); return; } dataURI = new DataURI(); dataURI.format(path.basename(file.path), file.contents); basename = path.basename(file.path, path.extname(file.path)); className = basename; if (options.customClass != null) { className = options.customClass(className, file); } if (((ref = options.template) != null ? ref.file : void 0) != null) { if (options.template.adapter != null) { dataURI.templateAdapter = options.template.adapter; } dataURIArgs = [options.template.file]; if ((options.template.adapter == null) && (options.template.engine != null)) { dataURIArgs.push(options.template.engine); } variables = (ref1 = options.template.variables) != null ? ref1 : {}; variables.className = className; dataURIArgs.push(variables); fileContents = dataURI.template.apply(dataURI, dataURIArgs); } else { fileContents = dataURI.template(path.resolve(__dirname, 'template.css'), { className: className }); } file.contents = new Buffer(fileContents); file.path = path.join(path.dirname(file.path), basename + '.css'); this.push(file); return cb(); }); };