Repository navigation
fix(repack): pass inlined asset scales to pickScale in ascending order #1505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RaphaelFakhri
wants to merge
5
commits into
callstack:main
Choose a base branch
from
RaphaelFakhri:fix/inline-assets-scale-order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef27b57
fix(repack): pass inlined asset scales to pickScale in ascending order
RaphaelFakhri 9f83101
Merge branch 'main' into fix/inline-assets-scale-order
dannyhw 43c6bd7
Merge branch 'main' into fix/inline-assets-scale-order
dannyhw cec54ad
test: cover fractional scales end to end and sort them in inlineAssets
RaphaelFakhri 2934bce
Merge branch 'main' into fix/inline-assets-scale-order
dannyhw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
47 changes: 47 additions & 0 deletions
47
packages/repack/src/loaders/assetsLoader/__tests__/inlineAssets.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 47 additions & 3 deletions
50
tests/integration/src/loaders/__snapshots__/rspack/assets-loader.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
50 changes: 47 additions & 3 deletions
50
tests/integration/src/loaders/__snapshots__/webpack/assets-loader.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.