@rollipop/core 0.0.0 → 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # @rollipop/core
2
+
3
+ ## 0.1.0-alpha.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 7a1d9a7: pre-alpha
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [7a1d9a7]
12
+ - @rollipop/common@0.1.0-alpha.0
package/README.md CHANGED
@@ -1 +1 @@
1
- # rollipop-empty
1
+ # @rollipop/core
@@ -0,0 +1,21 @@
1
+ import { createRequire } from "node:module";
2
+
3
+ //#region rolldown:runtime
4
+ var __defProp = Object.defineProperty;
5
+ var __export = (all, symbols) => {
6
+ let target = {};
7
+ for (var name in all) {
8
+ __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ }
13
+ if (symbols) {
14
+ __defProp(target, Symbol.toStringTag, { value: "Module" });
15
+ }
16
+ return target;
17
+ };
18
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
19
+
20
+ //#endregion
21
+ export { __require as n, __export as t };
@@ -0,0 +1,212 @@
1
+ import LogBox from '../LogBox/LogBox';
2
+ import NativeRedBox from '../NativeModules/specs/NativeRedBox';
3
+
4
+ //#region src/runtime/hmr-client.ts
5
+ const Platform = require("./Platform").default;
6
+ const prettyFormat = require("pretty-format");
7
+ var HMRClient = class HMRClient {
8
+ static STARTUP_ERROR = "Expected HMRClient.setup() call at startup";
9
+ static MAX_PENDING_LOGS = 100;
10
+ enabled = true;
11
+ _socketHolder = null;
12
+ unavailableMessage = null;
13
+ compileErrorMessage = null;
14
+ pendingUpdatesCount = 0;
15
+ pendingLogs = [];
16
+ get DevLoadingView() {
17
+ return require("./DevLoadingView").default;
18
+ }
19
+ enable() {
20
+ if (this.unavailableMessage) {
21
+ throw new Error(this.unavailableMessage);
22
+ }
23
+ if (this._socketHolder == null) {
24
+ throw new Error(HMRClient.STARTUP_ERROR);
25
+ }
26
+ this.enabled = true;
27
+ this.showCompileErrorIfNeeded();
28
+ }
29
+ disable() {
30
+ this.enabled = false;
31
+ }
32
+ registerBundle(requestUrl) {
33
+ if (this._socketHolder == null) {
34
+ throw new Error(HMRClient.STARTUP_ERROR);
35
+ }
36
+ if (!requestUrl.startsWith(this._socketHolder.origin)) {
37
+ console.warn(`[HMR]: Cannot register bundle from unknown origin:\n${requestUrl}\n` + `(expected: ${this._socketHolder.origin})`);
38
+ return;
39
+ }
40
+ }
41
+ log(level, data) {
42
+ if (this._socketHolder == null) {
43
+ this.pendingLogs.push([level, data]);
44
+ if (this.pendingLogs.length > HMRClient.MAX_PENDING_LOGS) {
45
+ this.pendingLogs.shift();
46
+ }
47
+ return;
48
+ }
49
+ try {
50
+ const prettifyData = data.map((item) => typeof item === "string" ? item : prettyFormat.format(item, {
51
+ escapeString: true,
52
+ highlight: true,
53
+ maxDepth: 3,
54
+ min: true,
55
+ plugins: [prettyFormat.plugins.ReactElement]
56
+ }));
57
+ this.send({
58
+ type: "hmr:log",
59
+ level,
60
+ data: prettifyData
61
+ });
62
+ } catch {}
63
+ }
64
+ setup(platform, bundleEntry, host, port, isEnabled = true, protocol = "http") {
65
+ if (!__DEV__) {
66
+ throw new Error("HMR is only available in development mode");
67
+ }
68
+ if (this._socketHolder != null) {
69
+ throw new Error("Cannot initialize HMRClient more than once");
70
+ }
71
+ if (platform == null) {
72
+ throw new Error("Missing required parameter `platform`");
73
+ }
74
+ if (bundleEntry == null) {
75
+ throw new Error("Missing required parameter `bundleEntry`");
76
+ }
77
+ if (host == null) {
78
+ throw new Error("Missing required parameter `host`");
79
+ }
80
+ const serverHost = port !== null && port !== "" ? `${host}:${port}` : host;
81
+ const origin = `${protocol}://${serverHost}`;
82
+ const socket = new __ROLLIPOP_GLOBAL__.WebSocket(`${origin}/hot`);
83
+ this._socketHolder = {
84
+ socket,
85
+ origin
86
+ };
87
+ socket.addEventListener("open", () => {
88
+ socket.send(JSON.stringify({
89
+ type: "hmr:connected",
90
+ bundleEntry,
91
+ platform
92
+ }));
93
+ this.handleConnection();
94
+ });
95
+ socket.addEventListener("error", (event) => {
96
+ this.handleConnectionError(event.error, origin);
97
+ });
98
+ socket.addEventListener("message", (event) => {
99
+ this.handleMessage(event);
100
+ });
101
+ socket.addEventListener("close", (event) => {
102
+ this.handleClose(event);
103
+ });
104
+ __ROLLIPOP_GLOBAL__.__rolldown_runtime__.setup(socket);
105
+ this.enabled = isEnabled;
106
+ }
107
+ send(payload) {
108
+ if (this._socketHolder == null) {
109
+ return;
110
+ }
111
+ if (this._socketHolder.socket.readyState === WebSocket.OPEN) {
112
+ this._socketHolder.socket.send(JSON.stringify(payload));
113
+ }
114
+ }
115
+ flushEarlyLogs() {
116
+ if (this._socketHolder == null || this._socketHolder.socket.readyState !== WebSocket.OPEN) {
117
+ return;
118
+ }
119
+ for (const [level, data] of this.pendingLogs) {
120
+ this.send({
121
+ type: "hmr:log",
122
+ level,
123
+ data
124
+ });
125
+ }
126
+ this.pendingLogs.length = 0;
127
+ }
128
+ dismissRedbox() {
129
+ if (Platform.OS === "ios" && NativeRedBox != null && NativeRedBox.dismiss != null) {
130
+ NativeRedBox.dismiss();
131
+ } else {
132
+ const NativeExceptionsManager = require("../Core/NativeExceptionsManager").default;
133
+ if (NativeExceptionsManager != null && NativeExceptionsManager.dismissRedbox) {
134
+ NativeExceptionsManager.dismissRedbox();
135
+ }
136
+ }
137
+ }
138
+ showCompileErrorIfNeeded() {
139
+ if (this.compileErrorMessage == null) {
140
+ return;
141
+ }
142
+ this.dismissRedbox();
143
+ const error = new Error(this.compileErrorMessage);
144
+ this.compileErrorMessage = null;
145
+ Object.defineProperty(error, "preventSymbolication", { value: true });
146
+ throw error;
147
+ }
148
+ showUnavailableMessageIfNeeded() {
149
+ if (this.unavailableMessage == null) {
150
+ return;
151
+ }
152
+ this.DevLoadingView.hide();
153
+ if (this.enabled) {
154
+ this.DevLoadingView.showMessage("Fast Refresh disconnected. Reload app to reconnect.", "error", { dismissButton: true });
155
+ console.warn(this.unavailableMessage);
156
+ }
157
+ }
158
+ handleConnection() {
159
+ this.DevLoadingView.hide();
160
+ this.flushEarlyLogs();
161
+ }
162
+ handleConnectionError(error, origin) {
163
+ let errorMessage = "Cannot connect to Metro.\n\n" + "Try the following to fix the issue:\n" + "- Ensure that Metro is running and available on the same network";
164
+ if (Platform.OS === "ios") {
165
+ errorMessage += "- Ensure that the Metro URL is correctly set in AppDelegate";
166
+ } else {
167
+ errorMessage += `- Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices\n` + `- If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device\n` + `- If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:8081`;
168
+ }
169
+ errorMessage += `\n\nURL: ${origin}` + `\n\nError: ${error.message}`;
170
+ this.unavailableMessage ??= errorMessage;
171
+ this.showCompileErrorIfNeeded();
172
+ }
173
+ handleMessage(message) {
174
+ const data = JSON.parse(String(message.data));
175
+ if (!this.enabled && data.type.startsWith("hmr:")) {
176
+ return;
177
+ }
178
+ switch (data.type) {
179
+ case "hmr:update-start":
180
+ this.pendingUpdatesCount++;
181
+ this.compileErrorMessage = null;
182
+ this.DevLoadingView.showMessage("Refreshing...", "refresh");
183
+ break;
184
+ case "hmr:update":
185
+ this.dismissRedbox();
186
+ LogBox.clearAllLogs();
187
+ break;
188
+ case "hmr:update-done":
189
+ this.pendingUpdatesCount = Math.max(0, this.pendingUpdatesCount - 1);
190
+ if (this.pendingUpdatesCount === 0) {
191
+ this.DevLoadingView.hide();
192
+ }
193
+ break;
194
+ case "hmr:error":
195
+ this.compileErrorMessage = data.payload.message;
196
+ this.showCompileErrorIfNeeded();
197
+ break;
198
+ }
199
+ }
200
+ handleClose(event) {
201
+ const { code, reason } = event;
202
+ const isNormalOrUnsetCloseReason = code === 1e3 || code === 1005;
203
+ const message = isNormalOrUnsetCloseReason ? "Disconnected from Metro." : `Disconnected from Metro (${code}: "${reason}").`;
204
+ this.unavailableMessage ??= message + "\n\n" + "To reconnect:\n" + "- Ensure that Metro is running and available on the same network\n" + "- Reload this app (will trigger further help if Metro cannot be connected to)\n";
205
+ this.showUnavailableMessageIfNeeded();
206
+ }
207
+ };
208
+ const instance = new HMRClient();
209
+ var hmr_client_default = Object.defineProperty(instance, "default", { get: () => instance });
210
+
211
+ //#endregion
212
+ module.exports = hmr_client_default;
@@ -0,0 +1,158 @@
1
+ //#region src/runtime/react-refresh-utils.ts
2
+ function isReactRefreshBoundary(moduleExports) {
3
+ if (__ROLLIPOP_GLOBAL__.__ReactRefresh.isLikelyComponentType(moduleExports)) {
4
+ return true;
5
+ }
6
+ if (moduleExports === undefined || moduleExports === null || typeof moduleExports !== "object") {
7
+ return false;
8
+ }
9
+ var hasExports = false;
10
+ var areAllExportsComponents = true;
11
+ for (var key in moduleExports) {
12
+ hasExports = true;
13
+ if (key === "__esModule") {
14
+ continue;
15
+ }
16
+ var exportValue = moduleExports[key];
17
+ if (!__ROLLIPOP_GLOBAL__.__ReactRefresh.isLikelyComponentType(exportValue)) {
18
+ areAllExportsComponents = false;
19
+ }
20
+ }
21
+ return hasExports && areAllExportsComponents;
22
+ }
23
+ let timer = null;
24
+ function enqueueUpdate() {
25
+ if (timer) {
26
+ return;
27
+ }
28
+ timer = setTimeout(() => {
29
+ __ROLLIPOP_GLOBAL__.__ReactRefresh.performReactRefresh();
30
+ timer = null;
31
+ }, 50);
32
+ }
33
+
34
+ //#endregion
35
+ //#region src/runtime/hmr-runtime.ts
36
+ var BaseDevRuntime = DevRuntime;
37
+ var ModuleHotContext = class {
38
+ acceptCallbacks = [];
39
+ constructor(moduleId, devRuntime) {
40
+ this.moduleId = moduleId;
41
+ this.devRuntime = devRuntime;
42
+ }
43
+ get refresh() {
44
+ return __ROLLIPOP_GLOBAL__.__ReactRefresh;
45
+ }
46
+ get refreshUtils() {
47
+ return {
48
+ isReactRefreshBoundary,
49
+ enqueueUpdate
50
+ };
51
+ }
52
+ accept(...args) {
53
+ if (args.length === 1) {
54
+ const [cb] = args;
55
+ const acceptingPath = this.moduleId;
56
+ this.acceptCallbacks.push({
57
+ deps: [acceptingPath],
58
+ fn: cb
59
+ });
60
+ } else if (args.length === 0) {} else {
61
+ throw new Error("Invalid arguments for `import.meta.hot.accept`");
62
+ }
63
+ }
64
+ invalidate() {
65
+ if (ReactNativeDevRuntime.socket == null || ReactNativeDevRuntime.socket.readyState !== WebSocket.OPEN) {
66
+ return;
67
+ }
68
+ ReactNativeDevRuntime.socket.send(JSON.stringify({
69
+ type: "hmr:invalidate",
70
+ moduleId: this.moduleId
71
+ }));
72
+ }
73
+ };
74
+ var ReactNativeDevRuntime = class ReactNativeDevRuntime extends BaseDevRuntime {
75
+ static socket = null;
76
+ static queuedMessages = [];
77
+ moduleHotContexts = new Map();
78
+ moduleHotContextsToBeUpdated = new Map();
79
+ constructor() {
80
+ const messenger = { send(message) {
81
+ if (ReactNativeDevRuntime.socket === null || ReactNativeDevRuntime.socket.readyState !== WebSocket.OPEN) {
82
+ ReactNativeDevRuntime.queuedMessages.push(JSON.stringify(message));
83
+ return;
84
+ }
85
+ ReactNativeDevRuntime.socket.send(JSON.stringify(message));
86
+ } };
87
+ super(messenger);
88
+ }
89
+ createModuleHotContext(moduleId) {
90
+ const hotContext = new ModuleHotContext(moduleId, this);
91
+ if (this.moduleHotContexts.has(moduleId)) {
92
+ this.moduleHotContextsToBeUpdated.set(moduleId, hotContext);
93
+ } else {
94
+ this.moduleHotContexts.set(moduleId, hotContext);
95
+ }
96
+ return hotContext;
97
+ }
98
+ applyUpdates(boundaries) {
99
+ for (let [moduleId, _acceptedVia] of boundaries) {
100
+ const hotContext = this.moduleHotContexts.get(moduleId);
101
+ if (hotContext) {
102
+ const acceptCallbacks = hotContext.acceptCallbacks;
103
+ acceptCallbacks.filter((cb) => {
104
+ cb.fn(this.modules[moduleId].exports);
105
+ });
106
+ }
107
+ }
108
+ this.moduleHotContextsToBeUpdated.forEach((hotContext, moduleId) => {
109
+ this.moduleHotContexts.set(moduleId, hotContext);
110
+ });
111
+ this.moduleHotContextsToBeUpdated.clear();
112
+ }
113
+ setup(socket) {
114
+ if (ReactNativeDevRuntime.socket != null) {
115
+ console.warn("[HMR]: ReactNativeDevRuntime already setup");
116
+ return;
117
+ }
118
+ if (socket.readyState !== WebSocket.OPEN) {
119
+ socket.addEventListener("open", () => {
120
+ this.flushQueuedMessages(socket);
121
+ });
122
+ } else {
123
+ this.flushQueuedMessages(socket);
124
+ }
125
+ socket.addEventListener("message", (event) => {
126
+ const message = JSON.parse(event.data);
127
+ switch (message.type) {
128
+ case "hmr:update":
129
+ this.evaluate(message.code);
130
+ break;
131
+ case "hmr:reload":
132
+ this.reload();
133
+ break;
134
+ }
135
+ });
136
+ ReactNativeDevRuntime.socket = socket;
137
+ }
138
+ flushQueuedMessages(socket) {
139
+ for (const message of ReactNativeDevRuntime.queuedMessages) {
140
+ socket.send(message);
141
+ }
142
+ ReactNativeDevRuntime.queuedMessages.length = 0;
143
+ }
144
+ evaluate(code, sourceURL) {
145
+ if (__ROLLIPOP_GLOBAL__.globalEvalWithSourceUrl) {
146
+ __ROLLIPOP_GLOBAL__.globalEvalWithSourceUrl(code, sourceURL);
147
+ } else {
148
+ eval(code);
149
+ }
150
+ }
151
+ reload() {
152
+ const moduleName = "DevSettings";
153
+ (__ROLLIPOP_GLOBAL__.__turboModuleProxy ? __ROLLIPOP_GLOBAL__.__turboModuleProxy(moduleName) : __ROLLIPOP_GLOBAL__.nativeModuleProxy[moduleName]).reload();
154
+ }
155
+ };
156
+ __ROLLIPOP_GLOBAL__.__rolldown_runtime__ = new ReactNativeDevRuntime();
157
+
158
+ //#endregion
@@ -0,0 +1,10 @@
1
+ //#region src/runtime/hmr-shims.ts
2
+ /**
3
+ * For CJS modules without export statements, an error occurs when the HMR boundary references a non-existent module identifier (appears to be a rolldown bug).
4
+ * I've added an empty module object as a temporary workaround for the reference issue.
5
+ *
6
+ * @see https://github.com/rolldown/rolldown/pull/7544
7
+ */
8
+ var module = { exports: {} };
9
+
10
+ //#endregion