forked from expo/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.js
86 lines (73 loc) · 2.45 KB
/
entry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { registerRootComponent } from "expo";
import { ExpoRoot, SplashScreen } from "expo-router";
import { View, Platform } from "react-native";
// Must be exported or Fast Refresh won't update the context >:[
export function App() {
// Babel + Expo CLI: process.env.EXPO_ROUTER_APP_ROOT -> '../../apps/demo/app'
// console.log("output", process.env.EXPO_ROUTER_APP_ROOT);
const ctx = require.context(process.env.EXPO_ROUTER_APP_ROOT);
return <ExpoRoot context={ctx} />;
}
function isBaseObject(obj) {
if (Object.prototype.toString.call(obj) !== "[object Object]") {
return false;
}
const proto = Object.getPrototypeOf(obj);
if (proto === null) {
return true;
}
return proto === Object.prototype;
}
function isErrorShaped(error) {
return (
error &&
typeof error === "object" &&
typeof error.name === "string" &&
typeof error.message === "string"
);
}
/**
* After we throw this error, any number of tools could handle it.
* This check ensures the error is always in a reason state before surfacing it to the runtime.
*/
function convertError(error) {
if (isErrorShaped(error)) {
return error;
}
if (process.env.NODE_ENV === "development") {
if (error == null) {
return new Error("A null/undefined error was thrown.");
}
}
if (isBaseObject(error)) {
return new Error(JSON.stringify(error));
}
return new Error(String(error));
}
(() => {
try {
SplashScreen.preventAutoHideAsync();
registerRootComponent(App);
} catch (e) {
// Hide the splash screen if there was an error so the user can see it.
SplashScreen.hideAsync();
const error = convertError(e);
// Prevent the app from throwing confusing:
// ERROR Invariant Violation: "main" has not been registered. This can happen if:
// * Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
// * A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
registerRootComponent(View);
// Console is pretty useless on native, on web you get interactive stack traces.
if (Platform.OS === "web") {
console.error(error);
console.error(
`A runtime error has occurred while rendering the root component.`
);
}
// Give React a tick to render before throwing.
setTimeout(() => {
throw error;
});
// TODO: Render a production-only error screen.
}
})();