svelte-on-rails 0.0.13 → 0.0.15
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.
- checksums.yaml +4 -4
- data/README.md +44 -13
- data/lib/svelte_on_rails/render_server_side.rb +3 -1
- data/lib/tasks/svelte_on_rails_tasks.rake +157 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a2ed41a0affd2cea3341eba61240ed04d6faf5566b825cc2b5998ced521ac2a
|
4
|
+
data.tar.gz: 66ea8b433279d645e7e7a4947031a167e3404bcdcbb65671851c0b1cd0feacc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9de5f9e82cad01f18ddbf0a9e579cc1de688cb679aebdf6108f4881885bb73c40e38e3d8f816ff8a023788ae76665370774d70ebb950c31ac852717e211f3d9d
|
7
|
+
data.tar.gz: 2eb846846e3d664c4883df6db37c5afc37f62a887bc25f6c827c0ab867d9f3e3e41185b0f5dc3790db49add22e26f0bba043b1ac368bf99bd1bca87a8a0e24bb
|
data/README.md
CHANGED
@@ -34,27 +34,49 @@ If you have issues, please open one and contributors are welcome!
|
|
34
34
|
- svelte v5 (see: [how to install svelte on rails/vite](https://dev.to/chmich/setup-inertia-and-svelte-on-rails-7-3glk))
|
35
35
|
- turbo (recommended / [how to install turbo on rails](https://github.com/hotwired/turbo-rails?tab=readme-ov-file#installation))
|
36
36
|
|
37
|
+
## Setup from cero
|
38
|
+
|
39
|
+
If you want to start from a new rails app, follow theese tutorials
|
40
|
+
|
41
|
+
- [Create a new rails app with vite_rails](https://dev.to/chmich/setup-vite-on-rails-7-f1i)
|
42
|
+
- [Setup Svelte](https://dev.to/chmich/setup-inertia-and-svelte-on-rails-7-3glk)
|
43
|
+
|
37
44
|
## Installation
|
38
45
|
|
39
|
-
|
40
|
-
|
41
|
-
|
46
|
+
within the app folder
|
47
|
+
|
48
|
+
```bash
|
49
|
+
bundle add svelte-on-rails
|
50
|
+
```
|
51
|
+
```bash
|
52
|
+
rails svelte_on_rails:install_for_vite_and_turbo
|
42
53
|
```
|
43
|
-
to your gemfile and run
|
44
54
|
|
45
|
-
|
46
|
-
|
55
|
+
|
56
|
+
This will create a little config file, istalls the npm package,
|
57
|
+
creates a initializer file and adds the import statement on `appplication.js` entrypoint file
|
58
|
+
and it adds a HelloWorld.svelte component.
|
59
|
+
If not installed, it installs `@hotwired/turbo-rails` too.
|
60
|
+
|
61
|
+
Add a controller and a view with the view helper and you should see «Greetings from Svelte on your view».
|
62
|
+
|
63
|
+
There is also a smaller installer:
|
64
|
+
|
65
|
+
```bash
|
47
66
|
$ rails svelte_on_rails:install
|
48
67
|
```
|
49
68
|
|
50
|
-
|
69
|
+
that does nothing else than creating a config file.
|
70
|
+
|
71
|
+
Within the config file, there are two tags:
|
51
72
|
|
52
73
|
- `frontend_folder` (relative to Rails.root)
|
53
74
|
- `components_folder` (relative to frontend_folder)
|
54
75
|
|
55
|
-
Set up the npm module [@csedl/svelte-on-rails](https://www.npmjs.com/package/@csedl/svelte-on-rails)
|
76
|
+
Set up the npm module [@csedl/svelte-on-rails](https://www.npmjs.com/package/@csedl/svelte-on-rails)
|
77
|
+
on the client side.
|
56
78
|
|
57
|
-
##
|
79
|
+
## Test if it works
|
58
80
|
|
59
81
|
On the view
|
60
82
|
|
@@ -62,7 +84,7 @@ On the view
|
|
62
84
|
= svelte_component("myComponent", items: ['item1', 'item2'])
|
63
85
|
```
|
64
86
|
|
65
|
-
|
87
|
+
Above mentioned installer adds `HelloWorld.svelte`, like so:
|
66
88
|
|
67
89
|
```sveltehtml
|
68
90
|
<script>
|
@@ -84,8 +106,11 @@ Test: Within the `components_folder` (configs above), create a component like `m
|
|
84
106
|
</ul>
|
85
107
|
|
86
108
|
<style>
|
87
|
-
|
88
|
-
|
109
|
+
button {
|
110
|
+
background-color: darkred;
|
111
|
+
color: white;
|
112
|
+
padding: 10px;
|
113
|
+
border: none;
|
89
114
|
}
|
90
115
|
</style>
|
91
116
|
```
|
@@ -94,8 +119,14 @@ And you should see «Greetings from svelte» on the browser.
|
|
94
119
|
|
95
120
|
Click the counter button and check if the component is alive.
|
96
121
|
|
122
|
+
Without the npm package installed,
|
123
|
+
or by passing `render_server_side: true` and `hydrate: false` to the view helper,
|
124
|
+
you would see the same html, and the styled button,
|
125
|
+
but the increment button would not work.
|
126
|
+
|
97
127
|
I inserted very detailed error messages on rails and on the
|
98
|
-
npm package,
|
128
|
+
npm package, for making sure that thisk setup is working.
|
129
|
+
So please check the logs on your browser.
|
99
130
|
|
100
131
|
### Import statements
|
101
132
|
|
@@ -125,7 +125,9 @@ module SvelteOnRails
|
|
125
125
|
full_path = File.join(containing_dir, filename)
|
126
126
|
|
127
127
|
# Check if the file exists and the path matches case-sensitively
|
128
|
-
File.exist?(full_path) && Dir[File.join(containing_dir, "
|
128
|
+
File.exist?(full_path) && Dir[File.join(containing_dir, "**/*")].any? do |f|
|
129
|
+
f == full_path
|
130
|
+
end
|
129
131
|
end
|
130
132
|
|
131
133
|
|
@@ -1,5 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
1
|
Rake::Task["assets:precompile"].enhance do
|
4
2
|
SvelteOnRails::RenderServerSide.reset_dist
|
5
3
|
puts "Mount Svelte: Reset dist ausgeführt"
|
@@ -8,10 +6,165 @@ end
|
|
8
6
|
namespace :svelte_on_rails do
|
9
7
|
desc "Installs SvelteOnRails YAML configuration into your Rails application"
|
10
8
|
task :install do
|
11
|
-
config_path = Rails.root.join("config", "svelte_on_rails.yml")
|
12
9
|
|
10
|
+
create_configuration_file
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Installs SvelteOnRails for usage together with vite_rails and @hotwired/turbo-rails"
|
15
|
+
task :install_for_and_vite_turbo do
|
16
|
+
|
17
|
+
create_configuration_file
|
18
|
+
|
19
|
+
install_npm_package
|
20
|
+
install_turbo
|
21
|
+
|
22
|
+
create_folder(Rails.root.join("app", "frontend", "initializers"))
|
23
|
+
create_folder(Rails.root.join("app", "frontend", "javascript", "components"))
|
24
|
+
add_line_to_file(Rails.root.join("app", "frontend", "entrypoints", "application.js"), "import '../initializers/svelte.js';")
|
25
|
+
|
26
|
+
create_javascript_initializer
|
27
|
+
create_svelte_hello_world
|
28
|
+
|
29
|
+
puts ''
|
30
|
+
puts 'SvelteOnRails installed successfully for using together with vite_rails and @hotwired/turbo-rails.'
|
31
|
+
puts 'Happy coding!'
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
def install_npm_package
|
36
|
+
package_name = "@csedl/svelte-on-rails@latest"
|
37
|
+
puts "Installing #{package_name} via npm..."
|
38
|
+
|
39
|
+
if system("npm install #{package_name}")
|
40
|
+
puts "#{package_name} successfully installed."
|
41
|
+
else
|
42
|
+
abort "Failed to install #{package_name}. Please ensure npm is installed and try running 'npm install #{package_name}' manually."
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def install_turbo
|
47
|
+
|
48
|
+
pkg_js = Rails.root.join("package.json")
|
49
|
+
package_name = "@hotwired/turbo-rails"
|
50
|
+
file_content = File.exist?(pkg_js) ? File.read(pkg_js) : ""
|
51
|
+
|
52
|
+
if file_content.match?(/#{line}/)
|
53
|
+
puts "#{package_name} is already installed."
|
54
|
+
else
|
55
|
+
puts "Installing #{package_name} via npm..."
|
56
|
+
if system("npm install #{package_name}")
|
57
|
+
puts "#{package_name} successfully installed."
|
58
|
+
else
|
59
|
+
abort "Failed to install #{package_name}. Please ensure npm is installed and try running 'npm install #{package_name}' manually."
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
def create_javascript_initializer
|
66
|
+
config_path = Rails.root.join("app", "frontend", "initializers", "svelte.js")
|
67
|
+
if File.exist?(config_path)
|
68
|
+
puts "Initializer already exists: file://#{config_path}"
|
69
|
+
else
|
70
|
+
File.write(config_path, <<~JAVASCRIPT)
|
71
|
+
|
72
|
+
import { initializeSvelteComponents, cleanupSvelteComponents } from '@csedl/svelte-on-rails';
|
73
|
+
|
74
|
+
const components = import.meta.glob('/javascript/components/**/*.svelte', { eager: true });
|
75
|
+
const componentsRoot = '/javascript/components';
|
76
|
+
|
77
|
+
// Initialize Svelte components
|
78
|
+
initializeSvelteComponents(componentsRoot, components, true);
|
79
|
+
|
80
|
+
// Turbo event listener for page load
|
81
|
+
document.addEventListener('turbo:load', () => {
|
82
|
+
initializeSvelteComponents(componentsRoot, components, true);
|
83
|
+
});
|
84
|
+
|
85
|
+
// Turbo event listener for cleanup before page cache
|
86
|
+
document.addEventListener('turbo:before-cache', () => {
|
87
|
+
cleanupSvelteComponents(false);
|
88
|
+
});
|
89
|
+
JAVASCRIPT
|
90
|
+
puts "Created initializer file at file://#{config_path}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def create_svelte_hello_world
|
95
|
+
file_path = Rails.root.join("app", "frontend", "javascript", "components", "HelloWorld.svelte")
|
96
|
+
if File.exist?(file_path)
|
97
|
+
puts "Hello World file already exists: file://#{file_path}"
|
98
|
+
else
|
99
|
+
File.write(file_path, <<~HTML)
|
100
|
+
<script>
|
101
|
+
export let items
|
102
|
+
let count = 0;
|
103
|
+
|
104
|
+
function increment() {
|
105
|
+
count += 1;
|
106
|
+
}
|
107
|
+
</script>
|
108
|
+
|
109
|
+
<h1>Greetings from svelte</h1>
|
110
|
+
|
111
|
+
<button on:click={increment}>Increment: {count}</button>
|
112
|
+
<ul>
|
113
|
+
{#each items as item}
|
114
|
+
<li>{item}</li>
|
115
|
+
{/each}
|
116
|
+
</ul>
|
117
|
+
|
118
|
+
<style>
|
119
|
+
button {
|
120
|
+
background-color: darkred;
|
121
|
+
color: white;
|
122
|
+
padding: 10px;
|
123
|
+
border: none;
|
124
|
+
}
|
125
|
+
</style>
|
126
|
+
HTML
|
127
|
+
puts "Hello World file at file://#{file_path}"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def create_file(file_path)
|
132
|
+
|
133
|
+
unless File.exist?(file_path)
|
134
|
+
FileUtils.touch(file_path)
|
135
|
+
puts "Created empty file at file://#{file_path}"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def add_line_to_file(file_path, line)
|
140
|
+
file_content = File.exist?(file_path) ? File.read(file_path) : ""
|
141
|
+
|
142
|
+
if file_content.match?(/#{line}/)
|
143
|
+
puts "#{line} already present in #{file_path}, nothing changed here."
|
144
|
+
return
|
145
|
+
end
|
146
|
+
|
147
|
+
File.open(file_path, 'a') do |file|
|
148
|
+
file.puts(line)
|
149
|
+
end
|
150
|
+
puts "added #{line} to #{file_path}."
|
151
|
+
rescue StandardError => e
|
152
|
+
puts "Error: #{e.message}"
|
153
|
+
end
|
154
|
+
|
155
|
+
def create_folder(folder)
|
156
|
+
if Dir.exist?(folder)
|
157
|
+
puts "Folder already exists: #{folder}"
|
158
|
+
else
|
159
|
+
FileUtils.mkdir_p(folder)
|
160
|
+
puts "Created folder: #{folder}"
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def create_configuration_file
|
165
|
+
config_path = Rails.root.join("config", "svelte_on_rails.yml")
|
13
166
|
if File.exist?(config_path)
|
14
|
-
puts "Configuration file already exists at
|
167
|
+
puts "Configuration file already exists at file://#{config_path}"
|
15
168
|
else
|
16
169
|
File.write(config_path, <<~YAML)
|
17
170
|
|