Skip to content
Merged
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/shaggy-planets-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": minor
---

Support platform specific assets
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.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ describe('assetLoader', () => {
expect(context.Export?.default).toMatchSnapshot();
expect(volume.toTree()).toMatchSnapshot();
});

it('should prefer platform specific asset', async () => {
const platformFixtures = loadFixtures('logo.png', `logo.${platform}.png`);
const { code, volume } = await compileBundle(platform, {
...platformFixtures,
'./index.js': "export { default } from './__fixtures__/logo.png';",
});

const context: { Export?: { default: Record<string, any> } } = {};
vm.runInNewContext(code, context);

expect(context.Export?.default).toMatchSnapshot();
expect(volume.toTree()).toMatchSnapshot();
});
});

describe('should inline asset', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/repack/src/loaders/assetsLoader/assetsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default async function repackAssetsLoader(
resourceExtensionType,
scalableAssetExtensions,
scalableAssetResolutions,
options.platform,
readDirAsync
);

Expand Down
35 changes: 21 additions & 14 deletions packages/repack/src/loaders/assetsLoader/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,35 @@ export async function collectScales(
resourceExtension: string,
scalableAssetExtensions: string[],
scalableAssetResolutions: string[],
platform: string,
readDirAsync: (path: string) => Promise<string[]>
): Promise<CollectedScales> {
if (!scalableAssetExtensions.includes(resourceExtension)) {
return {
'@1x': path.join(
resourceAbsoluteDirname,
resourceFilename + '.' + resourceExtension
),
};
}
// implicit 1x scale
let candidates = [
['@1x', resourceFilename + '.' + resourceExtension],
['@1x', resourceFilename + '.' + platform + '.' + resourceExtension],
];

// explicit scales
const candidates = scalableAssetResolutions.map((scaleKey) => {
const scale = '@' + scaleKey + 'x';
return [scale, resourceFilename + scale + '.' + resourceExtension];
});
// implicit 1x scale
candidates.push(['@1x', resourceFilename + '.' + resourceExtension]);
if (scalableAssetExtensions.includes(resourceExtension)) {
candidates = candidates.concat(
scalableAssetResolutions.flatMap((scaleKey) => {
const scale = '@' + scaleKey + 'x';
return [
[scale, resourceFilename + scale + '.' + resourceExtension],
[
scale,
resourceFilename + scale + '.' + platform + '.' + resourceExtension,
],
];
})
);
}

const contents = await readDirAsync(resourceAbsoluteDirname);
const entries = new Set(contents);

// assets with platform extensions are more specific and take precedence
const collectedScales: Record<string, string> = {};
for (const candidate of candidates) {
const [scaleKey, candidateFilename] = candidate;
Expand Down
Loading