Skip to content

Commit

Permalink
Revert "Use asset catalog for ios images"
Browse files Browse the repository at this point in the history
This reverts commit ce0652c.
  • Loading branch information
janicduplessis committed Oct 1, 2022
1 parent ce0652c commit 7963c59
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 131 deletions.
70 changes: 0 additions & 70 deletions packages/cli-plugin-metro/src/commands/bundle/assetCatalogIOS.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function getAndroidResourceFolderName(
return androidFolder;
}

function getResourceIdentifier(asset: PackagerAsset): string {
function getAndroidResourceIdentifier(asset: PackagerAsset): string {
const folderPath = getBasePath(asset);
return `${folderPath}/${asset.name}`
.toLowerCase()
Expand All @@ -84,6 +84,6 @@ function getBasePath(asset: PackagerAsset): string {
export default {
getAndroidAssetSuffix,
getAndroidResourceFolderName,
getResourceIdentifier,
getAndroidResourceIdentifier,
getBasePath,
};
7 changes: 1 addition & 6 deletions packages/cli-plugin-metro/src/commands/bundle/buildBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ export async function buildBundleWithConfig(
});

// When we're done saving bundle output and the assets, we're done.
return await saveAssets(
outputAssets,
args.platform,
args.assetsDest,
args.assetCatalogDest,
);
return await saveAssets(outputAssets, args.platform, args.assetsDest);
} finally {
server.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import path from 'path';

export interface CommandLineArgs {
assetsDest?: string;
assetCatalogDest?: string;
entryFile: string;
resetCache: boolean;
resetGlobalCache: boolean;
Expand Down Expand Up @@ -103,10 +102,6 @@ export default [
description:
'Experimental, transform JS for a specific JS engine. Currently supported: hermes, hermes-canary, default',
},
{
name: '--asset-catalog-dest [string]',
description: 'Path where to create an iOS Asset Catalog for images',
},
{
name: '--reset-cache',
description: 'Removes cached files',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getAssetDestPathAndroid(asset: PackagerAsset, scale: number): string {
asset,
scale,
);
const fileName = assetPathUtils.getResourceIdentifier(asset);
const fileName = assetPathUtils.getAndroidResourceIdentifier(asset);
return path.join(androidFolder, `${fileName}.${asset.type}`);
}

Expand Down
56 changes: 9 additions & 47 deletions packages/cli-plugin-metro/src/commands/bundle/saveAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@
*
*/

import {logger} from '@react-native-community/cli-tools';
import fs from 'fs';
import path from 'path';
import {
cleanAssetCatalog,
getImageSet,
isCatalogAsset,
writeImageSet,
} from './assetCatalogIOS';
import {AssetData} from './buildBundle';
import fs from 'fs';

import filterPlatformAssetScales from './filterPlatformAssetScales';
import getAssetDestPathAndroid from './getAssetDestPathAndroid';
import getAssetDestPathIOS from './getAssetDestPathIOS';
import {logger} from '@react-native-community/cli-tools';
import type {AssetData} from './buildBundle';

interface CopiedFiles {
[src: string]: string;
Expand All @@ -28,23 +23,20 @@ function saveAssets(
assets: AssetData[],
platform: string,
assetsDest: string | undefined,
assetCatalogDest: string | undefined,
) {
if (!assetsDest) {
logger.warn('Assets destination folder is not set, skipping...');
return;
return Promise.resolve();
}

const filesToCopy: CopiedFiles = Object.create(null); // Map src -> dest

const getAssetDestPath =
platform === 'android' ? getAssetDestPathAndroid : getAssetDestPathIOS;

const addAssetToCopy = (asset: AssetData) => {
const filesToCopy: CopiedFiles = Object.create(null); // Map src -> dest
assets.forEach((asset) => {
const validScales = new Set(
filterPlatformAssetScales(platform, asset.scales),
);

asset.scales.forEach((scale, idx) => {
if (!validScales.has(scale)) {
return;
Expand All @@ -53,37 +45,7 @@ function saveAssets(
const dest = path.join(assetsDest, getAssetDestPath(asset, scale));
filesToCopy[src] = dest;
});
};

if (platform === 'ios' && assetCatalogDest != null) {
// Use iOS Asset Catalog for images. This will allow Apple app thinning to
// remove unused scales from the optimized bundle.
const catalogDir = path.join(assetCatalogDest, 'RNAssets.xcassets');
if (!fs.existsSync(catalogDir)) {
logger.error(
`Could not find asset catalog 'RNAssets.xcassets' in ${assetCatalogDest}. Make sure to create it if it does not exist.`,
);
return;
}

logger.info('Adding images to asset catalog', catalogDir);
cleanAssetCatalog(catalogDir);
for (const asset of assets) {
if (isCatalogAsset(asset)) {
const imageSet = getImageSet(
catalogDir,
asset,
filterPlatformAssetScales(platform, asset.scales),
);
writeImageSet(imageSet);
} else {
addAssetToCopy(asset);
}
}
logger.info('Done adding images to asset catalog');
} else {
assets.forEach(addAssetToCopy);
}
});

return copyAll(filesToCopy);
}
Expand All @@ -95,7 +57,7 @@ function copyAll(filesToCopy: CopiedFiles) {
}

logger.info(`Copying ${queue.length} asset files`);
return new Promise<void>((resolve, reject) => {
return new Promise((resolve, reject) => {
const copyNext = (error?: NodeJS.ErrnoException) => {
if (error) {
reject(error);
Expand Down

0 comments on commit 7963c59

Please sign in to comment.