svelte-on-rails 1.0.13 → 2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2bddaae521fc71ed3c7975612b8b9109830fd25210eda2d30c00c6150265dca
4
- data.tar.gz: 7adeb7e7181508d704ff98087f5b601a596159d1205cb0ffb08e98f5fca81272
3
+ metadata.gz: a0e1e1cb1a95e07c184fb9b943315765340d46690f2c6a66f0041aaa233fbc19
4
+ data.tar.gz: 0b93fc9a113b43b5b997e81f912535015415d66e1e6dd480129a5f28091d227b
5
5
  SHA512:
6
- metadata.gz: 77f1fd03901c9c73c8893c471b464db9e2e4fd212b9a36dad9a78f15b34f155865155b4796af3409e32b741dd4693a9756c369bcfb32caf05b1a1abdfa6e1af5
7
- data.tar.gz: 7be8fd8640a3d7431dd6e8274a3bc3f3a9eca0c9c9e30f46d42f4eb00735f13a8e8421e8b0e6142c17ac0a0aab9bc9df5cc0914b17add50677498575401ac20b
6
+ metadata.gz: 4801f3526444fbc17446bfb06270eae0cf8962be0c1c07c51d3a4b166c0ef2376b3fa73532b3629f18d99e296df9b34b55b34287a9bea6e77dcf194b01ea5adc
7
+ data.tar.gz: 5641533b5e6625231c1a5803e5d2cf75c1e0b1f3749d44e0cb43176b60b0cf03437ade435fb50013b997e5d8b5252ebb42b4946084e416123c1dc5c2d6af7ccb
data/README.md CHANGED
@@ -24,7 +24,7 @@ On every app there are parts where you want it to shine. This is where Svelte co
24
24
  - Super fast
25
25
  - **Compared to Stimulus**
26
26
  - No more writing double logic of the initial HTML state
27
- - Stimulus is great for rails views with some javascript, but, complex parts: Svelte. Nothing else.
27
+ - Stimulus is great for rails views with some javascript, but, complex parts: You will love svelte.
28
28
  - **Compared to React**
29
29
  - No more shadow dom and all those packages that are supposed to improve performance (e.g. useCallback...)
30
30
  - Slimmer packages
@@ -39,9 +39,9 @@ but: Where needed, we want full power.
39
39
 
40
40
  # Features
41
41
 
42
- - 🤝 Fully integrated with `assets:precompile`
43
- - 🚀 Cero-config deployment
44
42
  - ✍️ Sophisticated error messages
43
+ - 🚀 Cero-config deployment
44
+ - 🤝 Fully integrated with `assets:precompile`
45
45
  - 😍 Contributor friendly
46
46
 
47
47
  # Known Issues
@@ -163,14 +163,24 @@ and run the minimal installer (you are free to add any options from above)
163
163
  rails g svelte_on_rails:install
164
164
  ```
165
165
 
166
- Restart the server, add a example component like `app/frontend/javascript/HelloWorld.svelte`
167
- and the helper to a view, like
166
+ Restart the server, add a hello world component `app/frontend/javascript/HelloWorld.svelte` like
167
+
168
+ ```sveltehtml
169
+ <script>
170
+ export let title
171
+ </script>
172
+
173
+ <h1>Svelte {title}</h1>
174
+ ```
175
+
176
+ Add it to the view
168
177
 
169
178
  ```erb
170
- <%= svelte_component('HelloWorld') %>
179
+ <%= svelte_component('HelloWorld', title: 'Hello World') %>
171
180
  ```
172
181
 
173
- And you should see your component rendered on the browser! 👍 🤗
182
+
183
+ And you should see "Svelte Hello World" on the browser! 👍 🤗
174
184
 
175
185
  **Explanation**
176
186
 
@@ -259,6 +269,11 @@ loaded on very first load to the frontend, then the most work is done
259
269
  in frontend and the server is relieved, except on initial request.
260
270
  You will see no unpleasant «blink» on the page.
261
271
 
272
+ **Tip: Performance optimisation for dropdowns**
273
+
274
+ For example, if you have Svelte components inside dropdowns, you can pass `ssr: false` to the view helper
275
+ because you will never see a "blink" there. This will reduce the load on the server.
276
+
262
277
  ## More rake tasks
263
278
 
264
279
  This tasks are more for testing/playground purposes
@@ -61,7 +61,10 @@ module SvelteOnRails
61
61
  File.write('package.json', JSON.pretty_generate(pkg_js))
62
62
 
63
63
  if @local_npm_package_url
64
- npm_i.link_local_package(NPM_PACKAGE_NAME, @local_npm_package_url)
64
+ npm_i.link_local_package(NPM_PACKAGE_NAME, @local_npm_package_url)
65
+
66
+ # npm does not resolve peer dependencies on local package url
67
+ npm_i.install_or_update_package('@hotwired/stimulus')
65
68
  else
66
69
  npm_i.install_or_update_package(NPM_PACKAGE_NAME)
67
70
  end
@@ -69,7 +72,7 @@ module SvelteOnRails
69
72
  npm_i.install_or_update_package('@types/node')
70
73
 
71
74
  js_i = SvelteOnRails::Installer::Javascript
72
- init_stat = '../initializers/svelte.js'
75
+ init_stat = '@csedl/svelte-on-rails'
73
76
  js_i.append_import_statement(application_js_path, init_stat, "import '#{init_stat}';")
74
77
  end
75
78
 
@@ -37,6 +37,7 @@ module SvelteOnRails
37
37
 
38
38
  def self.link_local_package(package_name, local_package_url)
39
39
  Dir.chdir(local_package_url) do
40
+ `npm unlink`
40
41
  `npm link`
41
42
  end
42
43
  Dir.chdir(Rails.root) do
@@ -59,35 +59,50 @@ module SvelteOnRails
59
59
  def self.precompile(last_mtime = nil)
60
60
  config = SvelteOnRails::Configuration.instance
61
61
 
62
- mtime = (File.exist?(config.ssr_dist_folder.join('last_mtime')) ? File.read(config.ssr_dist_folder.join('last_mtime')).to_f : 0.0)
63
-
64
62
  Dir.chdir(config.rails_root) do
65
63
 
66
64
  # run build
67
65
 
68
66
  cmd = "./node_modules/.bin/vite build --config vite-ssr.config.ts"
67
+
69
68
  stdout, stderr, status = Open3.capture3(cmd)
70
- mtime2 = (File.exist?(config.ssr_dist_folder.join('last_mtime')) ? File.read(config.ssr_dist_folder.join('last_mtime')).to_f : 0.0)
69
+
70
+ warnings = stderr.to_s.split("\n")
71
+ errors_matcher = Regexp.new('(Could not resolve|failed to resolve)')
72
+ error_lines = warnings.select { |e| e.match(errors_matcher) }
73
+ have_error = error_lines.present? || status.to_s.match(/exit 1/)
71
74
 
72
75
  # error handling
73
76
 
74
77
  if stderr.present?
75
78
 
76
- nothing_done = mtime2 == mtime
77
- color = (nothing_done ? "\033[97;41m" : "\033[30;106m")
78
-
79
- msg = " +++ #{nothing_done ? 'ERROR' : 'WARNING'} compiling Svelte components#{nothing_done ? ' failed' : ''} («#{cmd}») +++ "
80
- puts "#{color}#{msg}\033[0m"
81
- errs = stderr.split("\n")
82
- errs.each { |e| puts "#{color} \033[0m #{e}" }
83
- puts "#{color} +++ End of error message +++ \033[0m"
84
- puts "#{color} +++ Run «npm run build:ssr» on the console to see the original error message +++ \033[0m"
85
-
86
- if nothing_done
87
- critical_lines = errs.select { |e| e.match(/Could not resolve/i) }
88
- cl_str = if critical_lines.present?
89
- "#{critical_lines.join("\n")}\n\n"
90
- end
79
+ red_background = "\033[97;41m"
80
+ light_blue_background = "\033[30;106m"
81
+ clear_colors = "\033[0m"
82
+
83
+ msg = " +++ #{have_error ? 'ERROR' : 'WARNING'} compiling Svelte components#{have_error ? ' failed' : ''} («#{cmd}») +++ "
84
+ puts "#{have_error ? red_background : light_blue_background}#{msg}#{clear_colors}"
85
+
86
+ warnings.each do |e|
87
+ if e.match(errors_matcher)
88
+ red_font = "\033[31m"
89
+ puts "#{red_background} #{clear_colors}#{red_font} #{e}#{clear_colors}"
90
+ else
91
+ puts "#{light_blue_background} #{clear_colors}#{e}"
92
+ end
93
+ end
94
+
95
+ if have_error
96
+ puts "#{red_background} +++ End of error message +++ #{clear_colors}"
97
+ else
98
+ puts "#{light_blue_background} +++ End of compiling warnings +++ #{clear_colors}"
99
+ end
100
+ puts "#{have_error ? red_background : light_blue_background} +++ Run «npm run build:ssr» on the console to see the original error message +++ #{clear_colors}"
101
+
102
+ if have_error
103
+ cl_str = if error_lines.present?
104
+ "#{error_lines.join("\n")}\n\n"
105
+ end
91
106
  raise "Svelte components compilation failed\n\n#{cl_str}Full message:\n+++\n#{stderr}+++\n\nYou can run «npm run build:ssr» on the console to see the original error message\n"
92
107
  end
93
108
 
@@ -104,6 +119,15 @@ module SvelteOnRails
104
119
  mtime_path = config.ssr_dist_folder.join('last_mtime')
105
120
  File.write(mtime_path, last_mtime.to_s)
106
121
  end
122
+
123
+ end
124
+
125
+ def self.puts_error(text)
126
+ red_background = "\033[97;41m"
127
+ clear_colors = "\033[0m"
128
+ text.split("\n").each do |line|
129
+ puts "#{red_background} #{clear_colors} #{line}"
130
+ end
107
131
  end
108
132
 
109
133
  end
@@ -33,7 +33,8 @@ module SvelteOnRails
33
33
 
34
34
  unless status.to_s.match(/^pid [0-9]+ exit 0$/)
35
35
  cmp = "#{@component_files[:svelte_filename]} could not be rendered Server-side\n\n"
36
- raise "#{cmp}Error output from render.js (stderr) =>\n+++\n" + stderr + "+++\n\nRender Svelte Server-side =>\n#{cmd}\n\n"
36
+ msg = "#{cmp}Error output from render.js (stderr) =>\n+++\n" + stderr + "+++\n\nRender Svelte Server-side =>\n#{cmd}\n\n"
37
+ utils.puts_error(msg)
37
38
  end
38
39
 
39
40
  begin
@@ -46,10 +46,11 @@ module SvelteOnRails
46
46
  # set up html
47
47
 
48
48
  options[:class] = options[:class].to_s + ' svelte-component'
49
- options[:class] += ' please-hydrate-me-svelte-on-rails' if hydrate
49
+ #options[:class] += ' please-hydrate-me-svelte-on-rails' if hydrate
50
50
  options[:data] ||= {}
51
51
  options[:data][:props] = props.to_json
52
- options[:data][:svelte_component] = filename
52
+ options[:data][:svelte_component] = "/#{conf.components_folder + filename}"
53
+ options[:data][:controller] = 'svelte-on-rails'
53
54
 
54
55
  if ssr
55
56
 
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: 1.0.13
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair
@@ -49,7 +49,6 @@ files:
49
49
  - lib/svelte_on_rails/renderer/utils.js
50
50
  - lib/svelte_on_rails/view_helpers.rb
51
51
  - lib/tasks/svelte_on_rails_tasks.rake
52
- - templates/config_base/app/frontend/initializers/svelte.js
53
52
  - templates/config_base/app/frontend/ssr/ssr.js
54
53
  - templates/config_base/config/svelte_on_rails.yml
55
54
  - templates/config_base/vite-ssr.config.ts
@@ -1,18 +0,0 @@
1
-
2
- import { initializeSvelteComponents, cleanupSvelteComponents } from '@csedl/svelte-on-rails';
3
-
4
- const components = import.meta.glob('/javascript/components/**/*.svelte', { eager: true });
5
- const componentsRoot = '/javascript/components';
6
-
7
- // Initialize Svelte components
8
- initializeSvelteComponents(componentsRoot, components, true);
9
-
10
- // Turbo event listener for page load
11
- document.addEventListener('turbo:load', () => {
12
- initializeSvelteComponents(componentsRoot, components, true);
13
- });
14
-
15
- // Turbo event listener for cleanup before page cache
16
- document.addEventListener('turbo:before-cache', () => {
17
- cleanupSvelteComponents(false);
18
- });