forked from free6k/gulp-css-merge-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (155 loc) · 5.13 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
var path = require('path');
var through = require('through2');
var binaryString = require('binary-string');
var cssparse = require('css');
var crypto = require('crypto');
function clearSource(string) {
return string.replace(/([\s|"|'])/i, '');
}
function md5(string) {
return crypto.createHash('md5').update(string).digest('hex');
}
function parseCMU(rule) {
var cmu = {};
var source_pattern = /(url\(.*?\))/i;
var source_pattern_g = /(url\(.*?\))/ig;
var sel = rule.selectors;
var decl = rule.declarations;
if (decl && decl.length > 0) {
decl.forEach(function (declaration, i) {
var property = declaration.property;
var value = declaration.value;
if (property && value
&& property.indexOf('background') > -1
&& value.indexOf('url') > -1) {
var source = value.match(source_pattern);
if (source && source.length > 0) {
var s = source[0];
var key = md5(clearSource(s));
if (key in cmu) {
cmu[key].selectors = cmu[key].selectors.concat(sel);
} else {
cmu[key] = {url: s, selectors: sel};
}
if (property == 'background-image') {
rule.declarations.splice(i, 1);
} else {
declaration.value = declaration.value.replace(source_pattern_g, '').trim();
if (declaration.value.split(' ').length == 1 && declaration.value.length == 0) {
rule.declarations.splice(i, 1);
}
}
}
}
});
return cmu;
}
}
function mergeCMU(cmu_parse, cmu) {
var key = getCMUKey(cmu_parse);
if (cmu === undefined) {
cmu = {};
}
if (key in cmu) {
cmu[key].selectors = cmu[key].selectors.concat(cmu_parse[key].selectors);
} else {
cmu[key] = cmu_parse[key];
}
return cmu;
}
function getCMUKey(cmu) {
var keys = Object.keys(cmu);
if (keys.length > 0) {
return keys[0];
}
return undefined;
}
function goCMU(rules, resultObj, type) {
rules.forEach(function (rule) {
var parseRule = {};
if (type === undefined) {
type = 'rule';
}
if (rule.type == 'media') {
goCMU(rule.rules, resultObj, rule.media);
} else if (rule.type == 'rule' && rule.declarations.length > 0) {
parseRule = parseCMU(rule);
if (Object.keys(parseRule).length > 0) {
resultObj[type] = mergeCMU(parseRule, resultObj[type]);
}
}
});
}
function insertCSSObject(cmu, css) {
var cmu_rules = {};
Object.keys(cmu).forEach(function (key) {
Object.keys(cmu[key]).forEach(function (key2) {
var item = cmu[key][key2];
var rule = {
type: 'rule',
selectors: item.selectors,
declarations: [
{
type: 'declaration',
property: 'background-image',
value: item.url
}
]
};
if (key == 'rule') {
if ('rules' in cmu_rules) {
cmu_rules['rules'].push(rule);
} else {
cmu_rules['rules'] = [rule];
}
} else {
if (key in cmu_rules) {
cmu_rules[key][0].rules.push(rule);
} else {
cmu_rules[key] = [{
type: 'media',
media: key,
rules: [rule]
}];
}
}
});
});
Object.keys(cmu_rules).forEach(function (key) {
css.stylesheet.rules.push.apply(css.stylesheet.rules, cmu_rules[key]);
});
}
function gulpCMU() {
return through.obj(function (file, enc, cb) {
if (!file) {
throw new PluginError('gulp-css-merge-url', 'Missing file option gulp-css-merge-url');
}
if (file.isNull()) {
cb();
return;
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-css-merge-url', 'Streaming not supported'));
cb();
return;
}
var css = null;
var CSSObjects = {};
if (file.isBuffer()) {
css = binaryString.fromBuffer(file.contents);
css = cssparse.parse(css);
}
if (typeof css !== 'undefined' && css) {
if (css.type === 'stylesheet' && css.stylesheet.rules.length > 0) {
goCMU(css.stylesheet.rules, CSSObjects);
}
if (file.isBuffer() && Object.keys(CSSObjects).length > 0) {
insertCSSObject(CSSObjects, css);
file.contents = new Buffer(cssparse.stringify(css));
}
}
cb(null, file);
});
}
module.exports = gulpCMU;