@a16n/web-sim 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/bin/cli.ts +255 -0
- package/bin/web-sim.js +39 -0
- package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
- package/overrides/@react-native-firebase/app/overrides.ts +34 -0
- package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
- package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
- package/overrides/expo-application/ExpoApplication.ts +81 -0
- package/overrides/expo-application/overrides.ts +16 -0
- package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
- package/overrides/expo-background-task/overrides.ts +9 -0
- package/overrides/expo-constants/ExponentConstants.ts +66 -0
- package/overrides/expo-constants/overrides.ts +9 -0
- package/overrides/expo-device/ExpoDevice.ts +104 -0
- package/overrides/expo-device/overrides.ts +9 -0
- package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
- package/overrides/expo-glass-effect/GlassView.tsx +95 -0
- package/overrides/expo-glass-effect/availability.ts +14 -0
- package/overrides/expo-glass-effect/overrides.ts +14 -0
- package/overrides/expo-haptics/Haptics.ts +67 -0
- package/overrides/expo-haptics/overrides.ts +5 -0
- package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
- package/overrides/expo-mail-composer/overrides.ts +9 -0
- package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
- package/overrides/expo-mesh-gradient/overrides.ts +9 -0
- package/overrides/expo-modules-core/overrides.ts +12 -0
- package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
- package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
- package/overrides/expo-navigation-bar/overrides.ts +12 -0
- package/overrides/expo-router/NativeStackView.tsx +74 -0
- package/overrides/expo-router/NativeTabsView.tsx +263 -0
- package/overrides/expo-router/SafeAreaProvider.ts +6 -0
- package/overrides/expo-router/overrides.ts +23 -0
- package/overrides/expo-router/statusBarRequests.ts +51 -0
- package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
- package/overrides/expo-screen-orientation/overrides.ts +21 -0
- package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
- package/overrides/expo-sharing/overrides.ts +9 -0
- package/overrides/expo-sms/ExpoSMS.ts +24 -0
- package/overrides/expo-sms/overrides.ts +9 -0
- package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
- package/overrides/expo-splash-screen/overrides.ts +5 -0
- package/overrides/expo-status-bar/StatusBar.tsx +44 -0
- package/overrides/expo-status-bar/overrides.ts +5 -0
- package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
- package/overrides/expo-store-review/overrides.ts +9 -0
- package/overrides/expo-symbols/SymbolView.tsx +192 -0
- package/overrides/expo-symbols/overrides.ts +9 -0
- package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
- package/overrides/expo-system-ui/overrides.ts +9 -0
- package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
- package/overrides/expo-task-manager/overrides.ts +9 -0
- package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
- package/overrides/expo-tracking-transparency/overrides.ts +11 -0
- package/overrides/expo-video/ExpoVideo.ts +16 -0
- package/overrides/expo-video/overrides.ts +9 -0
- package/overrides/lottie-react-native/LottieView.tsx +87 -0
- package/overrides/lottie-react-native/overrides.ts +9 -0
- package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
- package/overrides/react-native-pager-view/overrides.ts +9 -0
- package/overrides/react-native-reanimated/overrides.ts +28 -0
- package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
- package/overrides/react-native-safe-area-context/overrides.ts +12 -0
- package/overrides/react-native-share/NativeRNShare.ts +22 -0
- package/overrides/react-native-share/overrides.ts +12 -0
- package/overrides/react-native-share/platform.ts +14 -0
- package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
- package/overrides/react-native-turbo-image/overrides.ts +9 -0
- package/overrides/react-native-web/RefreshControl.tsx +57 -0
- package/overrides/react-native-web/overrides.ts +153 -0
- package/overrides/react-native-web/useElementLayout.ts +140 -0
- package/package.json +78 -4
- package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
- package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
- package/plugins/battery/plugin.ts +8 -0
- package/plugins/battery/shell.tsx +56 -0
- package/plugins/battery/state.ts +11 -0
- package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
- package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
- package/plugins/biometrics/plugin.ts +8 -0
- package/plugins/biometrics/shell.tsx +88 -0
- package/plugins/biometrics/state.ts +41 -0
- package/plugins/brightness/app.ts +5 -0
- package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
- package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
- package/plugins/brightness/plugin.ts +9 -0
- package/plugins/brightness/shell.tsx +55 -0
- package/plugins/brightness/state.ts +19 -0
- package/plugins/calendar/calendar.ts +299 -0
- package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
- package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
- package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
- package/plugins/calendar/plugin.ts +8 -0
- package/plugins/calendar/shell.tsx +123 -0
- package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
- package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
- package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
- package/plugins/camera/plugin.ts +9 -0
- package/plugins/camera/shell.tsx +107 -0
- package/plugins/camera/state.ts +61 -0
- package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
- package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
- package/plugins/clipboard/plugin.ts +8 -0
- package/plugins/clipboard/shell.tsx +122 -0
- package/plugins/clipboard/state.ts +93 -0
- package/plugins/clock/app.ts +25 -0
- package/plugins/clock/plugin.ts +8 -0
- package/plugins/clock/shell.tsx +225 -0
- package/plugins/clock/state.ts +12 -0
- package/plugins/contacts/contacts.ts +125 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
- package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
- package/plugins/contacts/plugin.ts +8 -0
- package/plugins/contacts/shell.tsx +109 -0
- package/plugins/home-indicator/app.ts +5 -0
- package/plugins/home-indicator/plugin.ts +9 -0
- package/plugins/home-indicator/requests.ts +48 -0
- package/plugins/home-indicator/shell.tsx +68 -0
- package/plugins/home-indicator/state.ts +31 -0
- package/plugins/keep-awake/app.ts +5 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
- package/plugins/keep-awake/plugin.ts +9 -0
- package/plugins/keep-awake/shell.tsx +80 -0
- package/plugins/keep-awake/state.ts +12 -0
- package/plugins/keyboard/Keyboard.tsx +354 -0
- package/plugins/keyboard/app.ts +90 -0
- package/plugins/keyboard/controller.ts +384 -0
- package/plugins/keyboard/fields.ts +244 -0
- package/plugins/keyboard/icons.tsx +283 -0
- package/plugins/keyboard/keyboard.css +362 -0
- package/plugins/keyboard/layouts/android.ts +175 -0
- package/plugins/keyboard/layouts/index.ts +10 -0
- package/plugins/keyboard/layouts/ios.ts +219 -0
- package/plugins/keyboard/layouts/types.ts +80 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
- package/plugins/keyboard/plugin.ts +13 -0
- package/plugins/keyboard/shell.tsx +133 -0
- package/plugins/keyboard/state.ts +92 -0
- package/plugins/keyboard/suggestions.ts +63 -0
- package/plugins/keyboard/taps.ts +77 -0
- package/plugins/keyboard/typing.ts +112 -0
- package/plugins/locale/app.ts +232 -0
- package/plugins/locale/locale.ts +143 -0
- package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
- package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
- package/plugins/locale/plugin.ts +9 -0
- package/plugins/locale/shell.tsx +147 -0
- package/plugins/locale/state.ts +36 -0
- package/plugins/location/geo.ts +56 -0
- package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
- package/plugins/location/overrides/expo-location/overrides.ts +11 -0
- package/plugins/location/places.ts +38 -0
- package/plugins/location/plugin.ts +8 -0
- package/plugins/location/shell.tsx +188 -0
- package/plugins/location/state.ts +51 -0
- package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
- package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
- package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
- package/plugins/motion/plugin.ts +8 -0
- package/plugins/motion/sensors.ts +116 -0
- package/plugins/motion/shell.tsx +136 -0
- package/plugins/motion/state.ts +46 -0
- package/plugins/network/app.ts +400 -0
- package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
- package/plugins/network/overrides/expo-network/overrides.ts +9 -0
- package/plugins/network/plugin.ts +8 -0
- package/plugins/network/shell.tsx +67 -0
- package/plugins/network/state.ts +29 -0
- package/plugins/notifications/center.ts +255 -0
- package/plugins/notifications/content.ts +36 -0
- package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
- package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
- package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
- package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
- package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
- package/plugins/notifications/plugin.ts +8 -0
- package/plugins/notifications/shell.tsx +192 -0
- package/plugins/notifications/state.ts +56 -0
- package/plugins/notifications/tokens.ts +47 -0
- package/plugins/notifications/trigger.ts +107 -0
- package/plugins/permissions/plugin.ts +6 -0
- package/plugins/permissions/shell.tsx +50 -0
- package/plugins/permissions/state.ts +38 -0
- package/plugins/photos/appLibrary.ts +114 -0
- package/plugins/photos/library.ts +322 -0
- package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
- package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
- package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
- package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
- package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
- package/plugins/photos/plugin.ts +8 -0
- package/plugins/photos/samples.ts +367 -0
- package/plugins/photos/shell.tsx +164 -0
- package/plugins/photos/state.ts +88 -0
- package/plugins/storage/files.ts +351 -0
- package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
- package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
- package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
- package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
- package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
- package/plugins/storage/plugin.ts +8 -0
- package/plugins/storage/reset.ts +136 -0
- package/plugins/storage/secure.ts +71 -0
- package/plugins/storage/shell.tsx +365 -0
- package/plugins/storage/state.ts +13 -0
- package/src/client/app/appDocument.ts +113 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +59 -0
- package/src/client/app/main.tsx +99 -0
- package/src/client/app/routeContext.ts +45 -0
- package/src/client/app/touch.ts +640 -0
- package/src/client/entry.ts +20 -0
- package/src/client/modules.d.ts +8 -0
- package/src/client/react-native/app.ts +57 -0
- package/src/client/react-native/deep.ts +13 -0
- package/src/client/react-native/keyboard.tsx +192 -0
- package/src/client/react-native/library.ts +256 -0
- package/src/client/shell/Cookies.tsx +95 -0
- package/src/client/shell/LogDrawer.tsx +415 -0
- package/src/client/shell/PhotoPicker.tsx +174 -0
- package/src/client/shell/PluginOverlays.tsx +42 -0
- package/src/client/shell/PluginPanels.tsx +82 -0
- package/src/client/shell/ResetApp.tsx +87 -0
- package/src/client/shell/Resizer.tsx +99 -0
- package/src/client/shell/Shell.tsx +531 -0
- package/src/client/shell/SystemBars.tsx +376 -0
- package/src/client/shell/SystemCamera.tsx +181 -0
- package/src/client/shell/SystemOverlay.tsx +422 -0
- package/src/client/shell/SystemSheets.tsx +462 -0
- package/src/client/shell/backdrop.ts +229 -0
- package/src/client/shell/control.ts +91 -0
- package/src/client/shell/events.ts +226 -0
- package/src/client/shell/geometry.ts +68 -0
- package/src/client/shell/host.ts +492 -0
- package/src/client/shell/icons.tsx +34 -0
- package/src/client/shell/logFeed.ts +102 -0
- package/src/client/shell/main.tsx +22 -0
- package/src/client/shell/permissionCopy.ts +169 -0
- package/src/client/shell/plugins.ts +266 -0
- package/src/client/shell/popout.ts +185 -0
- package/src/client/shell/popover.ts +27 -0
- package/src/client/shell/shell.css +878 -0
- package/src/client/shell/statusTime.ts +31 -0
- package/src/client/shell/tooltips.ts +104 -0
- package/src/client/virtual.d.ts +21 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +186 -0
- package/src/node/appSource.ts +138 -0
- package/src/node/config.ts +180 -0
- package/src/node/control.ts +144 -0
- package/src/node/doctor.ts +199 -0
- package/src/node/events/cli.ts +206 -0
- package/src/node/events/shared.ts +238 -0
- package/src/node/events/store.ts +266 -0
- package/src/node/index.ts +5 -0
- package/src/node/overrides/define.ts +56 -0
- package/src/node/overrides/packages.ts +136 -0
- package/src/node/overrides/platformPatch.ts +56 -0
- package/src/node/overrides/plugin.ts +166 -0
- package/src/node/overrides/registry.ts +258 -0
- package/src/node/paths.ts +18 -0
- package/src/node/platform.ts +171 -0
- package/src/node/plugins.ts +98 -0
- package/src/node/proxy.ts +421 -0
- package/src/node/server.ts +253 -0
- package/src/plugin/index.ts +33 -0
- package/src/plugin/types.ts +127 -0
- package/src/plugin/ui.tsx +562 -0
- package/src/runtime/clock.ts +112 -0
- package/src/runtime/draw.ts +196 -0
- package/src/runtime/index.ts +513 -0
- package/src/runtime/media.ts +379 -0
- package/src/runtime/records.ts +76 -0
- package/src/runtime/timezone.ts +81 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Level } from '../../node/events/shared.ts'
|
|
2
|
+
import { getHost } from '../../runtime/index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Puts the app's console output and uncaught errors in the simulator's event log, as `console.<method>` and
|
|
6
|
+
* `app.error` events, so `web-sim logs console` shows what Chrome's DevTools would. The first module the app's frame
|
|
7
|
+
* runs, so it sees the app's first line. The console still prints as before; DevTools skips this file's frames
|
|
8
|
+
* (`sourcemapIgnoreList`), so each line still points at the app's own code.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const LEVELS: Record<string, Level> = { debug: 'debug', log: 'info', info: 'info', warn: 'warn', error: 'error', trace: 'debug' }
|
|
12
|
+
|
|
13
|
+
let logging = false
|
|
14
|
+
|
|
15
|
+
function record(kind: string, level: Level, summary: string, data: unknown): void {
|
|
16
|
+
// A console call made while logging (by the log itself) would loop.
|
|
17
|
+
if (logging) {
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
logging = true
|
|
21
|
+
try {
|
|
22
|
+
getHost().log?.({ kind, level, actor: 'app', summary, data, maxString: 4096 })
|
|
23
|
+
} catch {
|
|
24
|
+
// The shell is going away: the line still reaches the console.
|
|
25
|
+
} finally {
|
|
26
|
+
logging = false
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A value as the console would show it on one line. */
|
|
31
|
+
function show(value: unknown): string {
|
|
32
|
+
if (typeof value === 'string') {
|
|
33
|
+
return value
|
|
34
|
+
}
|
|
35
|
+
if (value instanceof Error || (typeof value === 'object' && value !== null && 'stack' in value && 'message' in value)) {
|
|
36
|
+
const error = value as Error
|
|
37
|
+
return `${error.name}: ${error.message}`
|
|
38
|
+
}
|
|
39
|
+
if (typeof value === 'function') {
|
|
40
|
+
return `ƒ ${value.name || 'anonymous'}()`
|
|
41
|
+
}
|
|
42
|
+
if (typeof value === 'object' && value !== null) {
|
|
43
|
+
try {
|
|
44
|
+
const text = JSON.stringify(value)
|
|
45
|
+
return text.length > 200 ? `${text.slice(0, 199)}…` : text
|
|
46
|
+
} catch {
|
|
47
|
+
return Object.prototype.toString.call(value)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return String(value)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The arguments as one line, with the console's `%s %d %i %f %o %O %c` substitutions in a leading string. */
|
|
54
|
+
export function formatArgs(args: unknown[]): string {
|
|
55
|
+
const rest = [...args]
|
|
56
|
+
let head = ''
|
|
57
|
+
if (typeof rest[0] === 'string' && rest[0].includes('%')) {
|
|
58
|
+
const format = rest.shift() as string
|
|
59
|
+
head = format.replace(/%[sdifoOc%]/g, (token) => {
|
|
60
|
+
if (token === '%%') {
|
|
61
|
+
return '%'
|
|
62
|
+
}
|
|
63
|
+
if (rest.length === 0) {
|
|
64
|
+
return token
|
|
65
|
+
}
|
|
66
|
+
const value = rest.shift()
|
|
67
|
+
switch (token) {
|
|
68
|
+
case '%c':
|
|
69
|
+
return ''
|
|
70
|
+
case '%d':
|
|
71
|
+
case '%i':
|
|
72
|
+
return String(Math.trunc(Number(value)))
|
|
73
|
+
case '%f':
|
|
74
|
+
return String(Number(value))
|
|
75
|
+
default:
|
|
76
|
+
return show(value)
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
const text = [head, ...rest.map(show)].filter((part, index) => index > 0 || part !== '').join(' ')
|
|
81
|
+
return text.length > 1000 ? `${text.slice(0, 999)}…` : text
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (const method of Object.keys(LEVELS) as (keyof Console)[]) {
|
|
85
|
+
const original = console[method] as (...args: unknown[]) => void
|
|
86
|
+
if (typeof original !== 'function') {
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
console[method] = function (this: Console, ...args: unknown[]) {
|
|
90
|
+
const data: Record<string, unknown> = { args }
|
|
91
|
+
if (method === 'trace' || method === 'error') {
|
|
92
|
+
data.stack = new Error().stack?.split('\n').slice(2).join('\n')
|
|
93
|
+
}
|
|
94
|
+
record(`console.${method}`, LEVELS[method], formatArgs(args), data)
|
|
95
|
+
return original.apply(this, args)
|
|
96
|
+
} as never
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
window.addEventListener('error', (event) => {
|
|
100
|
+
const error = event.error as Error | undefined
|
|
101
|
+
record('app.error', 'error', `Uncaught ${error?.name ?? 'Error'}: ${error?.message ?? event.message}`, {
|
|
102
|
+
message: error?.message ?? event.message,
|
|
103
|
+
stack: error?.stack,
|
|
104
|
+
source: event.filename,
|
|
105
|
+
line: event.lineno,
|
|
106
|
+
column: event.colno,
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
window.addEventListener('unhandledrejection', (event) => {
|
|
111
|
+
const reason = event.reason as unknown
|
|
112
|
+
record('app.error', 'error', `Unhandled rejection: ${show(reason)}`, { reason, stack: (reason as Error | undefined)?.stack })
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
/** An error the app's error boundary or its start caught, which never reaches `window.onerror`. */
|
|
116
|
+
export function reportCrash(error: unknown, componentStack?: string): void {
|
|
117
|
+
record('app.error', 'error', `App crashed: ${show(error)}`, { error, componentStack })
|
|
118
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Platform } from '../../devices/types.ts'
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface Window {
|
|
5
|
+
__webSimFontFamily?: (family: string) => string
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const SYSTEM = {
|
|
10
|
+
ios: '-apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro", system-ui, "Helvetica Neue", "Apple Color Emoji", sans-serif',
|
|
11
|
+
android: 'Roboto, "Google Sans", system-ui, "Noto Color Emoji", sans-serif',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Family names the platform knows that a browser does not, or knows differently. Android names its families by
|
|
16
|
+
* role; iOS's `ui-*` designs are Safari-only, so Chrome is given the fonts behind them, where installed.
|
|
17
|
+
*/
|
|
18
|
+
const ALIASES: Record<Platform, Record<string, string>> = {
|
|
19
|
+
ios: {
|
|
20
|
+
'system-ui': SYSTEM.ios,
|
|
21
|
+
'ui-rounded': '"SF Pro Rounded", ui-rounded',
|
|
22
|
+
'ui-serif': '"New York", ui-serif, Georgia, serif',
|
|
23
|
+
'ui-monospace': '"SF Mono", ui-monospace, Menlo, monospace',
|
|
24
|
+
monospace: '"SF Mono", Menlo, monospace',
|
|
25
|
+
'Courier New': '"Courier New", Courier, monospace',
|
|
26
|
+
},
|
|
27
|
+
android: {
|
|
28
|
+
normal: 'Roboto',
|
|
29
|
+
'sans-serif': 'Roboto',
|
|
30
|
+
'sans-serif-light': 'Roboto',
|
|
31
|
+
'sans-serif-thin': 'Roboto',
|
|
32
|
+
'sans-serif-medium': 'Roboto',
|
|
33
|
+
'sans-serif-black': 'Roboto',
|
|
34
|
+
'sans-serif-condensed': '"Roboto Condensed", Roboto',
|
|
35
|
+
serif: '"Noto Serif", serif',
|
|
36
|
+
monospace: '"Roboto Mono", "Droid Sans Mono", monospace',
|
|
37
|
+
'system-ui': SYSTEM.android,
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Fonts the Android system ships, loaded so Android's family names have something to draw with. */
|
|
42
|
+
export const ANDROID_FONTS =
|
|
43
|
+
'https://fonts.googleapis.com/css2?family=Roboto:wght@100;300;400;500;700;900&family=Roboto+Mono:wght@400;500;700&family=Roboto+Condensed:wght@400;700&family=Noto+Serif:wght@400;700&display=swap'
|
|
44
|
+
|
|
45
|
+
export function installFonts(platform: Platform): void {
|
|
46
|
+
const aliases = ALIASES[platform]
|
|
47
|
+
document.documentElement.style.setProperty('--web-sim-system-font', SYSTEM[platform])
|
|
48
|
+
window.__webSimFontFamily = (value: string) => {
|
|
49
|
+
const families = value
|
|
50
|
+
.split(/\s*,\s*/)
|
|
51
|
+
.map((family) => family.replace(/^["']|["']$/g, ''))
|
|
52
|
+
.map((family) => (family === 'System' ? 'var(--web-sim-system-font)' : (aliases[family] ?? quote(family))))
|
|
53
|
+
return `${families.join(', ')}, var(--web-sim-system-font)`
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function quote(family: string): string {
|
|
58
|
+
return /^[\w-]+$/.test(family) ? family : `"${family}"`
|
|
59
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// First, so the event log has the app's console from its first line.
|
|
2
|
+
import { reportCrash } from './console.ts'
|
|
3
|
+
import './appFocus.ts'
|
|
4
|
+
import './appDocument.ts'
|
|
5
|
+
import { Component, type ErrorInfo, type ReactNode } from 'react'
|
|
6
|
+
import { createRoot } from 'react-dom/client'
|
|
7
|
+
import { AppRegistry } from 'react-native'
|
|
8
|
+
import { getHost, getState } from '../../runtime/index.ts'
|
|
9
|
+
import { routeContext } from './routeContext.ts'
|
|
10
|
+
import { enableDragScroll, showFingerCursor } from './touch.ts'
|
|
11
|
+
|
|
12
|
+
enableDragScroll(document)
|
|
13
|
+
showFingerCursor(document)
|
|
14
|
+
|
|
15
|
+
const rootElement = document.getElementById('root')!
|
|
16
|
+
|
|
17
|
+
/** Once the app has drawn its first frame, the splash screen hides, unless the app holds it. */
|
|
18
|
+
function reportFirstRender() {
|
|
19
|
+
requestAnimationFrame(() => requestAnimationFrame(() => getHost().appRendered()))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function start() {
|
|
23
|
+
// Loaded here, not imported above: it imports every route, so a route (or anything it imports) that throws as it
|
|
24
|
+
// loads lands in `start`'s catch and shows on the screen, rather than leaving it blank.
|
|
25
|
+
const { loadEntry, mode, routes, routesDir } = await import('virtual:web-sim/app-config')
|
|
26
|
+
if (mode === 'router' && routes !== null && routesDir !== null) {
|
|
27
|
+
const { ExpoRoot } = await import('expo-router')
|
|
28
|
+
const context = routeContext(routes, routesDir, getState().device.platform)
|
|
29
|
+
createRoot(rootElement).render(
|
|
30
|
+
<CrashReport>
|
|
31
|
+
<ExpoRoot context={context as never} />
|
|
32
|
+
</CrashReport>,
|
|
33
|
+
)
|
|
34
|
+
reportFirstRender()
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
const module = await loadEntry!()
|
|
38
|
+
// An entry that default-exports its root component, rather than registering it.
|
|
39
|
+
if (typeof module.default === 'function' && AppRegistry.getAppKeys().length === 0) {
|
|
40
|
+
AppRegistry.registerComponent('main', () => module.default as never)
|
|
41
|
+
}
|
|
42
|
+
// `registerRootComponent` runs the app itself on the web; `AppRegistry.registerComponent` alone does not.
|
|
43
|
+
const [key] = AppRegistry.getAppKeys()
|
|
44
|
+
if (key !== undefined && rootElement.childElementCount === 0) {
|
|
45
|
+
AppRegistry.runApplication(key, { rootTag: rootElement, initialProps: {} } as never)
|
|
46
|
+
}
|
|
47
|
+
reportFirstRender()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type CrashReportState = { error: Error | null; componentStack: string }
|
|
51
|
+
|
|
52
|
+
class CrashReport extends Component<{ children: ReactNode }, CrashReportState> {
|
|
53
|
+
state: CrashReportState = { error: null, componentStack: '' }
|
|
54
|
+
|
|
55
|
+
static getDerivedStateFromError(error: Error): Partial<CrashReportState> {
|
|
56
|
+
return { error }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
componentDidCatch(error: Error, info: ErrorInfo) {
|
|
60
|
+
reportCrash(error, info.componentStack ?? undefined)
|
|
61
|
+
this.setState({ componentStack: info.componentStack ?? '' })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
render() {
|
|
65
|
+
const { error, componentStack } = this.state
|
|
66
|
+
if (error === null) {
|
|
67
|
+
return this.props.children
|
|
68
|
+
}
|
|
69
|
+
return <CrashScreen text={`${error.message}\n\n${error.stack ?? ''}\n\nComponent stack:${componentStack}`} />
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function CrashScreen({ text }: { text: string }) {
|
|
74
|
+
return (
|
|
75
|
+
<pre
|
|
76
|
+
id="web-sim-crash"
|
|
77
|
+
style={{
|
|
78
|
+
margin: 0,
|
|
79
|
+
padding: '72px 16px 16px',
|
|
80
|
+
height: '100%',
|
|
81
|
+
overflow: 'auto',
|
|
82
|
+
boxSizing: 'border-box',
|
|
83
|
+
fontSize: 11,
|
|
84
|
+
whiteSpace: 'pre-wrap',
|
|
85
|
+
background: '#b3261e',
|
|
86
|
+
color: 'white',
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
{text}
|
|
90
|
+
</pre>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
start().catch((error: unknown) => {
|
|
95
|
+
reportCrash(error)
|
|
96
|
+
const message = `The app failed to load.\n\n${error instanceof Error ? `${error.message}\n\n${error.stack ?? ''}` : String(error)}`
|
|
97
|
+
createRoot(rootElement).render(<CrashScreen text={message} />)
|
|
98
|
+
throw error
|
|
99
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Platform } from '../../devices/types.ts'
|
|
2
|
+
|
|
3
|
+
type RouteContext = {
|
|
4
|
+
(key: string): unknown
|
|
5
|
+
keys(): string[]
|
|
6
|
+
resolve(key: string): string
|
|
7
|
+
id: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Stands in for the `require.context` Metro gives expo-router. Keys are paths relative to the routes directory,
|
|
12
|
+
* such as `./(tabs)/index.tsx`. Of a route's platform variants, the device's own wins, then `.native`, then the
|
|
13
|
+
* plain file, then `.web`, and the key drops the variant, as Metro's resolution would.
|
|
14
|
+
*/
|
|
15
|
+
export function routeContext(modules: Record<string, unknown>, routesDir: string, platform: Platform): RouteContext {
|
|
16
|
+
const ranks: Record<string, number> = { [platform]: 0, native: 1, '': 2, web: 3 }
|
|
17
|
+
const chosen = new Map<string, { file: string; rank: number }>()
|
|
18
|
+
for (const file of Object.keys(modules)) {
|
|
19
|
+
const relative = `./${file.slice(routesDir.length)}`
|
|
20
|
+
const match = /^(.*?)(?:\.(ios|android|native|web))?(\.[cm]?[jt]sx?)$/.exec(relative)
|
|
21
|
+
if (match === null) {
|
|
22
|
+
continue
|
|
23
|
+
}
|
|
24
|
+
const [, stem, variant = '', extension] = match
|
|
25
|
+
const rank = ranks[variant]
|
|
26
|
+
if (rank === undefined) {
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
const key = stem + extension
|
|
30
|
+
const current = chosen.get(key)
|
|
31
|
+
if (current === undefined || rank < current.rank) {
|
|
32
|
+
chosen.set(key, { file, rank })
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const routes = new Map([...chosen].map(([key, { file }]) => [key, modules[file]]))
|
|
36
|
+
return Object.assign(
|
|
37
|
+
(key: string) => {
|
|
38
|
+
if (!routes.has(key)) {
|
|
39
|
+
throw new Error(`No route file ${key}`)
|
|
40
|
+
}
|
|
41
|
+
return routes.get(key)
|
|
42
|
+
},
|
|
43
|
+
{ keys: () => [...routes.keys()], resolve: (key: string) => key, id: 'web-sim-routes' },
|
|
44
|
+
)
|
|
45
|
+
}
|