-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (51 loc) · 1.7 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
var through = require('through2');
var cssPurge = require('css-purge');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
const PLUGIN_NAME = 'gulp-css-purge';
var gulpCSSPurge = function(options) {
function purgedStream(modifiedCSS) {
return through().write(modifiedCSS);
}
return through.obj(function(file, encoding, callback){
if (options !== undefined && (options.reduceConfig !== undefined || options.reduceConfig !== null)) {
delete options.reduceConfig;
}
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
var fileContents = file.contents ? file.contents.toString() : '';
cssPurge.purgeCSS(fileContents, options, function(error, results){
if (error) {
return callback(new gutil.PluginError(PLUGIN_NAME, error));
}
file.contents = file.contents.pipe(purgedStream(results));
callback(null, file);
});
} else if (file.isBuffer()) {
var fileContents = file.contents ? file.contents.toString() : '';
//default options
if (options === null || options === undefined) {
options = {
"trim" : true,
"shorten" : true,
"format_font_family": true
};
}
try {
cssPurge.purgeCSS(fileContents, options, function(error, results){
if (error) {
return callback(new gutil.PluginError(PLUGIN_NAME, error));
}
file.contents = new Buffer(results);
callback(null, file);
});
} catch (error) {
return callback(new gutil.PluginError(PLUGIN_NAME, error));
}
}
});
};
module.exports = gulpCSSPurge;