@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,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A change to one source file of a package, made as the file is loaded. It must change the file: one that no longer
|
|
3
|
+
* matches after an upgrade is an error, not a silent no-op.
|
|
4
|
+
*/
|
|
5
|
+
export type SourcePatch = {
|
|
6
|
+
/** The file, relative to the package's root (`dist/exports/View/index.js`), or a pattern over that path. */
|
|
7
|
+
file: string | RegExp
|
|
8
|
+
replace: (source: string) => string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* What web-sim does to one range of a package's versions. Paths to replacement files are relative to the file that
|
|
13
|
+
* defines the override.
|
|
14
|
+
*
|
|
15
|
+
* Replacement files are compiled as if they sat where the file they replace sits, so their relative and bare imports
|
|
16
|
+
* resolve from inside the package. `web-sim:original` imports the file being replaced, and `web-sim/runtime` the
|
|
17
|
+
* simulator's state (the device, insets, appearance).
|
|
18
|
+
*/
|
|
19
|
+
export type OverrideRule = {
|
|
20
|
+
/** A semver range, matched against the installed version of the package that each import reaches. */
|
|
21
|
+
versions: string
|
|
22
|
+
/** A file that replaces the package's entry point. */
|
|
23
|
+
replace?: string
|
|
24
|
+
/** Files that replace files of the package, keyed by their path from the package's root. */
|
|
25
|
+
files?: Record<string, string>
|
|
26
|
+
/** Changes to the package's own source files. */
|
|
27
|
+
patch?: SourcePatch[]
|
|
28
|
+
/** Replace the whole package with a stub where every call does nothing: for native-only modules with no web build. */
|
|
29
|
+
stub?: boolean
|
|
30
|
+
/**
|
|
31
|
+
* `'device'`: the package sees the simulated device's platform, as app code does. Its `react-native` import gets a
|
|
32
|
+
* `Platform` that reports `ios` or `android` (with `Platform.select` and `Platform.Version` to match) and its
|
|
33
|
+
* `process.env.EXPO_OS` is the device's platform. It still loads its web files (`.web.js`, then plain), so no
|
|
34
|
+
* native module is reached. `{ device: [...] }` limits that to some of its files: paths from the package's root,
|
|
35
|
+
* where one ending in `/` covers a folder, or patterns over that path.
|
|
36
|
+
*
|
|
37
|
+
* For packages whose platform branches are look and feel (sizes, fonts, layout), not for ones whose
|
|
38
|
+
* `Platform.OS === 'web'` checks pick the only path that works in a browser.
|
|
39
|
+
*/
|
|
40
|
+
platform?: 'device' | { device: (string | RegExp)[] }
|
|
41
|
+
/** Why this override exists, shown by `web-sim doctor`. */
|
|
42
|
+
note?: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type PackageOverrides = {
|
|
46
|
+
package: string
|
|
47
|
+
rules: OverrideRule[]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Overrides for one package. The first rule whose range matches the installed version applies; a version no rule
|
|
52
|
+
* matches gets a warning, so an upgrade past what an override knows is visible.
|
|
53
|
+
*/
|
|
54
|
+
export function defineOverrides(packageName: string, rules: OverrideRule[]): PackageOverrides {
|
|
55
|
+
return { package: packageName, rules }
|
|
56
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
export type PackageInfo = {
|
|
5
|
+
name: string
|
|
6
|
+
version: string
|
|
7
|
+
/** The package's root directory, real path. */
|
|
8
|
+
dir: string
|
|
9
|
+
json: Record<string, unknown>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const byDir = new Map<string, PackageInfo | null>()
|
|
13
|
+
|
|
14
|
+
function readPackage(dir: string): PackageInfo | null {
|
|
15
|
+
if (byDir.has(dir)) {
|
|
16
|
+
return byDir.get(dir)!
|
|
17
|
+
}
|
|
18
|
+
const file = path.join(dir, 'package.json')
|
|
19
|
+
let info: PackageInfo | null = null
|
|
20
|
+
if (existsSync(file)) {
|
|
21
|
+
try {
|
|
22
|
+
const json = JSON.parse(readFileSync(file, 'utf8')) as Record<string, unknown>
|
|
23
|
+
if (typeof json.name === 'string' && typeof json.version === 'string') {
|
|
24
|
+
info = { name: json.name, version: json.version, dir, json }
|
|
25
|
+
}
|
|
26
|
+
} catch {
|
|
27
|
+
info = null
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
byDir.set(dir, info)
|
|
31
|
+
return info
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The package a file belongs to: the nearest `package.json` above it with a name and a version. Nested
|
|
36
|
+
* `package.json` files that only set `type` or `sideEffects` are skipped.
|
|
37
|
+
*/
|
|
38
|
+
export function packageOfFile(file: string): { info: PackageInfo; relative: string } | null {
|
|
39
|
+
let dir = path.dirname(file)
|
|
40
|
+
while (true) {
|
|
41
|
+
const info = readPackage(dir)
|
|
42
|
+
if (info !== null) {
|
|
43
|
+
return { info, relative: path.relative(dir, file).split(path.sep).join('/') }
|
|
44
|
+
}
|
|
45
|
+
const parent = path.dirname(dir)
|
|
46
|
+
if (parent === dir || path.basename(dir) === 'node_modules') {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
dir = parent
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The installed copy of a package that an import from `fromDir` reaches, found as Node finds it: the nearest
|
|
55
|
+
* `node_modules/<name>` above. With pnpm, each package's own dependencies sit next to it, so two importers can reach
|
|
56
|
+
* two versions.
|
|
57
|
+
*/
|
|
58
|
+
export function findPackage(name: string, fromDir: string): PackageInfo | null {
|
|
59
|
+
let dir = fromDir
|
|
60
|
+
while (true) {
|
|
61
|
+
const candidate = path.join(dir, 'node_modules', name)
|
|
62
|
+
if (existsSync(path.join(candidate, 'package.json'))) {
|
|
63
|
+
return readPackage(realpathSync(candidate))
|
|
64
|
+
}
|
|
65
|
+
const parent = path.dirname(dir)
|
|
66
|
+
if (parent === dir) {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
dir = parent
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** `@scope/name/sub/path` → `['@scope/name', 'sub/path']`. Null for relative, absolute and virtual specifiers. */
|
|
74
|
+
export function splitSpecifier(specifier: string): [string, string] | null {
|
|
75
|
+
if (!/^[@a-zA-Z0-9]/.test(specifier) || specifier.includes(':')) {
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
78
|
+
const parts = specifier.split('/')
|
|
79
|
+
const nameLength = specifier.startsWith('@') ? 2 : 1
|
|
80
|
+
if (parts.length < nameLength) {
|
|
81
|
+
return null
|
|
82
|
+
}
|
|
83
|
+
return [parts.slice(0, nameLength).join('/'), parts.slice(nameLength).join('/')]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const ENTRY_EXTENSIONS = ['.web.js', '.web.mjs', '.js', '.mjs', '.cjs', '/index.web.js', '/index.js']
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The files a bundler might pick as a package's entry point for the web: its `exports` conditions, `browser`,
|
|
90
|
+
* `module` and `main`. A `replace` override applies to all of them, so it holds whichever one an import reaches.
|
|
91
|
+
*/
|
|
92
|
+
export function entryFiles(info: PackageInfo): string[] {
|
|
93
|
+
const candidates: string[] = []
|
|
94
|
+
const exportsField = info.json.exports
|
|
95
|
+
const rootExport =
|
|
96
|
+
typeof exportsField === 'string'
|
|
97
|
+
? exportsField
|
|
98
|
+
: exportsField !== null && typeof exportsField === 'object'
|
|
99
|
+
? ((exportsField as Record<string, unknown>)['.'] ?? null)
|
|
100
|
+
: null
|
|
101
|
+
collectExportTargets(rootExport, candidates)
|
|
102
|
+
for (const field of ['browser', 'module', 'main']) {
|
|
103
|
+
const value = info.json[field]
|
|
104
|
+
if (typeof value === 'string') {
|
|
105
|
+
candidates.push(value)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (candidates.length === 0) {
|
|
109
|
+
candidates.push('index')
|
|
110
|
+
}
|
|
111
|
+
const files = new Set<string>()
|
|
112
|
+
for (const candidate of candidates) {
|
|
113
|
+
const base = path.join(info.dir, candidate)
|
|
114
|
+
for (const suffix of ['', ...ENTRY_EXTENSIONS]) {
|
|
115
|
+
const file = base + suffix
|
|
116
|
+
if (existsSync(file) && statSync(file).isFile()) {
|
|
117
|
+
files.add(file)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return [...files]
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function collectExportTargets(target: unknown, into: string[]): void {
|
|
125
|
+
if (typeof target === 'string') {
|
|
126
|
+
into.push(target)
|
|
127
|
+
} else if (Array.isArray(target)) {
|
|
128
|
+
target.forEach((item) => collectExportTargets(item, into))
|
|
129
|
+
} else if (target !== null && typeof target === 'object') {
|
|
130
|
+
for (const [condition, value] of Object.entries(target)) {
|
|
131
|
+
if (condition !== 'types' && condition !== 'react-native' && condition !== 'node') {
|
|
132
|
+
collectExportTargets(value, into)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import type { SourcePatch } from './define.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A patch that makes a library file's `Platform.OS` checks see the simulated device rather than `web`. For a
|
|
7
|
+
* module's JS layer whose branches decide what the native module offers (iOS-only reminders, web-only refusals),
|
|
8
|
+
* when the native module under it is simulated.
|
|
9
|
+
*/
|
|
10
|
+
export function simulatedPlatform(file: string): SourcePatch {
|
|
11
|
+
return {
|
|
12
|
+
file,
|
|
13
|
+
replace: (source) =>
|
|
14
|
+
/\bPlatform\.OS\b/.test(source)
|
|
15
|
+
? `import { platform as __webSimPlatform } from 'web-sim/runtime';\n${source.replace(/\bPlatform\.OS\b/g, '__webSimPlatform()')}`
|
|
16
|
+
: source,
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const PLATFORM_IMAGE = /\brequire\(\s*(['"])(\.{1,2}\/[^'"]+)\.(png|jpe?g|gif|webp)\1\s*\)/g
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A dependency's source, for a file that sees the device's platform: `process.env.EXPO_OS` is read from the simulator
|
|
24
|
+
* as the app runs, and an image with `.ios` / `.android` variants (`back-icon@3x.ios.png`) loads the device's one,
|
|
25
|
+
* as Metro picks it. Both are decided when the app runs, not written in, so the pre-bundle is the same for both
|
|
26
|
+
* platforms. `file` is the file's path, which relative image paths start from.
|
|
27
|
+
*/
|
|
28
|
+
export function devicePlatformSource(source: string, file: string): string {
|
|
29
|
+
let body = source.replaceAll('process.env.EXPO_OS', '__webSimPlatform()')
|
|
30
|
+
body = body.replace(PLATFORM_IMAGE, (whole: string, quote: string, stem: string, extension: string) => {
|
|
31
|
+
const ios = platformVariant(file, stem, extension, 'ios')
|
|
32
|
+
const android = platformVariant(file, stem, extension, 'android')
|
|
33
|
+
if (ios === null && android === null) {
|
|
34
|
+
return whole
|
|
35
|
+
}
|
|
36
|
+
const load = (variant: string | null) => (variant === null ? whole : `require(${quote}${variant}${quote})`)
|
|
37
|
+
return `(__webSimPlatform() === 'ios' ? ${load(ios)} : __webSimPlatform() === 'android' ? ${load(android)} : ${whole})`
|
|
38
|
+
})
|
|
39
|
+
if (body === source) {
|
|
40
|
+
return source
|
|
41
|
+
}
|
|
42
|
+
return /^\s*(import|export)\b/m.test(source)
|
|
43
|
+
? `import { platform as __webSimPlatform } from 'web-sim/runtime';\n${body}`
|
|
44
|
+
: `var __webSimPlatform = require('web-sim/runtime').platform;\n${body}`
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The sharpest of an image's variants for one platform, as a path from the importing file, or null. */
|
|
48
|
+
function platformVariant(file: string, stem: string, extension: string, platform: 'ios' | 'android'): string | null {
|
|
49
|
+
for (const scale of ['@3x', '@2x', '@1x', '']) {
|
|
50
|
+
const candidate = `${stem}${scale}.${platform}.${extension}`
|
|
51
|
+
if (existsSync(path.resolve(path.dirname(file), candidate))) {
|
|
52
|
+
return candidate
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { type Loader, type Plugin as EsbuildPlugin, transform } from 'esbuild'
|
|
4
|
+
import type { Plugin } from 'vite'
|
|
5
|
+
import { RUNTIME_ENTRY } from '../paths.ts'
|
|
6
|
+
import { devicePlatformSource } from './platformPatch.ts'
|
|
7
|
+
import { applyPatches, type FileOverride, type OverrideRegistry } from './registry.ts'
|
|
8
|
+
import type { Worklets } from '../worklets.ts'
|
|
9
|
+
|
|
10
|
+
const ORIGINAL = 'web-sim:original'
|
|
11
|
+
const ORIGINAL_PREFIX = '\0web-sim-original:'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Applies the overrides to dependencies as Vite pre-bundles them, which is where nearly all of a package's files are
|
|
15
|
+
* loaded. The plugin is named after a hash of the overrides, because Vite keys the pre-bundle by esbuild plugin
|
|
16
|
+
* names: an edited override then rebuilds it, and browsers fetch the new bundle. Worklets are compiled last, as
|
|
17
|
+
* Metro compiles them in every dependency.
|
|
18
|
+
*/
|
|
19
|
+
export function overridesEsbuild(registry: OverrideRegistry, worklets: Worklets | null = null): EsbuildPlugin {
|
|
20
|
+
return {
|
|
21
|
+
name: `web-sim-overrides-${registry.hash()}${worklets === null ? '' : `-${worklets.key}`}`,
|
|
22
|
+
setup(build) {
|
|
23
|
+
build.onResolve({ filter: /^web-sim\/runtime$/ }, () => ({ path: RUNTIME_ENTRY }))
|
|
24
|
+
build.onResolve({ filter: /^web-sim:original$/ }, ({ importer }) => ({
|
|
25
|
+
path: importer,
|
|
26
|
+
namespace: 'web-sim-original',
|
|
27
|
+
}))
|
|
28
|
+
// A replacement's relative imports find files beside it first (a plugin's shared modules), then the package's.
|
|
29
|
+
build.onResolve({ filter: /^\.\.?\// }, ({ path: source, pluginData }) => {
|
|
30
|
+
const own = typeof pluginData?.overrideFile === 'string' ? resolveBeside(pluginData.overrideFile, source) : null
|
|
31
|
+
return own === null ? undefined : { path: own }
|
|
32
|
+
})
|
|
33
|
+
build.onLoad({ filter: /.*/, namespace: 'web-sim-original' }, ({ path: file }) => ({
|
|
34
|
+
contents: readFileSync(file, 'utf8'),
|
|
35
|
+
loader: loaderFor(file),
|
|
36
|
+
resolveDir: path.dirname(file),
|
|
37
|
+
}))
|
|
38
|
+
build.onLoad({ filter: /[\\/]node_modules[\\/]/ }, async ({ path: file }) => {
|
|
39
|
+
const override = registry.overrideForFile(file)
|
|
40
|
+
const loaded = override === null ? undefined : loadOverride(file, override)
|
|
41
|
+
if (!/\.[cm]?[jt]sx?$/.test(file) || (override !== null && override.kind !== 'patch' && override.kind !== 'replace')) {
|
|
42
|
+
return loaded
|
|
43
|
+
}
|
|
44
|
+
const source = loaded?.contents ?? readFileSync(file, 'utf8')
|
|
45
|
+
let contents = registry.seesDevicePlatform(file) && override?.kind !== 'replace' ? devicePlatformSource(source, file) : source
|
|
46
|
+
contents = (await worklets?.transform(contents, loaded?.pluginData?.overrideFile ?? file)) ?? contents
|
|
47
|
+
return contents === source ? loaded : { ...loaded, contents, loader: loaded?.loader ?? loaderFor(file), resolveDir: path.dirname(file) }
|
|
48
|
+
})
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function loadOverride(file: string, override: FileOverride) {
|
|
54
|
+
switch (override.kind) {
|
|
55
|
+
case 'replace':
|
|
56
|
+
return {
|
|
57
|
+
contents: readFileSync(override.file, 'utf8'),
|
|
58
|
+
loader: loaderFor(override.file),
|
|
59
|
+
// Compiled as if it sat where the file it replaces sits.
|
|
60
|
+
resolveDir: path.dirname(file),
|
|
61
|
+
watchFiles: [override.file],
|
|
62
|
+
pluginData: { overrideFile: override.file },
|
|
63
|
+
}
|
|
64
|
+
case 'stub':
|
|
65
|
+
// CommonJS, so importers may take any name from it.
|
|
66
|
+
return { contents: `${STUB_FACTORY}\nmodule.exports = createStub(${JSON.stringify(override.info.name)});`, loader: 'js' as const }
|
|
67
|
+
case 'patch':
|
|
68
|
+
return {
|
|
69
|
+
contents: applyPatches(readFileSync(file, 'utf8'), override),
|
|
70
|
+
loader: loaderFor(file),
|
|
71
|
+
resolveDir: path.dirname(file),
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The same overrides for files Vite serves without pre-bundling: excluded or linked dependencies.
|
|
78
|
+
*/
|
|
79
|
+
export function overridesPlugin(registry: OverrideRegistry): Plugin {
|
|
80
|
+
return {
|
|
81
|
+
name: 'web-sim:overrides',
|
|
82
|
+
enforce: 'pre',
|
|
83
|
+
resolveId(source, importer) {
|
|
84
|
+
if (source === 'web-sim/runtime') {
|
|
85
|
+
return RUNTIME_ENTRY
|
|
86
|
+
}
|
|
87
|
+
if (source === ORIGINAL && importer !== undefined) {
|
|
88
|
+
return ORIGINAL_PREFIX + importer.split('?')[0]
|
|
89
|
+
}
|
|
90
|
+
if (/^\.\.?\//.test(source) && importer !== undefined) {
|
|
91
|
+
const override = registry.overrideForFile(importer.split('?')[0])
|
|
92
|
+
if (override?.kind === 'replace') {
|
|
93
|
+
return resolveBeside(override.file, source)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return null
|
|
97
|
+
},
|
|
98
|
+
async load(id) {
|
|
99
|
+
if (id.startsWith(ORIGINAL_PREFIX)) {
|
|
100
|
+
const file = id.slice(ORIGINAL_PREFIX.length)
|
|
101
|
+
return compile(readFileSync(file, 'utf8'), file)
|
|
102
|
+
}
|
|
103
|
+
const file = id.split('?')[0]
|
|
104
|
+
const override = registry.overrideForFile(file)
|
|
105
|
+
if (override === null) {
|
|
106
|
+
if (!registry.seesDevicePlatform(file) || !/\.[cm]?[jt]sx?$/.test(file)) {
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
const source = readFileSync(file, 'utf8')
|
|
110
|
+
const contents = devicePlatformSource(source, file)
|
|
111
|
+
return contents === source ? null : contents
|
|
112
|
+
}
|
|
113
|
+
switch (override.kind) {
|
|
114
|
+
case 'replace':
|
|
115
|
+
this.addWatchFile(override.file)
|
|
116
|
+
return compile(readFileSync(override.file, 'utf8'), override.file)
|
|
117
|
+
case 'stub':
|
|
118
|
+
return `${STUB_FACTORY}\nexport default createStub(${JSON.stringify(override.info.name)});`
|
|
119
|
+
case 'patch': {
|
|
120
|
+
const source = applyPatches(readFileSync(file, 'utf8'), override)
|
|
121
|
+
return registry.seesDevicePlatform(file) ? devicePlatformSource(source, file) : source
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const SOURCE_EXTENSIONS = ['', '.ts', '.tsx', '.js', '.jsx', '.mjs', '/index.ts', '/index.tsx', '/index.js']
|
|
129
|
+
|
|
130
|
+
/** `source` relative to the replacement file `file`, when a file is there. */
|
|
131
|
+
function resolveBeside(file: string, source: string): string | null {
|
|
132
|
+
const base = path.resolve(path.dirname(file), source)
|
|
133
|
+
for (const extension of SOURCE_EXTENSIONS) {
|
|
134
|
+
const candidate = base + extension
|
|
135
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
|
136
|
+
return candidate
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return null
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function compile(source: string, file: string): Promise<string> {
|
|
143
|
+
const result = await transform(source, { loader: loaderFor(file), jsx: 'automatic', format: 'esm', sourcefile: file })
|
|
144
|
+
return result.code
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function loaderFor(file: string): Loader {
|
|
148
|
+
const extension = path.extname(file)
|
|
149
|
+
return extension === '.ts' ? 'ts' : extension === '.tsx' ? 'tsx' : extension === '.css' ? 'css' : 'jsx'
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* A stub where any property read, call or construction returns another stub, so `Module.configure().then` chains
|
|
154
|
+
* succeed and do nothing. `then` is undefined, so a stub is never mistaken for a promise.
|
|
155
|
+
*/
|
|
156
|
+
export const STUB_FACTORY = `
|
|
157
|
+
function createStub(name) {
|
|
158
|
+
const target = function () {};
|
|
159
|
+
return new Proxy(target, {
|
|
160
|
+
get(_, property) {
|
|
161
|
+
if (property === 'then' || typeof property === 'symbol') return undefined;
|
|
162
|
+
if (property === '__esModule') return false;
|
|
163
|
+
if (property === 'toString') return () => '[web-sim stub ' + name + ']';
|
|
164
|
+
return createStub(name + '.' + property);
|
|
165
|
+
},
|
|
166
|
+
apply() { return createStub(name + '()'); },
|
|
167
|
+
construct() { return createStub('new ' + name); },
|
|
168
|
+
});
|
|
169
|
+
}`
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { pathToFileURL } from 'node:url'
|
|
5
|
+
import semver from 'semver'
|
|
6
|
+
import type { OverrideRule, PackageOverrides, SourcePatch } from './define.ts'
|
|
7
|
+
import { RN_APP_SHIM, RUNTIME_ENTRY } from '../paths.ts'
|
|
8
|
+
import { entryFiles, type PackageInfo, packageOfFile } from './packages.ts'
|
|
9
|
+
|
|
10
|
+
export const DEFINITION_FILE = 'overrides.ts'
|
|
11
|
+
|
|
12
|
+
export type OverrideOrigin = 'project' | 'built-in' | 'config'
|
|
13
|
+
|
|
14
|
+
export type LoadedRule = OverrideRule & {
|
|
15
|
+
origin: OverrideOrigin
|
|
16
|
+
/** The file the rule was defined in, which its paths are relative to. */
|
|
17
|
+
definedIn: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* What loading one file of a package turns into.
|
|
22
|
+
*/
|
|
23
|
+
export type FileOverride =
|
|
24
|
+
| { kind: 'replace'; file: string; rule: LoadedRule; info: PackageInfo }
|
|
25
|
+
| { kind: 'stub'; rule: LoadedRule; info: PackageInfo }
|
|
26
|
+
| { kind: 'patch'; patches: SourcePatch[]; rule: LoadedRule; info: PackageInfo; relative: string }
|
|
27
|
+
|
|
28
|
+
export type Warning = { package: string; version: string; message: string }
|
|
29
|
+
|
|
30
|
+
export class OverrideRegistry {
|
|
31
|
+
private readonly byPackage = new Map<string, LoadedRule[]>()
|
|
32
|
+
private readonly ruleCache = new Map<string, LoadedRule | null>()
|
|
33
|
+
private readonly entryCache = new Map<string, Set<string>>()
|
|
34
|
+
readonly warnings = new Map<string, Warning>()
|
|
35
|
+
/** Every file the overrides are made of, for the pre-bundle's hash and the watcher. */
|
|
36
|
+
readonly files: string[] = []
|
|
37
|
+
/** Packages the project's config says see the device's platform, for any version. */
|
|
38
|
+
devicePlatformPackages = new Set<string>()
|
|
39
|
+
private readonly devicePlatformCache = new Map<string, boolean>()
|
|
40
|
+
onWarning: (warning: Warning) => void = () => undefined
|
|
41
|
+
|
|
42
|
+
static async load(dirs: { dir: string; origin: OverrideOrigin }[], extra: PackageOverrides[] = []) {
|
|
43
|
+
const registry = new OverrideRegistry()
|
|
44
|
+
for (const { dir, origin } of dirs) {
|
|
45
|
+
if (!existsSync(dir)) {
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
for (const file of walk(dir)) {
|
|
49
|
+
registry.files.push(file)
|
|
50
|
+
if (path.basename(file) !== DEFINITION_FILE) {
|
|
51
|
+
continue
|
|
52
|
+
}
|
|
53
|
+
// A query per load, because the dev server reloads overrides in the same process and Node caches modules
|
|
54
|
+
// by URL: without it, a restart after an edit would hash the new file but run the old one.
|
|
55
|
+
const module = (await import(`${pathToFileURL(file).href}?v=${Date.now()}`)) as { default?: PackageOverrides | PackageOverrides[] }
|
|
56
|
+
const definitions = Array.isArray(module.default) ? module.default : [module.default]
|
|
57
|
+
for (const definition of definitions) {
|
|
58
|
+
if (definition === undefined || typeof definition.package !== 'string') {
|
|
59
|
+
throw new Error(`${file} must default-export defineOverrides(...)`)
|
|
60
|
+
}
|
|
61
|
+
registry.add(definition, origin, file)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
for (const definition of extra) {
|
|
66
|
+
registry.add(definition, 'config', '')
|
|
67
|
+
}
|
|
68
|
+
return registry
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Counts a folder's files in the hash: a plugin's modules its overrides import. */
|
|
72
|
+
addHashedDir(dir: string): void {
|
|
73
|
+
for (const file of walk(dir)) {
|
|
74
|
+
if (!this.files.includes(file)) {
|
|
75
|
+
this.files.push(file)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private add(definition: PackageOverrides, origin: OverrideOrigin, definedIn: string) {
|
|
81
|
+
for (const rule of definition.rules) {
|
|
82
|
+
if (semver.validRange(rule.versions) === null) {
|
|
83
|
+
throw new Error(`${definedIn || 'web-sim.config'}: '${rule.versions}' is not a semver range`)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const rules = this.byPackage.get(definition.package) ?? []
|
|
87
|
+
const loaded = definition.rules.map((rule) => ({ ...rule, origin, definedIn }))
|
|
88
|
+
// Project and config rules are tried before built-in ones.
|
|
89
|
+
this.byPackage.set(definition.package, origin === 'built-in' ? [...rules, ...loaded] : [...loaded, ...rules])
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
packages(): string[] {
|
|
93
|
+
return [...this.byPackage.keys()]
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
rulesFor(name: string): LoadedRule[] {
|
|
97
|
+
return this.byPackage.get(name) ?? []
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The rule for an installed copy of a package, or null when it has none. A package with overrides but none for
|
|
102
|
+
* this version is warned about once.
|
|
103
|
+
*/
|
|
104
|
+
ruleFor(info: PackageInfo): LoadedRule | null {
|
|
105
|
+
const key = `${info.name}@${info.version}`
|
|
106
|
+
if (this.ruleCache.has(key)) {
|
|
107
|
+
return this.ruleCache.get(key)!
|
|
108
|
+
}
|
|
109
|
+
const rules = this.byPackage.get(info.name)
|
|
110
|
+
let match: LoadedRule | null = null
|
|
111
|
+
if (rules !== undefined) {
|
|
112
|
+
match = rules.find((rule) => semver.satisfies(info.version, rule.versions, { includePrerelease: true })) ?? null
|
|
113
|
+
if (match === null) {
|
|
114
|
+
const warning = {
|
|
115
|
+
package: info.name,
|
|
116
|
+
version: info.version,
|
|
117
|
+
message: `${key} has overrides for ${rules.map((rule) => `'${rule.versions}'`).join(', ')} but none for this version, so it runs unchanged`,
|
|
118
|
+
}
|
|
119
|
+
this.warnings.set(key, warning)
|
|
120
|
+
this.onWarning(warning)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
this.ruleCache.set(key, match)
|
|
124
|
+
return match
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* What loading `file` should give instead of its contents, if anything.
|
|
129
|
+
*/
|
|
130
|
+
overrideForFile(file: string): FileOverride | null {
|
|
131
|
+
if (!file.includes(`${path.sep}node_modules${path.sep}`)) {
|
|
132
|
+
return null
|
|
133
|
+
}
|
|
134
|
+
const owner = packageOfFile(file)
|
|
135
|
+
if (owner === null) {
|
|
136
|
+
return null
|
|
137
|
+
}
|
|
138
|
+
const { info, relative } = owner
|
|
139
|
+
const rule = this.ruleFor(info)
|
|
140
|
+
if (rule === null) {
|
|
141
|
+
return null
|
|
142
|
+
}
|
|
143
|
+
if (rule.stub) {
|
|
144
|
+
return { kind: 'stub', rule, info }
|
|
145
|
+
}
|
|
146
|
+
const replacement = rule.files?.[relative]
|
|
147
|
+
if (replacement !== undefined) {
|
|
148
|
+
return { kind: 'replace', file: resolveFrom(rule, replacement), rule, info }
|
|
149
|
+
}
|
|
150
|
+
if (rule.replace !== undefined && this.entriesOf(info).has(file)) {
|
|
151
|
+
return { kind: 'replace', file: resolveFrom(rule, rule.replace), rule, info }
|
|
152
|
+
}
|
|
153
|
+
const patches = (rule.patch ?? []).filter((patch) =>
|
|
154
|
+
typeof patch.file === 'string' ? patch.file === relative : patch.file.test(relative),
|
|
155
|
+
)
|
|
156
|
+
if (patches.length > 0) {
|
|
157
|
+
return { kind: 'patch', patches, rule, info, relative }
|
|
158
|
+
}
|
|
159
|
+
return null
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Whether a dependency's file sees the simulated device's platform rather than `web`: its package is listed in the
|
|
164
|
+
* config's `devicePlatform`, or the rule for its version has `platform: 'device'` covering it.
|
|
165
|
+
*/
|
|
166
|
+
seesDevicePlatform(file: string): boolean {
|
|
167
|
+
let sees = this.devicePlatformCache.get(file)
|
|
168
|
+
if (sees === undefined) {
|
|
169
|
+
sees = this.computeDevicePlatform(file)
|
|
170
|
+
this.devicePlatformCache.set(file, sees)
|
|
171
|
+
}
|
|
172
|
+
return sees
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private computeDevicePlatform(file: string): boolean {
|
|
176
|
+
if (!file.includes(`${path.sep}node_modules${path.sep}`)) {
|
|
177
|
+
return false
|
|
178
|
+
}
|
|
179
|
+
const owner = packageOfFile(file)
|
|
180
|
+
if (owner === null) {
|
|
181
|
+
return false
|
|
182
|
+
}
|
|
183
|
+
if (this.devicePlatformPackages.has(owner.info.name)) {
|
|
184
|
+
return true
|
|
185
|
+
}
|
|
186
|
+
const platform = this.byPackage.has(owner.info.name) ? this.ruleFor(owner.info)?.platform : undefined
|
|
187
|
+
if (platform === undefined) {
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
if (platform === 'device') {
|
|
191
|
+
return true
|
|
192
|
+
}
|
|
193
|
+
return platform.device.some((pattern) =>
|
|
194
|
+
typeof pattern === 'string'
|
|
195
|
+
? pattern.endsWith('/')
|
|
196
|
+
? owner.relative.startsWith(pattern)
|
|
197
|
+
: owner.relative === pattern
|
|
198
|
+
: pattern.test(owner.relative),
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private entriesOf(info: PackageInfo): Set<string> {
|
|
203
|
+
let entries = this.entryCache.get(info.dir)
|
|
204
|
+
if (entries === undefined) {
|
|
205
|
+
entries = new Set(entryFiles(info))
|
|
206
|
+
this.entryCache.set(info.dir, entries)
|
|
207
|
+
}
|
|
208
|
+
return entries
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* A hash of everything that changes what the pre-bundle holds, so an edited override rebuilds it.
|
|
213
|
+
*/
|
|
214
|
+
hash(): string {
|
|
215
|
+
const hash = createHash('sha256')
|
|
216
|
+
// The code that applies overrides, and the `react-native` shims, decide what the pre-bundle holds too.
|
|
217
|
+
// Overrides bundle `web-sim/runtime` into the dependencies they replace, so it is part of what they are.
|
|
218
|
+
const runtime = [...walk(path.dirname(RUNTIME_ENTRY)), ...walk(path.dirname(RN_APP_SHIM)), ...walk(import.meta.dirname)]
|
|
219
|
+
for (const file of [...this.files, ...runtime].sort()) {
|
|
220
|
+
hash.update(file)
|
|
221
|
+
hash.update(readFileSync(file))
|
|
222
|
+
}
|
|
223
|
+
for (const [name, rules] of this.byPackage) {
|
|
224
|
+
if (rules.some((rule) => rule.origin === 'config')) {
|
|
225
|
+
hash.update(name + JSON.stringify(rules, (_, value) => (typeof value === 'function' ? String(value) : value)))
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
hash.update(JSON.stringify([...this.devicePlatformPackages].sort()))
|
|
229
|
+
return hash.digest('hex').slice(0, 10)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function applyPatches(source: string, override: Extract<FileOverride, { kind: 'patch' }>): string {
|
|
234
|
+
return override.patches.reduce((current, patch) => {
|
|
235
|
+
const next = patch.replace(current)
|
|
236
|
+
if (next === current) {
|
|
237
|
+
throw new Error(
|
|
238
|
+
`web-sim: a patch for ${override.info.name}@${override.info.version} (${override.relative}, from ${override.rule.definedIn || 'web-sim.config'}) no longer changes the file. Update the override, or narrow its 'versions'.`,
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
return next
|
|
242
|
+
}, source)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function resolveFrom(rule: LoadedRule, file: string): string {
|
|
246
|
+
return path.resolve(path.dirname(rule.definedIn), file)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function* walk(dir: string): Generator<string> {
|
|
250
|
+
for (const entry of readdirSync(dir)) {
|
|
251
|
+
const full = path.join(dir, entry)
|
|
252
|
+
if (statSync(full).isDirectory()) {
|
|
253
|
+
yield* walk(full)
|
|
254
|
+
} else if (/\.(tsx?|jsx?|mjs|css)$/.test(entry)) {
|
|
255
|
+
yield full
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|