@csedl/svelte-on-rails 12.3.2 → 13.0.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/index.js +3 -20
- package/package.json +10 -14
- package/src/config.js +48 -19
- package/src/logger.js +17 -2
- package/src/utils.js +26 -1
- package/ssr.d.ts +10 -0
- package/ssr.js +4 -0
- package/vite.config.ts +1 -1
- package/utils/index.js +0 -3
package/index.js
CHANGED
|
@@ -1,23 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export { addComponentStreamListener } from "./src/streams/componentStreamListener.js";
|
|
2
|
+
export { dispatchSvelteStreamEvent } from "./src/streams/dispatch-event.js";
|
|
3
|
+
export { debugLog, streamDebugLog } from "./src/logger.js";
|
|
3
4
|
|
|
4
5
|
|
|
5
|
-
//console.error('test-error')
|
|
6
6
|
|
|
7
|
-
if (!window.Stimulus) {
|
|
8
|
-
window.Stimulus = Application.start()
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// initialize svelte-on-rails
|
|
12
|
-
|
|
13
|
-
import controller from "./src/hydrate-build/svelte-on-rails-controller.js";
|
|
14
|
-
|
|
15
|
-
Stimulus.register('svelte-on-rails', controller)
|
|
16
|
-
|
|
17
|
-
// initialize stream actions
|
|
18
|
-
|
|
19
|
-
import turboStreamController from "./src/streams/turbo-stream-controller.js";
|
|
20
|
-
|
|
21
|
-
Stimulus.register('svelte-on-rails-turbo-stream', turboStreamController)
|
|
22
|
-
|
|
23
|
-
export { SvelteOnRails };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@csedl/svelte-on-rails",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.0.0",
|
|
4
4
|
"description": "Client-side runtime for svelte-on-rails: hydration, SSR build support & Turbo Stream actions",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -12,20 +12,12 @@
|
|
|
12
12
|
},
|
|
13
13
|
"exports": {
|
|
14
14
|
".": "./index.js",
|
|
15
|
-
"./
|
|
16
|
-
"./config": "./src/config.js",
|
|
15
|
+
"./init": "./src/config.js",
|
|
17
16
|
"./ssr": {
|
|
18
|
-
"types": "./
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"./src/streams/dispatch-event.js": "./src/streams/dispatch-event.js",
|
|
23
|
-
"./src/logger.js": "./src/logger.js",
|
|
24
|
-
"./src/config.js": "./src/config.js"
|
|
25
|
-
},
|
|
26
|
-
"peerDependencies": {
|
|
27
|
-
"@hotwired/stimulus": "^3.2.2",
|
|
28
|
-
"svelte": "^5.50.0"
|
|
17
|
+
"types": "./ssr.d.ts",
|
|
18
|
+
"import": "./ssr.js",
|
|
19
|
+
"default": "./ssr.js"
|
|
20
|
+
}
|
|
29
21
|
},
|
|
30
22
|
"repository": {
|
|
31
23
|
"type": "git",
|
|
@@ -45,6 +37,10 @@
|
|
|
45
37
|
"vite": "^7.3.1",
|
|
46
38
|
"vite-plugin-dts": "^4.5.4"
|
|
47
39
|
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@hotwired/stimulus": "^3.2.2",
|
|
42
|
+
"svelte": "^5.50.0"
|
|
43
|
+
},
|
|
48
44
|
"dependencies": {
|
|
49
45
|
"fast-glob": "^3.3.2"
|
|
50
46
|
},
|
package/src/config.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
+
import {Application} from "@hotwired/stimulus";
|
|
2
|
+
import controller from "./hydrate-build/svelte-on-rails-controller.js";
|
|
3
|
+
import turboStreamController from "./streams/turbo-stream-controller.js";
|
|
4
|
+
import {debugLog} from "./logger.js";
|
|
5
|
+
import {validateOptions} from "./utils.js";
|
|
1
6
|
|
|
2
7
|
let _debug = false;
|
|
3
|
-
|
|
4
|
-
let
|
|
5
|
-
let _defaultComponents = import.meta.glob("/**/*.svelte", { eager: false, import: "default" });
|
|
6
|
-
|
|
7
|
-
import {debugLog} from "./logger.js";
|
|
8
|
+
let _lazyComponents = {};
|
|
9
|
+
let _defaultComponents = import.meta.glob("/**/*.svelte", {eager: false, import: "default"});
|
|
8
10
|
|
|
9
11
|
export const SvelteOnRails = {
|
|
10
12
|
|
|
11
13
|
async loadComponent(componentKey) {
|
|
12
|
-
|
|
13
14
|
// fetch loader from store
|
|
14
15
|
|
|
15
|
-
let components
|
|
16
|
+
let components;
|
|
16
17
|
if (Object.keys(_lazyComponents).length === 0) {
|
|
17
18
|
debugLog("lazyComponents not set: falling back to default «import.meta.glob(['/**/*.svelte'], {eager: false, import: 'default'})»");
|
|
18
19
|
components = _defaultComponents;
|
|
19
20
|
} else {
|
|
20
|
-
debugLog(
|
|
21
|
-
components = _lazyComponents
|
|
21
|
+
debugLog("using given lazyComponents");
|
|
22
|
+
components = _lazyComponents;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
|
|
25
25
|
const loader = components[componentKey];
|
|
26
26
|
|
|
27
27
|
if (!loader) {
|
|
28
|
+
console.error(`[SOR] components =>`, components);
|
|
28
29
|
throw new Error(`[SOR] Component not found: «${componentKey}»\nAvailable imported Components are:\n\n+++\n • ${Object.keys(components).join('\n • ')}\n+++\n`);
|
|
29
|
-
//return;
|
|
30
30
|
} else {
|
|
31
31
|
debugLog(`Component found: ${componentKey}`);
|
|
32
32
|
}
|
|
@@ -39,30 +39,59 @@ export const SvelteOnRails = {
|
|
|
39
39
|
comp = loader;
|
|
40
40
|
} else {
|
|
41
41
|
comp = await loader();
|
|
42
|
-
if (typeof comp !==
|
|
42
|
+
if (typeof comp !== "function") {
|
|
43
43
|
throw new Error(`[SOR] Component is not a function: ${componentKey}\nYou may need to use vite.meta.glob(..., {...import: "default"})`);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
// finish
|
|
48
48
|
|
|
49
|
-
debugLog(
|
|
49
|
+
debugLog("Component loaded:", comp);
|
|
50
50
|
return comp;
|
|
51
51
|
},
|
|
52
52
|
|
|
53
|
-
set lazyComponents(value) {
|
|
54
|
-
debugLog(`lazyComponents set:`, value);
|
|
55
|
-
_lazyComponents = value;
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
|
|
59
53
|
get debug() {
|
|
60
54
|
return _debug;
|
|
61
55
|
},
|
|
56
|
+
|
|
62
57
|
set debug(value) {
|
|
63
58
|
_debug = !!value;
|
|
64
59
|
debugLog(`Debug mode: ${_debug ? "enabled" : "disabled"}`);
|
|
65
60
|
},
|
|
61
|
+
|
|
62
|
+
get lazyComponents() {
|
|
63
|
+
return _lazyComponents;
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
set lazyComponents(value) {
|
|
67
|
+
debugLog("lazyComponents set:", value);
|
|
68
|
+
_lazyComponents = value;
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
start(options = {}) {
|
|
72
|
+
|
|
73
|
+
validateOptions(options, {
|
|
74
|
+
debug: "boolean",
|
|
75
|
+
lazyComponents: "object",
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (Object.prototype.hasOwnProperty.call(options, "debug")) {
|
|
79
|
+
this.debug = options.debug;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (Object.prototype.hasOwnProperty.call(options, "lazyComponents")) {
|
|
83
|
+
this.lazyComponents = options.lazyComponents;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!window.Stimulus) {
|
|
87
|
+
window.Stimulus = Application.start();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
window.Stimulus.register("svelte-on-rails", controller);
|
|
91
|
+
window.Stimulus.register("svelte-on-rails-turbo-stream", turboStreamController);
|
|
92
|
+
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
66
95
|
};
|
|
67
96
|
|
|
68
97
|
window.SvelteOnRails = SvelteOnRails;
|
package/src/logger.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {SvelteOnRails} from './config.js'
|
|
2
2
|
|
|
3
3
|
export function debugLog(message, object, object2) {
|
|
4
|
-
log(
|
|
4
|
+
log(null, message, object, object2);
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
export function streamDebugLog(message, object, object2) {
|
|
8
|
-
log(
|
|
8
|
+
log('stream', message, object, object2);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
function log(namespace, message, object, object2) {
|
|
@@ -21,4 +21,19 @@ function log(namespace, message, object, object2) {
|
|
|
21
21
|
console.log(`[SOR]${nmsp} ${message}`);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function logSeparator(title) {
|
|
27
|
+
if (!title) {
|
|
28
|
+
return '='.repeat(80)
|
|
29
|
+
} else {
|
|
30
|
+
const tit = `=== ${title} ===`;
|
|
31
|
+
const l = 80 - tit.length;
|
|
32
|
+
if (l >= 1) {
|
|
33
|
+
const l1 = Math.floor(l / 2);
|
|
34
|
+
return ('='.repeat(l1) + tit + '='.repeat(l - l1))
|
|
35
|
+
} else {
|
|
36
|
+
return tit;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
24
39
|
}
|
package/src/utils.js
CHANGED
|
@@ -29,4 +29,29 @@ export function componentRenderError(component, error, ssr = false) {
|
|
|
29
29
|
</small>
|
|
30
30
|
</div>
|
|
31
31
|
`)
|
|
32
|
-
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function validateOptions(givenOptions, validations) {
|
|
35
|
+
const validKeys = Object.keys(validations);
|
|
36
|
+
for (const option of Object.keys(givenOptions)) {
|
|
37
|
+
// Check if the option is allowed
|
|
38
|
+
if (!validKeys.includes(option)) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`Invalid option: "${option}". ` +
|
|
41
|
+
`Available options are:\n • ${validKeys.map(k => `"${k}"`).join("\n • ")}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Check the type
|
|
46
|
+
const expectedType = validations[option];
|
|
47
|
+
const actualType = typeof givenOptions[option];
|
|
48
|
+
|
|
49
|
+
if (actualType !== expectedType) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`Option «${option}»: expected type «${expectedType}», ` +
|
|
52
|
+
`but received «${actualType}» ` +
|
|
53
|
+
`(value: ${JSON.stringify(givenOptions[option])})`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
package/ssr.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import * as fastGlob from 'fast-glob';
|
|
3
|
+
|
|
4
|
+
export declare function devModuleMap(options?: {
|
|
5
|
+
fileName?: string;
|
|
6
|
+
includeNodeModules?: boolean;
|
|
7
|
+
moduleFilter?: (id: string) => boolean;
|
|
8
|
+
}): Plugin;
|
|
9
|
+
|
|
10
|
+
export { fastGlob };
|
package/ssr.js
ADDED
package/vite.config.ts
CHANGED
package/utils/index.js
DELETED