Skip to content

Commit 81b4e8f

Browse files
committed
fix(jpeg): android jpeg drawable resources
1 parent b8b1e8e commit 81b4e8f

7 files changed

Lines changed: 55 additions & 8 deletions

File tree

‎.changeset/sharp-jpegs-draw.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Fix `.jpeg` images not showing in Android release builds. They were emitted to `raw` instead of `drawable-*`, where React Native looks for them.
6+
7+
If you have a `.jpeg` and a `.png`/`.jpg`/`.gif`/`.webp` image with the same name in the same directory (e.g. `logo.png` and `logo.jpeg`), rename one of them. Both now resolve to the same Android drawable resource name, and the build fails with a duplicate resources error.

‎packages/repack/src/loaders/assetsLoader/assetsLoader.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const raw = true;
1515

1616
const testXml = /\.(xml)$/;
1717
const testMP4 = /\.(mp4)$/;
18-
const testImages = /\.(png|jpg|gif|webp)$/;
18+
const testImages = /\.(png|jpg|jpeg|gif|webp)$/;
1919
const testFonts = /\.(ttf|otf|ttc)$/;
2020

2121
export default async function repackAssetsLoader(
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

‎tests/integration/src/loaders/assets-loader.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,43 @@ describe('assetLoader', () => {
176176
}
177177
);
178178

179+
it('should emit scaled .jpeg images to matching drawable-* folders', async () => {
180+
const { code, volume } = await compileBundle(
181+
'android',
182+
{
183+
...getReactNativeVirtualModules(),
184+
'./index.js':
185+
"export { default } from './__fixtures__/assets/TestPhoto.jpeg';",
186+
},
187+
false,
188+
undefined,
189+
undefined,
190+
{ extensions: ['jpeg'] }
191+
);
192+
193+
const context: { Export?: { default: Record<string, unknown> } } = {};
194+
vm.runInNewContext(code, context);
195+
196+
expect(context.Export?.default).toMatchObject({
197+
name: 'TestPhoto',
198+
type: 'jpeg',
199+
scales: [1, 2, 3],
200+
});
201+
for (const [scale, destination] of [
202+
[1, 'drawable-mdpi'],
203+
[2, 'drawable-xhdpi'],
204+
[3, 'drawable-xxhdpi'],
205+
]) {
206+
expect(
207+
volume.readFileSync(
208+
`/out/${destination}/__fixtures___assets_testphoto.jpeg`,
209+
'utf8'
210+
)
211+
).toBe(`image fixture jpeg @${scale}x\n`);
212+
}
213+
expect(volume.existsSync('/out/raw')).toBe(false);
214+
});
215+
179216
describe.each([
180217
{ mode: 'iOS', platform: 'ios', devServer: false, remote: false },
181218
{

‎website/src/latest/api/loaders/assets-loader.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ By default, extracted asset files are copied to `assets/` directory for iOS and
1010

1111
When bundling for Android without the development server, extracted assets are placed in the following resource directories:
1212

13-
| Asset | Output directory |
14-
| ---------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
15-
| Images (`png`, `jpg`, `gif`, `webp`) | `drawable-*` (e.g. `drawable-mdpi`, `drawable-hdpi`) per scale |
16-
| Font binaries (`ttf`, `otf`, `ttc`) | `font` **and** `raw` |
17-
| XML font families (`<font-family>`) | `font` |
18-
| Other XML files | `drawable-*` |
19-
| Everything else (other image formats such as `jpeg` or `svg`, video, audio, other files) | `raw` |
13+
| Asset | Output directory |
14+
| --------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
15+
| Images (`png`, `jpg`, `jpeg`, `gif`, `webp`) | `drawable-*` (e.g. `drawable-mdpi`, `drawable-hdpi`) per scale |
16+
| Font binaries (`ttf`, `otf`, `ttc`) | `font` **and** `raw` |
17+
| XML font families (`<font-family>`) | `font` |
18+
| Other XML files | `drawable-*` |
19+
| Everything else (other image formats such as `bmp` or `svg`, video, audio, other files) | `raw` |
2020

2121
`xml` and `ttc` are not included in the default [asset extensions](/api/utils/constants#asset_extensions), so `getAssetTransformRules()` doesn't process them. To bundle them, add a rule that uses the Assets loader for these extensions.
2222

0 commit comments

Comments
 (0)