Skip to content

Commit

Permalink
Merge 11025fd into d9acf3d
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Dec 30, 2014
2 parents d9acf3d + 11025fd commit 0f83ba7
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 9 deletions.
34 changes: 25 additions & 9 deletions lib/transforms/processImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,25 @@ module.exports = function (queryObj, options) {
targetContentType = imageAsset.contentType,
matchQueryString = imageAsset.url.match(/\?([^#]*)/),
usedQueryString,
leftOverQueryString;
leftOverQueryString,
dpr = imageAsset.devicePixelRatio || 1; // Svg assets as images might not have a dpr

if (matchQueryString) {
var filterInfosAndTargetContentType = getFilterInfosAndTargetContentTypeFromQueryString(matchQueryString[1], {rootPath: urlTools.fileUrlToFsPath(assetGraph.root), sourceFilePath: urlTools.fileUrlToFsPath(imageAsset.nonInlineAncestor.url)});

// Pick out any device pixel ratio setter
var leftOverQueryStringFragments = filterInfosAndTargetContentType.leftOverQueryStringFragments.filter(function (keyValuePair) {
if (/^dpr=\d+(?:[\.,]\d+)?$/.test(keyValuePair)) {
dpr = keyValuePair.split('=')[1];
return false;
} else {
return true;
}
});

usedQueryString = filterInfosAndTargetContentType.usedQueryStringFragments.join('-').replace(/[^a-z0-9\.\-=,]/gi, '-');
leftOverQueryString = leftOverQueryStringFragments.join('&');

if (filterInfosAndTargetContentType.filterInfos.length > 0) {
Array.prototype.push.apply(filters, filterInfosAndTargetContentType.filterInfos.map(function (filterInfo) {
return filterInfo.create();
Expand All @@ -58,11 +73,6 @@ module.exports = function (queryObj, options) {
if (filterInfosAndTargetContentType.targetContentType) {
targetContentType = filterInfosAndTargetContentType.targetContentType;
}

usedQueryString = filterInfosAndTargetContentType.usedQueryStringFragments.join('-').replace(/[^a-z0-9\.\-=,]/gi, '-');
leftOverQueryString = filterInfosAndTargetContentType.leftOverQueryStringFragments ?
filterInfosAndTargetContentType.leftOverQueryStringFragments.join('&') :
'';
}
}

Expand Down Expand Up @@ -96,6 +106,9 @@ module.exports = function (queryObj, options) {
}
}
}

var newUrl = imageAsset.url.replace(/\/([^\.\/]*)([^\?\/]*)\?[^#]*/, '/$1' + (usedQueryString ? '.' + usedQueryString : '') + '$2' + (leftOverQueryString ? '?' + leftOverQueryString : ''));

if (filters.length > 0) {
var callbackCalled = false;
filters.forEach(function (filter, i) {
Expand Down Expand Up @@ -128,26 +141,29 @@ module.exports = function (queryObj, options) {
return;
}
callbackCalled = true;
var rawSrc = Buffer.concat(chunks),
newUrl = imageAsset.url.replace(/\/([^\.\/]*)([^\?\/]*)\?[^#]*/, '/$1.' + usedQueryString + '$2' + (leftOverQueryString ? '?' + leftOverQueryString : ''));
var rawSrc = Buffer.concat(chunks);
if (targetContentType && targetContentType !== imageAsset.contentType) {
var replacementImageAsset = assetGraph.createAsset({
contentType: targetContentType,
type: assetGraph.typeByContentType[targetContentType] || 'Image',
url: newUrl,
rawSrc: rawSrc
rawSrc: rawSrc,
devicePixelRatio: dpr
});
imageAsset.replaceWith(replacementImageAsset);
replacementImageAsset.extension = replacementImageAsset.defaultExtension;
} else {
imageAsset.rawSrc = rawSrc;
imageAsset.url = newUrl;
imageAsset.devicePixelRatio = dpr;
}
cb();
});

filters[0].end(imageAsset.rawSrc);
} else {
imageAsset.url = newUrl;
imageAsset.devicePixelRatio = dpr;
cb();
}
}, cb);
Expand Down
62 changes: 62 additions & 0 deletions test/transforms/processImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,66 @@ describe('processImages', function () {
})
.run(done);
});

it('should apply device pixel ratio to images', function (done) {
new AssetGraph({root: __dirname + '/../../testdata/transforms/processImages/devicePixelRatio/'})
.loadAssets('style.css')
.populate()
.queue(function (assetGraph) {
expect(assetGraph, 'to contain asset', 'Css', 1);
expect(assetGraph, 'to contain asset', 'Png', 9);

expect(_.pluck(assetGraph.findAssets({ isImage: true }), 'devicePixelRatio'), 'to equal', [1, 2, 1, 1, 1, 1, 1, 8, 1]);
})
.processImages()
.queue(function (assetGraph) {
var outputs = [
{
type: 'Png',
devicePixelRatio: 1
},
{
type: 'Png',
devicePixelRatio: 2
},
{
type: 'Png',
devicePixelRatio: 3,
url: /foo\.png$/
},
{
type: 'Png',
devicePixelRatio: 1
},
{
type: 'Png',
devicePixelRatio: 5,
url: /foo\.resize=,200\.png$/
},
{
type: 'Png',
devicePixelRatio: 6,
url: /foo\.resize=,200\.png$/
},
{
type: 'Png',
devicePixelRatio: 7,
url: /foo\.png\?foo=bar$/
},
{
type: 'Png',
devicePixelRatio: 1
},
{
type: 'Jpeg',
devicePixelRatio: 9
}
];

expect(assetGraph.findAssets({ isImage: true }), 'to be an array whose items satisfy', function (img, idx) {
expect(img, 'to satisfy', outputs[idx]);
});
})
.run(done);
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions testdata/transforms/processImages/devicePixelRatio/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.p1 {
background-image: url(foo.png);
}

.p2 {
background-image: url(foo@2x.png);
}

.p3 {
background-image: url(foo.png?dpr=3);
}

.p4 {
background-image: url(foo.png?resize=,200);
}

.p5 {
background-image: url(foo.png?resize=,200&dpr=5);
}

.p6 {
background-image: url(foo.png?dpr=6&resize=,200);
}

.p7 {
background-image: url(foo.png?dpr=7&foo=bar);
}

.p8 {
background-image: url(foo@8x.png?dpr=1);
}

.p9 {
background-image: url(foo.png?setFormat=jpg&dpr=9);
}

0 comments on commit 0f83ba7

Please sign in to comment.