@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,18 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
|
|
4
|
+
/** The web-sim package's root. */
|
|
5
|
+
export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
|
|
6
|
+
|
|
7
|
+
const at = (relative: string) => path.join(PACKAGE_ROOT, relative)
|
|
8
|
+
|
|
9
|
+
export const CLIENT_ENTRY = at('src/client/entry.ts')
|
|
10
|
+
export const RUNTIME_ENTRY = at('src/runtime/index.ts')
|
|
11
|
+
export const BUILT_IN_OVERRIDES = at('overrides')
|
|
12
|
+
export const BUILT_IN_PLUGINS = at('plugins')
|
|
13
|
+
export const SHELL_API_ENTRY = at('src/plugin/index.ts')
|
|
14
|
+
/** What `react-native` resolves to for dependencies: react-native-web, plus what it lacks. */
|
|
15
|
+
export const RN_LIBRARY_SHIM = at('src/client/react-native/library.ts')
|
|
16
|
+
/** What `react-native` resolves to for app code: the same, reporting the simulated platform. */
|
|
17
|
+
export const RN_APP_SHIM = at('src/client/react-native/app.ts')
|
|
18
|
+
export const RN_DEEP_SHIM = at('src/client/react-native/deep.ts')
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import type { Plugin as EsbuildPlugin } from 'esbuild'
|
|
4
|
+
import type { Plugin, ViteDevServer } from 'vite'
|
|
5
|
+
import type { Platform } from '../devices/types.ts'
|
|
6
|
+
import { STUB_FACTORY } from './overrides/plugin.ts'
|
|
7
|
+
import { RN_APP_SHIM, RN_DEEP_SHIM, RN_LIBRARY_SHIM } from './paths.ts'
|
|
8
|
+
|
|
9
|
+
const SCRIPT_EXTENSIONS = ['.tsx', '.ts', '.jsx', '.js', '.mjs']
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Deep imports into React Native's own source that libraries make, and what in `deep.ts` stands in for each one's
|
|
13
|
+
* default export. Any other deep import gets a stub.
|
|
14
|
+
*/
|
|
15
|
+
const DEEP_IMPORTS: Record<string, string> = {
|
|
16
|
+
'Libraries/Utilities/codegenNativeComponent': 'codegenNativeComponent',
|
|
17
|
+
'Libraries/Utilities/codegenNativeCommands': 'codegenNativeCommands',
|
|
18
|
+
'Libraries/TurboModule/TurboModuleRegistry': 'TurboModuleRegistry',
|
|
19
|
+
'Libraries/Image/resolveAssetSource': 'resolveAssetSource',
|
|
20
|
+
'Libraries/Utilities/Platform': 'Platform',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The platform split. App code is resolved and run as if on the simulated device: `.ios.tsx` (or `.android.tsx`),
|
|
25
|
+
* then `.native.tsx`, then `.tsx`, falling back to `.web.tsx` only when nothing else exists, and `Platform.OS` and
|
|
26
|
+
* `process.env.EXPO_OS` report the device's platform. Dependencies keep their web builds and see `web`, because
|
|
27
|
+
* their native branches call native modules that do not exist here, unless an override's `platform: 'device'` (or
|
|
28
|
+
* the config's `devicePlatform`) says a package's branches are look and feel: then its `react-native` is the app's,
|
|
29
|
+
* and its `process.env.EXPO_OS` the device's platform, while its files still resolve as web.
|
|
30
|
+
*
|
|
31
|
+
* One dev server serves one platform at a time. Switching it drops every transformed app module, so the app reloads
|
|
32
|
+
* with the other platform's files.
|
|
33
|
+
*/
|
|
34
|
+
export class PlatformScope {
|
|
35
|
+
platform: Platform
|
|
36
|
+
private server: ViteDevServer | null = null
|
|
37
|
+
|
|
38
|
+
readonly projectRoot: string
|
|
39
|
+
readonly resolveNativeFiles: boolean
|
|
40
|
+
/** Whether a dependency's file sees the device's platform (`platform: 'device'` overrides, `devicePlatform`). */
|
|
41
|
+
readonly libraryOnDevice: (file: string) => boolean
|
|
42
|
+
|
|
43
|
+
constructor(projectRoot: string, initial: Platform, resolveNativeFiles: boolean, libraryOnDevice: (file: string) => boolean = () => false) {
|
|
44
|
+
this.projectRoot = projectRoot
|
|
45
|
+
this.resolveNativeFiles = resolveNativeFiles
|
|
46
|
+
this.platform = initial
|
|
47
|
+
this.libraryOnDevice = libraryOnDevice
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** What `react-native` resolves to for an import from `importer`. */
|
|
51
|
+
reactNativeFor(importer: string | undefined): string {
|
|
52
|
+
if (importer === undefined) {
|
|
53
|
+
return RN_LIBRARY_SHIM
|
|
54
|
+
}
|
|
55
|
+
const file = importer.split('?')[0]
|
|
56
|
+
return this.isAppSource(file) || this.libraryOnDevice(file) ? RN_APP_SHIM : RN_LIBRARY_SHIM
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
isAppSource(file: string): boolean {
|
|
60
|
+
return (
|
|
61
|
+
file.startsWith(this.projectRoot + path.sep) &&
|
|
62
|
+
!file.includes(`${path.sep}node_modules${path.sep}`) &&
|
|
63
|
+
!file.startsWith('\0')
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setPlatform(platform: Platform): boolean {
|
|
68
|
+
if (platform === this.platform) {
|
|
69
|
+
return false
|
|
70
|
+
}
|
|
71
|
+
this.platform = platform
|
|
72
|
+
this.server?.environments.client.moduleGraph.invalidateAll()
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The file an app import should load on this platform, given the one Vite's web resolution found. */
|
|
77
|
+
variantOf(file: string): string {
|
|
78
|
+
const extension = path.extname(file)
|
|
79
|
+
if (!SCRIPT_EXTENSIONS.includes(extension)) {
|
|
80
|
+
return file
|
|
81
|
+
}
|
|
82
|
+
const stem = file.slice(0, -extension.length).replace(/\.(web|ios|android|native)$/, '')
|
|
83
|
+
for (const suffix of [`.${this.platform}`, '.native', '']) {
|
|
84
|
+
for (const candidate of SCRIPT_EXTENSIONS) {
|
|
85
|
+
if (existsSync(stem + suffix + candidate)) {
|
|
86
|
+
return stem + suffix + candidate
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return file
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
vitePlugin(): Plugin {
|
|
94
|
+
const scope = this
|
|
95
|
+
return {
|
|
96
|
+
name: 'web-sim:platform',
|
|
97
|
+
enforce: 'pre',
|
|
98
|
+
configureServer: (server) => {
|
|
99
|
+
this.server = server
|
|
100
|
+
},
|
|
101
|
+
async resolveId(source, importer, options) {
|
|
102
|
+
if (source === 'react-native') {
|
|
103
|
+
return scope.reactNativeFor(importer)
|
|
104
|
+
}
|
|
105
|
+
if (source.startsWith('react-native/') && !source.endsWith('.json')) {
|
|
106
|
+
return deepImportId(source)
|
|
107
|
+
}
|
|
108
|
+
if (
|
|
109
|
+
!scope.resolveNativeFiles ||
|
|
110
|
+
importer === undefined ||
|
|
111
|
+
!scope.isAppSource(importer.split('?')[0]) ||
|
|
112
|
+
SCRIPT_EXTENSIONS.includes(path.extname(source)) ||
|
|
113
|
+
source.includes('?')
|
|
114
|
+
) {
|
|
115
|
+
return null
|
|
116
|
+
}
|
|
117
|
+
const resolved = await this.resolve(source, importer, { ...options, skipSelf: true })
|
|
118
|
+
if (resolved == null || !scope.isAppSource(resolved.id)) {
|
|
119
|
+
return resolved
|
|
120
|
+
}
|
|
121
|
+
const variant = scope.variantOf(resolved.id)
|
|
122
|
+
return variant === resolved.id ? resolved : variant
|
|
123
|
+
},
|
|
124
|
+
load(id) {
|
|
125
|
+
return id.startsWith(DEEP_PREFIX) ? deepImportModule(id.slice(DEEP_PREFIX.length), 'esm') : null
|
|
126
|
+
},
|
|
127
|
+
transform: (code, id) => {
|
|
128
|
+
const file = id.split('?')[0]
|
|
129
|
+
if (!this.isAppSource(file) || !code.includes('process.env.EXPO_OS')) {
|
|
130
|
+
return null
|
|
131
|
+
}
|
|
132
|
+
return { code: code.replaceAll('process.env.EXPO_OS', JSON.stringify(this.platform)), map: null }
|
|
133
|
+
},
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
esbuildPlugin(): EsbuildPlugin {
|
|
138
|
+
const scope = this
|
|
139
|
+
return {
|
|
140
|
+
name: 'web-sim-platform',
|
|
141
|
+
setup(build) {
|
|
142
|
+
build.onResolve({ filter: /^react-native$/ }, ({ importer }) => ({ path: scope.reactNativeFor(importer || undefined) }))
|
|
143
|
+
build.onResolve({ filter: /^react-native\/.+/ }, ({ path: source }) =>
|
|
144
|
+
source.endsWith('.json') ? undefined : { path: source, namespace: 'web-sim-rn-deep' },
|
|
145
|
+
)
|
|
146
|
+
build.onLoad({ filter: /.*/, namespace: 'web-sim-rn-deep' }, ({ path: source }) => ({
|
|
147
|
+
contents: deepImportModule(source, 'cjs-compatible'),
|
|
148
|
+
loader: 'js',
|
|
149
|
+
resolveDir: path.dirname(RN_DEEP_SHIM),
|
|
150
|
+
}))
|
|
151
|
+
},
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const DEEP_PREFIX = '\0web-sim-rn-deep:'
|
|
157
|
+
|
|
158
|
+
function deepImportId(source: string): string {
|
|
159
|
+
return DEEP_PREFIX + source
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function deepImportModule(source: string, mode: 'esm' | 'cjs-compatible'): string {
|
|
163
|
+
const subpath = source.slice('react-native/'.length).replace(/\.js$/, '')
|
|
164
|
+
const name = DEEP_IMPORTS[subpath]
|
|
165
|
+
if (name !== undefined) {
|
|
166
|
+
return `export * from ${JSON.stringify(RN_DEEP_SHIM)};\nexport { ${name} as default } from ${JSON.stringify(RN_DEEP_SHIM)};`
|
|
167
|
+
}
|
|
168
|
+
return mode === 'esm'
|
|
169
|
+
? `${STUB_FACTORY}\nexport default createStub(${JSON.stringify(source)});`
|
|
170
|
+
: `${STUB_FACTORY}\nmodule.exports = createStub(${JSON.stringify(source)});`
|
|
171
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { pathToFileURL } from 'node:url'
|
|
4
|
+
import type { Plugin as VitePlugin } from 'vite'
|
|
5
|
+
import { findPackage } from './overrides/packages.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A web-sim plugin: what it simulates, as overrides for the app's packages and a module for the shell with its state,
|
|
9
|
+
* panel and actions. Paths are relative to the file the plugin is defined in, or absolute.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* export default definePlugin({
|
|
13
|
+
* name: 'battery',
|
|
14
|
+
* packages: ['expo-battery'],
|
|
15
|
+
* overrides: './overrides',
|
|
16
|
+
* shell: './shell.tsx',
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type WebSimPlugin = {
|
|
21
|
+
name: string
|
|
22
|
+
/** The packages it simulates. Its panel shows when the app has one installed; without any, always. */
|
|
23
|
+
packages?: string[]
|
|
24
|
+
/** A folder of overrides, one folder per package with an `overrides.ts`. */
|
|
25
|
+
overrides?: string
|
|
26
|
+
/** A module that default-exports `defineShellPlugin(...)` from `web-sim/shell`. */
|
|
27
|
+
shell?: string
|
|
28
|
+
/**
|
|
29
|
+
* A module run in the app's window before the app, for what no package override reaches: `fetch`, timers, globals.
|
|
30
|
+
* It talks to the shell through `plugin(id)` from `web-sim/runtime`.
|
|
31
|
+
*/
|
|
32
|
+
app?: string
|
|
33
|
+
/** More Vite plugins, for what overrides cannot do. */
|
|
34
|
+
vite?: VitePlugin[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function definePlugin(plugin: WebSimPlugin): WebSimPlugin {
|
|
38
|
+
return plugin
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type LoadedPlugin = WebSimPlugin & {
|
|
42
|
+
origin: 'built-in' | 'config'
|
|
43
|
+
/** Whether the app uses it: no `packages`, or one of them installed. */
|
|
44
|
+
active: boolean
|
|
45
|
+
overridesDir: string | null
|
|
46
|
+
shellFile: string | null
|
|
47
|
+
appFile: string | null
|
|
48
|
+
/** The plugin's own folder, watched for changes. Null for plugins defined inline in the config. */
|
|
49
|
+
dir: string | null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const PLUGIN_FILE = 'plugin.ts'
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The built-in plugins (one folder each in `builtInDir`, with a `plugin.ts`), then the config's, which replace
|
|
56
|
+
* built-in ones of the same name. `disabled` names plugins to leave out.
|
|
57
|
+
*/
|
|
58
|
+
export async function loadPlugins(options: {
|
|
59
|
+
projectRoot: string
|
|
60
|
+
builtInDir: string
|
|
61
|
+
configured: WebSimPlugin[]
|
|
62
|
+
configFile: string | null
|
|
63
|
+
disabled: string[]
|
|
64
|
+
}): Promise<LoadedPlugin[]> {
|
|
65
|
+
const { projectRoot, builtInDir, configured, configFile, disabled } = options
|
|
66
|
+
const byName = new Map<string, LoadedPlugin>()
|
|
67
|
+
const add = (plugin: WebSimPlugin, origin: LoadedPlugin['origin'], base: string, dir: string | null) => {
|
|
68
|
+
const resolve = (file: string | undefined) => (file === undefined ? null : path.resolve(base, file))
|
|
69
|
+
byName.set(plugin.name, {
|
|
70
|
+
...plugin,
|
|
71
|
+
origin,
|
|
72
|
+
active: plugin.packages === undefined || plugin.packages.length === 0 || plugin.packages.some((name) => findPackage(name, projectRoot) !== null),
|
|
73
|
+
overridesDir: resolve(plugin.overrides),
|
|
74
|
+
shellFile: resolve(plugin.shell),
|
|
75
|
+
appFile: resolve(plugin.app),
|
|
76
|
+
dir,
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
if (existsSync(builtInDir)) {
|
|
80
|
+
for (const entry of readdirSync(builtInDir).sort()) {
|
|
81
|
+
const file = path.join(builtInDir, entry, PLUGIN_FILE)
|
|
82
|
+
if (!existsSync(file)) {
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
// A query per load: the dev server reloads plugins in the same process, and Node caches modules by URL.
|
|
86
|
+
const module = (await import(`${pathToFileURL(file).href}?v=${Date.now()}`)) as { default?: WebSimPlugin }
|
|
87
|
+
if (module.default === undefined || typeof module.default.name !== 'string') {
|
|
88
|
+
throw new Error(`${file} must default-export definePlugin(...)`)
|
|
89
|
+
}
|
|
90
|
+
add(module.default, 'built-in', path.dirname(file), path.dirname(file))
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const configBase = configFile !== null ? path.dirname(configFile) : projectRoot
|
|
94
|
+
for (const plugin of configured) {
|
|
95
|
+
add(plugin, 'config', configBase, null)
|
|
96
|
+
}
|
|
97
|
+
return [...byName.values()].filter((plugin) => !disabled.includes(plugin.name))
|
|
98
|
+
}
|