@csedl/svelte-on-rails 0.0.6 → 0.0.7
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/README.md +9 -14
- package/package.json +1 -1
- package/src/debugUtils.js +14 -0
- package/src/initializeSvelteComponents.js +10 -2
package/README.md
CHANGED
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Works together with the [svelte-on-rails](https://gitlab.com/sedl/svelte-on-rails) gem.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Please follow the [gem](https://gitlab.com/sedl/svelte-on-rails) instructions.
|
|
8
|
+
- Svelte >= 5
|
|
9
|
+
- tested with ruby 3.2.2 and rails 7.1
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
## Installation
|
|
8
12
|
|
|
9
13
|
```
|
|
10
14
|
npm i @csedl/svelte-on-rails
|
|
11
15
|
```
|
|
12
16
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
A file like `initializeSvelte.js` that is imported into your entrypoint file
|
|
17
|
+
Create a file like `initializeSvelte.js` and import it into your entrypoint file, like `application.js`
|
|
16
18
|
|
|
17
19
|
```javascript
|
|
18
20
|
import { initializeSvelteComponents, cleanupSvelteComponents } from 'svelte-on-rails';
|
|
@@ -34,7 +36,8 @@ document.addEventListener('turbo:before-cache', () => {
|
|
|
34
36
|
});
|
|
35
37
|
```
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
- The last boolean argument is for debugging logs to the console
|
|
40
|
+
- I have inserted very detailed error logs, so please check your browser console for logs.
|
|
38
41
|
|
|
39
42
|
## How it works
|
|
40
43
|
|
|
@@ -52,16 +55,8 @@ to the wrapping element. Then, this lib
|
|
|
52
55
|
- adds the attribute `data-svelte-initialized='true'`
|
|
53
56
|
- removes the class `svelte-on-rails-not-initialized-component`
|
|
54
57
|
|
|
55
|
-
## Requirements
|
|
56
|
-
|
|
57
|
-
- Svelte >= 5
|
|
58
|
-
- tested with ruby 3.2.2 and rails 7.1
|
|
59
|
-
|
|
60
58
|
## Testing
|
|
61
59
|
|
|
62
|
-
I developed this all on Rails-7, together with `vite_rails`.
|
|
63
|
-
|
|
64
|
-
|
|
65
60
|
There is a [rails project](https://gitlab.com/sedl/svelte-on-rails-tests) with testings (based on playwright) where the gem and this package are tested together.
|
|
66
61
|
|
|
67
62
|
The gem includes its own tests.
|
package/package.json
CHANGED
package/src/debugUtils.js
CHANGED
|
@@ -8,4 +8,18 @@ export function debugLog2(msg, object, debug) {
|
|
|
8
8
|
if (debug) {
|
|
9
9
|
console.log(`[initializeSvelteComponents] ${msg}`, object);
|
|
10
10
|
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function checkComponentsRoot(componentsRoot, firstImport) {
|
|
14
|
+
const reg = new RegExp(`^${componentsRoot}`);
|
|
15
|
+
|
|
16
|
+
if (!reg.test(firstImport)) {
|
|
17
|
+
console.error(
|
|
18
|
+
`[initializeSvelteComponents] ERROR\nThe componentsRoot («${componentsRoot}») does not match.\n` +
|
|
19
|
+
`The first found import is: «${firstImport}».\n` +
|
|
20
|
+
`components root must equal the beginning of the import path, example:\n` +
|
|
21
|
+
`componentsRoot: «/javascript», and ` +
|
|
22
|
+
`import: «/javascript/components/MyComponent.svelte» => would work`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
11
25
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { mount, hydrate } from 'svelte';
|
|
2
|
-
import { debugLog, debugLog2 } from './debugUtils.js';
|
|
2
|
+
import { debugLog, debugLog2, checkComponentsRoot } from './debugUtils.js';
|
|
3
3
|
|
|
4
4
|
// Store for tracking initialized Svelte component instances
|
|
5
5
|
const svelteInstances = new WeakMap();
|
|
@@ -19,6 +19,9 @@ export function initializeSvelteComponents(componentsRoot, components, debug = f
|
|
|
19
19
|
// Convert live HTMLCollection to a static array
|
|
20
20
|
const elementsArray = Array.from(virginElements);
|
|
21
21
|
|
|
22
|
+
// Check if componentsRoot is setup correctly
|
|
23
|
+
checkComponentsRoot(componentsRoot, Object.keys(components)[0]);
|
|
24
|
+
|
|
22
25
|
// Iterate over the static array
|
|
23
26
|
for (const element of elementsArray) {
|
|
24
27
|
const action = element.innerHTML ? 'hydrate' : 'mount';
|
|
@@ -27,7 +30,12 @@ export function initializeSvelteComponents(componentsRoot, components, debug = f
|
|
|
27
30
|
// Check component
|
|
28
31
|
const componentPath = `${componentsRoot}/${componentName}.svelte`.replace(/\/+/g, '/');
|
|
29
32
|
if (!components[componentPath]) {
|
|
30
|
-
console.error(
|
|
33
|
+
console.error(
|
|
34
|
+
`[initializeSvelteComponents] ERROR\n` +
|
|
35
|
+
`Component not found for path: «${componentPath}»\n` +
|
|
36
|
+
`Imported paths are:\n` +
|
|
37
|
+
`«${Object.keys(components).join('»\n«')}»`
|
|
38
|
+
);
|
|
31
39
|
continue; // Proceed to the next element
|
|
32
40
|
}
|
|
33
41
|
const component = components[componentPath].default;
|