Skip to content

Commit a5f2654

Browse files
committed
pre reactivity
1 parent 642a2ce commit a5f2654

File tree

3 files changed

+56
-89
lines changed

3 files changed

+56
-89
lines changed

demo/src/lib/dyn.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

docs/reactivity.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Solutions for Rune-Based Reactive State Sharing
2+
3+
Solution 1: Reactive Context Injection
4+
5+
Modify the factory function to accept reactive context objects that get injected into the
6+
component's compilation scope. Instead of passing static values, pass rune references directly
7+
through the compilation process.
8+
9+
Approach:
10+
- Extend ComponentCompiler.render() to accept a reactiveContext parameter containing rune
11+
objects
12+
- Modify transformCompiledCode() to inject these runes as imports/globals in the compiled
13+
component
14+
- The dynamic component accesses parent runes directly via injected context
15+
16+
Solution 2: Rune Reference Passing
17+
18+
Create a mechanism to pass actual rune references (not their values) through the mount system by
19+
serializing rune getters/setters and reconstructing them in the dynamic component.
20+
21+
Approach:
22+
- Extend the factory function to accept rune descriptors ({ get: () => value, set: (v) => {...}
23+
})
24+
- Transform the compiled code to wire these descriptors to local rune state
25+
- Dynamic components get direct access to parent runes through the descriptor interface
26+
27+
Solution 3: Shared Rune Store
28+
29+
Implement a global rune store that both parent and dynamic components can subscribe to, using
30+
Svelte 5's $state() with a singleton pattern.
31+
32+
Approach:
33+
- Create a SharedRuneStore class with $state() values
34+
- Parent components register their runes in the store
35+
- Dynamic components access the same rune references from the store
36+
- No wrapper/proxy - direct rune access through shared state container
37+
38+
Solution 4: Compilation-Time Rune Binding
39+
40+
Modify the source transformation to replace rune references in the dynamic component with
41+
bindings to externally provided rune objects.
42+
43+
Approach:
44+
- Parse the dynamic component source for rune usage patterns
45+
- Replace let { count = $bindable(0) } = $props() with direct external rune bindings
46+
- Inject the actual parent runes as module-level imports during compilation
47+
- Component uses parent runes as if they were its own

src/compiler/runtime.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ export class ComponentCompiler {
6262
const compileOptions: CompileOptions = {
6363
generate: "client",
6464
css: options.css ? "injected" : "external",
65-
// If true, returns the modern version of the AST. Will become true by default in Svelte 6, and the option will be removed in Svelte 7.
65+
// If true, returns the modern version of the AST. Will become true by default in Svelte 6,
66+
// and the option will be removed in Svelte 7.
6667
modernAst: true,
67-
// Set to true to force the compiler into runes mode, even if there are no indications of runes usage. Set to false to force the compiler into ignoring runes, even if there are indications of runes usage. Set to undefined (the default) to infer runes mode from the component code. Is always true for JS/TS modules compiled with Svelte. Will be true by default in Svelte 6. Note that setting this to true in your svelte.config.js will force runes mode for your entire project, including components in node_modules, which is likely not what you want. If you're using Vite, consider using dynamicCompileOptions instead.
68+
// Set to true to force the compiler into runes mode, even if there are no indications of runes usage.
69+
// Set to false to force the compiler into ignoring runes, even if there are indications of runes usage.
70+
// Set to undefined (the default) to infer runes mode from the component code.
71+
// Is always true for JS/TS modules compiled with Svelte. Will be true by default in Svelte 6.
72+
// Note that setting this to true in your svelte.config.js will force runes mode for your entire project,
73+
// including components in node_modules, which is likely not what you want. If you're using Vite, consider
74+
// using dynamicCompileOptions instead.
6875
runes: true,
6976
filename: options.name || "DynamicComponent.svelte"
7077
};

0 commit comments

Comments
 (0)