@csedl/svelte-on-rails 10.0.0 → 10.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.
- package/index.js +3 -3
- package/package.json +1 -1
- package/src/config.js +13 -2
- package/src/{cleanupSvelteComponent.js → hydrate-build/cleanupSvelteComponent.js} +1 -1
- package/src/hydrate-build/initializeSvelteComponent.js +65 -0
- package/src/{svelte-on-rails-controller.js → hydrate-build/svelte-on-rails-controller.js} +3 -4
- package/src/{dispatch-event.js → streams/dispatch-event.js} +1 -1
- package/src/{turbo-stream-controller.js → streams/turbo-stream-controller.js} +1 -1
- package/src/initializeSvelteComponent.js +0 -78
- /package/src/{componentStreamListener.js → streams/componentStreamListener.js} +0 -0
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import SvelteOnRails from "./src/config.js";
|
|
2
2
|
import {Application} from "@hotwired/stimulus"
|
|
3
|
-
import {dispatchSvelteStreamEvent} from "./src/dispatch-event.js";
|
|
3
|
+
import {dispatchSvelteStreamEvent} from "./src/streams/dispatch-event.js";
|
|
4
4
|
import {actionCableDebugLog} from "./src/logger.js";
|
|
5
5
|
|
|
6
6
|
if (!window.Stimulus) {
|
|
@@ -9,13 +9,13 @@ if (!window.Stimulus) {
|
|
|
9
9
|
|
|
10
10
|
// initialize svelte-on-rails
|
|
11
11
|
|
|
12
|
-
import controller from "./src/svelte-on-rails-controller.js";
|
|
12
|
+
import controller from "./src/hydrate-build/svelte-on-rails-controller.js";
|
|
13
13
|
|
|
14
14
|
Stimulus.register('svelte-on-rails', controller)
|
|
15
15
|
|
|
16
16
|
// initialize stream actions
|
|
17
17
|
|
|
18
|
-
import turboStreamController from "./src/turbo-stream-controller.js";
|
|
18
|
+
import turboStreamController from "./src/streams/turbo-stream-controller.js";
|
|
19
19
|
|
|
20
20
|
Stimulus.register('svelte-on-rails-turbo-stream', turboStreamController)
|
|
21
21
|
|
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/config.js
|
|
2
|
-
let _debug = false;
|
|
2
|
+
let _debug = false;
|
|
3
3
|
let _includeRailsViews = false;
|
|
4
|
+
const _componentModules = import.meta.glob("/**/*.svelte", {eager: false, import: 'default'});
|
|
4
5
|
|
|
5
6
|
import {debugLog} from "./logger.js";
|
|
6
7
|
|
|
@@ -12,13 +13,23 @@ const SvelteOnRails = {
|
|
|
12
13
|
_debug = !!value;
|
|
13
14
|
debugLog(`Debug mode: ${_debug ? "enabled" : "disabled"}`);
|
|
14
15
|
},
|
|
16
|
+
|
|
17
|
+
// includeRailsViews
|
|
15
18
|
get includeRailsViews() { // ← clearer, reads like a question
|
|
16
19
|
return _includeRailsViews;
|
|
17
20
|
},
|
|
18
21
|
set includeRailsViews(value) {
|
|
19
22
|
_includeRailsViews = !!value;
|
|
20
|
-
debugLog(`
|
|
23
|
+
debugLog(`Include components within app/views: ${_includeRailsViews ? "enabled" : "disabled"}`);
|
|
24
|
+
if (_includeRailsViews) {
|
|
25
|
+
const _viewComponents = import.meta.glob("/../../app/views/**/*.svelte", {eager: false, import: "default"});
|
|
26
|
+
Object.assign(_componentModules, _viewComponents);
|
|
27
|
+
debugLog(`loaded view-components: `);
|
|
28
|
+
}
|
|
21
29
|
},
|
|
30
|
+
get components() {
|
|
31
|
+
return(_componentModules);
|
|
32
|
+
}
|
|
22
33
|
};
|
|
23
34
|
|
|
24
35
|
window.SvelteOnRails = SvelteOnRails;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {mount, hydrate} from "svelte";
|
|
2
|
+
import {debugLog} from "../logger.js";
|
|
3
|
+
import SvelteOnRails from "../config.js";
|
|
4
|
+
|
|
5
|
+
// Store for tracking initialized Svelte component instances
|
|
6
|
+
const svelteInstances = new WeakMap();
|
|
7
|
+
|
|
8
|
+
export async function initializeSvelteComponent(element) {
|
|
9
|
+
|
|
10
|
+
// Decide mount vs hydrate
|
|
11
|
+
const action = element.innerHTML.trim() ? "hydrate" : "mount";
|
|
12
|
+
|
|
13
|
+
// fetch the component path
|
|
14
|
+
const componentKey = element.getAttribute("data-component")?.trim();
|
|
15
|
+
if (!componentKey) {
|
|
16
|
+
console.warn("[svelte-on-rails] Missing data-component attribute");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
debugLog(`start :${action} for ${componentKey}`);
|
|
21
|
+
|
|
22
|
+
// load the component from lazy import
|
|
23
|
+
|
|
24
|
+
const loader = SvelteOnRails.components[componentKey];
|
|
25
|
+
if (!loader) {
|
|
26
|
+
console.error(`[svelte-on-rails] Component not found: ${componentKey} (relative to vites sourceCodeDir / frontend-folder)`);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
debugLog(`found: ${componentKey} from lazy-imported components`);
|
|
30
|
+
|
|
31
|
+
const Component = await loader();
|
|
32
|
+
debugLog(`loaded successfully: ${componentKey}`);
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
// Parse props (unchanged)
|
|
36
|
+
let props = {};
|
|
37
|
+
const propsString = element.getAttribute("data-props");
|
|
38
|
+
if (propsString) {
|
|
39
|
+
try {
|
|
40
|
+
props = JSON.parse(propsString);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
console.error(`Error parsing data-props for ${componentKey}:`, e);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
let instance;
|
|
49
|
+
if (action === "mount") {
|
|
50
|
+
element.innerHTML = ""; // clear any server-rendered placeholder
|
|
51
|
+
instance = mount(Component, {target: element, props});
|
|
52
|
+
} else {
|
|
53
|
+
instance = hydrate(Component, {target: element, props});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Preparing for cleanup
|
|
57
|
+
svelteInstances.set(element, instance);
|
|
58
|
+
|
|
59
|
+
element.setAttribute("data-svelte-status", "initialized");
|
|
60
|
+
debugLog(`Success: ${action} ${componentKey}`);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
console.error(`[svelte-on-rails] Failed to ${action} ${componentKey}:`, e);
|
|
63
|
+
element.setAttribute("data-svelte-status", "error");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import {Controller} from "@hotwired/stimulus"
|
|
2
|
-
import {initializeSvelteComponent} from "./initializeSvelteComponent";
|
|
3
|
-
import {cleanupSvelteComponent} from "./cleanupSvelteComponent";
|
|
4
|
-
import {debugLog} from "./logger.js";
|
|
2
|
+
import {initializeSvelteComponent} from "./initializeSvelteComponent.js";
|
|
3
|
+
import {cleanupSvelteComponent} from "./cleanupSvelteComponent.js";
|
|
5
4
|
|
|
6
5
|
export default class extends Controller {
|
|
7
6
|
|
|
8
7
|
connect() {
|
|
9
8
|
const stat = this.element.getAttribute('data-svelte-status');
|
|
10
|
-
if (stat === 'do-not-hydrate-me') return;
|
|
9
|
+
if (stat === 'do-not-hydrate-build-me') return;
|
|
11
10
|
|
|
12
11
|
initializeSvelteComponent(this.element, false);
|
|
13
12
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Controller} from "@hotwired/stimulus"
|
|
2
|
-
import {receiveTurboStreamDebugLog} from "
|
|
2
|
+
import {receiveTurboStreamDebugLog} from "../logger.js";
|
|
3
3
|
import {dispatchSvelteStreamEvent} from "./dispatch-event.js";
|
|
4
4
|
|
|
5
5
|
export default class extends Controller {
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import {mount, hydrate} from 'svelte';
|
|
2
|
-
import {debugLog} from "./logger.js";
|
|
3
|
-
import SvelteOnRails from "./config.js";
|
|
4
|
-
|
|
5
|
-
// Store for tracking initialized Svelte component instances
|
|
6
|
-
const svelteInstances = new WeakMap();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export function initializeSvelteComponent(element, debug = false) {
|
|
12
|
-
|
|
13
|
-
const incl = SvelteOnRails.includeRailsViews;
|
|
14
|
-
let allComponents
|
|
15
|
-
if (SvelteOnRails.includeRailsViews) {
|
|
16
|
-
allComponents = import.meta.glob(['/**/*.svelte', '/../../app/views/**/*.svelte'], {eager: true});
|
|
17
|
-
debugLog("Svelte components loaded from vite-source + app/views");
|
|
18
|
-
} else {
|
|
19
|
-
allComponents = import.meta.glob(['/**/*.svelte'], {eager: true});
|
|
20
|
-
debugLog("Svelte components loaded from vite-source");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const action = element.innerHTML ? 'hydrate' : 'mount';
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// find component
|
|
27
|
-
const componentName = element.getAttribute('data-component');
|
|
28
|
-
debugLog(`Start ${action}: ${componentName}`);
|
|
29
|
-
let foundComponent = allComponents[componentName];
|
|
30
|
-
if (!foundComponent) {
|
|
31
|
-
console.error(`[svelte-on-rails:initializeSvelteComponent] component not found: «${componentName}»\navailable components:\n${Object.keys(allComponents).join('\n')}`);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// Parse props
|
|
37
|
-
let props = {};
|
|
38
|
-
const attrKey = 'data-props';
|
|
39
|
-
const propsString = element.getAttribute(attrKey);
|
|
40
|
-
try {
|
|
41
|
-
if (propsString) props = JSON.parse(propsString);
|
|
42
|
-
} catch (e) {
|
|
43
|
-
console.error(`[initializeSvelteComponents] Error parsing ${attrKey} ("${propsString}") for ${componentName}:`, e);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// Mount/hydrate component
|
|
48
|
-
try {
|
|
49
|
-
let instance;
|
|
50
|
-
if (action === 'mount') {
|
|
51
|
-
debugLog(`Start mounting ${componentName} (${typeof component})`);
|
|
52
|
-
element.innerHTML = '';
|
|
53
|
-
instance = mount(foundComponent.default, {
|
|
54
|
-
target: element,
|
|
55
|
-
props,
|
|
56
|
-
hydrate: false,
|
|
57
|
-
});
|
|
58
|
-
debugLog(`Mounted successfully ${componentName}, with parsed props:`, props, debug);
|
|
59
|
-
} else if (action === 'hydrate') {
|
|
60
|
-
debugLog(`Start hydrating: ${componentName} (${typeof component})`);
|
|
61
|
-
instance = hydrate(foundComponent.default, {
|
|
62
|
-
target: element,
|
|
63
|
-
hydrate: true,
|
|
64
|
-
props: props,
|
|
65
|
-
});
|
|
66
|
-
debugLog(`Hydrated successfully ${componentName}, with parsed props:`, props, debug);
|
|
67
|
-
} else {
|
|
68
|
-
console.error(`[initializeSvelteComponents] Unknown action: "${action}" for component ${componentName}`);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// Store instance and set attributes only after successful mount/hydrate
|
|
73
|
-
svelteInstances.set(element, instance);
|
|
74
|
-
element.setAttribute('data-svelte-status', 'initialized');
|
|
75
|
-
} catch (e) {
|
|
76
|
-
console.error(`[initializeSvelteComponents] Error ${action} ${componentName}:`, e);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
File without changes
|