svelte-on-rails 0.0.14 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b31efba828e0d1f3416c684638903a5d80e42978b1ebe30a67fd447ae8d2bad
4
- data.tar.gz: 45dbe8cd7110c0e36401ab1816c5186265850f79337783cd7a034a1677a6697e
3
+ metadata.gz: 4a2ed41a0affd2cea3341eba61240ed04d6faf5566b825cc2b5998ced521ac2a
4
+ data.tar.gz: 66ea8b433279d645e7e7a4947031a167e3404bcdcbb65671851c0b1cd0feacc2
5
5
  SHA512:
6
- metadata.gz: 9a73256d665867beb93c8221ecbdda94c4f8a5fab1cad3e226c4345804bd13a101b0da58177a6b9e58eeb822ca5bed99cd124eba01e939da348f5ec15ab2ca35
7
- data.tar.gz: e7d878a42a7b7e57db9a9c061d059b7e8afd26a46df0f1bf1eac431227e3575d44f944bed9b0362bbb42b2d8343a43c1487b87012d7d68975d7a78b0d9587002
6
+ metadata.gz: 9de5f9e82cad01f18ddbf0a9e579cc1de688cb679aebdf6108f4881885bb73c40e38e3d8f816ff8a023788ae76665370774d70ebb950c31ac852717e211f3d9d
7
+ data.tar.gz: 2eb846846e3d664c4883df6db37c5afc37f62a887bc25f6c827c0ab867d9f3e3e41185b0f5dc3790db49add22e26f0bba043b1ac368bf99bd1bca87a8a0e24bb
data/README.md CHANGED
@@ -34,25 +34,47 @@ 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
- add
40
- ```ruby
41
- gem "svelte-on-rails"
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
- ```ruby
46
- $ bundle
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
- This will create a little config file, please read the comments there and set the tags:
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) on the client side.
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
 
@@ -62,7 +84,7 @@ On the view
62
84
  = svelte_component("myComponent", items: ['item1', 'item2'])
63
85
  ```
64
86
 
65
- Within the `components_folder` (configs above), create a component like `myComponent.svelte`
87
+ Above mentioned installer adds `HelloWorld.svelte`, like so:
66
88
 
67
89
  ```sveltehtml
68
90
  <script>
@@ -84,8 +106,11 @@ Within the `components_folder` (configs above), create a component like `myCompo
84
106
  </ul>
85
107
 
86
108
  <style>
87
- ul {
88
- list-style: none;
109
+ button {
110
+ background-color: darkred;
111
+ color: white;
112
+ padding: 10px;
113
+ border: none;
89
114
  }
90
115
  </style>
91
116
  ```
@@ -94,10 +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
 
97
- Without the npm package installed, you would see the same html, but the increment button would not work.
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.
98
126
 
99
127
  I inserted very detailed error messages on rails and on the
100
- npm package, so please check the logs on your browser.
128
+ npm package, for making sure that thisk setup is working.
129
+ So please check the logs on your browser.
101
130
 
102
131
  ### Import statements
103
132
 
@@ -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?(/#{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
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.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair