svelte-on-rails 0.0.14 → 0.0.16

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b31efba828e0d1f3416c684638903a5d80e42978b1ebe30a67fd447ae8d2bad
4
- data.tar.gz: 45dbe8cd7110c0e36401ab1816c5186265850f79337783cd7a034a1677a6697e
3
+ metadata.gz: 677cbdec13a7a29dad5a6601755976aebce32a9cd5d4d132d62682650887b81d
4
+ data.tar.gz: 3cd12a78ff280021ff44ba0bc29371a3268a4b50de93264f9388925cc6fdad21
5
5
  SHA512:
6
- metadata.gz: 9a73256d665867beb93c8221ecbdda94c4f8a5fab1cad3e226c4345804bd13a101b0da58177a6b9e58eeb822ca5bed99cd124eba01e939da348f5ec15ab2ca35
7
- data.tar.gz: e7d878a42a7b7e57db9a9c061d059b7e8afd26a46df0f1bf1eac431227e3575d44f944bed9b0362bbb42b2d8343a43c1487b87012d7d68975d7a78b0d9587002
6
+ metadata.gz: 289186502e09d9e27b75b17f633cd017f066470efeafcaf1c9ab979f716f60705f4b7d7505da496380171d00ef971eef14b22fcfbb83781be810732ed3f4d3dc
7
+ data.tar.gz: 620d66d405bfea65fc134c837c7832ea3c7dae6c566aaedea3431a2ebb8906700a9255afd088ff65ab607ea5789132760c2b12f3b3e2c4676ce36a6ecf540747
data/README.md CHANGED
@@ -34,25 +34,50 @@ 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
+ - create a controller and a view and you should see `Vite ⚡️ Rails` on the browser console.
43
+ - make sure vite is on its latest version, sometimes you have to `npm i vite@latest`
44
+ - [Setup Svelte](https://dev.to/chmich/setup-inertia-and-svelte-on-rails-7-3glk)
45
+ - and restart the app
46
+
37
47
  ## Installation
38
48
 
39
- add
40
- ```ruby
41
- gem "svelte-on-rails"
49
+ within the app folder
50
+
51
+ ```bash
52
+ bundle add svelte-on-rails
53
+ ```
54
+ ```bash
55
+ rails svelte_on_rails:install_for_vite_and_turbo
42
56
  ```
43
- to your gemfile and run
44
57
 
45
- ```ruby
46
- $ bundle
58
+
59
+ This will create a little config file, istalls the npm package,
60
+ creates a initializer file and adds the import statement on `appplication.js` entrypoint file
61
+ and it adds a HelloWorld.svelte component.
62
+ If not installed, it installs `@hotwired/turbo-rails` too.
63
+
64
+ Add a controller and a view with the view helper and you should see «Greetings from Svelte on your view».
65
+
66
+ There is also a smaller installer:
67
+
68
+ ```bash
47
69
  $ rails svelte_on_rails:install
48
70
  ```
49
71
 
50
- This will create a little config file, please read the comments there and set the tags:
72
+ that does nothing else than creating a config file.
73
+
74
+ Within the config file, there are two tags:
51
75
 
52
76
  - `frontend_folder` (relative to Rails.root)
53
77
  - `components_folder` (relative to frontend_folder)
54
78
 
55
- Set up the npm module [@csedl/svelte-on-rails](https://www.npmjs.com/package/@csedl/svelte-on-rails) on the client side.
79
+ Set up the npm module [@csedl/svelte-on-rails](https://www.npmjs.com/package/@csedl/svelte-on-rails)
80
+ on the client side.
56
81
 
57
82
  ## Test if it works
58
83
 
@@ -62,7 +87,7 @@ On the view
62
87
  = svelte_component("myComponent", items: ['item1', 'item2'])
63
88
  ```
64
89
 
65
- Within the `components_folder` (configs above), create a component like `myComponent.svelte`
90
+ Above mentioned installer adds `HelloWorld.svelte`, like so:
66
91
 
67
92
  ```sveltehtml
68
93
  <script>
@@ -84,8 +109,11 @@ Within the `components_folder` (configs above), create a component like `myCompo
84
109
  </ul>
85
110
 
86
111
  <style>
87
- ul {
88
- list-style: none;
112
+ button {
113
+ background-color: darkred;
114
+ color: white;
115
+ padding: 10px;
116
+ border: none;
89
117
  }
90
118
  </style>
91
119
  ```
@@ -94,10 +122,14 @@ And you should see «Greetings from svelte» on the browser.
94
122
 
95
123
  Click the counter button and check if the component is alive.
96
124
 
97
- Without the npm package installed, you would see the same html, but the increment button would not work.
125
+ Without the npm package installed,
126
+ or by passing `render_server_side: true` and `hydrate: false` to the view helper,
127
+ you would see the same html, and the styled button,
128
+ but the increment button would not work.
98
129
 
99
130
  I inserted very detailed error messages on rails and on the
100
- npm package, so please check the logs on your browser.
131
+ npm package, for making sure that thisk setup is working.
132
+ So please check the logs on your browser.
101
133
 
102
134
  ### Import statements
103
135
 
@@ -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,8 +6,163 @@ 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?(/#{package_name}/)
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
167
  puts "Configuration file already exists at file://#{config_path}"
15
168
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair