@expo/metro-runtime 3.0.1 → 3.0.2

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.
Files changed (112) hide show
  1. package/build/LoadingView.d.ts +6 -0
  2. package/build/LoadingView.d.ts.map +1 -1
  3. package/build/LoadingView.js +9 -3
  4. package/build/LoadingView.js.map +1 -1
  5. package/build/async-require/fetchAsync.d.ts +6 -0
  6. package/build/async-require/fetchAsync.d.ts.map +1 -1
  7. package/build/async-require/fetchAsync.js +1 -2
  8. package/build/async-require/fetchAsync.js.map +1 -1
  9. package/build/async-require/fetchThenEval.d.ts +1 -6
  10. package/build/async-require/fetchThenEval.d.ts.map +1 -1
  11. package/build/async-require/fetchThenEval.js +2 -32
  12. package/build/async-require/fetchThenEval.js.map +1 -1
  13. package/build/async-require/fetchThenEval.web.js +1 -1
  14. package/build/async-require/fetchThenEval.web.js.map +1 -1
  15. package/build/async-require/fetchThenEvalJs.d.ts +7 -0
  16. package/build/async-require/fetchThenEvalJs.d.ts.map +1 -0
  17. package/build/async-require/fetchThenEvalJs.js +36 -0
  18. package/build/async-require/fetchThenEvalJs.js.map +1 -0
  19. package/build/async-require/index.native.d.ts +7 -0
  20. package/build/async-require/index.native.d.ts.map +1 -0
  21. package/build/async-require/index.native.js +14 -0
  22. package/build/async-require/index.native.js.map +1 -0
  23. package/build/effects.d.ts +0 -1
  24. package/build/effects.js +1 -6
  25. package/build/effects.js.map +1 -1
  26. package/build/error-overlay/Data/parseLogBoxLog.d.ts.map +1 -1
  27. package/build/error-overlay/Data/parseLogBoxLog.js +1 -2
  28. package/build/error-overlay/Data/parseLogBoxLog.js.map +1 -1
  29. package/build/error-overlay/LogBox.web.d.ts.map +1 -1
  30. package/build/error-overlay/LogBox.web.js +1 -2
  31. package/build/error-overlay/LogBox.web.js.map +1 -1
  32. package/build/error-overlay/index.d.ts.map +1 -1
  33. package/build/error-overlay/index.js +1 -0
  34. package/build/error-overlay/index.js.map +1 -1
  35. package/build/getDevServer.d.ts.map +1 -1
  36. package/build/getDevServer.js +2 -6
  37. package/build/getDevServer.js.map +1 -1
  38. package/build/index.d.ts +8 -0
  39. package/build/index.d.ts.map +1 -1
  40. package/build/index.js +10 -8
  41. package/build/index.js.map +1 -1
  42. package/build/setupHMR.js +21 -24
  43. package/build/setupHMR.js.map +1 -1
  44. package/package.json +5 -2
  45. package/src/HMRClient.native.ts +3 -0
  46. package/src/HMRClient.ts +316 -0
  47. package/src/LoadingView.native.ts +3 -0
  48. package/src/LoadingView.ts +24 -0
  49. package/src/__mocks__/LoadingView.ts +4 -0
  50. package/src/async-require/buildAsyncRequire.ts +34 -0
  51. package/src/async-require/buildUrlForBundle.native.ts +28 -0
  52. package/src/async-require/buildUrlForBundle.ts +18 -0
  53. package/src/async-require/fetchAsync.native.ts +72 -0
  54. package/src/async-require/fetchAsync.ts +19 -0
  55. package/src/async-require/fetchThenEval.ts +1 -0
  56. package/src/async-require/fetchThenEval.web.ts +70 -0
  57. package/src/async-require/fetchThenEvalJs.ts +39 -0
  58. package/src/async-require/index.native.ts +15 -0
  59. package/src/async-require/index.ts +10 -0
  60. package/src/async-require/loadBundle.ts +46 -0
  61. package/src/effects.native.ts +0 -0
  62. package/src/effects.ts +11 -0
  63. package/src/error-overlay/Data/LogBoxData.tsx +438 -0
  64. package/src/error-overlay/Data/LogBoxLog.ts +221 -0
  65. package/src/error-overlay/Data/LogBoxSymbolication.tsx +64 -0
  66. package/src/error-overlay/Data/LogContext.tsx +41 -0
  67. package/src/error-overlay/Data/parseLogBoxLog.tsx +342 -0
  68. package/src/error-overlay/ErrorOverlay.tsx +191 -0
  69. package/src/error-overlay/LogBox.ts +51 -0
  70. package/src/error-overlay/LogBox.web.ts +174 -0
  71. package/src/error-overlay/UI/AnsiHighlight.tsx +96 -0
  72. package/src/error-overlay/UI/LogBoxButton.tsx +63 -0
  73. package/src/error-overlay/UI/LogBoxMessage.tsx +73 -0
  74. package/src/error-overlay/UI/LogBoxStyle.ts +64 -0
  75. package/src/error-overlay/UI/constants.ts +7 -0
  76. package/src/error-overlay/formatProjectFilePath.ts +38 -0
  77. package/src/error-overlay/index.tsx +34 -0
  78. package/src/error-overlay/modules/ExceptionsManager/index.native.ts +4 -0
  79. package/src/error-overlay/modules/ExceptionsManager/index.ts +82 -0
  80. package/src/error-overlay/modules/NativeLogBox/index.native.ts +3 -0
  81. package/src/error-overlay/modules/NativeLogBox/index.tsx +27 -0
  82. package/src/error-overlay/modules/openFileInEditor/index.native.ts +3 -0
  83. package/src/error-overlay/modules/openFileInEditor/index.ts +16 -0
  84. package/src/error-overlay/modules/parseErrorStack/index.ts +26 -0
  85. package/src/error-overlay/modules/parseErrorStack/parseHermesStack.ts +3 -0
  86. package/src/error-overlay/modules/stringifySafe/index.ts +115 -0
  87. package/src/error-overlay/modules/symbolicateStackTrace/index.native.ts +3 -0
  88. package/src/error-overlay/modules/symbolicateStackTrace/index.ts +39 -0
  89. package/src/error-overlay/overlay/LogBoxInspectorCodeFrame.tsx +102 -0
  90. package/src/error-overlay/overlay/LogBoxInspectorFooter.tsx +111 -0
  91. package/src/error-overlay/overlay/LogBoxInspectorHeader.tsx +167 -0
  92. package/src/error-overlay/overlay/LogBoxInspectorMessageHeader.tsx +116 -0
  93. package/src/error-overlay/overlay/LogBoxInspectorSection.tsx +52 -0
  94. package/src/error-overlay/overlay/LogBoxInspectorSourceMapStatus.tsx +125 -0
  95. package/src/error-overlay/overlay/LogBoxInspectorStackFrame.tsx +89 -0
  96. package/src/error-overlay/overlay/LogBoxInspectorStackFrames.tsx +201 -0
  97. package/src/error-overlay/toast/ErrorToast.tsx +167 -0
  98. package/src/error-overlay/toast/ErrorToastContainer.tsx +9 -0
  99. package/src/error-overlay/toast/ErrorToastContainer.web.tsx +92 -0
  100. package/src/error-overlay/toast/ErrorToastMessage.tsx +28 -0
  101. package/src/error-overlay/useRejectionHandler.ts +61 -0
  102. package/src/getDevServer.native.ts +3 -0
  103. package/src/getDevServer.ts +34 -0
  104. package/src/index.ts +12 -0
  105. package/src/location/Location.native.ts +201 -0
  106. package/src/location/Location.ts +3 -0
  107. package/src/location/install.native.ts +90 -0
  108. package/src/location/install.ts +0 -0
  109. package/src/messageSocket.ts +25 -0
  110. package/src/setupFastRefresh.ts +30 -0
  111. package/src/setupHMR.ts +28 -0
  112. package/src/symbolicate.ts +6 -0
@@ -0,0 +1,201 @@
1
+ // Copyright © 2023 650 Industries.
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+
4
+ import URL from 'url-parse';
5
+
6
+ class DOMException extends Error {
7
+ constructor(message: string, name: string) {
8
+ super(message);
9
+ this.name = name;
10
+ }
11
+ }
12
+
13
+ // The differences between the definitions of `Location` and `WorkerLocation`
14
+ // are because of the `LegacyUnforgeable` attribute only specified upon
15
+ // `Location`'s properties. See:
16
+ // - https://html.spec.whatwg.org/multipage/history.html#the-location-interface
17
+ // - https://heycam.github.io/webidl/#LegacyUnforgeable
18
+ class Location {
19
+ constructor(href: string | null = null) {
20
+ const url = new URL(
21
+ // @ts-expect-error
22
+ href
23
+ );
24
+ // @ts-expect-error
25
+ url.username = '';
26
+ // @ts-expect-error
27
+ url.password = '';
28
+ Object.defineProperties(this, {
29
+ hash: {
30
+ get() {
31
+ return url.hash;
32
+ },
33
+ set() {
34
+ throw new DOMException(`Cannot set "location.hash".`, 'NotSupportedError');
35
+ },
36
+ enumerable: true,
37
+ },
38
+ host: {
39
+ get() {
40
+ return url.host;
41
+ },
42
+ set() {
43
+ throw new DOMException(`Cannot set "location.host".`, 'NotSupportedError');
44
+ },
45
+ enumerable: true,
46
+ },
47
+ hostname: {
48
+ get() {
49
+ return url.hostname;
50
+ },
51
+ set() {
52
+ throw new DOMException(`Cannot set "location.hostname".`, 'NotSupportedError');
53
+ },
54
+ enumerable: true,
55
+ },
56
+ href: {
57
+ get() {
58
+ return url.href;
59
+ },
60
+ set() {
61
+ throw new DOMException(`Cannot set "location.href".`, 'NotSupportedError');
62
+ },
63
+ enumerable: true,
64
+ },
65
+ origin: {
66
+ get() {
67
+ return url.origin;
68
+ },
69
+ enumerable: true,
70
+ },
71
+ pathname: {
72
+ get() {
73
+ return url.pathname;
74
+ },
75
+ set() {
76
+ throw new DOMException(`Cannot set "location.pathname".`, 'NotSupportedError');
77
+ },
78
+ enumerable: true,
79
+ },
80
+ port: {
81
+ get() {
82
+ return url.port;
83
+ },
84
+ set() {
85
+ throw new DOMException(`Cannot set "location.port".`, 'NotSupportedError');
86
+ },
87
+ enumerable: true,
88
+ },
89
+ protocol: {
90
+ get() {
91
+ return url.protocol;
92
+ },
93
+ set() {
94
+ throw new DOMException(`Cannot set "location.protocol".`, 'NotSupportedError');
95
+ },
96
+ enumerable: true,
97
+ },
98
+ search: {
99
+ get() {
100
+ // @ts-expect-error
101
+ return url.search;
102
+ },
103
+ set() {
104
+ throw new DOMException(`Cannot set "location.search".`, 'NotSupportedError');
105
+ },
106
+ enumerable: true,
107
+ },
108
+ ancestorOrigins: {
109
+ get() {
110
+ return {
111
+ length: 0,
112
+ item: () => null,
113
+ contains: () => false,
114
+ };
115
+ },
116
+ enumerable: true,
117
+ },
118
+ assign: {
119
+ value: function assign() {
120
+ throw new DOMException(`Cannot call "location.assign()".`, 'NotSupportedError');
121
+ },
122
+ enumerable: true,
123
+ },
124
+ reload: {
125
+ value: function reload() {
126
+ if (process.env.NODE_ENV !== 'production') {
127
+ // NOTE: This does change how native fast refresh works. The upstream metro-runtime will check
128
+ // if `location.reload` exists before falling back on an implementation that is nearly identical to
129
+ // this. The main difference is that on iOS there is a "reason" message sent, but at the time of writing
130
+ // this, that message is unused (ref: `RCTTriggerReloadCommandNotification`).
131
+ const DevSettings = (require('react-native') as typeof import('react-native'))
132
+ .DevSettings;
133
+ return DevSettings.reload();
134
+ } else {
135
+ throw new DOMException(`Cannot call "location.reload()".`, 'NotSupportedError');
136
+ }
137
+ },
138
+ enumerable: true,
139
+ },
140
+ replace: {
141
+ value: function replace() {
142
+ throw new DOMException(`Cannot call "location.replace()".`, 'NotSupportedError');
143
+ },
144
+ enumerable: true,
145
+ },
146
+ toString: {
147
+ value: function toString() {
148
+ return url.href;
149
+ },
150
+ enumerable: true,
151
+ },
152
+ [Symbol.for('Expo.privateCustomInspect')]: {
153
+ value(inspect: any) {
154
+ const object = {
155
+ hash: this.hash,
156
+ host: this.host,
157
+ hostname: this.hostname,
158
+ href: this.href,
159
+ origin: this.origin,
160
+ pathname: this.pathname,
161
+ port: this.port,
162
+ protocol: this.protocol,
163
+ search: this.search,
164
+ };
165
+ return `${this.constructor.name} ${inspect(object)}`;
166
+ },
167
+ },
168
+ });
169
+ }
170
+ }
171
+
172
+ Object.defineProperties(Location.prototype, {
173
+ [Symbol.toString()]: {
174
+ value: 'Location',
175
+ configurable: true,
176
+ },
177
+ });
178
+
179
+ let location: Location | undefined = undefined;
180
+
181
+ export function setLocationHref(href: string) {
182
+ location = new Location(href);
183
+ }
184
+
185
+ export function install() {
186
+ Object.defineProperty(global, 'Location', {
187
+ value: Location,
188
+ configurable: true,
189
+ writable: true,
190
+ });
191
+
192
+ Object.defineProperty(window, 'location', {
193
+ get() {
194
+ return location;
195
+ },
196
+ set() {
197
+ throw new DOMException(`Cannot set "location".`, 'NotSupportedError');
198
+ },
199
+ enumerable: true,
200
+ });
201
+ }
@@ -0,0 +1,3 @@
1
+ export function install() {}
2
+
3
+ export function setLocationHref(href: string) {}
@@ -0,0 +1,90 @@
1
+ // This MUST be first to ensure that `fetch` is defined in the React Native environment.
2
+ import 'react-native/Libraries/Core/InitializeCore';
3
+
4
+ import Constants from 'expo-constants';
5
+ import URL from 'url-parse';
6
+
7
+ import { install, setLocationHref } from './Location';
8
+ import getDevServer from '../getDevServer';
9
+
10
+ let hasWarned = false;
11
+
12
+ const manifest = Constants.expoConfig as Record<string, any> | null;
13
+
14
+ // Add a development warning for fetch requests with relative paths
15
+ // to ensure developers are aware of the need to configure a production
16
+ // base URL in the Expo config (app.json) under `expo.extra.router.origin`.
17
+ function warnProductionOriginNotConfigured(requestUrl: string) {
18
+ if (hasWarned) {
19
+ return;
20
+ }
21
+ hasWarned = true;
22
+ if (!manifest?.extra?.router?.origin) {
23
+ console.warn(
24
+ `The relative fetch request "${requestUrl}" will not work in production until the Expo Router Config Plugin (app.json) is configured with the \`origin\` prop set to the base URL of your web server, e.g. \`{ plugins: [["expo-router", { origin: "..." }]] }\`. [Learn more](https://expo.github.io/router/docs/lab/runtime-location)`
25
+ );
26
+ }
27
+ }
28
+
29
+ // TODO: This would be better if native and tied as close to the JS engine as possible, i.e. it should
30
+ // reflect the exact location of the JS file that was executed.
31
+ function getBaseUrl() {
32
+ if (process.env.NODE_ENV !== 'production') {
33
+ // e.g. http://localhost:19006
34
+ return getDevServer().url?.replace(/\/$/, '');
35
+ }
36
+
37
+ // TODO: Make it official by moving out of `extra`
38
+ const productionBaseUrl = manifest?.extra?.router?.origin;
39
+
40
+ if (!productionBaseUrl) {
41
+ return null;
42
+ }
43
+
44
+ // Ensure no trailing slash
45
+ return productionBaseUrl?.replace(/\/$/, '');
46
+ }
47
+
48
+ function wrapFetchWithWindowLocation(fetch: Function & { __EXPO_BASE_URL_POLYFILLED?: boolean }) {
49
+ if (fetch.__EXPO_BASE_URL_POLYFILLED) {
50
+ return fetch;
51
+ }
52
+
53
+ const _fetch = (...props: any[]) => {
54
+ if (props[0] && typeof props[0] === 'string' && props[0].startsWith('/')) {
55
+ if (process.env.NODE_ENV !== 'production') {
56
+ warnProductionOriginNotConfigured(props[0]);
57
+ }
58
+
59
+ props[0] = new URL(props[0], window.location?.origin).toString();
60
+ } else if (props[0] && typeof props[0] === 'object') {
61
+ if (props[0].url && typeof props[0].url === 'string' && props[0].url.startsWith('/')) {
62
+ if (process.env.NODE_ENV !== 'production') {
63
+ warnProductionOriginNotConfigured(props[0]);
64
+ }
65
+
66
+ props[0].url = new URL(props[0].url, window.location?.origin).toString();
67
+ }
68
+ }
69
+ return fetch(...props);
70
+ };
71
+
72
+ _fetch.__EXPO_BASE_URL_POLYFILLED = true;
73
+
74
+ return _fetch;
75
+ }
76
+
77
+ if (manifest?.extra?.router?.origin !== false) {
78
+ // Polyfill window.location in native runtimes.
79
+ if (typeof window !== 'undefined' && !window.location) {
80
+ const url = getBaseUrl();
81
+ if (url) {
82
+ setLocationHref(url);
83
+ install();
84
+ }
85
+ }
86
+ // Polyfill native fetch to support relative URLs
87
+ Object.defineProperty(global, 'fetch', {
88
+ value: wrapFetchWithWindowLocation(fetch),
89
+ });
90
+ }
File without changes
@@ -0,0 +1,25 @@
1
+ /* eslint-env browser */
2
+
3
+ // Setup websocket messages for reloading the page from the command line.
4
+ // This is normally setup on the native client.
5
+
6
+ const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
7
+ const messageSocket = new WebSocket(`${protocol}://${window.location.host}/message`);
8
+ messageSocket.onmessage = (message) => {
9
+ const data = JSON.parse(String(message.data));
10
+ switch (data.method) {
11
+ case 'sendDevCommand':
12
+ switch (data.params.name) {
13
+ case 'reload':
14
+ window.location.reload();
15
+ break;
16
+ }
17
+ break;
18
+ case 'reload':
19
+ window.location.reload();
20
+ break;
21
+ case 'devMenu':
22
+ // no-op
23
+ break;
24
+ }
25
+ };
@@ -0,0 +1,30 @@
1
+ // This needs to run before the renderer initializes.
2
+
3
+ const ReactRefreshRuntime = require('react-refresh/runtime');
4
+ ReactRefreshRuntime.injectIntoGlobalHook(global);
5
+
6
+ const Refresh = {
7
+ performFullRefresh() {
8
+ location.reload();
9
+ },
10
+
11
+ createSignatureFunctionForTransform: ReactRefreshRuntime.createSignatureFunctionForTransform,
12
+
13
+ isLikelyComponentType: ReactRefreshRuntime.isLikelyComponentType,
14
+
15
+ getFamilyByType: ReactRefreshRuntime.getFamilyByType,
16
+
17
+ register: ReactRefreshRuntime.register,
18
+
19
+ performReactRefresh() {
20
+ if (ReactRefreshRuntime.hasUnrecoverableErrors()) {
21
+ location.reload();
22
+ return;
23
+ }
24
+ ReactRefreshRuntime.performReactRefresh();
25
+ },
26
+ };
27
+
28
+ // The metro require polyfill can not have dependencies (applies for all polyfills).
29
+ // Expose `Refresh` by assigning it to global to make it available in the polyfill.
30
+ global[(global.__METRO_GLOBAL_PREFIX__ || '') + '__ReactRefresh'] = Refresh;
@@ -0,0 +1,28 @@
1
+ import HMRClient from './HMRClient';
2
+
3
+ // Sets up developer tools for React Native web.
4
+ // We assume full control over the console and send JavaScript logs to Metro.
5
+ // [
6
+ // 'trace',
7
+ // 'info',
8
+ // 'warn',
9
+ // 'error',
10
+ // 'log',
11
+ // 'group',
12
+ // 'groupCollapsed',
13
+ // 'groupEnd',
14
+ // 'debug',
15
+ // ].forEach(level => {
16
+ // const originalFunction = console[level];
17
+ // console[level] = function (...args: readonly any[]) {
18
+ // HMRClient.log(
19
+ // // @ts-expect-error
20
+ // level, args);
21
+ // originalFunction.apply(console, args);
22
+ // };
23
+ // });
24
+
25
+ HMRClient.log('log', [`[web] Logs will appear in the browser console`]);
26
+
27
+ // This is called native on native platforms
28
+ HMRClient.setup({ isEnabled: true });
@@ -0,0 +1,6 @@
1
+ import { LogBoxLog } from './error-overlay/Data/LogBoxLog';
2
+ import parseErrorStack from './error-overlay/modules/parseErrorStack';
3
+
4
+ export { LogBoxLog, parseErrorStack };
5
+
6
+ export * from './error-overlay/formatProjectFilePath';