Skip to content

Commit e0dd903

Browse files
committed
Merge pull request #4 from acelan86/master
fix image path
2 parents e4affc9 + 991ecce commit e0dd903

11 files changed

+35
-40
lines changed

lib/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ function imageSetMixin(decl, i) {
353353
obj.path = "url(" + obj.url + ")";
354354
obj.whichImg = paths[j][3];
355355
if (obj.whichImg == "1x" || obj.whichImg == "1x") {
356-
var normalSizes = sizeOf(path.join(process.cwd(), obj.url));
356+
var normalSizes = sizeOf(_getResourceAbsolutePath(obj.url));
357357
normalWidth = normalSizes.width + 'px';
358358
normalHeight = normalSizes.height + 'px';
359359

@@ -362,7 +362,7 @@ function imageSetMixin(decl, i) {
362362
value: obj.path
363363
});
364364
} else if (obj.whichImg == "2x" || obj.whichImg == "2x") {
365-
var rSizes = sizeOf(path.join(process.cwd(), obj.url)); //2倍图尺寸
365+
var rSizes = sizeOf(_getResourceAbsolutePath(obj.url)); //2倍图尺寸
366366
rWidth = rSizes.width / 2 + 'px';
367367
rHeight = rSizes.height / 2 + 'px';
368368
bgSize = rWidth + ' ' + rHeight; //2倍图的宽和高
@@ -405,7 +405,7 @@ function imageSetMixin(decl, i) {
405405
} else if (decl.value.indexOf('url(') != -1) {
406406
//没有image-set,执行以下
407407
var retinaPaths = returnURL(decl.value, reNO_SETURL) //获取第一个url图片的路径
408-
var retinaSizes = sizeOf(path.join(process.cwd(), retinaPaths[0][2]));
408+
var retinaSizes = sizeOf(_getResourceAbsolutePath(retinaPaths[0][2]));
409409
normalWidth = retinaSizes.width + 'px';
410410
normalHeight = retinaSizes.height + 'px';
411411
}
@@ -481,12 +481,24 @@ function returnURL(val, reg) {
481481
return paths;
482482
}
483483

484+
//当前处理文件的路径,可以通过处理函数的opts.from得到
485+
var _currentProcessFilePath = '';
486+
//获取css文件中的资源的绝对地址
487+
function _getResourceAbsolutePath(sourcePath) {
488+
return path.resolve(_currentProcessFilePath, sourcePath);
489+
}
490+
484491
// PostCSS Processor
485-
var cssprocess = function(css) {
492+
var cssprocess = function(css, opts) {
493+
//保存当前处理文件路径
494+
_currentProcessFilePath = path.dirname(opts.from);
486495
css.eachRule(cssgraceRule);
487496
}
488497

489498
var pack = function(css, opts) {
499+
//保存当前处理文件路径
500+
_currentProcessFilePath = path.dirname(opts.from);
501+
console.log(_currentProcessFilePath);
490502
return postcss(cssprocess).process(css, opts).css;
491503
}
492504

test/fixtures/image-set-out.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.foo {
2-
background-image: url(./test/img/yuxifan@1x.jpg);
2+
background-image: url(../img/yuxifan@1x.jpg);
33
background-image: -webkit-image-set(
4-
url(./test/img/yuxifan@1x.jpg) 1x,
5-
url(./test/img/yuxifan@2x.jpg) 2x);
4+
url(../img/yuxifan@1x.jpg) 1x,
5+
url(../img/yuxifan@2x.jpg) 2x);
66
width: 320px;
77
height: 427px;
88
}
@@ -14,7 +14,7 @@
1414
only screen and (min-resolution: 192dpi),
1515
only screen and (min-resolution: 2dppx) {
1616
.foo {
17-
background-image: url(./test/img/yuxifan@2x.jpg);
17+
background-image: url(../img/yuxifan@2x.jpg);
1818
background-size: 320px 427px;
1919
}
2020
}

test/fixtures/image-set-real.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.foo {
2-
background-image: url(./test/img/yuxifan@1x.jpg);
2+
background-image: url(../img/yuxifan@1x.jpg);
33
background-image: -webkit-image-set(
4-
url(./test/img/yuxifan@1x.jpg) 1x,
5-
url(./test/img/yuxifan@2x.jpg) 2x);
4+
url(../img/yuxifan@1x.jpg) 1x,
5+
url(../img/yuxifan@2x.jpg) 2x);
66
width: 320px;
77
height: 427px;
88
}
@@ -14,7 +14,7 @@
1414
only screen and (min-resolution: 192dpi),
1515
only screen and (min-resolution: 2dppx) {
1616
.foo {
17-
background-image: url(./test/img/yuxifan@2x.jpg);
17+
background-image: url(../img/yuxifan@2x.jpg);
1818
background-size: 320px 427px;
1919
}
2020
}

test/fixtures/image-set.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.foo {
22
background-image: -webkit-image-set(
3-
url(./test/img/yuxifan@1x.jpg) 1x,
4-
url(./test/img/yuxifan@2x.jpg) 2x);
3+
url(../img/yuxifan@1x.jpg) 1x,
4+
url(../img/yuxifan@2x.jpg) 2x);
55
width: image-width;
66
height: image-height;
77
}

test/fixtures/image-size-multiple-out.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.foo {
2-
background: url(./test/img/yuxifan@1x.jpg);
2+
background: url(../img/yuxifan@1x.jpg);
33
border-image: url(../image-width/image-height.png);
44
width: 320px;
55
height: 427px;

test/fixtures/image-size-multiple-real.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.foo {
2-
background: url(./test/img/yuxifan@1x.jpg);
2+
background: url(../img/yuxifan@1x.jpg);
33
border-image: url(../image-width/image-height.png);
44
width: 320px;
55
height: 427px;

test/fixtures/image-size-multiple.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.foo {
2-
background: url(./test/img/yuxifan@1x.jpg);
2+
background: url(../img/yuxifan@1x.jpg);
33
border-image: url(../image-width/image-height.png);
44
width: image-width;
55
height: image-height;

test/fixtures/image-size-out.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.foo {
2-
background: url(./test/img/post-and-pre.png);
2+
background: url(../img/post-and-pre.png);
33
width: 720px;
44
height: 719px;
55
}
66

77
.foo {
8-
background: url(./test/img/post-and-pre.png);
8+
background: url(../img/post-and-pre.png);
99
margin: 720px 719px -719px;
1010
content: 'image-width';
1111
}

test/fixtures/image-size-real.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.foo {
2-
background: url(./test/img/post-and-pre.png);
2+
background: url(../img/post-and-pre.png);
33
width: 720px;
44
height: 719px;
55
}
66

77
.foo {
8-
background: url(./test/img/post-and-pre.png);
8+
background: url(../img/post-and-pre.png);
99
margin: 720px 719px -719px;
1010
content: 'image-width';
1111
}

test/fixtures/image-size.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.foo {
2-
background: url(./test/img/post-and-pre.png);
2+
background: url(../img/post-and-pre.png);
33
width: image-width;
44
height: image-height;
55
}
66

77
.foo {
8-
background: url(./test/img/post-and-pre.png);
8+
background: url(../img/post-and-pre.png);
99
margin: image-width image-height -image-height;
1010
content: 'image-width';
1111
}

test/fixtures/rgba-real.css

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)