Skip to content

Commit

Permalink
Merge pull request #73 from evandavis/file-relative-sprites
Browse files Browse the repository at this point in the history
locate sprites relative to the stylesheet
  • Loading branch information
vvasilev- committed Nov 22, 2016
2 parents 1d0c2b3 + e910a7c commit b7d3019
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core.js
Expand Up @@ -30,7 +30,7 @@ const TYPE_VECTOR = 'vector';
*/
export const defaults = {
basePath: './',
stylesheetPath: './',
stylesheetPath: null,
spritePath: './',
relativeTo: 'file',
filterBy: [],
Expand Down Expand Up @@ -403,7 +403,7 @@ export function updateReferences(root, opts, images, spritesheets) {
// Update the rule with background declarations
if (image) {
// Generate CSS url to sprite
image.spriteUrl = path.relative(opts.stylesheetPath, image.spritePath);
image.spriteUrl = path.relative(opts.stylesheetPath || path.dirname(root.source.input.file), image.spritePath);
image.spriteUrl = image.spriteUrl.split(path.sep).join('/');

// Update rule
Expand Down
18 changes: 18 additions & 0 deletions test/10-update-references.js
Expand Up @@ -39,6 +39,24 @@ test('should update CSS declarations', async (t) => {
t.deepEqual(root.toString(), expected);
});

test('should update CSS declarations with relative paths', async (t) => {
const input = await readFileAsync('./fixtures/relative/style.css');
const expected = await readFileAsync('./expectations/relative/style.css', 'utf8');
const ast = postcss.parse(input, { from: './fixtures/relative/style.css' });
let images, spritesheets, opts, root;

t.context.opts.spritePath = './build/relative';

[ opts, images ] = await extractImages(ast, t.context.opts);
[ root, opts, images ] = await setTokens(ast, t.context.opts, images);
[ opts, images, spritesheets ] = await runSpritesmith(t.context.opts, images);
[ opts, images, spritesheets ] = await saveSpritesheets(t.context.opts, images, spritesheets);
[ opts, images, spritesheets ] = await mapSpritesheetProps(t.context.opts, images, spritesheets);
[ root, opts, images, spritesheets ] = await updateReferences(root, t.context.opts, images, spritesheets);

t.deepEqual(root.toString(), expected);
});

test('should use function provided by onUpdateRule hook', async (t) => {
const input = await readFileAsync('./fixtures/basic/style.css');
const expected = await readFileAsync('./expectations/basic-on-update-rule-hook/style.css', 'utf8');
Expand Down
10 changes: 10 additions & 0 deletions test/11-e2e.js
Expand Up @@ -86,6 +86,16 @@ test('absolute path', async (t) => {
return run(inputPath, expectedPath, opts, t);
});

test('relative path', async (t) => {
const inputPath = './fixtures/relative/style.css';
const expectedPath = './expectations/relative/style.css';
const opts = {
spritePath: './build/relative/'
};

return run(inputPath, expectedPath, opts, t);
});

test('filter by', async (t) => {
const inputPath = './fixtures/filter-by/style.css';
const expectedPath = './expectations/filter-by/style.css';
Expand Down
2 changes: 2 additions & 0 deletions test/expectations/relative/style.css
@@ -0,0 +1,2 @@
.selector-a { background-image: url(../../build/relative/sprite.png); background-position: 0px 0px; }
.selector-b { background-image: url(../../build/relative/sprite.png); background-position: -25px 0px; }
Binary file added test/fixtures/relative/images/circle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/relative/images/square.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/fixtures/relative/style.css
@@ -0,0 +1,2 @@
.selector-a { background-image: url(./images/circle.png); }
.selector-b { background: url(./images/square.png) no-repeat 0 0; }

0 comments on commit b7d3019

Please sign in to comment.