Skip to content

Instantly share code, notes, and snippets.

@pugson
Forked from lauridskern/withExpoP3.ts
Created March 10, 2025 12:48
Show Gist options
  • Save pugson/c4daa584f9588cd585d75cc22db2a7b6 to your computer and use it in GitHub Desktop.
Save pugson/c4daa584f9588cd585d75cc22db2a7b6 to your computer and use it in GitHub Desktop.
Expo config plugin to enable P3 colors
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