-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
38 lines (31 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
module.exports = function() {
// create a stream through which each file will pass
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
this.push(file);
// do nothing if no contents
return callback();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-font64', 'Streaming not supported'));
return callback();
}
if (file.isBuffer()) {
var file64 = new Buffer(file.contents).toString('base64');
var mtype = mime.lookup(file.path);
var fileext = path.extname(file.path);
var filename = path.basename(file.path, fileext);
var csswrapper = '@font-face {font-family: '+filename+'; src: url(data:'+mtype+';base64,'+file64+');}';
var output = csswrapper;
file.contents = new Buffer(output);
file.path = gutil.replaceExtension(file.path, '.css');
return callback(null, file);
}
});
};