@a16n/web-sim 0.0.0-stage → 0.1.1
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 +157 -0
- package/overrides/react-native-web/useElementLayout.ts +152 -0
- package/package.json +80 -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 +129 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +76 -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 +23 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +188 -0
- package/src/node/appSource.ts +140 -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/fonts.ts +377 -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 +169 -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 +254 -0
- package/src/node/worklets.ts +86 -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 +519 -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,140 @@
|
|
|
1
|
+
import type { Plugin } from 'vite'
|
|
2
|
+
import type { LoadedPlugin } from './plugins.ts'
|
|
3
|
+
import { CLIENT_ENTRY } from './paths.ts'
|
|
4
|
+
import type { Project } from './config.ts'
|
|
5
|
+
import type { PlatformScope } from './platform.ts'
|
|
6
|
+
import { readAppInfo } from './appInfo.ts'
|
|
7
|
+
|
|
8
|
+
const ASSET_EXTENSIONS = [
|
|
9
|
+
'png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'bmp', 'svg', 'ttf', 'otf', 'woff', 'woff2',
|
|
10
|
+
'mp4', 'mov', 'webm', 'mp3', 'wav', 'm4a', 'aac', 'lottie', 'riv',
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
const ASSET_REQUIRE = new RegExp(`\\brequire\\(\\s*(['"])([^'"]+\\.(?:${ASSET_EXTENSIONS.join('|')}))\\1\\s*\\)`, 'g')
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Metro lets app code `require('./image.png')`, which gives an asset an image or font component accepts. Vite only
|
|
17
|
+
* has imports, so each asset require becomes an import of the asset's URL.
|
|
18
|
+
*/
|
|
19
|
+
export function assetRequires(scope: PlatformScope): Plugin {
|
|
20
|
+
return {
|
|
21
|
+
name: 'web-sim:asset-requires',
|
|
22
|
+
enforce: 'pre',
|
|
23
|
+
transform(code, id) {
|
|
24
|
+
const file = id.split('?')[0]
|
|
25
|
+
if (!scope.isAppSource(file) || !/\.[cm]?[jt]sx?$/.test(file) || !code.includes('require(')) {
|
|
26
|
+
return null
|
|
27
|
+
}
|
|
28
|
+
const imports: string[] = []
|
|
29
|
+
const seen = new Map<string, string>()
|
|
30
|
+
const replaced = code.replace(ASSET_REQUIRE, (_, _quote: string, specifier: string) => {
|
|
31
|
+
let name = seen.get(specifier)
|
|
32
|
+
if (name === undefined) {
|
|
33
|
+
name = `__webSimAsset${seen.size}`
|
|
34
|
+
seen.set(specifier, name)
|
|
35
|
+
imports.push(`import ${name} from ${JSON.stringify(specifier)};`)
|
|
36
|
+
}
|
|
37
|
+
return name
|
|
38
|
+
})
|
|
39
|
+
return imports.length === 0 ? null : { code: `${imports.join('\n')}\n${replaced}`, map: null }
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const HTML = `<!doctype html>
|
|
45
|
+
<html lang="en">
|
|
46
|
+
<head>
|
|
47
|
+
<meta charset="UTF-8" />
|
|
48
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
49
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect x='8' y='2' width='16' height='28' rx='4' fill='%23222'/%3E%3Crect x='10' y='5' width='12' height='22' rx='2' fill='%2368a0ff'/%3E%3C/svg%3E" />
|
|
50
|
+
<title>web-sim</title>
|
|
51
|
+
</head>
|
|
52
|
+
<body>
|
|
53
|
+
<div id="root"></div>
|
|
54
|
+
<script type="module" src="/@fs${CLIENT_ENTRY}"></script>
|
|
55
|
+
</body>
|
|
56
|
+
</html>
|
|
57
|
+
`
|
|
58
|
+
|
|
59
|
+
const APP_CONFIG = 'virtual:web-sim/app-config'
|
|
60
|
+
const SIM_CONFIG = 'virtual:web-sim/sim-config'
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Serves the page, and the module that tells it how to start the app. The shell and the app load the same page;
|
|
64
|
+
* `src/client/entry.ts` picks which one by whether it is framed. Any path serves it, because expo-router keeps the
|
|
65
|
+
* screen in the app frame's URL.
|
|
66
|
+
*/
|
|
67
|
+
export function appPage(project: Project, deviceIds: { devices: unknown[]; defaultDevice: string; plugins: LoadedPlugin[] }): Plugin {
|
|
68
|
+
return {
|
|
69
|
+
name: 'web-sim:page',
|
|
70
|
+
configureServer(server) {
|
|
71
|
+
server.middlewares.use(async (req, res, next) => {
|
|
72
|
+
const url = req.url ?? '/'
|
|
73
|
+
const pathname = url.split('?')[0]
|
|
74
|
+
if (
|
|
75
|
+
req.method !== 'GET' ||
|
|
76
|
+
!(req.headers.accept ?? '').includes('text/html') ||
|
|
77
|
+
pathname.startsWith('/@') ||
|
|
78
|
+
pathname.startsWith('/__') ||
|
|
79
|
+
/\.[a-z0-9]+$/i.test(pathname)
|
|
80
|
+
) {
|
|
81
|
+
next()
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
const html = await server.transformIndexHtml(url, HTML.replace('<title>web-sim</title>', `<title>${project.name} · web-sim</title>`))
|
|
85
|
+
res.setHeader('Content-Type', 'text/html')
|
|
86
|
+
res.end(html)
|
|
87
|
+
})
|
|
88
|
+
},
|
|
89
|
+
resolveId(source) {
|
|
90
|
+
return source === APP_CONFIG || source === SIM_CONFIG ? `\0${source}` : null
|
|
91
|
+
},
|
|
92
|
+
load(id) {
|
|
93
|
+
if (id === `\0${SIM_CONFIG}`) {
|
|
94
|
+
const shells = deviceIds.plugins.filter((plugin) => plugin.active && plugin.shellFile !== null)
|
|
95
|
+
return [
|
|
96
|
+
...shells.map((plugin, index) => `import plugin${index} from ${JSON.stringify(`/@fs${plugin.shellFile}`)};`),
|
|
97
|
+
`export const plugins = [${shells.map((_, index) => `plugin${index}`).join(', ')}];`,
|
|
98
|
+
`export const projectName = ${JSON.stringify(project.name)};`,
|
|
99
|
+
`export const devices = ${JSON.stringify(deviceIds.devices)};`,
|
|
100
|
+
`export const defaultDevice = ${JSON.stringify(deviceIds.defaultDevice)};`,
|
|
101
|
+
`export const appInfo = ${JSON.stringify(readAppInfo(project.root, project.name))};`,
|
|
102
|
+
].join('\n')
|
|
103
|
+
}
|
|
104
|
+
if (id !== `\0${APP_CONFIG}`) {
|
|
105
|
+
return null
|
|
106
|
+
}
|
|
107
|
+
// Plugins' app modules run first: imports evaluate before the app starts.
|
|
108
|
+
const lines: string[] = deviceIds.plugins
|
|
109
|
+
.filter((plugin) => plugin.active && plugin.appFile !== null)
|
|
110
|
+
.map((plugin) => `import ${JSON.stringify(`/@fs${plugin.appFile}`)};`)
|
|
111
|
+
if (project.routesDir !== null) {
|
|
112
|
+
const dir = project.routesDir
|
|
113
|
+
lines.push(
|
|
114
|
+
`export const mode = 'router';`,
|
|
115
|
+
`export const routesDir = ${JSON.stringify(`${dir}/`)};`,
|
|
116
|
+
`export const routes = import.meta.glob(${JSON.stringify([
|
|
117
|
+
`${dir}/**/*.{ts,tsx,js,jsx}`,
|
|
118
|
+
'!**/*.test.*',
|
|
119
|
+
'!**/*.d.ts',
|
|
120
|
+
'!**/+api.*',
|
|
121
|
+
'!**/+html.*',
|
|
122
|
+
'!**/+native-intent.*',
|
|
123
|
+
'!**/+middleware.*',
|
|
124
|
+
])}, { eager: true });`,
|
|
125
|
+
`export const loadEntry = null;`,
|
|
126
|
+
`export const loadRouter = () => import('expo-router');`,
|
|
127
|
+
)
|
|
128
|
+
} else {
|
|
129
|
+
lines.push(
|
|
130
|
+
`export const mode = 'entry';`,
|
|
131
|
+
`export const routesDir = null;`,
|
|
132
|
+
`export const routes = null;`,
|
|
133
|
+
`export const loadEntry = () => import(${JSON.stringify(project.entry)});`,
|
|
134
|
+
`export const loadRouter = null;`,
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
return lines.join('\n')
|
|
138
|
+
},
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { pathToFileURL } from 'node:url'
|
|
4
|
+
import type { InlineConfig } from 'vite'
|
|
5
|
+
import type { Device } from '../devices/types.ts'
|
|
6
|
+
import type { PackageOverrides } from './overrides/define.ts'
|
|
7
|
+
import type { WebSimPlugin } from './plugins.ts'
|
|
8
|
+
import { findPackage } from './overrides/packages.ts'
|
|
9
|
+
|
|
10
|
+
export type WebSimConfig = {
|
|
11
|
+
/** The expo-router routes directory. Found on its own: `src/app` or `app`. */
|
|
12
|
+
appRoot?: string
|
|
13
|
+
/**
|
|
14
|
+
* For apps without expo-router: the module that registers the app (with `registerRootComponent` or
|
|
15
|
+
* `AppRegistry.registerComponent`), or default-exports its root component.
|
|
16
|
+
*/
|
|
17
|
+
entry?: string
|
|
18
|
+
/** The project's own overrides, one folder per package with an `overrides.ts`. Default `web-sim/overrides`. */
|
|
19
|
+
overridesDir?: string
|
|
20
|
+
/** Installed native-only packages to replace with stubs, for any version. */
|
|
21
|
+
stub?: string[]
|
|
22
|
+
/**
|
|
23
|
+
* Installed packages that see the simulated device's platform (`Platform.OS` is `ios` or `android`), for any
|
|
24
|
+
* version: the project's own packages in a monorepo, or a library whose platform branches are look and feel. They
|
|
25
|
+
* still load their web files. See `platform` on an override rule to do this for one range of versions.
|
|
26
|
+
*/
|
|
27
|
+
devicePlatform?: string[]
|
|
28
|
+
/** Overrides written inline instead of in `overridesDir`. */
|
|
29
|
+
overrides?: PackageOverrides[]
|
|
30
|
+
/** Plugins, added to the built-in ones; one with a built-in plugin's name replaces it. */
|
|
31
|
+
plugins?: WebSimPlugin[]
|
|
32
|
+
/** Built-in plugins to leave out, by name. */
|
|
33
|
+
disablePlugins?: string[]
|
|
34
|
+
/** More devices, added to the built-in ones. */
|
|
35
|
+
devices?: Device[]
|
|
36
|
+
/** The device a new tab starts on. */
|
|
37
|
+
defaultDevice?: string
|
|
38
|
+
/**
|
|
39
|
+
* Resolve app code as the device's platform would: `.ios.tsx` or `.android.tsx`, then `.native.tsx`, then `.tsx`,
|
|
40
|
+
* before `.web.tsx`. Default true. Set false for apps whose web files are the ones to simulate.
|
|
41
|
+
*/
|
|
42
|
+
resolveNativeFiles?: boolean
|
|
43
|
+
/** Babel plugins for app code. web-sim adds the Worklets plugin when installed, for app and dependencies alike. */
|
|
44
|
+
babelPlugins?: unknown[]
|
|
45
|
+
port?: number
|
|
46
|
+
/**
|
|
47
|
+
* Send the app's cross-origin requests through the dev server, as a phone sends them: no CORS, no `Origin`, the
|
|
48
|
+
* headers the app sets, a cookie jar, and `Set-Cookie` readable. Default true.
|
|
49
|
+
*/
|
|
50
|
+
proxy?: boolean
|
|
51
|
+
/** Merged into the Vite config. */
|
|
52
|
+
vite?: InlineConfig
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function defineConfig(config: WebSimConfig): WebSimConfig {
|
|
56
|
+
return config
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const CONFIG_FILES = ['web-sim.config.ts', 'web-sim.config.mts', 'web-sim.config.js', 'web-sim.config.mjs']
|
|
60
|
+
|
|
61
|
+
export async function loadConfig(projectRoot: string): Promise<{ config: WebSimConfig; file: string | null }> {
|
|
62
|
+
for (const name of CONFIG_FILES) {
|
|
63
|
+
const file = path.join(projectRoot, name)
|
|
64
|
+
if (existsSync(file)) {
|
|
65
|
+
const module = (await import(`${pathToFileURL(file).href}?t=${Date.now()}`)) as { default?: WebSimConfig }
|
|
66
|
+
return { config: module.default ?? {}, file }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { config: {}, file: null }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type Project = {
|
|
73
|
+
root: string
|
|
74
|
+
name: string
|
|
75
|
+
config: WebSimConfig
|
|
76
|
+
configFile: string | null
|
|
77
|
+
/** The routes directory, relative to the root with a leading slash, when the app uses expo-router. */
|
|
78
|
+
routesDir: string | null
|
|
79
|
+
/** The entry module, root-relative, when it does not. */
|
|
80
|
+
entry: string | null
|
|
81
|
+
/** tsconfig `paths`, as Vite aliases. */
|
|
82
|
+
aliases: { find: RegExp; replacement: string }[]
|
|
83
|
+
/** `EXPO_PUBLIC_` variables from the project's `.env` files. */
|
|
84
|
+
env: Record<string, string>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function loadProject(root: string): Promise<Project> {
|
|
88
|
+
const { config, file } = await loadConfig(root)
|
|
89
|
+
const packageJson = readJson(path.join(root, 'package.json')) ?? {}
|
|
90
|
+
const hasRouter = findPackage('expo-router', root) !== null
|
|
91
|
+
let routesDir: string | null = null
|
|
92
|
+
let entry: string | null = null
|
|
93
|
+
if (config.entry !== undefined) {
|
|
94
|
+
entry = `/${path.relative(root, path.resolve(root, config.entry))}`
|
|
95
|
+
} else if (config.appRoot !== undefined) {
|
|
96
|
+
routesDir = `/${path.relative(root, path.resolve(root, config.appRoot))}`
|
|
97
|
+
} else if (hasRouter) {
|
|
98
|
+
routesDir = ['src/app', 'app'].map((dir) => `/${dir}`).find((dir) => existsSync(path.join(root, dir))) ?? null
|
|
99
|
+
}
|
|
100
|
+
if (routesDir === null && entry === null) {
|
|
101
|
+
const main = typeof packageJson.main === 'string' && !packageJson.main.startsWith('expo-router') ? packageJson.main : null
|
|
102
|
+
const candidate = [main, 'index.tsx', 'index.ts', 'index.js', 'App.tsx', 'App.js']
|
|
103
|
+
.filter((name): name is string => name !== null)
|
|
104
|
+
.find((name) => existsSync(path.join(root, name)))
|
|
105
|
+
if (candidate === undefined) {
|
|
106
|
+
throw new Error(`web-sim: no expo-router app directory or entry file found in ${root}. Set 'entry' in web-sim.config.ts.`)
|
|
107
|
+
}
|
|
108
|
+
entry = `/${candidate}`
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
root,
|
|
112
|
+
name: typeof packageJson.name === 'string' ? packageJson.name : path.basename(root),
|
|
113
|
+
config,
|
|
114
|
+
configFile: file,
|
|
115
|
+
routesDir,
|
|
116
|
+
entry,
|
|
117
|
+
aliases: tsconfigAliases(root),
|
|
118
|
+
env: expoPublicEnv(root),
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function readJson(file: string): Record<string, unknown> | null {
|
|
123
|
+
if (!existsSync(file)) {
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
const text = readFileSync(file, 'utf8')
|
|
127
|
+
try {
|
|
128
|
+
return JSON.parse(text) as Record<string, unknown>
|
|
129
|
+
} catch {
|
|
130
|
+
// tsconfig files allow comments and trailing commas.
|
|
131
|
+
const stripped = text
|
|
132
|
+
.replace(/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, string: string | undefined) => string ?? '')
|
|
133
|
+
.replace(/,(\s*[}\]])/g, '$1')
|
|
134
|
+
return JSON.parse(stripped) as Record<string, unknown>
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* tsconfig `paths` as aliases, most specific first, so `@/assets/*` wins over `@/*`.
|
|
140
|
+
*/
|
|
141
|
+
function tsconfigAliases(root: string): { find: RegExp; replacement: string }[] {
|
|
142
|
+
const tsconfig = readJson(path.join(root, 'tsconfig.json'))
|
|
143
|
+
const options = (tsconfig?.compilerOptions ?? {}) as { paths?: Record<string, string[]>; baseUrl?: string }
|
|
144
|
+
const base = path.resolve(root, options.baseUrl ?? '.')
|
|
145
|
+
return Object.entries(options.paths ?? {})
|
|
146
|
+
.filter(([, targets]) => targets.length > 0)
|
|
147
|
+
.sort(([a], [b]) => b.length - a.length)
|
|
148
|
+
.map(([pattern, [target]]) => {
|
|
149
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
|
|
150
|
+
if (pattern.endsWith('/*')) {
|
|
151
|
+
return {
|
|
152
|
+
find: new RegExp(`^${escaped.slice(0, -2)}/(.*)$`),
|
|
153
|
+
replacement: `${path.join(base, target.slice(0, -2))}/$1`,
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return { find: new RegExp(`^${escaped}$`), replacement: path.join(base, target) }
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function expoPublicEnv(root: string): Record<string, string> {
|
|
161
|
+
const env: Record<string, string> = {}
|
|
162
|
+
for (const name of ['.env', '.env.development', '.env.local', '.env.development.local']) {
|
|
163
|
+
const file = path.join(root, name)
|
|
164
|
+
if (!existsSync(file)) {
|
|
165
|
+
continue
|
|
166
|
+
}
|
|
167
|
+
for (const line of readFileSync(file, 'utf8').split('\n')) {
|
|
168
|
+
const match = /^\s*(EXPO_PUBLIC_[A-Z0-9_]+)\s*=\s*(.*)\s*$/.exec(line)
|
|
169
|
+
if (match !== null) {
|
|
170
|
+
env[match[1]] = match[2].replace(/^(['"])(.*)\1$/, '$2')
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
175
|
+
if (key.startsWith('EXPO_PUBLIC_') && value !== undefined) {
|
|
176
|
+
env[key] = value
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return env
|
|
180
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http'
|
|
2
|
+
import type { Plugin, WebSocketClient } from 'vite'
|
|
3
|
+
import type { EventStore } from './events/store.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A command for the open simulator tab, from the CLI or a test.
|
|
7
|
+
*
|
|
8
|
+
* - `state`: the simulator's state, and each plugin's (or one plugin's).
|
|
9
|
+
* - `set`: changes a plugin's state. Plugin `sim` is the simulator itself: `device`, `orientation`, `appearance`.
|
|
10
|
+
* - `run`: runs a plugin's action.
|
|
11
|
+
* - `plugins`: the loaded plugins, with their actions.
|
|
12
|
+
*/
|
|
13
|
+
export type ControlCommand =
|
|
14
|
+
| { type: 'state'; plugin?: string }
|
|
15
|
+
| { type: 'set'; plugin: string; patch: Record<string, unknown> }
|
|
16
|
+
| { type: 'run'; plugin: string; action: string; args?: unknown }
|
|
17
|
+
| { type: 'plugins' }
|
|
18
|
+
|
|
19
|
+
export type ControlReply = { id: number; ok: true; result: unknown } | { id: number; ok: false; error: string }
|
|
20
|
+
|
|
21
|
+
export const CONTROL_PATH = '/__web-sim/control'
|
|
22
|
+
const TIMEOUT_MS = 5000
|
|
23
|
+
|
|
24
|
+
/** The command as you would type it: `web-sim set battery level=0.15`. */
|
|
25
|
+
export function describeCommand(command: ControlCommand): string {
|
|
26
|
+
switch (command.type) {
|
|
27
|
+
case 'plugins':
|
|
28
|
+
return 'web-sim plugins'
|
|
29
|
+
case 'state':
|
|
30
|
+
return `web-sim state${command.plugin === undefined ? '' : ` ${command.plugin}`}`
|
|
31
|
+
case 'set':
|
|
32
|
+
return `web-sim set ${command.plugin} ${Object.entries(command.patch)
|
|
33
|
+
.map(([key, value]) => `${key}=${typeof value === 'string' ? value : JSON.stringify(value)}`)
|
|
34
|
+
.join(' ')}`
|
|
35
|
+
case 'run':
|
|
36
|
+
return `web-sim run ${command.plugin} ${command.action}${command.args === undefined ? '' : ` ${JSON.stringify(command.args)}`}`
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A value for the log, or a note of its size when it is too big to keep. */
|
|
41
|
+
function capped(value: unknown, max = 4096): unknown {
|
|
42
|
+
const text = JSON.stringify(value)
|
|
43
|
+
return text === undefined || text.length <= max ? value : `<${Math.round(text.length / 1024)} KB>`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Relays commands posted to `/__web-sim/control` to a simulator tab over Vite's websocket, and answers with its reply.
|
|
48
|
+
* The tab holds all the state, so with no tab open there is nothing to answer. Each command goes to one tab, the one
|
|
49
|
+
* used last, so it runs once however many are open; it goes to all only before any tab has said which it is.
|
|
50
|
+
*
|
|
51
|
+
* Each command is logged here, once, as `cli.command` (its span is `cli-<n>`, which the tab puts as the `cause` of
|
|
52
|
+
* what the command changes), then `cli.result` or `cli.failed`. Commands that only read are logged at debug level.
|
|
53
|
+
*/
|
|
54
|
+
export function controlChannel(events?: EventStore): Plugin {
|
|
55
|
+
return {
|
|
56
|
+
name: 'web-sim:control',
|
|
57
|
+
configureServer(server) {
|
|
58
|
+
let nextId = 1
|
|
59
|
+
const pending = new Map<number, (reply: ControlReply) => void>()
|
|
60
|
+
const shells = new Map<string, { client: WebSocketClient; at: number }>()
|
|
61
|
+
server.ws.on('web-sim:shell', (data: { tab: string }, client) => {
|
|
62
|
+
if (typeof data?.tab === 'string') {
|
|
63
|
+
shells.set(data.tab, { client, at: Date.now() })
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
/** The tab used last whose socket is still open. */
|
|
67
|
+
const target = (): WebSocketClient | undefined => {
|
|
68
|
+
let best: { client: WebSocketClient; at: number } | undefined
|
|
69
|
+
for (const [tab, shell] of shells) {
|
|
70
|
+
if (shell.client.socket.readyState !== 1) {
|
|
71
|
+
shells.delete(tab)
|
|
72
|
+
} else if (best === undefined || shell.at > best.at) {
|
|
73
|
+
best = shell
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return best?.client
|
|
77
|
+
}
|
|
78
|
+
server.ws.on('web-sim:control-reply', (reply: ControlReply) => {
|
|
79
|
+
pending.get(reply.id)?.(reply)
|
|
80
|
+
pending.delete(reply.id)
|
|
81
|
+
})
|
|
82
|
+
server.middlewares.use(CONTROL_PATH, async (req, res) => {
|
|
83
|
+
res.setHeader('Content-Type', 'application/json')
|
|
84
|
+
if (req.method !== 'POST') {
|
|
85
|
+
res.statusCode = 405
|
|
86
|
+
res.end(JSON.stringify({ ok: false, error: 'POST a command' }))
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
let command: ControlCommand
|
|
90
|
+
try {
|
|
91
|
+
command = JSON.parse(await body(req)) as ControlCommand
|
|
92
|
+
} catch {
|
|
93
|
+
res.statusCode = 400
|
|
94
|
+
res.end(JSON.stringify({ ok: false, error: 'The body must be a JSON command' }))
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
const id = nextId++
|
|
98
|
+
const span = `cli-${id}-${Date.now().toString(36)}`
|
|
99
|
+
const readOnly = command.type === 'state' || command.type === 'plugins'
|
|
100
|
+
events?.add({
|
|
101
|
+
kind: 'cli.command',
|
|
102
|
+
level: readOnly ? 'debug' : 'info',
|
|
103
|
+
actor: 'cli',
|
|
104
|
+
span,
|
|
105
|
+
summary: describeCommand(command),
|
|
106
|
+
data: { command },
|
|
107
|
+
})
|
|
108
|
+
const reply = await new Promise<ControlReply>((resolve) => {
|
|
109
|
+
const timer = setTimeout(() => {
|
|
110
|
+
pending.delete(id)
|
|
111
|
+
resolve({ id, ok: false, error: 'No simulator tab answered. Open the simulator in Chrome first.' })
|
|
112
|
+
}, TIMEOUT_MS)
|
|
113
|
+
pending.set(id, (answer) => {
|
|
114
|
+
clearTimeout(timer)
|
|
115
|
+
resolve(answer)
|
|
116
|
+
})
|
|
117
|
+
const message = { id, command, cause: span }
|
|
118
|
+
const client = target()
|
|
119
|
+
if (client !== undefined) {
|
|
120
|
+
client.send('web-sim:control', message)
|
|
121
|
+
} else {
|
|
122
|
+
server.ws.send('web-sim:control', message)
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
events?.add(
|
|
126
|
+
reply.ok
|
|
127
|
+
? { kind: 'cli.result', level: 'debug', actor: 'cli', span, summary: `${describeCommand(command)}: ok`, data: { result: capped(reply.result) } }
|
|
128
|
+
: { kind: 'cli.failed', level: 'error', actor: 'cli', span, summary: `${describeCommand(command)}: ${reply.error}`, data: { error: reply.error } },
|
|
129
|
+
)
|
|
130
|
+
res.statusCode = reply.ok ? 200 : 422
|
|
131
|
+
res.end(JSON.stringify(reply))
|
|
132
|
+
})
|
|
133
|
+
},
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function body(req: IncomingMessage): Promise<string> {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
let data = ''
|
|
140
|
+
req.on('data', (chunk: Buffer) => (data += chunk.toString()))
|
|
141
|
+
req.on('end', () => resolve(data))
|
|
142
|
+
req.on('error', reject)
|
|
143
|
+
})
|
|
144
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { findPackage, type PackageInfo } from './overrides/packages.ts'
|
|
4
|
+
import type { SimContext } from './server.ts'
|
|
5
|
+
|
|
6
|
+
type Status = 'override' | 'stub' | 'unmatched' | 'native-no-web' | 'native-web' | 'js'
|
|
7
|
+
|
|
8
|
+
export type DoctorRow = { name: string; version: string | null; status: Status; detail: string }
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Every direct dependency of the app, and how it runs in the simulator: changed by an override, stubbed, a native
|
|
12
|
+
* module with its own web build, or a native module with nothing for the web, which needs an override or a stub.
|
|
13
|
+
*/
|
|
14
|
+
export function diagnose(context: SimContext): DoctorRow[] {
|
|
15
|
+
const { project, registry } = context
|
|
16
|
+
const packageJson = JSON.parse(readFileSync(path.join(project.root, 'package.json'), 'utf8')) as {
|
|
17
|
+
dependencies?: Record<string, string>
|
|
18
|
+
}
|
|
19
|
+
const rows: DoctorRow[] = []
|
|
20
|
+
for (const name of Object.keys(packageJson.dependencies ?? {}).sort()) {
|
|
21
|
+
const info = findPackage(name, project.root)
|
|
22
|
+
if (info === null) {
|
|
23
|
+
rows.push({ name, version: null, status: 'js', detail: 'not installed' })
|
|
24
|
+
continue
|
|
25
|
+
}
|
|
26
|
+
const rule = registry.ruleFor(info)
|
|
27
|
+
if (rule !== null) {
|
|
28
|
+
const kinds = [
|
|
29
|
+
rule.stub && 'stub',
|
|
30
|
+
rule.replace && 'replaces entry',
|
|
31
|
+
rule.files && `replaces ${Object.keys(rule.files).length} file(s)`,
|
|
32
|
+
rule.patch && `${rule.patch.length} patch(es)`,
|
|
33
|
+
rule.platform === 'device' && 'sees the device platform',
|
|
34
|
+
typeof rule.platform === 'object' && `${rule.platform.device.length} path(s) see the device platform`,
|
|
35
|
+
].filter(Boolean)
|
|
36
|
+
rows.push({
|
|
37
|
+
name,
|
|
38
|
+
version: info.version,
|
|
39
|
+
status: rule.stub ? 'stub' : 'override',
|
|
40
|
+
detail: `${kinds.join(', ')} · ${rule.origin} '${rule.versions}'${rule.note ? ` · ${rule.note}` : ''}`,
|
|
41
|
+
})
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
if (registry.devicePlatformPackages.has(name)) {
|
|
45
|
+
rows.push({ name, version: info.version, status: 'override', detail: 'sees the device platform · devicePlatform in web-sim.config' })
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
if (registry.rulesFor(name).length > 0) {
|
|
49
|
+
rows.push({
|
|
50
|
+
name,
|
|
51
|
+
version: info.version,
|
|
52
|
+
status: 'unmatched',
|
|
53
|
+
detail: `overrides exist for ${registry.rulesFor(name).map((r) => `'${r.versions}'`).join(', ')}, none for this version`,
|
|
54
|
+
})
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
if (name === 'react-native') {
|
|
58
|
+
rows.push({ name, version: info.version, status: 'native-web', detail: 'provided by react-native-web' })
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
const native = nativeKind(info)
|
|
62
|
+
const web = native === null ? null : webSupport(info)
|
|
63
|
+
if (native === null) {
|
|
64
|
+
rows.push({ name, version: info.version, status: 'js', detail: '' })
|
|
65
|
+
} else if (web !== null) {
|
|
66
|
+
rows.push({ name, version: info.version, status: 'native-web', detail: `${native}, ${web}` })
|
|
67
|
+
} else {
|
|
68
|
+
rows.push({ name, version: info.version, status: 'native-no-web', detail: `${native} with no web build: add an override or a stub` })
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return rows
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function nativeKind(info: PackageInfo): string | null {
|
|
75
|
+
if (existsSync(path.join(info.dir, 'expo-module.config.json'))) {
|
|
76
|
+
return 'Expo module'
|
|
77
|
+
}
|
|
78
|
+
if (info.json.codegenConfig !== undefined) {
|
|
79
|
+
return 'native module'
|
|
80
|
+
}
|
|
81
|
+
if (existsSync(path.join(info.dir, 'ios')) || existsSync(path.join(info.dir, 'android'))) {
|
|
82
|
+
return 'native module'
|
|
83
|
+
}
|
|
84
|
+
return null
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* How a native package runs on the web, or null when nothing suggests it does. Packages show it in different ways:
|
|
89
|
+
* a web build in `expo-module.config.json` or `browser`, `.web.js` files or `web` folders anywhere in their JS,
|
|
90
|
+
* platform files (`.native.js`, `.ios.js`, `.android.js`) beside a plain file that is the web's fallback, using
|
|
91
|
+
* react-native-web directly, or an Expo module whose JS works without its native half.
|
|
92
|
+
*/
|
|
93
|
+
function webSupport(info: PackageInfo): string | null {
|
|
94
|
+
const expoConfig = path.join(info.dir, 'expo-module.config.json')
|
|
95
|
+
if (existsSync(expoConfig)) {
|
|
96
|
+
const config = JSON.parse(readFileSync(expoConfig, 'utf8')) as { platforms?: string[] }
|
|
97
|
+
if (config.platforms?.includes('web')) {
|
|
98
|
+
return 'ships a web build'
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (typeof info.json.browser === 'string' || typeof info.json.browser === 'object') {
|
|
102
|
+
return 'ships a browser build'
|
|
103
|
+
}
|
|
104
|
+
const scan = { webFiles: false, fallbacks: false, reactNativeWeb: false, optionalNative: false, requiredNative: false }
|
|
105
|
+
for (const dir of jsRoots(info)) {
|
|
106
|
+
scanJs(dir, 8, scan)
|
|
107
|
+
}
|
|
108
|
+
const dependencies = { ...(info.json.dependencies as object), ...(info.json.peerDependencies as object) }
|
|
109
|
+
if (scan.webFiles) {
|
|
110
|
+
return 'ships a web build'
|
|
111
|
+
}
|
|
112
|
+
if (scan.fallbacks) {
|
|
113
|
+
return 'platform files with a web fallback'
|
|
114
|
+
}
|
|
115
|
+
if (scan.reactNativeWeb || 'react-native-web' in dependencies) {
|
|
116
|
+
return 'runs on react-native-web'
|
|
117
|
+
}
|
|
118
|
+
if (scan.optionalNative && !scan.requiredNative) {
|
|
119
|
+
return 'its JS runs without the native module, doing nothing'
|
|
120
|
+
}
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Where a package keeps its JS: its entry points' folders and the usual build folders. */
|
|
125
|
+
function jsRoots(info: PackageInfo): string[] {
|
|
126
|
+
const entries = [info.json.main, info.json.module, info.json['react-native']]
|
|
127
|
+
.filter((entry): entry is string => typeof entry === 'string')
|
|
128
|
+
.map((entry) => path.dirname(path.join(info.dir, entry)))
|
|
129
|
+
const roots = [...entries, ...['src', 'lib', 'build', 'dist'].map((name) => path.join(info.dir, name))]
|
|
130
|
+
.filter((dir) => dir !== info.dir && existsSync(dir))
|
|
131
|
+
// Nested roots (`lib/module` inside `lib`) are scanned once, through the outer one.
|
|
132
|
+
const unique = [...new Set(roots)].filter((dir, _, all) => !all.some((other) => other !== dir && dir.startsWith(other + path.sep)))
|
|
133
|
+
return unique.length > 0 ? unique : [info.dir]
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const SKIP = new Set(['node_modules', 'ios', 'android', 'apple', 'jest', '__tests__', '__mocks__', 'typescript', 'plugin'])
|
|
137
|
+
const JS = /\.(js|mjs|cjs|jsx|ts|tsx)$/
|
|
138
|
+
const PLATFORM_FILE = /^(.*)\.(native|ios|android)\.(js|mjs|cjs|jsx|ts|tsx)$/
|
|
139
|
+
|
|
140
|
+
type Scan = { webFiles: boolean; fallbacks: boolean; reactNativeWeb: boolean; optionalNative: boolean; requiredNative: boolean }
|
|
141
|
+
|
|
142
|
+
function scanJs(dir: string, depth: number, scan: Scan): void {
|
|
143
|
+
if (depth < 0 || scan.webFiles) {
|
|
144
|
+
return
|
|
145
|
+
}
|
|
146
|
+
const entries = readdirSync(dir, { withFileTypes: true })
|
|
147
|
+
const files = new Set(entries.filter((entry) => entry.isFile() && !entry.name.endsWith('.d.ts')).map((entry) => entry.name))
|
|
148
|
+
for (const name of files) {
|
|
149
|
+
if (/\.web\.(js|mjs|cjs|jsx|ts|tsx)$/.test(name)) {
|
|
150
|
+
scan.webFiles = true
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
const platformFile = PLATFORM_FILE.exec(name)
|
|
154
|
+
if (platformFile !== null && [...files].some((other) => other.startsWith(`${platformFile[1]}.`) && JS.test(other) && !PLATFORM_FILE.test(other))) {
|
|
155
|
+
scan.fallbacks = true
|
|
156
|
+
}
|
|
157
|
+
if (JS.test(name) && !PLATFORM_FILE.test(name) && !(scan.reactNativeWeb && scan.optionalNative && scan.requiredNative)) {
|
|
158
|
+
const source = readFileSync(path.join(dir, name), 'utf8')
|
|
159
|
+
scan.reactNativeWeb ||= /from ['"]react-native-web['"]|require\(['"]react-native-web['"]\)/.test(source)
|
|
160
|
+
scan.optionalNative ||= source.includes('requireOptionalNativeModule(')
|
|
161
|
+
scan.requiredNative ||= /requireNativeModule\(|TurboModuleRegistry\.getEnforcing\(/.test(source)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
for (const entry of entries) {
|
|
165
|
+
if (!entry.isDirectory() || entry.name.startsWith('.') || SKIP.has(entry.name)) {
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
168
|
+
if (entry.name === 'web' && readdirSync(path.join(dir, entry.name)).some((name) => JS.test(name))) {
|
|
169
|
+
scan.webFiles = true
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
scanJs(path.join(dir, entry.name), depth - 1, scan)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const LABELS: Record<Status, string> = {
|
|
177
|
+
override: '\x1b[36moverride\x1b[0m ',
|
|
178
|
+
stub: '\x1b[36mstub\x1b[0m ',
|
|
179
|
+
unmatched: '\x1b[33mno match\x1b[0m ',
|
|
180
|
+
'native-no-web': '\x1b[31mmissing\x1b[0m ',
|
|
181
|
+
'native-web': '\x1b[32mweb build\x1b[0m',
|
|
182
|
+
js: '\x1b[2mjs\x1b[0m ',
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function printDiagnosis(rows: DoctorRow[], verbose: boolean): number {
|
|
186
|
+
const width = Math.max(...rows.map((row) => row.name.length + (row.version?.length ?? 0) + 1))
|
|
187
|
+
for (const row of rows) {
|
|
188
|
+
if (!verbose && row.status === 'js') {
|
|
189
|
+
continue
|
|
190
|
+
}
|
|
191
|
+
const id = `${row.name}@${row.version ?? '?'}`
|
|
192
|
+
console.log(`${LABELS[row.status]} ${id.padEnd(width + 1)} ${row.detail}`)
|
|
193
|
+
}
|
|
194
|
+
const problems = rows.filter((row) => row.status === 'native-no-web' || row.status === 'unmatched')
|
|
195
|
+
console.log(
|
|
196
|
+
`\n${rows.length} dependencies · ${rows.filter((r) => r.status === 'override' || r.status === 'stub').length} overridden · ${problems.length} need attention${verbose ? '' : ' (--all lists plain JS packages too)'}`,
|
|
197
|
+
)
|
|
198
|
+
return problems.length > 0 ? 1 : 0
|
|
199
|
+
}
|