Closed re54k closed 8 years ago
Hi, Can you paste your configuration so I can reproduce the bug?
Configuration:
{
stylesheetPath: './src/css',
spritePath: './src/img/',
filterBy: function(img) {
if ( /\/sp\-/.test(img.url) ) {
return Promise.resolve();
}
return Promise.reject();
},
groupBy: function(img) {
var match = img.url.match(/\/(sp\-[^\/]+)\//);
return match ? Promise.resolve(match[1]) : Promise.reject();
}
}
With code _path2.default.relative(opts.stylesheetPath, image.spritePath);
sprite path will always be same path relative to options.stylesheetPath. But the expected url should be relative path from sprite path to css file which use this sprite.
I think that it's better to use the onUpdateRule
hook to rewrite the url of sprite.
var path = require('path');
var sprites = require('postcss-sprites').default;
var spritesUpdateRule = require('postcss-sprites').updateRule;
var opts = {
stylesheetPath: './src/css',
spritePath: './src/img/',
filterBy: function(img) {
if ( /\/sp\-/.test(img.url) ) {
return Promise.resolve();
}
return Promise.reject();
},
groupBy: function(img) {
var match = img.url.match(/\/(sp\-[^\/]+)\//);
return match ? Promise.resolve(match[1]) : Promise.reject();
},
hooks: {
onUpdateRule: function(rule, comment, image) {
image.spriteUrl = path.relative(rule.source.input.file, path.resolve(image.spritePath));
// Call the built-in logic
spritesUpdateRule(rule, comment, image);
}
}
}
See Output Dimensions for detailed usage of onUpdateRule
hook.
It looks better. Thank you!
Hi, I got a wrong path when using sprites in different level directories. e.g.
Code in
x.css
:Code in
y.css
:After transforming,
y.css
got a wrong url path:Right url should be
../../img/sp-num2.png
.Review the source code, line 459:
should be replaced by:
Please check this.