Skip to content

Commit 502982e

Browse files
committed
Fig config plugin
1 parent ae8754f commit 502982e

File tree

5 files changed

+206
-109
lines changed

5 files changed

+206
-109
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'ts-node/register'
2+
import { ExpoConfig, ConfigContext } from '@expo/config'
3+
4+
export default ({ config }: ConfigContext): ExpoConfig => ({
5+
name: 'ipad-multiscenes-headless-example',
6+
slug: 'ipad-multiscenes-headless-example',
7+
version: '1.0.0',
8+
orientation: 'portrait',
9+
scheme: 'ipad-miltiscenes',
10+
userInterfaceStyle: 'automatic',
11+
splash: {
12+
resizeMode: 'contain',
13+
backgroundColor: '#ffffff',
14+
},
15+
ios: {
16+
supportsTablet: true,
17+
bundleIdentifier: 'com.externaldisplay.ipad-example',
18+
infoPlist: {
19+
UIApplicationSceneManifest: {
20+
UIApplicationSupportsMultipleScenes: true,
21+
},
22+
UIRequiresFullScreen: false,
23+
},
24+
},
25+
plugins: ['./withMultipleSceneSupport.ts'],
26+
})

apps/ipad-multiscenes-headless-example/app.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

apps/ipad-multiscenes-headless-example/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"main": "./index.tsx",
1010
"dependencies": {
11+
"@expo/config-plugins": "^8.0.10",
1112
"expo": "~51.0.28",
1213
"expo-system-ui": "~3.0.7",
1314
"react": "18.2.0",
@@ -19,6 +20,7 @@
1920
"devDependencies": {
2021
"@babel/core": "^7.20.0",
2122
"@types/react": "~18.2.45",
23+
"ts-node": "^10.9.2",
2224
"typescript": "~5.3.3"
2325
},
2426
"private": true,
Lines changed: 29 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,45 @@
1-
import { ConfigPlugin, IOSConfig, BaseMods, withMod } from 'expo/config-plugins'
2-
import fs from 'fs'
1+
import { ConfigPlugin, withAppDelegate } from '@expo/config-plugins'
2+
import * as fs from 'fs'
3+
import * as path from 'path'
34

4-
/**
5-
* A plugin which adds new base modifiers to the prebuild config.
6-
*/
7-
export function withAppDelegateBaseMod(config) {
8-
return (
9-
BaseMods.withGeneratedBaseMods <
10-
'appDelegate' >
11-
(config,
12-
{
13-
platform: 'ios',
14-
providers: {
15-
// Append a custom rule to supply AppDelegate data to mods on `mods.ios.appDelegate`
16-
appDelegate:
17-
BaseMods.provider <
18-
IOSConfig.Paths.AppDelegateProjectFile >
19-
{
20-
// Get the local filepath that should be passed to the `read` method.
21-
getFilePath({ modRequest: { projectRoot } }) {
22-
return IOSConfig.Paths.getAppDelegateFilePath(projectRoot)
23-
},
24-
// Read the input file from the filesystem.
25-
async read(filePath) {
26-
return IOSConfig.Paths.getFileInfo(filePath)
27-
},
28-
// Write the resulting output to the filesystem.
29-
async write(filePath: string, { modResults: { contents } }) {
30-
// Modify the AppDelegate.m/mm file's contents
31-
const modifiedContents = modifyAppDelegate(contents)
32-
await fs.promises.writeFile(filePath, modifiedContents)
33-
},
34-
},
35-
},
36-
})
37-
)
38-
}
39-
40-
/**
41-
* Function to modify the AppDelegate.m/mm file contents.
42-
*/
43-
function modifyAppDelegate(contents) {
5+
// Helper function to modify AppDelegate.m
6+
function modifyAppDelegate(appDelegate: string): string {
447
const importStatement = `#import "RNExternalDisplayUtils.h"`
458

469
// Add the import statement if it's not already present
47-
if (!contents.includes(importStatement)) {
48-
contents = `${importStatement}\n${contents}`
10+
if (!appDelegate.includes(importStatement)) {
11+
appDelegate = `${importStatement}\n${appDelegate}`
4912
}
5013

51-
const method = `
52-
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) {
53-
UISceneConfiguration * configuration =
54-
[RNExternalAppDelegateUtil application:application
55-
configurationForConnectingSceneSession:connectingSceneSession
56-
options:options
57-
sceneOptions:@{
58-
@"headless": @YES
59-
}
60-
];
61-
return configuration;
62-
}
63-
`
14+
const customMethod = `
15+
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) {
16+
UISceneConfiguration * configuration =
17+
[RNExternalAppDelegateUtil application:application
18+
configurationForConnectingSceneSession:connectingSceneSession
19+
options:options
20+
sceneOptions:@{
21+
@"headless": @YES
22+
}
23+
];
24+
return configuration;
25+
}
26+
`
6427

65-
// Add the method if it's not already present
66-
if (
67-
!contents.includes(
68-
'application:(UIApplication *)application configurationForConnectingSceneSession',
69-
)
70-
) {
71-
contents = contents.replace('@end', `${method}\n@end`)
28+
// Insert the method before the '@end' in AppDelegate
29+
if (!appDelegate.includes('configurationForConnectingSceneSession')) {
30+
appDelegate = appDelegate.replace('@end', `${customMethod}\n@end`)
7231
}
7332

74-
return contents
33+
return appDelegate
7534
}
7635

77-
/**
78-
* (Utility) Provides the AppDelegate file for modification.
79-
*/
80-
export const withAppDelegate: ConfigPlugin<
81-
Mod<IOSConfig.Paths.AppDelegateProjectFile>,
82-
> = (config, action) => {
83-
return withMod(config, {
84-
platform: 'ios',
85-
mod: 'appDelegate',
86-
action,
87-
})
88-
}
36+
// Config plugin to modify AppDelegate.m
37+
const withMultipleSceneSupport: ConfigPlugin = (config) => {
38+
return withAppDelegate(config, async (config) => {
39+
config.modResults.contents = modifyAppDelegate(config.modResults.contents)
8940

90-
// (Example) Log the contents of the AppDelegate mod results.
91-
export const withSimpleAppDelegateMod = (config) => {
92-
return withAppDelegate(config, (config) => {
93-
console.log('modify AppDelegate:', config.modResults)
9441
return config
9542
})
9643
}
9744

98-
export default withAppDelegateBaseMod
45+
export default withMultipleSceneSupport

0 commit comments

Comments
 (0)