From a117af6ce065dc10ddb171183c0b8a3bf6ac2062 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Thu, 29 Jun 2023 22:07:09 +0200 Subject: [PATCH] fix(runtime): use proper expo-router entrypoint detection --- runtime/src/App.tsx | 10 ++++++---- runtime/src/Files.tsx | 2 +- runtime/src/Modules.tsx | 4 ---- runtime/src/NativeModules/ExpoRouterEntry.tsx | 7 +++++++ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/runtime/src/App.tsx b/runtime/src/App.tsx index 3a3f3a65..e91be08d 100644 --- a/runtime/src/App.tsx +++ b/runtime/src/App.tsx @@ -27,7 +27,7 @@ import * as Logger from './Logger'; import * as Messaging from './Messaging'; import * as Modules from './Modules'; import EXDevLauncher from './NativeModules/EXDevLauncher'; -import { ExpoRouterApp } from './NativeModules/ExpoRouterEntry'; +import { ExpoRouterApp, isExpoRouterEntry } from './NativeModules/ExpoRouterEntry'; import Linking from './NativeModules/Linking'; import { captureRef as takeSnapshotAsync } from './NativeModules/ViewShot'; import getDeviceIdAsync from './NativeModules/getDeviceIdAsync'; @@ -443,12 +443,14 @@ export default class App extends React.Component { await Modules.flush({ changedPaths, changedUris: [rootModuleUri] }); } - // Special handling for Expo Router projects - if (Modules.hasDependency('expo-router')) { + // Handle Expo Router root with a Snack compatible components + if (isExpoRouterEntry(Files.get(Files.entry())?.contents)) { const ctx = await Modules.load(createVirtualModulePath({ directory: 'module://app' })); Logger.info('Updating Expo Router root element'); rootElement = React.createElement(ExpoRouterApp, { ctx }); - } else { + } + // Handle normal default exports + else { const hasRootModuleUri = await Modules.has(rootModuleUri); if (!hasRootModuleUri) { const rootDefaultExport = (await Modules.load(rootModuleUri)).default; diff --git a/runtime/src/Files.tsx b/runtime/src/Files.tsx index 0ea035ce..f1f6fd5c 100644 --- a/runtime/src/Files.tsx +++ b/runtime/src/Files.tsx @@ -136,7 +136,7 @@ export const update = async ({ message }: { message: Message }) => { // Return the entrypoint path export const entry = () => { - const names = ['App.tsx', 'App.ts', 'App.js', 'app.js']; + const names = ['index.js', 'App.tsx', 'App.ts', 'App.js', 'app.js']; for (const name of names) { if (files[name]) { diff --git a/runtime/src/Modules.tsx b/runtime/src/Modules.tsx index 2122c120..6a05ac74 100644 --- a/runtime/src/Modules.tsx +++ b/runtime/src/Modules.tsx @@ -120,10 +120,6 @@ export const updateProjectDependencies = async (newProjectDependencies: Dependen return changedDependencies.map(sanitizeModule); }; -export function hasDependency(name: string) { - return !!projectDependencies[name]; -} - // SystemJS fetch pipeline const _get = (header: { [key: string]: string }, value: string) => header?.hasOwnProperty(value) ? header[value] : null; diff --git a/runtime/src/NativeModules/ExpoRouterEntry.tsx b/runtime/src/NativeModules/ExpoRouterEntry.tsx index e7802118..3f9158bc 100644 --- a/runtime/src/NativeModules/ExpoRouterEntry.tsx +++ b/runtime/src/NativeModules/ExpoRouterEntry.tsx @@ -16,3 +16,10 @@ export function ExpoRouterApp({ ctx }: ExpoRouterAppProps) { ); } + +/** + * Helper method to detect entry points of Expo Router. + */ +export function isExpoRouterEntry(fileContent = '') { + return /import.*expo-router\/entry/i.test(fileContent.trim()); +}