Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/inline-assets-scale-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

Pass scales of inlined assets to `AssetSourceResolver.pickScale` in ascending order. Previously, assets with fractional scales such as `@1.5x` resolved to the wrong variant, for example the `@1.5x` image on a device with a pixel ratio of 3.5.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import vm from 'node:vm';
import { inlineAssets } from '../inlineAssets.js';
import type { Asset } from '../types.js';

// Same logic as `pickScale` from `react-native/Libraries/Image/AssetUtils`,
// which expects `scales` to be sorted in ascending order.
function pickScale(scales: number[], deviceScale: number) {
Comment thread
dannyhw marked this conversation as resolved.
Resolved
for (const scale of scales) {
if (scale >= deviceScale) {
return scale;
}
}
return scales[scales.length - 1] || 1;
}

function evaluateInlinedAsset(code: string, pixelRatio: number) {
const module = { exports: {} as { scale?: number } };
const require = (request: string) =>
request === 'react-native'
? { PixelRatio: { get: () => pixelRatio } }
: { pickScale };

vm.runInNewContext(code, { module, require });
return module.exports;
}

// out of order on purpose, `inlineAssets` must not rely on the caller sorting them
const assets: Asset[] = [2, 0.75, 3, 1.5, 1].map((scale) => ({
data: Buffer.from(`star@${scale}x`),
dimensions: { width: 10, height: 10 },
filename: `star@${scale}x.png`,
scale,
}));

describe('inlineAssets', () => {
it.each([
[0.75, 0.75],
[1, 1],
[1.5, 1.5],
[2.625, 3],
[3.5, 3],
])('picks the right scale for pixel ratio %s', (pixelRatio, expected) => {
const code = inlineAssets({ assets, resourcePath: 'star.png' });

expect(evaluateInlinedAsset(code, pixelRatio).scale).toBe(expected);
});
});
7 changes: 6 additions & 1 deletion packages/repack/src/loaders/assetsLoader/inlineAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export function inlineAssets({
{} as Record<string, URISource>
);

const scales = JSON.stringify(Object.keys(sourceSet).map(Number));
// `pickScale` expects scales in ascending order. They are sorted here rather
// than read with `Object.keys`, which lists integer keys (1, 2, 3) before
// fractional ones (0.75, 1.5).
const scales = JSON.stringify(
assets.map(({ scale }) => scale).sort((a, b) => a - b)
);

/**
* To enable scale resolution in runtime we need to import PixelRatio & AssetSourceResolver
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export function getReactNativeVirtualModules(
};
}
static pickScale(scales, pixelRatio) {
return scales[pixelRatio - 1];
for (var i = 0; i < scales.length; i++) {
if (scales[i] >= pixelRatio) return scales[i];
}
return scales[scales.length - 1] || 1;
}
};`,
};
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions tests/integration/src/loaders/assets-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ describe('assetLoader', () => {

it.each([
{ preferredScale: 1 },
{ preferredScale: 1.5 },
{ preferredScale: 2 },
{ preferredScale: 2.625 },
{ preferredScale: 3 },
])('with scales ($preferredScale)', async ({ preferredScale }) => {
const { code, volume } = await compileBundle(
Expand Down
Loading