svelte-on-rails 11.0.10 → 12.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: bde355e06f9c67b4bddea860a356d1574a0835966d5b63a2170293024c676a05
4
- data.tar.gz: 0a6f0db199a045ad2cc5338d0700b1679d114cc8e26389c4d8abc38db389a8be
3
+ metadata.gz: fb357cd39ec18f7dc77764d09b78cb12a7a002df261b85d022d87c0c4337c7fe
4
+ data.tar.gz: 335302f38a588c5dca3b997e20c93093a4022bbbcbe61ff58528ce5250a524ae
5
5
  SHA512:
6
- metadata.gz: ec78cc57811b3803d3130ad37c1c4a8c96f2736e515c4c2a69dc6d9fb22a6655f98d350d1a902bca767ded50e5c9ff08e4575343e0de019ab4de01cf015ffb54
7
- data.tar.gz: 259e6ac460b837e0e8faa9fa37163463c916a72a642993297335202a6a874ede1bc925b02f3c01c628806952fed2dc10df90bead409d7861a41069470659794c
6
+ metadata.gz: aac83b836117549e9d838d5d16fb86301f2e4d0fdf91a82125a1f216c9899c1ab2c702dc3cbf636a0caf1f7457788943007195bdb14146f62302e00fb31d7d83
7
+ data.tar.gz: '08f845a7c90c0a450e7f8eb549a7a5b6dcd73052528896dd76e8b3075b66629fe4da7c825e01101dca7a96615f5cde2718ab86693092c10d063bb7d5b5a10947'
@@ -31,6 +31,7 @@ require 'generators/svelte_on_rails/update/update_generator'
31
31
  require 'generators/showcase_generator'
32
32
 
33
33
  require 'digest/xxhash'
34
+ require 'benchmark'
34
35
 
35
36
  module SvelteOnRails
36
37
  class << self
@@ -61,7 +61,6 @@ module SvelteOnRails
61
61
  @redis_instance = Redis.new(url: redis_cache_store[:url])
62
62
  end
63
63
 
64
-
65
64
  validate_configs
66
65
  end
67
66
 
@@ -77,6 +76,24 @@ module SvelteOnRails
77
76
  @configs[:watch_changes] == true
78
77
  end
79
78
 
79
+ def debug_log(debug, component_basename, subject, &block)
80
+ msg = " [svelte-on-rails:#{component_basename}] #{subject}"
81
+ if !block_given?
82
+ Rails.logger.debug(msg)
83
+ else
84
+ if debug || @configs[:debug]
85
+ r = nil
86
+ sec = Benchmark.realtime do
87
+ r = block.call
88
+ end
89
+ Rails.logger.debug("#{msg} (#{(sec * 1000.0).round(2)}ms)")
90
+ r
91
+ else
92
+ block.call
93
+ end
94
+ end
95
+ end
96
+
80
97
  def rails_root(root_url = nil)
81
98
  root_url || Rails.root
82
99
  end
@@ -213,7 +230,6 @@ module SvelteOnRails
213
230
  raise "[svelte-on-rails] Missing configuration: #{k}" if !@configs.key?(k) && v.keys.include?(:required)
214
231
  end
215
232
 
216
-
217
233
  end
218
234
 
219
235
  end
@@ -74,6 +74,7 @@ module SvelteOnRails
74
74
  {
75
75
  path: path,
76
76
  component: [(dir.present? ? dir : nil), basename].compact.join('/'),
77
+ component_basename: basename,
77
78
  vite_path: vite_path
78
79
  }
79
80
  end
@@ -21,9 +21,10 @@ module SvelteOnRails
21
21
  validate_options
22
22
  ensure_valid_component_and_manifest!(component)
23
23
 
24
- if @conf.component_changed?(component_paths[:path])
25
- debug_log("Rebuilding assets...")
26
- @conf.vite_build
24
+ if @conf.component_changed?(component_paths[:path], debug?, component_paths[:component_basename])
25
+ @conf.debug_log(debug?, component_paths[:component_basename], "Rebuilding assets...") do
26
+ @conf.vite_build
27
+ end
27
28
  end
28
29
 
29
30
  end
@@ -71,24 +72,18 @@ module SvelteOnRails
71
72
  def log_completed(message)
72
73
  ms = ((Time.now - @start_time) * 1000)
73
74
  @conf.request_metrics[:total_time] += ms
74
- Rails.logger.info " [SvelteOnRails] #{message} (#{ms.round(1)}ms)"
75
+ Rails.logger.info " [svelte-on-rails:#{component_paths[:component_basename]}] #{message} (#{ms.round(1)}ms)"
75
76
 
76
77
  end
77
78
 
78
- def debug_log(message)
79
- if debug?
80
- Rails.logger.debug(" [SvelteOnRails:debug] #{message}")
81
- end
82
- end
83
-
84
79
  def render_cached(view_context, &block)
85
80
 
86
81
  # debug
87
82
 
88
83
  if debug?
89
84
 
90
- debug_log("Rendering #{component_paths[:component]}")
91
- debug_log("Redis configuration: #{@conf.redis_cache_store}")
85
+ @conf.debug_log(true, component_paths[:component_basename], "Rendering...")
86
+ @conf.debug_log(true, component_paths[:component_basename], "Redis configuration: #{@conf.redis_cache_store}")
92
87
  ttl = conf.redis_instance.ttl(@cache_key)
93
88
 
94
89
  key_stat = if @cached_content.present?
@@ -103,7 +98,7 @@ module SvelteOnRails
103
98
  ", ttl was: #{ttl} seconds, now set to: #{redis_expiration_seconds} seconds"
104
99
  end
105
100
 
106
- debug_log("Cache key: «#{@cache_key}» (#{key_stat}#{ttl_stat})")
101
+ @conf.debug_log(true, component_paths[:component_basename], "Cache key: «#{@cache_key}» (#{key_stat}#{ttl_stat})")
107
102
 
108
103
  end
109
104
 
@@ -114,13 +109,13 @@ module SvelteOnRails
114
109
  # render
115
110
 
116
111
  res = if @cached_content
117
- log_message = "Returned #{component_paths[:component]} from cache"
112
+ log_message = "Returned from cache"
118
113
  @cached_content.html_safe
119
114
  else
120
- log_message = "Rendered #{component_paths[:component]} and stored to cache"
121
- debug_log("Cache recalculating for key: #{@cache_key}")
115
+ log_message = "Rendered and stored to cache"
116
+ @conf.debug_log(debug?, component_paths[:component_basename], "Cache recalculating for key: #{@cache_key}")
122
117
  r = view_context.instance_eval(&block)
123
- debug_log("Cache recalculated")
118
+ @conf.debug_log(debug?, component_paths[:component_basename], "Cache recalculated")
124
119
 
125
120
  @conf.redis_instance.set(@cache_key, r)
126
121
  r
@@ -133,7 +128,7 @@ module SvelteOnRails
133
128
 
134
129
  def render(view_context, &block)
135
130
 
136
- debug_log("Rendering component #{component_paths[:component]}")
131
+ @conf.debug_log(debug?, component_paths[:component_basename], "Rendering ...")
137
132
 
138
133
  r = view_context.instance_eval(&block)
139
134
 
@@ -150,7 +145,7 @@ module SvelteOnRails
150
145
  end
151
146
  p = component_paths
152
147
 
153
- # validate file-path
148
+ # validate filename
154
149
 
155
150
  unless p[:path].present?
156
151
  raise <<~TEXT
@@ -158,12 +153,19 @@ module SvelteOnRails
158
153
 
159
154
  TEXT
160
155
  end
156
+ unless p[:component_basename].match?(/^[A-Z][A-Za-z0-9]+$/)
157
+ raise <<~TEXT
158
+ [svelte-on-rails] Invalid filename: «#{p[:component_basename]}»
159
+ Component names must be CamelCased.
160
+
161
+ TEXT
162
+ end
161
163
 
162
164
  # validate manifest.json matching to file-path derived from component-name
163
165
 
164
- unless @conf.manifest[p[:path]]
166
+ unless @conf.manifest(debug?, p[:component_basename])[p[:path]]
165
167
  @conf.vite_build
166
- unless @conf.manifest[p[:path]]
168
+ unless @conf.manifest(debug?, p[:component_basename])[p[:path]]
167
169
  raise <<~TEXT
168
170
  [svelte-on-rails] No manifest found for component: «#{p[:path]}»
169
171
  Available keys in current manifest.json are:
@@ -207,7 +209,7 @@ module SvelteOnRails
207
209
 
208
210
  def fetch_cached_content(component, controller_path, props, options, html_options)
209
211
 
210
- fingerprint = ( @conf.watch_changes? ? @conf.fingerprint(component_paths[:path]) : '' )
212
+ fingerprint = (@conf.watch_changes? ? @conf.fingerprint(component_paths[:path], debug?, component_paths[:component_basename]) : '')
211
213
 
212
214
  cache_key_raw = controller_path + component +
213
215
  props.to_s + options.to_s + html_options.to_s +
@@ -16,10 +16,10 @@ module SvelteOnRails
16
16
  end
17
17
 
18
18
  # go recursively through the entrypoints subtree and check if any of them changed
19
- def component_changed?(component_path)
19
+ def component_changed?(component_path, debug, component_basename)
20
20
  @component_files ||= {}
21
21
  @debug_double_files = []
22
- paths = @component_files[component_path] ||= fetch_source_files(component_path)
22
+ paths = @component_files[component_path] ||= fetch_source_files(component_path, debug, component_basename)
23
23
  paths.each do |file|
24
24
  mtime = File.mtime(file).to_f.round(3)
25
25
  if mtime > last_build
@@ -29,13 +29,15 @@ module SvelteOnRails
29
29
  false
30
30
  end
31
31
 
32
- def fingerprint(component_path)
32
+ def fingerprint(component_path, debug, component_basename)
33
33
  # We do not cache this as this function is only for development.
34
34
  # Therefore, it is fast enough.
35
- build_fingerprint(component_path)
35
+ debug_log(debug, component_basename, "Building fingerprint") do
36
+ build_fingerprint(component_path, debug, component_basename)
37
+ end
36
38
  end
37
39
 
38
- def manifest
40
+ def manifest(debug, component_basename)
39
41
 
40
42
  # ensure we have a current manifest loaded
41
43
 
@@ -43,13 +45,18 @@ module SvelteOnRails
43
45
  @manifest = {}
44
46
  end
45
47
 
46
- if !@manifest.present?
47
- @manifest_mtime = File.mtime(manifest_json_path).to_f.round(3)
48
- @manifest = JSON.parse(File.read(manifest_json_path))
49
- else
50
- @manifest
48
+ if @manifest.empty?
49
+ debug_log(
50
+ debug,
51
+ component_basename,
52
+ "Manifest loaded"
53
+ ) do
54
+ @manifest_mtime = File.mtime(manifest_json_path).to_f.round(3)
55
+ @manifest = JSON.parse(File.read(manifest_json_path))
56
+ end
51
57
  end
52
58
 
59
+ @manifest
53
60
  end
54
61
 
55
62
  private
@@ -71,19 +78,19 @@ module SvelteOnRails
71
78
  @last_build
72
79
  end
73
80
 
74
- def fetch_source_files(component, files = [])
75
- _manifest = manifest[component]
81
+ def fetch_source_files(component, debug, component_basename, files = [])
82
+ _manifest = manifest(debug, component_basename)[component]
76
83
  raise "ERROR: Could not find manifest entry for «#{component}»\n\nmanifest:\n+++\n#{manifest}\n+++" unless _manifest
77
84
 
78
85
  if _manifest['src']
79
86
  add_file(files, rails_root.join(_manifest['src']).to_s)
80
- add_files(files, chunk_files(component))
87
+ add_files(files, chunk_files(component, debug, component_basename))
81
88
  elsif _manifest['file']
82
- add_files(files, chunk_files(component))
89
+ add_files(files, chunk_files(component, debug, component_basename))
83
90
  end
84
91
 
85
92
  _manifest['imports'].to_a.each do |import|
86
- files = fetch_source_files(import, files)
93
+ files = fetch_source_files(import, debug, component_basename, files)
87
94
  end
88
95
 
89
96
  files
@@ -107,19 +114,19 @@ module SvelteOnRails
107
114
  end
108
115
  end
109
116
 
110
- def chunk_files(component_path)
111
- asset_file = manifest[component_path]['file']
117
+ def chunk_files(component_path, debug, component_basename)
118
+ asset_file = manifest(debug, component_basename)[component_path]['file']
112
119
  dev_modules_map[asset_file].to_a
113
120
  end
114
121
 
115
- def build_fingerprint(component, str = '')
116
- _manifest = manifest[component]
122
+ def build_fingerprint(component, debug, component_basename, str = '')
123
+ _manifest = manifest(debug, component_basename)[component]
117
124
  raise "ERROR: Could not find manifest entry for «#{component}»" unless _manifest
118
125
  _str = _manifest['file'].split(_manifest['name']).last
119
126
  str << _str[0, _str.rindex('.')]
120
127
 
121
128
  _manifest['imports'].to_a.each do |import|
122
- build_fingerprint(import, str)
129
+ build_fingerprint(import, debug, component_basename, str)
123
130
  end
124
131
 
125
132
  str
@@ -6,13 +6,14 @@ module SvelteOnRails
6
6
  require 'base64'
7
7
  require 'json'
8
8
 
9
- def self.render(component_path, props)
9
+ def self.render(paths, props, debug)
10
10
  utils = SvelteOnRails::Lib::Utils
11
11
  cnf = SvelteOnRails::Configuration.instance
12
- manifest = cnf.manifest[component_path]
12
+ mn = cnf.manifest(debug, paths[:component_basename])
13
+ manifest = mn[paths[:path]]
13
14
 
14
15
  if manifest.nil?
15
- raise "[svelte-on-rails:Renderer.render] ERROR: No manifest found for component: «#{component_path}»\nAvailable keys in current manifest.json are:\n\n+++\n • #{cnf.manifest.keys.join("\n • ")}\n+++\n."
16
+ raise "[svelte-on-rails:#{paths[:component_basename]}] ERROR: No manifest found!\nAvailable keys in current manifest.json are:\n\n+++\n • #{cnf.manifest.keys.join("\n • ")}\n+++\n."
16
17
  end
17
18
 
18
19
  cmd = [
@@ -30,7 +31,7 @@ module SvelteOnRails
30
31
  ary = stdout.split('[svelte-on-rails:successful-json-response]')
31
32
 
32
33
  unless ary.length == 2
33
- raise "[svelte-on-rails] render ERROR for component: #{component_path}\n\ncommand:\n+++\n#{cmd}\n+++\n\nstdout:\n+++\n#{stdout}+++\n\n\nstderr:\n+++\n#{stderr}+++"
34
+ raise "[svelte-on-rails:#{paths[:component_basename]}] RENDER ERROR\n\ncommand:\n+++\n#{cmd}\n+++\n\nstdout:\n+++\n#{stdout}+++\n\n\nstderr:\n+++\n#{stderr}+++"
34
35
  end
35
36
 
36
37
  begin
@@ -38,7 +39,7 @@ module SvelteOnRails
38
39
  res = JSON.parse(ary[1])
39
40
 
40
41
  unless status.to_s.match(/^pid [0-9]+ exit 0$/)
41
- cmp = "#{component_path} was returned «#{status.to_s}»\n\n"
42
+ cmp = "#{paths[:path]} was returned «#{status.to_s}»\n\n"
42
43
  msg = "#{cmp}output from render.js (stderr) =>\n+++\n" + stderr + "+++\n\nRender Svelte Server-side =>\n#{cmd}\n\n"
43
44
  utils.puts_warning(msg)
44
45
  end
@@ -32,7 +32,7 @@ module SvelteOnRails
32
32
 
33
33
  if support.ssr?
34
34
 
35
- ssr_result = SvelteOnRails::Renderer.render(support.component_paths[:path], props)
35
+ ssr_result = SvelteOnRails::Renderer.render(support.component_paths, props, support.debug?)
36
36
  content_tag(:div, support.tag_attributes(html_options, props)) do
37
37
  concat(ssr_result['head'].html_safe)
38
38
  concat(ssr_result['html'].sub('<!--[-->', '').sub('<!--]-->', '').html_safe)
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: 11.0.10
4
+ version: 12.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair