@useavalon/avalon 0.1.9 → 0.1.11
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/package.json +5 -1
- package/src/client/adapters/index.js +12 -0
- package/src/client/adapters/lit-adapter.js +467 -0
- package/src/client/adapters/preact-adapter.js +223 -0
- package/src/client/adapters/qwik-adapter.js +259 -0
- package/src/client/adapters/react-adapter.js +220 -0
- package/src/client/adapters/solid-adapter.js +295 -0
- package/src/client/adapters/svelte-adapter.js +368 -0
- package/src/client/adapters/vue-adapter.js +278 -0
- package/src/client/components.js +23 -0
- package/src/client/css-hmr-handler.js +263 -0
- package/src/client/framework-adapter.js +283 -0
- package/src/client/hmr-coordinator.js +274 -0
- package/src/client/main.js +8 -8
- package/src/vite-plugin/plugin.ts +25 -15
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solid HMR Adapter
|
|
3
|
+
*
|
|
4
|
+
* Provides Hot Module Replacement support for Solid components using Solid Refresh.
|
|
5
|
+
* Integrates with vite-plugin-solid to preserve signal subscriptions and reactive computations.
|
|
6
|
+
* Handles Solid's fine-grained reactivity system during hot updates.
|
|
7
|
+
*
|
|
8
|
+
* Requirements: 2.5
|
|
9
|
+
*/
|
|
10
|
+
/// <reference lib="dom" />
|
|
11
|
+
import { BaseFrameworkAdapter } from "../framework-adapter.js";
|
|
12
|
+
/**
|
|
13
|
+
* Solid HMR Adapter
|
|
14
|
+
*
|
|
15
|
+
* Leverages Solid Refresh (provided by vite-plugin-solid) to:
|
|
16
|
+
* - Preserve signal subscriptions across updates
|
|
17
|
+
* - Maintain reactive computations
|
|
18
|
+
* - Handle component updates without full remount
|
|
19
|
+
*
|
|
20
|
+
* Solid Refresh works through the __SOLID_HMR__ API:
|
|
21
|
+
* 1. When a component module is updated, Vite sends an HMR event
|
|
22
|
+
* 2. We use __SOLID_HMR__.reload() to hot-reload the component
|
|
23
|
+
* 3. Solid Refresh preserves signal subscriptions automatically
|
|
24
|
+
* 4. Reactive computations are maintained across updates
|
|
25
|
+
* 5. The component re-renders with preserved reactive state
|
|
26
|
+
*/
|
|
27
|
+
export class SolidHMRAdapter extends BaseFrameworkAdapter {
|
|
28
|
+
name = "solid";
|
|
29
|
+
/**
|
|
30
|
+
* Store Solid dispose functions for each island to enable proper cleanup
|
|
31
|
+
*/
|
|
32
|
+
disposers = new WeakMap();
|
|
33
|
+
/**
|
|
34
|
+
* Store component IDs for HMR runtime
|
|
35
|
+
*/
|
|
36
|
+
componentIds = new WeakMap();
|
|
37
|
+
/**
|
|
38
|
+
* Check if a component is a Solid component
|
|
39
|
+
*
|
|
40
|
+
* Solid components are:
|
|
41
|
+
* - Functions that return JSX
|
|
42
|
+
* - May use createSignal, createEffect, etc.
|
|
43
|
+
* - Typically have .solid.tsx extension (but not always)
|
|
44
|
+
*/
|
|
45
|
+
canHandle(component) {
|
|
46
|
+
if (!component) return false;
|
|
47
|
+
// Check if it's a function (Solid components are functions)
|
|
48
|
+
if (typeof component === "function") {
|
|
49
|
+
const comp = component;
|
|
50
|
+
// Check for Solid-specific markers
|
|
51
|
+
// Solid components may have __solid marker from vite-plugin-solid
|
|
52
|
+
if (comp.__solid) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
// Check function signature - Solid components typically accept props
|
|
56
|
+
// and return JSX (which is compiled to function calls)
|
|
57
|
+
try {
|
|
58
|
+
const funcStr = component.toString();
|
|
59
|
+
// Look for Solid-specific patterns
|
|
60
|
+
// - createSignal, createEffect, createMemo, etc.
|
|
61
|
+
// - JSX patterns (though this is less reliable after compilation)
|
|
62
|
+
if (funcStr.includes("createSignal") || funcStr.includes("createEffect") || funcStr.includes("createMemo") || funcStr.includes("createResource") || funcStr.includes("createStore") || funcStr.includes("_$") || funcStr.includes("_tmpl$")) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
} catch {}
|
|
66
|
+
// If we can't determine definitively, assume it could be a Solid component
|
|
67
|
+
// if it's a function (Solid components are just functions)
|
|
68
|
+
// This is a fallback - we'll let Solid's hydration handle validation
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
// Check if it's a Solid component object (wrapped or exported)
|
|
72
|
+
if (typeof component !== "object") {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const obj = component;
|
|
76
|
+
// Check for default export pattern
|
|
77
|
+
if (obj.default && typeof obj.default === "function") {
|
|
78
|
+
return this.canHandle(obj.default);
|
|
79
|
+
}
|
|
80
|
+
// Check for Solid component markers
|
|
81
|
+
if (obj.__solid) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Preserve Solid component state before HMR update
|
|
88
|
+
*
|
|
89
|
+
* Solid Refresh handles most state preservation automatically through
|
|
90
|
+
* its fine-grained reactivity system. We capture additional DOM state
|
|
91
|
+
* and props for fallback.
|
|
92
|
+
*
|
|
93
|
+
* Note: Solid's signals are not easily accessible from outside the component,
|
|
94
|
+
* so we rely on Solid Refresh to preserve them.
|
|
95
|
+
*/
|
|
96
|
+
preserveState(island) {
|
|
97
|
+
try {
|
|
98
|
+
// Get base DOM state
|
|
99
|
+
const baseSnapshot = super.preserveState(island);
|
|
100
|
+
if (!baseSnapshot) return null;
|
|
101
|
+
// Get Solid-specific data
|
|
102
|
+
const propsAttr = island.getAttribute("data-props");
|
|
103
|
+
const capturedProps = propsAttr ? JSON.parse(propsAttr) : {};
|
|
104
|
+
// Try to get component name from the island
|
|
105
|
+
const src = island.getAttribute("data-src") || "";
|
|
106
|
+
const componentName = this.extractComponentName(src);
|
|
107
|
+
// Get render ID for hydration
|
|
108
|
+
const renderId = island.dataset.solidRenderId || island.dataset.renderId;
|
|
109
|
+
const solidSnapshot = {
|
|
110
|
+
...baseSnapshot,
|
|
111
|
+
framework: "solid",
|
|
112
|
+
data: {
|
|
113
|
+
componentName,
|
|
114
|
+
capturedProps,
|
|
115
|
+
renderId
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return solidSnapshot;
|
|
119
|
+
} catch (error) {
|
|
120
|
+
console.warn("Failed to preserve Solid state:", error);
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Update Solid component with HMR
|
|
126
|
+
*
|
|
127
|
+
* This method integrates with Solid Refresh:
|
|
128
|
+
* 1. Solid Refresh is automatically enabled by vite-plugin-solid
|
|
129
|
+
* 2. When a module updates, Vite sends an HMR event
|
|
130
|
+
* 3. We use __SOLID_HMR__ to reload the component
|
|
131
|
+
* 4. Solid Refresh preserves signal subscriptions automatically
|
|
132
|
+
* 5. Reactive computations are maintained
|
|
133
|
+
* 6. The component re-renders with preserved reactive state
|
|
134
|
+
*/
|
|
135
|
+
async update(island, newComponent, props) {
|
|
136
|
+
if (!this.canHandle(newComponent)) {
|
|
137
|
+
throw new Error("Component is not a valid Solid component");
|
|
138
|
+
}
|
|
139
|
+
// Extract the actual component function
|
|
140
|
+
let Component;
|
|
141
|
+
if (typeof newComponent === "object" && newComponent !== null) {
|
|
142
|
+
const obj = newComponent;
|
|
143
|
+
if (obj.default && typeof obj.default === "function") {
|
|
144
|
+
Component = obj.default;
|
|
145
|
+
} else {
|
|
146
|
+
throw new Error("Solid component object must have a default export");
|
|
147
|
+
}
|
|
148
|
+
} else if (typeof newComponent === "function") {
|
|
149
|
+
Component = newComponent;
|
|
150
|
+
} else {
|
|
151
|
+
throw new Error("Invalid Solid component type");
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
// Check if we have an existing disposer
|
|
155
|
+
const existingDisposer = this.disposers.get(island);
|
|
156
|
+
const componentId = this.componentIds.get(island);
|
|
157
|
+
// Try to use Solid HMR runtime if available
|
|
158
|
+
const hmrRuntime = globalThis.__SOLID_HMR__;
|
|
159
|
+
if (hmrRuntime && componentId) {
|
|
160
|
+
// Use Solid's HMR runtime for hot reload
|
|
161
|
+
// This preserves signal subscriptions and reactive computations automatically
|
|
162
|
+
try {
|
|
163
|
+
hmrRuntime.reload(componentId, Component);
|
|
164
|
+
// If we have an existing component, Solid Refresh handles the update
|
|
165
|
+
// We don't need to do anything else
|
|
166
|
+
if (existingDisposer) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {
|
|
170
|
+
console.warn("Solid HMR runtime reload failed, falling back to full remount:", error);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// Clean up existing component if present
|
|
174
|
+
if (existingDisposer) {
|
|
175
|
+
try {
|
|
176
|
+
existingDisposer();
|
|
177
|
+
this.disposers.delete(island);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.warn("Failed to dispose existing Solid component:", error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Dynamically import Solid at runtime
|
|
183
|
+
// This is resolved by Vite in the browser
|
|
184
|
+
const solidWebModule = await import("solid-js/web");
|
|
185
|
+
const { hydrate, createComponent } = solidWebModule;
|
|
186
|
+
// Get render ID for hydration
|
|
187
|
+
const renderId = island.dataset.solidRenderId || island.dataset.renderId;
|
|
188
|
+
// Check if we have SSR content to hydrate
|
|
189
|
+
// Hydrate or render the component
|
|
190
|
+
const dispose = hydrate(() => createComponent(Component, props), island, { renderId });
|
|
191
|
+
// Store disposer for future updates
|
|
192
|
+
this.disposers.set(island, dispose);
|
|
193
|
+
// Generate component ID for HMR runtime
|
|
194
|
+
const src = island.getAttribute("data-src") || "";
|
|
195
|
+
const newComponentId = this.generateComponentId(src);
|
|
196
|
+
this.componentIds.set(island, newComponentId);
|
|
197
|
+
// Register with HMR runtime if available
|
|
198
|
+
if (hmrRuntime) {
|
|
199
|
+
try {
|
|
200
|
+
hmrRuntime.createRecord(newComponentId, Component);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
console.warn("Failed to register with Solid HMR runtime:", error);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Mark as hydrated
|
|
206
|
+
island.setAttribute("data-hydrated", "true");
|
|
207
|
+
island.setAttribute("data-hydration-status", "success");
|
|
208
|
+
} catch (error) {
|
|
209
|
+
console.error("Solid HMR update failed:", error);
|
|
210
|
+
island.setAttribute("data-hydration-status", "error");
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Restore Solid component state after HMR update
|
|
216
|
+
*
|
|
217
|
+
* Solid Refresh handles most state restoration automatically through
|
|
218
|
+
* its fine-grained reactivity system. We restore DOM state (scroll, focus,
|
|
219
|
+
* form values) as a supplement.
|
|
220
|
+
*/
|
|
221
|
+
restoreState(island, state) {
|
|
222
|
+
try {
|
|
223
|
+
// Restore DOM state (scroll, focus, form values)
|
|
224
|
+
super.restoreState(island, state);
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.warn("Failed to restore Solid state:", error);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Handle errors during Solid HMR update
|
|
231
|
+
*
|
|
232
|
+
* Provides Solid-specific error handling with helpful messages
|
|
233
|
+
*/
|
|
234
|
+
handleError(island, error) {
|
|
235
|
+
console.error("Solid HMR error:", error);
|
|
236
|
+
// Use base error handling
|
|
237
|
+
super.handleError(island, error);
|
|
238
|
+
// Add Solid-specific error information
|
|
239
|
+
const errorIndicator = island.querySelector(".hmr-error-indicator");
|
|
240
|
+
if (errorIndicator) {
|
|
241
|
+
const errorMessage = error.message;
|
|
242
|
+
// Provide helpful hints for common Solid errors
|
|
243
|
+
let hint = "";
|
|
244
|
+
if (errorMessage.includes("signal") || errorMessage.includes("Signal")) {
|
|
245
|
+
hint = " (Hint: Check signal usage - signals must be called as functions)";
|
|
246
|
+
} else if (errorMessage.includes("effect") || errorMessage.includes("Effect")) {
|
|
247
|
+
hint = " (Hint: Check effect usage - effects run after render)";
|
|
248
|
+
} else if (errorMessage.includes("hydration") || errorMessage.includes("hydrate")) {
|
|
249
|
+
hint = " (Hint: Server and client render must match)";
|
|
250
|
+
} else if (errorMessage.includes("createSignal") || errorMessage.includes("createEffect")) {
|
|
251
|
+
hint = " (Hint: Solid primitives must be called inside component functions)";
|
|
252
|
+
} else if (errorMessage.includes("reactive")) {
|
|
253
|
+
hint = " (Hint: Check reactive dependencies - they must be accessed inside tracking scopes)";
|
|
254
|
+
}
|
|
255
|
+
errorIndicator.textContent = `Solid HMR Error: ${errorMessage}${hint}`;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Extract component name from source path
|
|
260
|
+
* Used for debugging and error messages
|
|
261
|
+
*/
|
|
262
|
+
extractComponentName(src) {
|
|
263
|
+
const parts = src.split("/");
|
|
264
|
+
const filename = parts[parts.length - 1];
|
|
265
|
+
return filename.replace(/\.solid\.(tsx?|jsx?)$/, "").replace(/\.(tsx?|jsx?)$/, "");
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Generate a unique component ID for HMR runtime
|
|
269
|
+
*/
|
|
270
|
+
generateComponentId(src) {
|
|
271
|
+
// Use the source path as the component ID
|
|
272
|
+
// This ensures consistency across HMR updates
|
|
273
|
+
return src.replace(/[^a-zA-Z0-9]/g, "_");
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Clean up Solid component when island is removed
|
|
277
|
+
* This should be called when an island is unmounted
|
|
278
|
+
*/
|
|
279
|
+
unmount(island) {
|
|
280
|
+
const disposer = this.disposers.get(island);
|
|
281
|
+
if (disposer) {
|
|
282
|
+
try {
|
|
283
|
+
disposer();
|
|
284
|
+
this.disposers.delete(island);
|
|
285
|
+
this.componentIds.delete(island);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
console.warn("Failed to unmount Solid component:", error);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Create and export a singleton instance of the Solid HMR adapter
|
|
294
|
+
*/
|
|
295
|
+
export const solidAdapter = new SolidHMRAdapter();
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte HMR Adapter
|
|
3
|
+
*
|
|
4
|
+
* Provides Hot Module Replacement support for Svelte 5 components.
|
|
5
|
+
* Integrates with @sveltejs/vite-plugin-svelte for HMR updates.
|
|
6
|
+
*
|
|
7
|
+
* IMPORTANT: Svelte 5 HMR Behavior
|
|
8
|
+
* - HMR is controlled via compilerOptions.hmr in the Vite plugin config
|
|
9
|
+
* - Local state is NOT preserved during HMR (by design in Svelte 5)
|
|
10
|
+
* - CSS-only changes DO preserve state (100% preserved)
|
|
11
|
+
* - Store subscriptions are maintained across updates
|
|
12
|
+
* - The component is remounted with fresh state on JS changes
|
|
13
|
+
*
|
|
14
|
+
* Requirements: 2.4
|
|
15
|
+
*/
|
|
16
|
+
/// <reference lib="dom" />
|
|
17
|
+
import { BaseFrameworkAdapter } from "../framework-adapter.js";
|
|
18
|
+
/**
|
|
19
|
+
* Svelte HMR Adapter
|
|
20
|
+
*
|
|
21
|
+
* Handles HMR for Svelte 5 components in the Avalon islands architecture.
|
|
22
|
+
*
|
|
23
|
+
* Svelte 5 HMR Behavior:
|
|
24
|
+
* - HMR is controlled via compilerOptions.hmr in the Vite plugin config
|
|
25
|
+
* - Local state is NOT preserved during HMR (by design in Svelte 5)
|
|
26
|
+
* - CSS-only changes DO preserve state (100% preserved)
|
|
27
|
+
* - Store subscriptions are maintained across updates
|
|
28
|
+
* - The component is remounted with fresh state on JS changes
|
|
29
|
+
*
|
|
30
|
+
* How it works:
|
|
31
|
+
* 1. When a component module is updated, Vite sends an HMR event
|
|
32
|
+
* 2. Svelte's compiler-integrated HMR handles the update
|
|
33
|
+
* 3. We clean up the old instance and mount the new component
|
|
34
|
+
* 4. DOM state (scroll, focus, form values) is preserved where possible
|
|
35
|
+
* 5. The component re-renders with fresh state
|
|
36
|
+
*/
|
|
37
|
+
export class SvelteHMRAdapter extends BaseFrameworkAdapter {
|
|
38
|
+
name = "svelte";
|
|
39
|
+
/**
|
|
40
|
+
* Store Svelte component instances for each island to enable proper cleanup
|
|
41
|
+
*/
|
|
42
|
+
instances = new WeakMap();
|
|
43
|
+
/**
|
|
44
|
+
* Store component IDs for tracking
|
|
45
|
+
*/
|
|
46
|
+
componentIds = new WeakMap();
|
|
47
|
+
/**
|
|
48
|
+
* Store active store subscriptions for cleanup
|
|
49
|
+
*/
|
|
50
|
+
storeSubscriptions = new WeakMap();
|
|
51
|
+
/**
|
|
52
|
+
* Check if a function/class has Svelte component markers
|
|
53
|
+
*/
|
|
54
|
+
isSvelteFunction(component) {
|
|
55
|
+
const comp = component;
|
|
56
|
+
if (comp.$$render) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
const proto = component.prototype;
|
|
60
|
+
if (proto && (proto.$set && proto.$destroy || proto.$$)) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const funcStr = component.toString();
|
|
65
|
+
if (funcStr.includes("$set") || funcStr.includes("$destroy") || funcStr.includes("$$")) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
} catch {}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if a component is a Svelte component
|
|
73
|
+
*
|
|
74
|
+
* Svelte components are compiled to classes with specific markers:
|
|
75
|
+
* - Constructor function
|
|
76
|
+
* - $$render method (SSR marker)
|
|
77
|
+
* - Prototype with $set, $destroy methods
|
|
78
|
+
*/
|
|
79
|
+
canHandle(component) {
|
|
80
|
+
if (!component) return false;
|
|
81
|
+
if (typeof component === "function") {
|
|
82
|
+
return this.isSvelteFunction(component);
|
|
83
|
+
}
|
|
84
|
+
if (typeof component !== "object") {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
const obj = component;
|
|
88
|
+
if (obj.default && typeof obj.default === "function") {
|
|
89
|
+
return this.canHandle(obj.default);
|
|
90
|
+
}
|
|
91
|
+
return obj.$$render !== undefined;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Preserve Svelte component state before HMR update
|
|
95
|
+
*
|
|
96
|
+
* Note: In Svelte 5, local state is NOT preserved during HMR (by design).
|
|
97
|
+
* We capture DOM state (scroll, focus, form values) which CAN be restored.
|
|
98
|
+
*/
|
|
99
|
+
preserveState(island) {
|
|
100
|
+
try {
|
|
101
|
+
// Get base DOM state
|
|
102
|
+
const baseSnapshot = super.preserveState(island);
|
|
103
|
+
if (!baseSnapshot) return null;
|
|
104
|
+
// Get Svelte-specific data
|
|
105
|
+
const propsAttr = island.dataset.props;
|
|
106
|
+
const capturedProps = propsAttr ? JSON.parse(propsAttr) : {};
|
|
107
|
+
// Try to get component name from the island
|
|
108
|
+
const src = island.dataset.src || "";
|
|
109
|
+
const componentName = this.extractComponentName(src);
|
|
110
|
+
// Note: In Svelte 5, local state is not preserved during HMR
|
|
111
|
+
// We still capture it for debugging purposes, but it won't be restored
|
|
112
|
+
const localState = this.captureLocalState(island);
|
|
113
|
+
// Store values are maintained by Svelte's reactivity system
|
|
114
|
+
const storeValues = this.captureStoreValues(island);
|
|
115
|
+
const svelteSnapshot = {
|
|
116
|
+
...baseSnapshot,
|
|
117
|
+
framework: "svelte",
|
|
118
|
+
data: {
|
|
119
|
+
componentName,
|
|
120
|
+
capturedProps,
|
|
121
|
+
localState,
|
|
122
|
+
storeValues
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
return svelteSnapshot;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.warn("Failed to preserve Svelte state:", error);
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Extract the Svelte component class from a module or function
|
|
133
|
+
*/
|
|
134
|
+
extractComponent(newComponent) {
|
|
135
|
+
if (typeof newComponent === "object" && newComponent !== null) {
|
|
136
|
+
const obj = newComponent;
|
|
137
|
+
if (obj.default && typeof obj.default === "function") {
|
|
138
|
+
return obj.default;
|
|
139
|
+
}
|
|
140
|
+
throw new Error("Svelte component object must have a default export");
|
|
141
|
+
}
|
|
142
|
+
if (typeof newComponent === "function") {
|
|
143
|
+
return newComponent;
|
|
144
|
+
}
|
|
145
|
+
throw new TypeError("Invalid Svelte component type");
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Clean up an existing Svelte component instance
|
|
149
|
+
*/
|
|
150
|
+
async cleanupInstance(island, instance) {
|
|
151
|
+
try {
|
|
152
|
+
const subscriptions = this.storeSubscriptions.get(island);
|
|
153
|
+
if (subscriptions) {
|
|
154
|
+
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
155
|
+
this.storeSubscriptions.delete(island);
|
|
156
|
+
}
|
|
157
|
+
const svelteModule = await import("svelte");
|
|
158
|
+
const svelteUnmount = svelteModule.unmount;
|
|
159
|
+
if (svelteUnmount) {
|
|
160
|
+
svelteUnmount(instance);
|
|
161
|
+
} else if (instance.$destroy) {
|
|
162
|
+
instance.$destroy();
|
|
163
|
+
}
|
|
164
|
+
} catch {
|
|
165
|
+
if (instance.$destroy) {
|
|
166
|
+
instance.$destroy();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Mount a Svelte component, trying Svelte 5 API first then falling back to constructor
|
|
172
|
+
*/
|
|
173
|
+
async mountComponent(Component, island, props) {
|
|
174
|
+
try {
|
|
175
|
+
const svelteModule = await import("svelte");
|
|
176
|
+
const svelteMount = svelteModule.mount;
|
|
177
|
+
if (svelteMount) {
|
|
178
|
+
return svelteMount(Component, {
|
|
179
|
+
target: island,
|
|
180
|
+
props
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return new Component({
|
|
184
|
+
target: island,
|
|
185
|
+
props,
|
|
186
|
+
hydrate: false,
|
|
187
|
+
intro: false
|
|
188
|
+
});
|
|
189
|
+
} catch (svelte5Error) {
|
|
190
|
+
console.debug("Svelte 5 API not available, using constructor API:", svelte5Error);
|
|
191
|
+
return new Component({
|
|
192
|
+
target: island,
|
|
193
|
+
props,
|
|
194
|
+
hydrate: false,
|
|
195
|
+
intro: false
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Update Svelte component with HMR
|
|
201
|
+
*
|
|
202
|
+
* Svelte 5 HMR Behavior:
|
|
203
|
+
* - HMR is handled by the Svelte compiler via compilerOptions.hmr
|
|
204
|
+
* - Local state is NOT preserved (by design)
|
|
205
|
+
* - CSS-only changes preserve state 100%
|
|
206
|
+
* - We clean up the old instance and mount the new component
|
|
207
|
+
*/
|
|
208
|
+
async update(island, newComponent, props) {
|
|
209
|
+
if (!this.canHandle(newComponent)) {
|
|
210
|
+
throw new Error("Component is not a valid Svelte component");
|
|
211
|
+
}
|
|
212
|
+
const Component = this.extractComponent(newComponent);
|
|
213
|
+
try {
|
|
214
|
+
const existingInstance = this.instances.get(island);
|
|
215
|
+
if (existingInstance) {
|
|
216
|
+
await this.cleanupInstance(island, existingInstance).catch((error) => {
|
|
217
|
+
console.warn("Failed to destroy existing Svelte instance:", error);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
island.innerHTML = "";
|
|
221
|
+
const instance = await this.mountComponent(Component, island, props);
|
|
222
|
+
this.instances.set(island, instance);
|
|
223
|
+
const src = island.dataset.src || "";
|
|
224
|
+
this.componentIds.set(island, this.generateComponentId(src));
|
|
225
|
+
island.dataset.hydrated = "true";
|
|
226
|
+
island.dataset.hydrationStatus = "success";
|
|
227
|
+
} catch (error) {
|
|
228
|
+
console.error("Svelte HMR update failed:", error);
|
|
229
|
+
island.dataset.hydrationStatus = "error";
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Restore Svelte component state after HMR update
|
|
235
|
+
*
|
|
236
|
+
* Note: In Svelte 5, local state is NOT preserved during HMR (by design).
|
|
237
|
+
* We only restore DOM state (scroll, focus, form values).
|
|
238
|
+
*/
|
|
239
|
+
restoreState(island, state) {
|
|
240
|
+
try {
|
|
241
|
+
// Restore DOM state (scroll, focus, form values)
|
|
242
|
+
super.restoreState(island, state);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.warn("Failed to restore Svelte state:", error);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Handle errors during Svelte HMR update
|
|
249
|
+
*
|
|
250
|
+
* Provides Svelte-specific error handling with helpful messages
|
|
251
|
+
*/
|
|
252
|
+
handleError(island, error) {
|
|
253
|
+
console.error("Svelte HMR error:", error);
|
|
254
|
+
// Use base error handling
|
|
255
|
+
super.handleError(island, error);
|
|
256
|
+
// Add Svelte-specific error information
|
|
257
|
+
const errorIndicator = island.querySelector(".hmr-error-indicator");
|
|
258
|
+
if (errorIndicator) {
|
|
259
|
+
const errorMessage = error.message;
|
|
260
|
+
// Provide helpful hints for common Svelte errors
|
|
261
|
+
let hint = "";
|
|
262
|
+
if (errorMessage.includes("$:") || errorMessage.includes("reactive")) {
|
|
263
|
+
hint = " (Hint: Check reactive statements ($:) - they must be at component top level)";
|
|
264
|
+
} else if (errorMessage.includes("store")) {
|
|
265
|
+
hint = " (Hint: Check store usage - stores must be imported and subscribed correctly)";
|
|
266
|
+
} else if (errorMessage.includes("hydration") || errorMessage.includes("hydrate")) {
|
|
267
|
+
hint = " (Hint: Server and client render must match)";
|
|
268
|
+
} else if (errorMessage.includes("target")) {
|
|
269
|
+
hint = " (Hint: Check component target - it must be a valid DOM element)";
|
|
270
|
+
} else if (errorMessage.includes("props")) {
|
|
271
|
+
hint = " (Hint: Check component props - they must match the component definition)";
|
|
272
|
+
}
|
|
273
|
+
errorIndicator.textContent = `Svelte HMR Error: ${errorMessage}${hint}`;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Extract component name from source path
|
|
278
|
+
* Used for debugging and error messages
|
|
279
|
+
*/
|
|
280
|
+
extractComponentName(src) {
|
|
281
|
+
const parts = src.split("/");
|
|
282
|
+
const filename = parts.at(-1) ?? "";
|
|
283
|
+
return filename.replace(/\.svelte$/, "");
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Detect if an island has existing SSR content vs being an empty container
|
|
287
|
+
*
|
|
288
|
+
* @param island - Island element to check
|
|
289
|
+
* @returns True if island has SSR content
|
|
290
|
+
*/
|
|
291
|
+
detectSSRContent(island) {
|
|
292
|
+
// Check if element has any meaningful content
|
|
293
|
+
const hasTextContent = island.textContent && island.textContent.trim().length > 0;
|
|
294
|
+
const hasChildElements = island.children && island.children.length > 0;
|
|
295
|
+
const hasAttributes = island.dataset.ssrContent !== undefined || island.dataset.svelteRendered !== undefined;
|
|
296
|
+
// Consider it SSR content if it has text, child elements, or explicit markers
|
|
297
|
+
return hasTextContent || hasChildElements || hasAttributes;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Generate a unique component ID for HMR runtime
|
|
301
|
+
*/
|
|
302
|
+
generateComponentId(src) {
|
|
303
|
+
// Use the source path as the component ID
|
|
304
|
+
// This ensures consistency across HMR updates
|
|
305
|
+
return src.replaceAll(/[^a-zA-Z0-9]/g, "_");
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Attempt to capture local state from Svelte instance
|
|
309
|
+
* This is best-effort and may not work in all cases
|
|
310
|
+
*/
|
|
311
|
+
captureLocalState(island) {
|
|
312
|
+
try {
|
|
313
|
+
const instance = this.instances.get(island);
|
|
314
|
+
if (!instance) return undefined;
|
|
315
|
+
// Try to access Svelte's internal state
|
|
316
|
+
// This is internal API and may change, so we wrap in try-catch
|
|
317
|
+
const internalState = instance.$$;
|
|
318
|
+
if (internalState?.ctx) {
|
|
319
|
+
// ctx is an array of component state values
|
|
320
|
+
// We can't easily map these back to variable names,
|
|
321
|
+
// so we just store the raw values
|
|
322
|
+
return {
|
|
323
|
+
ctx: internalState.ctx,
|
|
324
|
+
props: internalState.props,
|
|
325
|
+
bound: internalState.bound
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
return undefined;
|
|
329
|
+
} catch (error) {
|
|
330
|
+
// Silently fail - this is best-effort
|
|
331
|
+
console.debug("Could not capture Svelte local state:", error);
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Attempt to capture store values
|
|
337
|
+
* This is best-effort and may not work in all cases
|
|
338
|
+
*/
|
|
339
|
+
captureStoreValues(_island) {
|
|
340
|
+
// Svelte stores are typically imported at module level,
|
|
341
|
+
// so we can't easily access them from the component instance
|
|
342
|
+
// The HMR system should handle store subscriptions automatically
|
|
343
|
+
// We could potentially track stores if we intercept store.subscribe calls,
|
|
344
|
+
// but that would require modifying the Svelte runtime, which is not feasible
|
|
345
|
+
// For now, we rely on Svelte's HMR to preserve store subscriptions
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Clean up Svelte component when island is removed
|
|
350
|
+
* This should be called when an island is unmounted
|
|
351
|
+
*/
|
|
352
|
+
async unmount(_island) {
|
|
353
|
+
const instance = this.instances.get(_island);
|
|
354
|
+
if (instance) {
|
|
355
|
+
try {
|
|
356
|
+
await this.cleanupInstance(_island, instance);
|
|
357
|
+
this.instances.delete(_island);
|
|
358
|
+
this.componentIds.delete(_island);
|
|
359
|
+
} catch (error) {
|
|
360
|
+
console.warn("Failed to unmount Svelte component:", error);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Create and export a singleton instance of the Svelte HMR adapter
|
|
367
|
+
*/
|
|
368
|
+
export const svelteAdapter = new SvelteHMRAdapter();
|