-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
61 lines (52 loc) · 1.58 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
var through = require("through2"),
rework = require("rework"),
gutil = require("gulp-util");
module.exports = function () {
"use strict";
// Fix nested rules
function addImportantToCssRules(rules) {
rules.forEach(function(r) {
// Ignore @font-face declarations
if (r.type === 'font-face') {
return false;
}
if (r.declarations) {
r.declarations.forEach(function(d) {
// Don't add important twice
if (d.value && d.value.indexOf('!important') === -1) {
d.value += ' !important';
}
});
}
if (r.rules) {
addImportantToCssRules(r.rules);
}
});
}
// see "Writing a plugin"
// https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/README.md
function cssVip(file, enc, callback) {
/*jshint validthis:true*/
// Do nothing if no contents
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isStream()) {
// accepting streams is optional
this.emit("error",
new gutil.PluginError("gulp-css-vip", "Stream content is not supported"));
return callback();
}
// check if file.contents is a `Buffer`
if (file.isBuffer()) {
var css = rework(String(file.contents)).use(function(values) {
addImportantToCssRules(values.rules);
}).toString();
file.contents = new Buffer(css);
this.push(file);
}
return callback();
}
return through.obj(cssVip);
};