svelte-on-rails 0.0.41 → 0.0.43

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -58
  3. data/lib/generators/svelte_on_rails/install/install_generator.rb +25 -18
  4. data/lib/svelte-on-rails.rb +0 -1
  5. data/lib/svelte_on_rails/configuration.rb +42 -5
  6. data/lib/svelte_on_rails/installer/hello_world.rb +5 -1
  7. data/lib/svelte_on_rails/installer/npm.rb +1 -1
  8. data/lib/svelte_on_rails/lib/development_utils.rb +24 -11
  9. data/lib/svelte_on_rails/lib/utils.rb +37 -1
  10. data/lib/svelte_on_rails/renderer/renderer.rb +6 -1
  11. data/lib/svelte_on_rails/renderer/utils.js +2 -1
  12. data/lib/svelte_on_rails/view_helpers.rb +1 -3
  13. data/lib/tasks/svelte_on_rails_tasks.rake +25 -31
  14. data/templates/config_base/app/frontend/ssr/ssr.js +4 -0
  15. data/templates/config_base/vite-ssr.config.ts +133 -0
  16. data/templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-england.png +0 -0
  17. data/templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-face-smile-wink.svg +1 -0
  18. data/templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-switzerland.jpg +0 -0
  19. data/templates/rails_vite_hello_world/app/frontend/javascript/components/JavascriptImport.svelte +7 -0
  20. data/templates/rails_vite_hello_world/app/frontend/javascript/components/JpgImport.svelte +7 -0
  21. data/templates/rails_vite_hello_world/app/frontend/javascript/components/ParentWithChild.svelte +6 -0
  22. data/templates/rails_vite_hello_world/app/frontend/javascript/components/PngImport.svelte +7 -0
  23. data/templates/rails_vite_hello_world/app/frontend/javascript/components/SvelteOnRailsHelloWorld.svelte +2 -6
  24. data/templates/rails_vite_hello_world/app/frontend/javascript/components/SvgRawImport.svelte +7 -0
  25. data/templates/rails_vite_hello_world/app/frontend/javascript/components/sub/NestedComponent.svelte +1 -1
  26. data/templates/rails_vite_hello_world/app/frontend/javascript/nestedJavascript.js +4 -1
  27. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/_nav.html.erb +11 -0
  28. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/_styles.html.erb +16 -0
  29. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/backend_frontend_rendered.html.erb +37 -0
  30. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/index.html.erb +8 -4
  31. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/ssr_auto_rendered.html.erb +26 -0
  32. metadata +17 -10
  33. data/lib/svelte_on_rails/compiler/compile.js +0 -123
  34. data/lib/svelte_on_rails/compiler/compiler.rb +0 -120
  35. data/lib/svelte_on_rails/compiler/customPlugins.js +0 -60
  36. data/templates/rails_vite_hello_world/app/frontend/images/atom.svg +0 -1
  37. data/templates/rails_vite_hello_world/app/frontend/images/check-circle-green.png +0 -0
  38. data/templates/rails_vite_hello_world/app/frontend/images/svg.svg +0 -3
  39. data/templates/rails_vite_hello_world/app/frontend/javascript/components/Pug.svelte +0 -14
  40. /data/templates/{svelte_on_rails_vite_base → config_base}/app/frontend/initializers/svelte.js +0 -0
  41. /data/templates/{svelte_on_rails_vite_base → config_base}/config/svelte_on_rails.yml +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 683e0e4881fc72969224d9792ba06ff4049c19adbcf55bc8e0ba817ba4be48d4
4
- data.tar.gz: 6a9ed00119ddbdb2b9a095458b010150ecd938d751e9112b26629343bc782656
3
+ metadata.gz: 0c45b43d02daa6e2a67f661348deecb1819734e2f16855b0595089663825a9ba
4
+ data.tar.gz: 430a7f1f67145346e4e589d1d7a08c93a77260b5035c9c1b87dfaf7fba00fa79
5
5
  SHA512:
6
- metadata.gz: a166ff91777a7969f50874feb855c522aeac968dae0c6f1659b92ced9ac461683cbac34eee6f3999b961db6669732dbcbea41d3f659f621a3c198fc6b3204e76
7
- data.tar.gz: dd7c49785a50f484c936fbac6f5a8000fc46d6fc7165113c55a7e437b4b2ac20a05a1da4d9311b24d8757d307fa14061c35b2fdf607614502bd6ff24aa0ee948
6
+ metadata.gz: 96973f2cbf7f97e5e8ac886fd38335fe064ab75a6ad06e06a50a85aa6a2ca039a02bceb42beee754d60c33435898d9bea684ea83ac6812c9df75b6e948a4edbb
7
+ data.tar.gz: a1e53b966389e9d1f196fe931bb4842237f46c07ad6b4b44c06311f2333b0dbf9fe6c9c970b5bc0d292c4c1c16646058b69711b1497b96615905a7939cf2b954
data/README.md CHANGED
@@ -8,17 +8,8 @@ requests it provides a empty tag that will be mounted on the frontend by the ass
8
8
 
9
9
  This way svelte works perfectly together with turbo. You will never notice
10
10
  this unpleasant «blink» on the frontend and it is maximum performance
11
- optimized. SSR compilation is handled by rollup.
11
+ optimized. SSR compilation is handled by vite.
12
12
 
13
- Server-side rendering is the bottleneck on such a pipeline.
14
- Although rollup is powerful, this gem is in an early state
15
- and there may be limitations.
16
-
17
- Javascript is written in ESM syntax, orientated by the functionality of vite.
18
- Although the common-js plugin is installed, I have not tested it,
19
- so for example `require` may not work in Svelte components.
20
-
21
- But we have done everything we can to make your setup work smoothly.
22
13
 
23
14
  This all is developed on Rails-7 together with `vite_rails`.
24
15
 
@@ -31,9 +22,11 @@ If you have issues, please open one and contributors are welcome!
31
22
 
32
23
  - actual node installed on the server
33
24
  - tested on ruby 3.2.2 and rails 7.1
25
+ - vite_rails (the installer will install it by option --full or --vite)
34
26
  - svelte v5 (see: [how to install svelte on rails/vite](https://dev.to/chmich/setup-inertia-and-svelte-on-rails-7-3glk))
35
27
  - turbo (recommended / [how to install turbo on rails](https://github.com/hotwired/turbo-rails?tab=readme-ov-file#installation))
36
28
  - if you use special packages (like pug) that requires commonjs, you may need
29
+ - npm on latest versions
37
30
 
38
31
  ## Installation from cero
39
32
 
@@ -154,21 +147,15 @@ Among others there are
154
147
 
155
148
  ### Precompile assets
156
149
 
157
- When the rails app runs `assets:precompile` a additional rake task is triggered: `rails svelte_on_rails:reset_and_compile_all`.
150
+ Usual vite has a `vite.config.ts` file, that is used for the client side precompilation.
158
151
 
159
- This is not absolutely necessary but it reduces the loading time of the very first calling of the first component.
160
- So, on deploying or doing `rails assets:precompile` you shoud see something like:
152
+ By running this installer it adds a npm runner so that you can do `npm run build:ssr`
153
+ which does the server side precompilation.
161
154
 
162
- ```
163
- --------------------------------------------------------------------------------
164
- compiled 1/3: javascript/components/Pug.svelte
165
- compiled 2/3: javascript/components/SvelteOnRailsHelloWorld.svelte
166
- compiled 3/3: javascript/components/sub/NestedComponent.svelte
167
- Svelte on Rails: Reset dist and compile-all executed
168
- --------------------------------------------------------------------------------
169
- ```
155
+ The same job is triggered alongside `rails assets:precompile` for production environments.
170
156
 
171
- on the console.
157
+ On development, when `watch_changes` is configured, the precompilation is triggered
158
+ after any `*.svelte` file within the configured `components_folder` changed.
172
159
 
173
160
  ## Option `ssr: :auto`
174
161
 
@@ -231,43 +218,23 @@ rails svelte_on_rails:add_hello_world
231
218
  rails svelte_on_rails:remove_hello_world
232
219
  ```
233
220
 
234
- [//]: # (```bash)
235
-
236
- [//]: # (rails svelte_on_rails:toggle_hello_world_svg)
221
+ ## Contributors Guide
237
222
 
238
- [//]: # (```)
223
+ Contributors welcome!
239
224
 
240
- [//]: # ()
241
- [//]: # (toggles the svg nested to the hello world component for check if thee svg is refreshed when imported image changes)
225
+ After downloaded the gem, please run the task
242
226
 
243
- ```bash
244
- rails svelte_on_rails:reset_and_compile_all
227
+ ```bash
228
+ rake svelte_on_rails:create_contributor_configs_file
245
229
  ```
230
+ and define a `generated_test_app_folder_path` (required) for apps, generated for the testings.
246
231
 
247
- This does the same step that ist triggered together with the
248
- `rails assets:precompile` step together with the deployment pipeline:
249
- it removes all contents of the svelte-on-rails compiled
250
- assets and compiles them all new.
251
-
252
- ## Performance
253
-
254
- Example from the rails console for a medium complex component
255
-
256
- - Compiled MyComponent.svelte.js: 0.411ms
257
- - => happens only once
258
- - Rendered MyComponent.svelte server-side: 0.518ms
259
- - => happens on every SSR request
260
- - Rendered MyComponent.svelte as empty element that will be mounted on the client side
261
- - => subsequent calls
262
- - Completed 200 OK in 521ms (Views: 520.2ms | ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.3ms)
263
-
264
- ## Contributors Guide
232
+ For development of the **npm package @csedl/svelte-on-rails** you can set the
233
+ `local_npm_package_path` (optional) and insert the path to the npm package on your local machine.
234
+ This will cause the installer, to install the npm package from a local path instead from the npm registry.
265
235
 
266
236
  Tests are based on the included templates, like the `hello world template` and on the installer.
267
237
 
268
- On the gem folder you can put a file `generated_test_app_path`, and put the path for a existing
269
- folder where the tests generate a test rails app.
270
-
271
238
  **Installer tests** starting with completely destroy the rails app within the `generated_test_app_path`,
272
239
  generating a new rails app and running the installer and test by `playwright` if the components are working.
273
240
 
@@ -293,13 +260,6 @@ The current test cases including (among others):
293
260
  create a completely new rails app, running the full installer and check if a hello World component is visible and javascript is working.
294
261
  run assets:precompile within a rails app and check if the gem does its precompiling too.
295
262
 
296
- For development of the **npm package @csedl/svelte-on-rails** you can add the file
297
- `local_npm_package_path` and insert the path to the npm package on your local machine.
298
- This will cause the installer, not to install the npm package from npm, but to use the local one.
299
-
300
- ‼️ **ATTENTION:** Please do never commit the file `local_npm_package_path` to the repo! This is only for developers!
301
- Otherwise a user would, on running the installer, get an error, because the npm package is not found.
302
-
303
263
  ## Licence
304
264
 
305
265
  License is MIT
@@ -10,6 +10,7 @@ module SvelteOnRails
10
10
  class_option :haml, type: :boolean, default: false, desc: "Use Haml"
11
11
  class_option :turbo, type: :boolean, default: false, desc: "Use @hotwired/turbo-rails"
12
12
  class_option :svelte, type: :boolean, default: false, desc: "Install Svelte"
13
+ class_option :pug, type: :boolean, default: false, desc: "Install Pug"
13
14
  class_option :hello_world, type: :boolean, default: false, desc: "Create Hello World component"
14
15
 
15
16
  require 'svelte_on_rails/installer/utils'
@@ -52,29 +53,35 @@ module SvelteOnRails
52
53
 
53
54
  utils = SvelteOnRails::Installer::Utils
54
55
  npm_i = SvelteOnRails::Installer::Npm
55
- utils.write_templates(['svelte_on_rails_vite_base'], ask_for_overwrite: !options[:force])
56
+ utils.write_templates(['config_base'], ask_for_overwrite: !options[:force])
57
+
58
+ # add script to package json
59
+ pkg_js = JSON.parse(File.read('package.json'))
60
+ pkg_js['scripts'] ||= { 'build:ssr': "vite build --config vite-ssr.config.ts" }
61
+ File.write('package.json', JSON.pretty_generate(pkg_js))
56
62
 
57
63
  if @local_npm_package_url
58
- puts '☝️ <<<'
59
- puts 'LINK LOCAL PACKAGE'
60
- puts 'BUGFIX: npm does not install peer dependencies of local linked packages!'
61
- puts ' For packages from the registry, this does since newer versins of npm.'
62
- puts ' So we need to install the peer dependencies manually.'
63
- npm_i.install_or_update_package('rollup-plugin-svelte', dev_dependency: true)
64
- npm_i.install_or_update_package('rollup', dev_dependency: true)
65
- npm_i.install_or_update_package('@rollup/plugin-node-resolve', dev_dependency: true)
66
- npm_i.install_or_update_package('@rollup/plugin-commonjs', dev_dependency: true)
67
- npm_i.install_or_update_package('@rollup/plugin-url', dev_dependency: true)
68
- npm_i.install_or_update_package('rollup-plugin-css-only', dev_dependency: true)
69
- npm_i.install_or_update_package('rollup-plugin-string', dev_dependency: true)
70
- npm_i.install_or_update_package('svelte-preprocess', dev_dependency: true)
71
- npm_i.install_or_update_package('pug', dev_dependency: true)
72
- npm_i.install_or_update_package('typescript', dev_dependency: true)
73
- puts '☝️ >>>'
74
- npm_i.link_local_package(NPM_PACKAGE_NAME, @local_npm_package_url)
64
+ # puts '☝️ <<<'
65
+ # puts 'LINK LOCAL PACKAGE'
66
+ # puts 'BUGFIX: npm does not install peer dependencies of local linked packages!'
67
+ # puts ' For packages from the registry, this does since newer versins of npm.'
68
+ # puts ' So we need to install the peer dependencies manually.'
69
+ # npm_i.install_or_update_package('rollup-plugin-svelte', dev_dependency: true)
70
+ # npm_i.install_or_update_package('rollup', dev_dependency: true)
71
+ # npm_i.install_or_update_package('@rollup/plugin-node-resolve', dev_dependency: true)
72
+ # npm_i.install_or_update_package('@rollup/plugin-commonjs', dev_dependency: true)
73
+ # npm_i.install_or_update_package('@rollup/plugin-url', dev_dependency: true)
74
+ # npm_i.install_or_update_package('rollup-plugin-css-only', dev_dependency: true)
75
+ # npm_i.install_or_update_package('rollup-plugin-string', dev_dependency: true)
76
+ # npm_i.install_or_update_package('svelte-preprocess', dev_dependency: true)
77
+ # npm_i.install_or_update_package('pug', dev_dependency: true) # must be there because compile.js on the backend imports is
78
+ # puts '☝️ >>>'
79
+ npm_i.link_local_package(NPM_PACKAGE_NAME, @local_npm_package_url)
75
80
  else
76
81
  npm_i.install_or_update_package(NPM_PACKAGE_NAME)
77
82
  end
83
+ npm_i.install_or_update_package('typescript')
84
+ npm_i.install_or_update_package('@types/node')
78
85
 
79
86
  # insert_initializer
80
87
  #uts = SvelteOnRails::Installer::Utils
@@ -1,7 +1,6 @@
1
1
  require "svelte_on_rails/configuration"
2
2
  require "svelte_on_rails/view_helpers"
3
3
  require "svelte_on_rails/renderer/renderer"
4
- require "svelte_on_rails/compiler/compiler"
5
4
  require "svelte_on_rails/lib/utils"
6
5
  require "svelte_on_rails/lib/initializable"
7
6
  require "svelte_on_rails/railtie" if defined?(Rails)
@@ -34,21 +34,46 @@ module SvelteOnRails
34
34
  end
35
35
 
36
36
  def frontend_folder
37
- @configs['frontend_folder']
37
+ Pathname.new(@configs['frontend_folder'].to_s)
38
38
  end
39
39
 
40
40
  def frontend_folder_full
41
41
  rails_root.join(@configs['frontend_folder'].to_s)
42
42
  end
43
43
 
44
+ def components_folder
45
+ Pathname.new(@configs['components_folder'].to_s)
46
+ end
47
+
44
48
  def components_folder_full
45
49
  Pathname.new(frontend_folder_full).join(components_folder.to_s)
46
50
  end
47
51
 
48
- def components_folder
49
- @configs['components_folder']
52
+ def assets_folder
53
+ dist_folder.join('assets')
54
+ end
55
+
56
+ def ssr_manifest
57
+ file = rails_root.join('public', 'vite-ssr', 'manifest.json')
58
+
59
+ if watch_changes?
60
+ begin
61
+ JSON.parse(File.read(file))
62
+ rescue
63
+ raise "ERROR: Could not read public/vite-ssr/manifest.json."
64
+ end
65
+ else
66
+ @manifest ||= JSON.parse(File.read(file))
67
+ end
68
+
50
69
  end
51
70
 
71
+ # def manifest=(manifest_hash)
72
+ # file = dist_folder.join('manifest.json')
73
+ # @manifest = manifest_hash
74
+ # File.write(file, JSON.pretty_generate(manifest_hash))
75
+ # end
76
+
52
77
  def ssr
53
78
  rss = @configs['ssr']
54
79
  if rss == false || rss == :auto
@@ -58,8 +83,20 @@ module SvelteOnRails
58
83
  end
59
84
  end
60
85
 
61
- def dist_folder(app_root = nil)
62
- rails_root(app_root).join('public', 'svelteDist')
86
+ # def dist_folder(app_root = nil)
87
+ # rails_root(app_root).join('public', 'svelteDist')
88
+ # end
89
+
90
+ def client_dist_folder(app_root = nil)
91
+ if Rails.env.development?
92
+ rails_root(app_root).join('public', 'vite')
93
+ else
94
+ rails_root(app_root).join('public', 'vite')
95
+ end
96
+ end
97
+
98
+ def ssr_dist_folder(app_root = nil)
99
+ rails_root(app_root).join('public', 'vite-ssr')
63
100
  end
64
101
 
65
102
  def system_type
@@ -13,6 +13,11 @@ module SvelteOnRails
13
13
  # route
14
14
 
15
15
  route = 'svelte_on_rails_hello_world#index'
16
+
17
+ utils_i.add_route(" get \"#{route.sub('#', '/')}\"", app_root: app_root)
18
+ utils_i.add_route(" get \"svelte_on_rails_hello_world/backend_frontend_rendered\"", app_root: app_root)
19
+ utils_i.add_route(" get \"svelte_on_rails_hello_world/ssr_auto_rendered\"", app_root: app_root)
20
+
16
21
  rr = utils_i.which_root_route(app_root)
17
22
  root_url = "/"
18
23
  url = root_url + route.sub('#', '/')
@@ -24,7 +29,6 @@ module SvelteOnRails
24
29
  puts "route «#{route}» already exists, skipping."
25
30
  url
26
31
  elsif rr
27
- utils_i.add_route(" get \"#{route.sub('#', '/')}\"", app_root: app_root)
28
32
  url
29
33
  else
30
34
  utils_i.add_route(' root "svelte_on_rails_hello_world#index"', app_root: app_root)
@@ -68,7 +68,7 @@ module SvelteOnRails
68
68
  if pkg
69
69
  {
70
70
  type: pkg.match(/^.*?(?=\/[^\/]*$)/).to_s,
71
- version: version.split('.').map(&:to_i)
71
+ version: version&.split('.')&.map(&:to_i)
72
72
  }
73
73
  end
74
74
 
@@ -2,18 +2,15 @@ module SvelteOnRails
2
2
  class DevelopmentUtils
3
3
 
4
4
  def self.local_npm_package_url
5
- config_file = gem_folder.join('local_npm_package_path')
6
- if !File.exist?(config_file)
7
- return nil
5
+ str = contributor_config('local_npm_package_path', required: false)
6
+ if !str.present?
7
+ nil
8
+ elsif !Dir.exist?(str)
9
+ raise "Invalid path given on local_npm_package_path: «#{str}»"
10
+ elsif !File.exist?(Pathname.new(str).join('package.json'))
11
+ raise "Given local_npm_package_path does not seem to be a valid npm package as it does not contain a package.json"
8
12
  else
9
- str = File.read(config_file).strip
10
- if !Dir.exist?(str)
11
- raise "Invalid path given on local_npm_package_path: «#{str}»"
12
- elsif !File.exist?(Pathname.new(str).join('package.json'))
13
- raise "Given local_npm_package_path does not seem to be a valid npm package as it does not contain a package.json"
14
- else
15
- return Pathname.new(str)
16
- end
13
+ return Pathname.new(str)
17
14
  end
18
15
  end
19
16
 
@@ -21,5 +18,21 @@ module SvelteOnRails
21
18
  Pathname.new(File.expand_path('../../..', __dir__))
22
19
  end
23
20
 
21
+ def self.contributor_config(key, required: true)
22
+ config_file = File.expand_path('../svelte_on_rails_contributor_configs.yml', gem_folder)
23
+ if !File.exist?(config_file)
24
+ if required
25
+ raise "Missing configuration file, searched at: «#{config_file}»"
26
+ end
27
+ else
28
+ yml = YAML.load_file(config_file)
29
+ value = yml[key.to_s]
30
+ if required && !value.present?
31
+ raise "Missing value «#{key}» in configuration file: «#{config_file}»"
32
+ end
33
+ value
34
+ end
35
+ end
36
+
24
37
  end
25
38
  end
@@ -6,6 +6,13 @@ module SvelteOnRails
6
6
  File.expand_path('../../svelte_on_rails', __dir__) + '/'
7
7
  end
8
8
 
9
+ def self.asset_path(filename)
10
+ config = SvelteOnRails::Configuration.instance
11
+ path = (config.frontend_folder + config.components_folder).join(filename)
12
+ manifest = config.ssr_manifest
13
+ manifest[path.to_s.sub(/\.svelte$/, '') + '.svelte']['file']
14
+ end
15
+
9
16
  def self.file_exist_case_sensitive?(containing_dir, filename)
10
17
  # Combine the directory path and filename
11
18
  full_path = File.join(containing_dir, filename)
@@ -21,7 +28,8 @@ module SvelteOnRails
21
28
  svelte_file = (base_path + fn).to_s + '.svelte'
22
29
  svelte_filename = fn + '.svelte'
23
30
  cnf = SvelteOnRails::Configuration.instance
24
- cf = cnf.dist_folder + cnf.components_folder + fn
31
+ cf = cnf.rails_root.join('public', 'vite-ssr', asset_path(filename).sub(/.js$/, ''))
32
+ a = 1
25
33
 
26
34
  {
27
35
  svelte_file: svelte_file,
@@ -30,6 +38,34 @@ module SvelteOnRails
30
38
  }
31
39
  end
32
40
 
41
+ def self.watch_changes_and_precompile
42
+ config = SvelteOnRails::Configuration.instance
43
+ return unless config.watch_changes?
44
+
45
+ mtime = Dir[File.join(config.components_folder_full, '**/*.svelte')].map do |file|
46
+ File.mtime(file).to_f
47
+ end.max || 0.0
48
+ mtime_path = config.ssr_dist_folder.join('last_mtime')
49
+ last = (File.exist?(mtime_path) ? File.read(mtime_path).to_f : 0.0)
50
+
51
+ if mtime > last
52
+ precompile(mtime)
53
+ end
54
+
55
+ end
56
+
57
+ def self.precompile(last_mtime = nil)
58
+ config = SvelteOnRails::Configuration.instance
59
+ Dir.chdir(config.rails_root) do
60
+ cmd = "./node_modules/.bin/vite build --config vite-ssr.config.ts"
61
+ puts `#{cmd}`
62
+ end
63
+ if last_mtime
64
+ mtime_path = config.ssr_dist_folder.join('last_mtime')
65
+ File.write(mtime_path, last_mtime.to_s)
66
+ end
67
+ end
68
+
33
69
  end
34
70
  end
35
71
  end
@@ -5,8 +5,13 @@ module SvelteOnRails
5
5
 
6
6
  def initialize(compiled_file: nil, component_name: nil, base_path: SvelteOnRails::Configuration.instance.components_folder_full)
7
7
 
8
+ config = SvelteOnRails::Configuration.instance
9
+ if config.watch_changes?
10
+ SvelteOnRails::Lib::Utils.watch_changes_and_precompile
11
+ end
12
+
8
13
  @compiled_file = if compiled_file
9
- compiled_file
14
+ compiled_file.sub(/\.svelte$/, '')
10
15
  elsif component_name
11
16
  utils = SvelteOnRails::Lib::Utils
12
17
  cf = utils.component_files(component_name, base_path: base_path)
@@ -17,7 +17,8 @@ export async function loadComponentModule(compiledFile) {
17
17
  const module = await import(modulePath);
18
18
  return module.default;
19
19
  } catch (error) {
20
- console.error(`Error loading component from ${compiledFile}:`, error);
20
+ console.error(`=> compiledFile: «${compiledFile}»`);
21
+ console.error(`[loadComponentModule] Error loading component from ${compiledFile}:`, error);
21
22
  process.exit(1);
22
23
  }
23
24
  }
@@ -56,9 +56,7 @@ module SvelteOnRails
56
56
  # render server side
57
57
 
58
58
  start_time = Time.now
59
- comp = SvelteOnRails::Compiler.new(filename)
60
- comp.compile_if_changes
61
- rend = SvelteOnRails::Renderer.new(compiled_file: comp.compiled_file)
59
+ rend = SvelteOnRails::Renderer.new(component_name: filename)
62
60
  res = rend.render(props)
63
61
  time = Time.now - start_time
64
62
  Rails.logger.info " Rendered #{filename}.svelte server-side: #{time.round(3)}ms"
@@ -1,8 +1,12 @@
1
1
  if defined?(Rails) && Rake::Task.task_defined?("assets:precompile")
2
2
  Rake::Task["assets:precompile"].enhance do
3
3
  puts '-' * 80
4
- SvelteOnRails::Compiler.reset_and_compile_all
5
- puts "Svelte on Rails: Reset dist and compile-all executed"
4
+ Dir.chdir(Rails.root) do
5
+ cmd = "npm run build:ssr"
6
+ puts cmd
7
+ puts '-' * 80
8
+ puts `#{cmd}`
9
+ end
6
10
  puts '-' * 80
7
11
  end
8
12
  end
@@ -34,41 +38,31 @@ namespace :svelte_on_rails do
34
38
 
35
39
  end
36
40
 
37
- desc "Toggle the svg, included in the Hello World component for checking if it is refreshed on the browser"
38
- task :toggle_hello_world_svg do
39
-
40
- utils_i = SvelteOnRails::Installer::Utils
41
-
42
- # write templates
41
+ desc "For contributors to this gem: writes a config file for developing options."
42
+ task :create_contributor_configs_file do
43
+ path = File.expand_path('../../../svelte_on_rails_contributor_configs.yml', __dir__)
43
44
 
44
- utils_i.write_templates(toggle_hello_world_files, ask_for_overwrite: false)
45
-
46
- puts '+++'
47
-
48
- toggler = File.expand_path('../svelte_on_rails/installer/rails_vite_template', __dir__) + '/hello_world_toggler'
49
- if File.exist?(toggler)
50
- File.delete(toggler)
51
- File.delete('app/frontend/images/svg.svg')
52
- FileUtils.mv('app/frontend/images/atom.svg', 'app/frontend/images/svg.svg')
53
- File.delete('app/frontend/javascript/nestedJavascript.js')
54
- FileUtils.mv('app/frontend/javascript/nestedJavascriptToggled.js', 'app/frontend/javascript/nestedJavascript.js')
55
- puts "toggled svg.svg and nestedJavascriptToggled.js"
56
- else
57
- puts "toggled svg.svg and nestedJavascriptToggled.js back to original"
58
- FileUtils.touch(toggler)
45
+ if File.exist?(path)
46
+ puts "Config file for Contributors already exists: file://#{path}"
47
+ puts 'nothing done.'
48
+ exit
59
49
  end
60
50
 
61
- puts '+++'
51
+ content = <<-END
52
+ #local_npm_package_path: /path/to/my/local/csedl-svelte-on-rails-main
62
53
 
63
- puts "=> The Svelte components are living within the components folder"
64
- puts "=> The hereby toggled files are outside of the components folder, but within the frontend folder"
65
- puts "=> this toggling should work because the whole javascript folder is watched for changes (if configured so)"
54
+ # optional
55
+ # If you want to develop the @csedl/svelte-on-rails package
56
+ # add full path like /path/to/csedl-svelte-on-rails-main
57
+ # Then, on running the generator, it installs this package from your local root folder
66
58
 
67
- end
59
+ generated_test_app_folder_path: /destination/folder/for/generated/test/app
68
60
 
69
- desc "Compile all Svelte components"
70
- task :reset_and_compile_all do
71
- SvelteOnRails::Compiler.reset_and_compile_all
61
+ # required!
62
+ # When you run tests, a rails app will be generated into this folder
63
+ END
64
+ File.write(path, content)
65
+ puts "Config file for Contributors created at: #{path}"
72
66
  end
73
67
 
74
68
  end
@@ -0,0 +1,4 @@
1
+ // Placeholder SSR entrypoint for vite-plugin-ruby
2
+ export default function () {
3
+ // No-op
4
+ }