-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
148 lines (120 loc) · 3.53 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
var path = require('path');
var fs = require('fs');
var dirname = require('path').dirname;
var postcss = require('postcss');
var chalk = require('chalk');
var _ = require('lodash');
var fastImageSize = require('./lib/fastimagesize');
/**
* a helper to find the real image absolute path,
* deal with the issue like `../../img.jpg` and so on.
*/
function fixAbsolutePath(dir, relative) {
// find the first time
var absolute = path.resolve(dir, relative);
// check if is a image file
var reg = /\.(jpg|jpeg|png|gif|svg|bmp)\b/i;
if (!reg.test(absolute)) {
pluginLog('Not a image file: ', absolute);
return;
}
if (!fs.existsSync(absolute) && (relative.indexOf('../') > -1)) {
relative = relative.replace('../', '');
// find the second time
absolute = path.resolve(dir, relative);
}
return absolute;
}
function pluginLog(str, arg) {
return console.log('[' + chalk.blue('postcss-lazyimagecss') + '] ' + chalk.red(str) + arg);
}
/**
* main function
*/
module.exports = postcss.plugin('lazyimagecss', function (options) {
return function (css) {
options = options || {};
options = _.extend({
width: true,
height: true,
backgroundSize: true,
imagePath: []
}, options);
var imagePath = options.imagePath;
if (imagePath.length) {
imagePath = '(' + options.imagePath.join('|') + '/)';
imagePath = imagePath.replace(/\./g, '\\.');
} else {
imagePath = '';
}
var imageRegex = new RegExp('url\\(["\']?(' + imagePath + '[^)]*?)["\']?\\)');
css.walkRules(function (rule) {
rule.walkDecls(/^background(-image)?$/, function (decl) {
var rule = decl.parent;
var nodes = rule.nodes;
var value = decl.value;
// var prop = decl.prop;
var CSSWidth = false;
var CSSHeight = false;
var CSSBGSize = false;
var matchValue = imageRegex.exec(value);
if (!matchValue || matchValue[1].indexOf('data:') === 0) {
return;
}
var relativePath = matchValue[1];
var inputDir = dirname(decl.source.input.file);
var absolutePath = fixAbsolutePath(inputDir, relativePath);
if (absolutePath === undefined) {
return;
}
nodes.forEach(function (node) {
if (node.prop === 'width') {
CSSWidth = true;
}
if (node.prop === 'height') {
CSSHeight = true;
}
if (node.prop === 'background-size' || node.prop === '-webkit-background-size') {
CSSBGSize = true;
}
});
if (value.indexOf('@2x') > -1) {
options.retina = true;
} else {
options.retina = false;
}
var info = fastImageSize(absolutePath);
if (info === undefined) {
pluginLog('File does not exist: ', absolutePath);
return;
}
if (info.type === 'unknown') {
pluginLog('Unknown type: ', absolutePath);
return;
}
// check if even dimensions
if (value.indexOf('@2x') > -1 && (info.width % 2 !== 0 || info.height % 2 !== 0)) {
pluginLog('Should have even dimensions: ', absolutePath);
return;
}
var valueWidth, valueHeight;
if (options.retina) {
valueWidth = (info.width / 2) + 'px';
valueHeight = (info.height / 2) + 'px';
} else {
valueWidth = info.width + 'px';
valueHeight = info.height + 'px';
}
if (options.width && !CSSWidth) {
rule.append({prop: 'width', value: valueWidth});
}
if (options.height && !CSSHeight) {
rule.append({prop: 'height', value: valueHeight});
}
if (options.backgroundSize && options.retina && !CSSBGSize) {
rule.append({prop: 'background-size', value: valueWidth + ' ' + valueHeight});
}
});
});
};
});