-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
151 lines (114 loc) · 4.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
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
var fs = require('fs');
var path = require('path');
var File = require('vinyl');
var async = require('async');
var _ = require('lodash');
var through = require('through2');
var fastImageSize = require('./lib/fastimagesize');
var css = require('css');
function lazyImageCSS(options) {
options = _.extend({
width: true,
height: true,
backgroundSize: true,
imagePath: []
}, options);
return through.obj(function (file, enc, cb) {
var cssContent = file.contents.toString();
var imagePath = options.imagePath;
if (imagePath.length) {
imagePath = '(' + options.imagePath.join('|') + '/)';
imagePath = imagePath.replace(/\./g, '\\.');
} else {
imagePath = '';
}
var imageRegex = new RegExp('url\\(["\']?(' + imagePath + '[^)]*?)["\']?\\)');
var obj = css.parse(cssContent);
function setProperty(property, value) {
return {
"type": "declaration",
"property": property,
"value": value
};
}
function parseRules(rules) {
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (rule.type === 'keyframes') {
parseRules(rule.keyframes);
}
if (rule.type === 'media') {
parseRules(rule.rules);
}
if (rule.type === 'rule' || rule.type === 'keyframe') {
var declarations = rule.declarations;
var code = {};
declarations.forEach(function (declaration) {
code[declaration.property] = declaration.value;
});
var property;
var hasImage = false;
if (code['background-image']) {
property = 'background-image';
hasImage = true;
} else if (code['background']) {
property = 'background';
hasImage = true;
}
if (!hasImage) {
continue;
}
var value = code[property];
var matchValue = imageRegex.exec(value);
if (!matchValue || matchValue[1].indexOf('data:') === 0) {
continue;
}
var relativePath = matchValue[1];
var absolutePath = path.join(path.dirname(file.path), relativePath);
if (value.indexOf('@2x') > -1) {
options.retina = true;
} else {
options.retina = false;
}
var info = fastImageSize(absolutePath);
if (info.type === 'unknown') {
console.log('' + 'unknown type: ' + absolutePath);
continue;
}
var width, height, newDeclaration;
if (options.retina) {
width = info.width / 2 + 'px';
height = info.height / 2 + 'px';
} else {
width = info.width + 'px';
height = info.height + 'px';
}
if (options.width && !code['width']) {
newDeclaration = setProperty('width', width);
declarations.push(newDeclaration);
}
if (options.height && !code['height']) {
newDeclaration = setProperty('height', height);
declarations.push(newDeclaration);
}
if (options.backgroundSize && (options.retina || info.type == 'svg') && !code['background-size'] && !code['-webkit-background-size']) {
newDeclaration = newDeclaration = setProperty('background-size', width + ' ' + height);
declarations.push(newDeclaration);
}
}
}
}
if (obj.stylesheet.rules.length > 0) {
parseRules(obj.stylesheet.rules);
cssContent = css.stringify(obj);
}
this.push(new File({
base: file.base,
path: file.path,
contents: new Buffer(cssContent)
}));
cb();
});
}
// Exporting the plugin main function
module.exports = lazyImageCSS;