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.

Revisions

  1. @lauridskern lauridskern created this gist Mar 10, 2025.
    77 changes: 77 additions & 0 deletions withExpoP3.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    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;