Created
March 10, 2025 11:36
-
-
Save lauridskern/29312e396bbc97690227ab20e2557430 to your computer and use it in GitHub Desktop.
Expo config plugin to enable P3 colors
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
const { withAppDelegate } = require("expo/config-plugins"); | |
const { | |
mergeContents, | |
} = require("@expo/config-plugins/build/utils/generateCode"); | |
const withExpoP3 = (config) => { | |
return withAppDelegate(config, (config) => { | |
if ( | |
config.modResults.language === "objc" || | |
config.modResults.language === "objcpp" | |
) { | |
config.modResults.contents = modifyObjCAppDelegate( | |
config.modResults.contents, | |
); | |
} else if (config.modResults.language === "swift") { | |
config.modResults.contents = modifySwiftAppDelegate( | |
config.modResults.contents, | |
); | |
} else { | |
console.warn( | |
`Unable to modify AppDelegate: Unsupported language ${config.modResults.language}`, | |
); | |
} | |
return config; | |
}); | |
}; | |
function modifyObjCAppDelegate(appDelegateContent) { | |
// Add import statement | |
appDelegateContent = mergeContents({ | |
src: appDelegateContent, | |
newSrc: "#import <React/RCTConvert.h>", | |
anchor: /#import "AppDelegate\.h"/, | |
offset: 1, | |
tag: "expo-p3-import", | |
comment: "//", | |
}).contents; | |
// Add RCTSetDefaultColorSpace call | |
appDelegateContent = mergeContents({ | |
src: appDelegateContent, | |
newSrc: " RCTSetDefaultColorSpace(RCTColorSpaceDisplayP3);", | |
anchor: | |
/return \[super application:application didFinishLaunchingWithOptions:launchOptions\];/, | |
offset: 0, | |
tag: "expo-p3-setup", | |
comment: "//", | |
}).contents; | |
return appDelegateContent; | |
} | |
function modifySwiftAppDelegate(appDelegateContent) { | |
// Add import statement | |
appDelegateContent = mergeContents({ | |
src: appDelegateContent, | |
newSrc: "import React", | |
anchor: /import UIKit/, | |
offset: 1, | |
tag: "expo-p3-import", | |
comment: "//", | |
}).contents; | |
// Add RCTSetDefaultColorSpace call | |
appDelegateContent = mergeContents({ | |
src: appDelegateContent, | |
newSrc: " RCTSetDefaultColorSpace(RCTColorSpaceDisplayP3)", | |
anchor: /super\.application\(.*\)/, | |
offset: 0, | |
tag: "expo-p3-setup", | |
comment: "//", | |
}).contents; | |
return appDelegateContent; | |
} | |
module.exports = withExpoP3; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment