-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.69 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
//
// # Rework Sass Image Functions Plugin
//
/* jslint node: true */
"use strict";
var imagesize = require('imagesize').Parser;
var visit = require('rework-visit');
var fs = require('fs');
var path = require('path');
var VALUES = ['image-width', 'image-height', 'image-size', 'hidpi-image-width', 'hidpi-image-height', 'hidpi-image-size'];
var RE = new RegExp(/(hidpi-)?image-(width|height|size)\(("|')(.+)("|')\)/);
module.exports = function (dir) {
return function (stylesheet) {
visit(stylesheet, function (declarations, rule) {
declarations.forEach(function (rule, index, arr) {
VALUES.forEach(function(value){
if (rule.value && rule.value.indexOf(value) !== -1) {
var dimension = rule.value.replace(RE, "$2");
var filename = rule.value.replace(RE, "$4");
var hidpiFactor = rule.value.replace(RE, "$1") && filename.match(/@2x/) ? 2 : 1;
var size = getImageSize(dir, filename);
if (size) {
if (dimension === "size") {
rule.value = size["width"] / hidpiFactor + "px " + size["height"] / hidpiFactor + "px"
} else {
rule.value = size[dimension] / hidpiFactor + "px"
}
} else {
throw new Error(
'Failed to get image size of: '+ path.join(dir, filename)
);
}
}
});
});
});
};
};
function getImageSize(dir, name) {
var parser = imagesize();
var data = fs.readFileSync(path.join(dir, name));
var result = false;
switch (parser.parse(data)) {
case imagesize.DONE:
result = parser.getResult();
break;
}
return result;
}