Skip to content

Commit

Permalink
Use asset catalog for ios images
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis authored and cipolleschi committed Oct 5, 2022
1 parent b56d709 commit 58b1917
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ package-lock.json
/packages/rn-tester/Pods/*
!/packages/rn-tester/Pods/__offline_mirrors__

# Generated asset catalog
/packages/rn-tester/RNTester/RNAssets.xcassets/*.imageset

# react-native-codegen
/React/FBReactNativeSpec/FBReactNativeSpec
/packages/react-native-codegen/lib
Expand Down
23 changes: 19 additions & 4 deletions Libraries/Image/AssetSourceResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Platform = require('../Utilities/Platform');
const {pickScale} = require('./AssetUtils');
const {
getAndroidResourceFolderName,
getAndroidResourceIdentifier,
getResourceIdentifier,
getBasePath,
} = require('@react-native/assets/path-support');
const invariant = require('invariant');
Expand All @@ -46,7 +46,7 @@ function getScaledAssetPath(asset: PackagerAsset): string {
function getAssetPathInDrawableFolder(asset: PackagerAsset): string {
const scale = pickScale(asset.scales, PixelRatio.get());
const drawableFolder = getAndroidResourceFolderName(asset, scale);
const fileName = getAndroidResourceIdentifier(asset);
const fileName = getResourceIdentifier(asset);
return drawableFolder + '/' + fileName + '.' + asset.type;
}

Expand All @@ -71,6 +71,15 @@ class AssetSourceResolver {
return !!(this.jsbundleUrl && this.jsbundleUrl.startsWith('file://'));
}

isCatalogAsset(): boolean {
return (
this.asset.__packager_asset &&
(this.asset.type === 'png' ||
this.asset.type === 'jpg' ||
this.asset.type === 'jpeg')
);
}

defaultAsset(): ResolvedAssetSource {
if (this.isLoadedFromServer()) {
return this.assetServerURL();
Expand All @@ -81,7 +90,9 @@ class AssetSourceResolver {
? this.drawableFolderInBundle()
: this.resourceIdentifierWithoutScale();
} else {
return this.scaledAssetURLNearBundle();
return this.isCatalogAsset()
? this.assetFromCatalog()
: this.scaledAssetURLNearBundle();
}
}

Expand Down Expand Up @@ -123,6 +134,10 @@ class AssetSourceResolver {
);
}

assetFromCatalog(): ResolvedAssetSource {
return this.fromSource(getResourceIdentifier(this.asset));
}

/**
* The default location of assets bundled with the app, located by
* resource identifier
Expand All @@ -134,7 +149,7 @@ class AssetSourceResolver {
Platform.OS === 'android',
'resource identifiers work on Android',
);
return this.fromSource(getAndroidResourceIdentifier(this.asset));
return this.fromSource(getResourceIdentifier(this.asset));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Image/__tests__/resolveAssetSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('resolveAssetSource', () => {
__packager_asset: true,
width: 100,
height: 200,
uri: 'file:///Path/To/Sample.app/assets/module/a/logo.png',
uri: 'module_a_logo',
scale: 1,
},
);
Expand All @@ -159,7 +159,7 @@ describe('resolveAssetSource', () => {
__packager_asset: true,
width: 100,
height: 200,
uri: 'file:///Path/To/Sample.app/assets/__module/a/logo.png',
uri: '__module_a_logo',
scale: 1,
},
);
Expand Down
5 changes: 4 additions & 1 deletion React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,11 @@ BOOL RCTIsGzippedData(NSData *__nullable data)

static BOOL RCTIsImageAssetsPath(NSString *path)
{
if (path == nil) {
return NO;
}
NSString *extension = [path pathExtension];
return [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
return extension.length == 0 || [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
}

BOOL RCTIsBundleAssetURL(NSURL *__nullable imageURL)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"@react-native-community/cli": "9.1.3",
"@react-native-community/cli-platform-android": "9.1.0",
"@react-native-community/cli-platform-ios": "9.1.2",
"@react-native/assets": "1.0.0",
"@react-native/assets": "1.1.0",
"@react-native/normalize-color": "2.0.0",
"@react-native/polyfills": "2.0.0",
"abort-controller": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/assets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-native/assets",
"version": "1.0.0",
"version": "1.1.0",
"description": "Asset support code for React Native.",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/assets/path-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function getAndroidResourceFolderName(
return 'drawable-' + suffix;
}

function getAndroidResourceIdentifier(asset: PackagerAsset): string {
function getResourceIdentifier(asset: PackagerAsset): string {
return (getBasePath(asset) + '/' + asset.name)
.toLowerCase()
.replace(/\//g, '_') // Encode folder structure in file name
Expand All @@ -87,6 +87,6 @@ function getBasePath(asset: PackagerAsset): string {

module.exports = {
getAndroidResourceFolderName,
getAndroidResourceIdentifier,
getResourceIdentifier,
getBasePath,
};
Binary file not shown.
6 changes: 6 additions & 0 deletions packages/rn-tester/RNTester/RNAssets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
6 changes: 5 additions & 1 deletion packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
05C2F7DDC87A628A70C2D06C /* libPods-RNTesterIntegrationTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 65B6EA573FF170102920BEF4 /* libPods-RNTesterIntegrationTests.a */; };
0CF641B628ECB21C00DCDD11 /* RNAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0CF641B528ECB21C00DCDD11 /* RNAssets.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
272E6B3F1BEA849E001FCF37 /* UpdatePropertiesExampleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 272E6B3C1BEA849E001FCF37 /* UpdatePropertiesExampleView.m */; };
27F441EC1BEBE5030039B79C /* FlexibleSizeExampleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 27F441E81BEBE5030039B79C /* FlexibleSizeExampleView.m */; };
Expand Down Expand Up @@ -79,6 +80,7 @@

/* Begin PBXFileReference section */
0CC3BE1A25DDB68A0033CAEB /* RNTester.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; name = RNTester.entitlements; path = RNTester/RNTester.entitlements; sourceTree = "<group>"; };
0CF641B528ECB21C00DCDD11 /* RNAssets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = RNAssets.xcassets; path = RNTester/RNAssets.xcassets; sourceTree = "<group>"; };
13B07F961A680F5B00A75B9A /* RNTester.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RNTester.app; sourceTree = BUILT_PRODUCTS_DIR; };
13B07FAF1A68108700A75B9A /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppDelegate.h; path = RNTester/AppDelegate.h; sourceTree = "<group>"; };
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = RNTester/Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -230,6 +232,7 @@
5CB07C99226467E60039471C /* RNTesterTurboModuleProvider.mm */,
13B07FB71A68108700A75B9A /* main.m */,
2DDEF00F1F84BF7B00DBDF73 /* Images.xcassets */,
0CF641B528ECB21C00DCDD11 /* RNAssets.xcassets */,
8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */,
680759612239798500290469 /* Fabric */,
272E6B3A1BEA846C001FCF37 /* NativeExampleViews */,
Expand Down Expand Up @@ -407,8 +410,8 @@
161EF0F0977844159431F6A5 /* [CP] Check Pods Manifest.lock */,
13B07F871A680F5B00A75B9A /* Sources */,
13B07F8C1A680F5B00A75B9A /* Frameworks */,
13B07F8E1A680F5B00A75B9A /* Resources */,
68CD48B71D2BCB2C007E06A9 /* Build JS Bundle */,
13B07F8E1A680F5B00A75B9A /* Resources */,
5CF0FD27207FC6EC00C13D65 /* Start Metro */,
2FCDFB64B37634EC8EC3139B /* [CP] Embed Pods Frameworks */,
A8DE6393E2BB72D8FBBF6C27 /* [CP] Copy Pods Resources */,
Expand Down Expand Up @@ -507,6 +510,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0CF641B628ECB21C00DCDD11 /* RNAssets.xcassets in Resources */,
2DDEF0101F84BF7B00DBDF73 /* Images.xcassets in Resources */,
8145AE06241172D900A3F8DA /* LaunchScreen.storyboard in Resources */,
3D2AFAF51D646CF80089D1A3 /* legacy_image@2x.png in Resources */,
Expand Down
5 changes: 5 additions & 0 deletions scripts/react-native-xcode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ if [[ $USE_HERMES != false && $DEV == false ]]; then
EXTRA_ARGS="$EXTRA_ARGS --minify false"
fi

# PRODUCT_SETTINGS_PATH is where the target Info.plist file is. The asset
# catalog will be in the same folder.
ASSET_CATALOG_DEST=${ASSET_CATALOG_DEST:-"$(dirname "$PRODUCT_SETTINGS_PATH")"}

"$NODE_BINARY" $NODE_ARGS "$CLI_PATH" $BUNDLE_COMMAND \
$CONFIG_ARG \
--entry-file "$ENTRY_FILE" \
Expand All @@ -161,6 +165,7 @@ fi
--reset-cache \
--bundle-output "$BUNDLE_FILE" \
--assets-dest "$DEST" \
--asset-catalog-dest "$ASSET_CATALOG_DEST" \
$EXTRA_ARGS \
$EXTRA_PACKAGER_ARGS

Expand Down
3 changes: 3 additions & 0 deletions template/_gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ buck-out/
# Ruby / CocoaPods
/ios/Pods/
/vendor/bundle/

# Generated asset catalog
**/RNAssets.xcassets/*.imageset
8 changes: 6 additions & 2 deletions template/ios/HelloWorld.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
00E356F31AD99517003FC87E /* HelloWorldTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* HelloWorldTests.m */; };
0C80B921A6F3F58F76C31292 /* libPods-HelloWorld.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5DCACB8F33CDC322A6C60F78 /* libPods-HelloWorld.a */; };
13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.mm */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
0C49AA4A252E5346004CE48B /* RNAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0C49AA49252E5346004CE48B /* RNAssets.xcassets */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
7699B88040F8A987B510C191 /* libPods-HelloWorld-HelloWorldTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19F6CBCC0A4E27FBF8BF4A61 /* libPods-HelloWorld-HelloWorldTests.a */; };
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; };
Expand All @@ -30,6 +31,7 @@
00E356EE1AD99517003FC87E /* HelloWorldTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HelloWorldTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
00E356F11AD99517003FC87E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
00E356F21AD99517003FC87E /* HelloWorldTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HelloWorldTests.m; sourceTree = "<group>"; };
0C49AA49252E5346004CE48B /* RNAssets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = RNAssets.xcassets; path = HelloWorld/RNAssets.xcassets; sourceTree = "<group>"; };
13B07F961A680F5B00A75B9A /* HelloWorld.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HelloWorld.app; sourceTree = BUILT_PRODUCTS_DIR; };
13B07FAF1A68108700A75B9A /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppDelegate.h; path = HelloWorld/AppDelegate.h; sourceTree = "<group>"; };
13B07FB01A68108700A75B9A /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = HelloWorld/AppDelegate.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -89,6 +91,7 @@
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
13B07FB01A68108700A75B9A /* AppDelegate.mm */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
0C49AA49252E5346004CE48B /* RNAssets.xcassets */,
13B07FB61A68108700A75B9A /* Info.plist */,
81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */,
13B07FB71A68108700A75B9A /* main.m */,
Expand Down Expand Up @@ -180,8 +183,8 @@
FD10A7F022414F080027D42C /* Start Packager */,
13B07F871A680F5B00A75B9A /* Sources */,
13B07F8C1A680F5B00A75B9A /* Frameworks */,
13B07F8E1A680F5B00A75B9A /* Resources */,
00DD1BFF1BD5951E006B06BC /* Bundle React Native code and images */,
13B07F8E1A680F5B00A75B9A /* Resources */,
00EEFC60759A1932668264C0 /* [CP] Embed Pods Frameworks */,
E235C05ADACE081382539298 /* [CP] Copy Pods Resources */,
);
Expand Down Expand Up @@ -243,6 +246,7 @@
buildActionMask = 2147483647;
files = (
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */,
0C49AA4A252E5346004CE48B /* RNAssets.xcassets in Resources */,
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 6 additions & 0 deletions template/ios/HelloWorld/RNAssets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}

0 comments on commit 58b1917

Please sign in to comment.