-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (47 loc) · 1.33 KB
/
index.js
File metadata and controls
49 lines (47 loc) · 1.33 KB
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
39
40
41
42
43
44
45
46
47
48
49
var through = require('through2');
var glob = require('glob');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var purify = require('purify-css');
const PLUGIN_NAME = 'gulp-purifycss';
module.exports = function(source, options) {
var sourceFiles = [];
source.forEach(function(pathPattern) {
var files = glob.sync(pathPattern);
sourceFiles = sourceFiles.concat(files);
});
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
cb(null, file);
}
if (file.isBuffer()) {
try {
purify(sourceFiles, file.contents.toString(), options, function(output){
file.contents = new Buffer(output);
cb(null, file);
});
} catch(error) {
this.emit('error', new PluginError(PLUGIN_NAME, error));
return cb(error);
}
}
if (file.isStream()) {
var css = '';
file.on('readable',function(buffer){
var part = buffer.read().toString();
css += part;
});
file.on('end',function(){
try {
purify(sourceFiles, css, options, function(output){
file.contents = new Buffer(output);
cb(null, file);
});
} catch(error) {
this.emit('error', new PluginError(PLUGIN_NAME, error));
return cb(error);
}
});
}
});
};