svelte-on-rails 0.0.42 → 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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -52
  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/lib/utils.rb +37 -1
  8. data/lib/svelte_on_rails/renderer/renderer.rb +6 -1
  9. data/lib/svelte_on_rails/renderer/utils.js +2 -1
  10. data/lib/svelte_on_rails/view_helpers.rb +1 -3
  11. data/lib/tasks/svelte_on_rails_tasks.rake +6 -39
  12. data/templates/config_base/app/frontend/ssr/ssr.js +4 -0
  13. data/templates/config_base/vite-ssr.config.ts +133 -0
  14. data/templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-england.png +0 -0
  15. data/templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-face-smile-wink.svg +1 -0
  16. data/templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-switzerland.jpg +0 -0
  17. data/templates/rails_vite_hello_world/app/frontend/javascript/components/JavascriptImport.svelte +7 -0
  18. data/templates/rails_vite_hello_world/app/frontend/javascript/components/JpgImport.svelte +7 -0
  19. data/templates/rails_vite_hello_world/app/frontend/javascript/components/ParentWithChild.svelte +6 -0
  20. data/templates/rails_vite_hello_world/app/frontend/javascript/components/PngImport.svelte +7 -0
  21. data/templates/rails_vite_hello_world/app/frontend/javascript/components/SvelteOnRailsHelloWorld.svelte +2 -6
  22. data/templates/rails_vite_hello_world/app/frontend/javascript/components/SvgRawImport.svelte +7 -0
  23. data/templates/rails_vite_hello_world/app/frontend/javascript/components/sub/NestedComponent.svelte +1 -1
  24. data/templates/rails_vite_hello_world/app/frontend/javascript/nestedJavascript.js +4 -1
  25. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/_nav.html.erb +11 -0
  26. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/_styles.html.erb +16 -0
  27. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/backend_frontend_rendered.html.erb +37 -0
  28. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/index.html.erb +8 -4
  29. data/templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/ssr_auto_rendered.html.erb +26 -0
  30. metadata +17 -10
  31. data/lib/svelte_on_rails/compiler/compile.js +0 -123
  32. data/lib/svelte_on_rails/compiler/compiler.rb +0 -120
  33. data/lib/svelte_on_rails/compiler/customPlugins.js +0 -60
  34. data/templates/rails_vite_hello_world/app/frontend/images/atom.svg +0 -1
  35. data/templates/rails_vite_hello_world/app/frontend/images/check-circle-green.png +0 -0
  36. data/templates/rails_vite_hello_world/app/frontend/images/svg.svg +0 -3
  37. data/templates/rails_vite_hello_world/app/frontend/javascript/components/Pug.svelte +0 -14
  38. /data/templates/{svelte_on_rails_vite_base → config_base}/app/frontend/initializers/svelte.js +0 -0
  39. /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: c46f6cb43c90617bc4798072b58a35e81a311886a858c379224216fc63c03d3e
4
- data.tar.gz: 2bd3286eac9420225ac3f641d1cf783a84f48f575d69c0bce53a9b1da9d9d262
3
+ metadata.gz: 0c45b43d02daa6e2a67f661348deecb1819734e2f16855b0595089663825a9ba
4
+ data.tar.gz: 430a7f1f67145346e4e589d1d7a08c93a77260b5035c9c1b87dfaf7fba00fa79
5
5
  SHA512:
6
- metadata.gz: 9e4ae887620aaabd737fc30864bed6eea15dcc9bb47e6ef9db4ab1e38f0ea96a1643b2f12cd81aeadf2c636e6e1575920da2efca62886e877a71dbe25b2818a2
7
- data.tar.gz: 821084886d515faf8e68440a001bc4a3fd5d310c6068efbb1578ca558774f0ad731e5cf13f142b3223db7e5a2b380775319de9ce3e75a54e597ff59ba1a505cc
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,36 +218,6 @@ 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)
237
-
238
- [//]: # (```)
239
-
240
- [//]: # ()
241
- [//]: # (toggles the svg nested to the hello world component for check if thee svg is refreshed when imported image changes)
242
-
243
- ```bash
244
- rails svelte_on_rails:reset_and_compile_all
245
- ```
246
-
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
221
  ## Contributors Guide
265
222
 
266
223
  Contributors welcome!
@@ -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)
@@ -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,43 +38,6 @@ 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
43
-
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)
59
- end
60
-
61
- puts '+++'
62
-
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)"
66
-
67
- end
68
-
69
- desc "Compile all Svelte components"
70
- task :reset_and_compile_all do
71
- SvelteOnRails::Compiler.reset_and_compile_all
72
- end
73
-
74
41
  desc "For contributors to this gem: writes a config file for developing options."
75
42
  task :create_contributor_configs_file do
76
43
  path = File.expand_path('../../../svelte_on_rails_contributor_configs.yml', __dir__)
@@ -0,0 +1,4 @@
1
+ // Placeholder SSR entrypoint for vite-plugin-ruby
2
+ export default function () {
3
+ // No-op
4
+ }
@@ -0,0 +1,133 @@
1
+ import { defineConfig } from 'vite';
2
+ import RubyPlugin from 'vite-plugin-ruby';
3
+ import { svelte } from '@sveltejs/vite-plugin-svelte';
4
+ import * as fs from 'fs';
5
+ import * as path from 'path';
6
+ import { Plugin } from 'vite';
7
+
8
+ function rewriteAssetPathsPlugin(): Plugin {
9
+ const manifestPath = path.resolve('public/vite/manifest.json');
10
+ let manifest: Record<string, { file: string }> | null = null;
11
+
12
+ // Load client-side manifest if it exists (production)
13
+ if (fs.existsSync(manifestPath)) {
14
+ manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
15
+ }
16
+
17
+ return {
18
+ name: 'rewrite-asset-paths',
19
+ enforce: 'pre',
20
+ transform(code, id) {
21
+ if (id.endsWith('.svelte')) {
22
+ if (manifest) {
23
+ // Production: Rewrite asset paths using client-side manifest
24
+ let transformedCode = code;
25
+ for (const [assetKey, assetData] of Object.entries(manifest)) {
26
+ // Match assets like /assets/image.jpg, /assets/script.js, /assets/style.css
27
+ const assetRegex = new RegExp(`/assets/${assetKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`, 'g');
28
+ transformedCode = transformedCode.replace(assetRegex, `/vite/${assetData.file}`);
29
+ }
30
+ return transformedCode;
31
+ } else {
32
+ // Development: Fallback to /vite-dev/assets/ for all assets
33
+ return code.replace(/\/assets\//g, '/vite-dev/assets/');
34
+ }
35
+ }
36
+ return null;
37
+ },
38
+ };
39
+ }
40
+
41
+ // Function to recursively find all .svelte files
42
+ function findSvelteFiles(dir: string, baseDir: string = dir): Record<string, string> {
43
+ const entries: Record<string, string> = {};
44
+ const files = fs.readdirSync(dir, { withFileTypes: true });
45
+
46
+ for (const file of files) {
47
+ const fullPath = path.join(dir, file.name);
48
+ if (file.isDirectory()) {
49
+ // Recurse into subdirectories
50
+ Object.assign(entries, findSvelteFiles(fullPath, baseDir));
51
+ } else if (file.isFile() && file.name.endsWith('.svelte')) {
52
+ // Create entry for .svelte file
53
+ const relativePath = path.relative(baseDir, fullPath);
54
+ const name = relativePath.replace('.svelte', '').replace(/\//g, '-'); // e.g., components-MyComponent
55
+ entries[`svelte-${name}`] = fullPath;
56
+ }
57
+ }
58
+ return entries;
59
+ }
60
+
61
+ // Collect all .svelte files in app/frontend
62
+ const svelteComponents = findSvelteFiles(path.resolve('app/frontend'));
63
+
64
+ export default defineConfig(({ mode }) => {
65
+
66
+ const isProduction = (process.env.RAILS_ENV === 'development' || process.env.RAILS_ENV === 'test' ? false : true);
67
+
68
+ return {
69
+ base: isProduction ? '/vite/' : '/vite-dev/',
70
+ plugins: [
71
+ rewriteAssetPathsPlugin(),
72
+ {
73
+ ...RubyPlugin(),
74
+ apply(config, { command }) {
75
+ if (command === 'build') {
76
+ if (config.plugins) { // Add this check
77
+ config.plugins = config.plugins.filter((plugin) => {
78
+ return !!(plugin && typeof plugin === 'object' && 'name' in plugin && plugin.name !== 'vite-plugin-ruby:assets-manifest');
79
+ });
80
+ }
81
+ }
82
+ return true;
83
+ },
84
+ },
85
+ svelte({
86
+ include: ['app/frontend/**/*.svelte'],
87
+ compilerOptions: {
88
+ // hydratable: false,
89
+ },
90
+ }),
91
+ {
92
+ name: 'force-manifest',
93
+ configResolved(config) {
94
+ console.log('Resolved build.manifest:', config.build.manifest);
95
+ if (!config.build.manifest) {
96
+ config.build.manifest = 'manifest-ssr.json';
97
+ console.log('Forced build.manifest to manifest-ssr.json');
98
+ }
99
+ },
100
+ },
101
+ ],
102
+ build: {
103
+ ssr: true,
104
+ outDir: 'public/vite-ssr',
105
+ manifest: 'manifest.json',
106
+ rollupOptions: {
107
+ input: {
108
+ ...svelteComponents,
109
+ ssr: 'app/frontend/ssr/ssr.js',
110
+ },
111
+ output: {
112
+ format: 'es',
113
+ entryFileNames: 'assets/[name]-[hash].js',
114
+ chunkFileNames: 'assets/[name]-[hash].js',
115
+ assetFileNames: 'assets/[name]-[hash][extname]',
116
+ manualChunks(id) {
117
+ if (id.indexOf('.svelte') !== -1) {
118
+ const lastSegment = id.split('/').pop();
119
+ if (lastSegment) {
120
+ const componentName = lastSegment.replace('.svelte', '');
121
+ return `svelte-${componentName}`;
122
+ }
123
+ }
124
+ }
125
+ },
126
+ },
127
+ },
128
+ optimizeDeps: {
129
+ include: ['svelte'],
130
+ },
131
+ logLevel: 'info',
132
+ };
133
+ });
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Pro 6.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path d="M0 256C0 114.6 114.6 0 256 0C397.4 0 512 114.6 512 256C512 397.4 397.4 512 256 512C114.6 512 0 397.4 0 256zM164.1 325.5C158.3 318.8 148.2 318.1 141.5 323.9C134.8 329.7 134.1 339.8 139.9 346.5C162.1 372.1 200.9 400 255.1 400C311.1 400 349.8 372.1 372.1 346.5C377.9 339.8 377.2 329.7 370.5 323.9C363.8 318.1 353.7 318.8 347.9 325.5C329.9 346.2 299.4 368 255.1 368C212.6 368 182 346.2 164.1 325.5H164.1zM176.4 176C158.7 176 144.4 190.3 144.4 208C144.4 225.7 158.7 240 176.4 240C194 240 208.4 225.7 208.4 208C208.4 190.3 194 176 176.4 176zM300.8 233.6C318.4 210.1 353.6 210.1 371.2 233.6C376.5 240.7 386.5 242.1 393.6 236.8C400.7 231.5 402.1 221.5 396.8 214.4C366.4 173.9 305.6 173.9 275.2 214.4C269.9 221.5 271.3 231.5 278.4 236.8C285.5 242.1 295.5 240.7 300.8 233.6z"/></svg>
@@ -0,0 +1,7 @@
1
+ <script>
2
+ export let title;
3
+ import {nestedJavascriptFunction} from "../nestedJavascript.js";
4
+ </script>
5
+
6
+ <p>{title}</p>
7
+ <p>{nestedJavascriptFunction()}</p>
@@ -0,0 +1,7 @@
1
+ <script>
2
+ export let title;
3
+ import jpg from '../../images/svelte-on-rails-hello-world-switzerland.jpg'
4
+ </script>
5
+
6
+ <p>{title}</p>
7
+ <img alt="Jpg not loaded!" src="{jpg}" />
@@ -0,0 +1,6 @@
1
+ <script>
2
+ import Nested from './sub/NestedComponent.svelte'
3
+ export let title
4
+ </script>
5
+ <p>{title}</p>
6
+ <Nested />
@@ -0,0 +1,7 @@
1
+ <script>
2
+ export let title;
3
+ import png from '../../images/svelte-on-rails-hello-world-england.png'
4
+ </script>
5
+
6
+ <p>{title}</p>
7
+ <img alt="Png not loaded!" src="{png}" />
@@ -1,7 +1,5 @@
1
1
  <script>
2
- import svgRaw from '../../images/svg.svg?raw'
3
- //import svg from '../../images/svg.svg'
4
- import png from '../../images/check-circle-green.png'
2
+ import jpg from '../../images/svelte-on-rails-hello-world-switzerland.jpg'
5
3
  import Nested from './sub/NestedComponent.svelte'
6
4
  import {nestedJavascriptFunction} from '../nestedJavascript.js'
7
5
  export let items
@@ -27,9 +25,7 @@
27
25
  </ul>
28
26
 
29
27
  <hr/>
30
- <span class="wrap-svg">{@html svgRaw}</span>
31
- <!-- <img alt="svg ERROR" src={svg} />-->
32
- <img alt="png ERROR" src={png} />
28
+ <img alt="png ERROR" src={jpg} />
33
29
 
34
30
  <hr/>
35
31
  <Nested />
@@ -0,0 +1,7 @@
1
+ <script>
2
+ export let title;
3
+ import svgRaw from '../../images/svelte-on-rails-hello-world-face-smile-wink.svg?raw'
4
+ </script>
5
+
6
+ <p>{title}</p>
7
+ {@html svgRaw}
@@ -1 +1 @@
1
- <p>Nested Svelte Component</p>
1
+ <p class="nested-component">Nested Svelte Component</p>
@@ -1,3 +1,6 @@
1
+
2
+
1
3
  export function nestedJavascriptFunction() {
2
- return 'nestedJavascript!';
4
+ const randomHex = Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, '0');
5
+ return `JS result: #${randomHex}`;
3
6
  }
@@ -0,0 +1,11 @@
1
+ <div class="nav">
2
+ <%= link_to 'Hello World', '/svelte_on_rails_hello_world/index' %>
3
+ |
4
+ <%= link_to 'Backend/Frontend Test', '/svelte_on_rails_hello_world/backend_frontend_rendered' %>
5
+ |
6
+ <%= link_to 'SSR-Auto rendered (default)', '/svelte_on_rails_hello_world/ssr_auto_rendered' %>
7
+ </div>
8
+
9
+ <% turbo_id = request.headers['X-Turbo-Request-ID'] %>
10
+ <p class="<%= (turbo_id.present? ? 'turbo-request' : 'initial-request') %>"><%= (turbo_id.blank? ? 'This is a initial request' : "Turbo-Request-ID: #{turbo_id}") %></p>
11
+ <p>Rails.environment: <%= Rails.env %></p>
@@ -0,0 +1,16 @@
1
+ <style>
2
+ p {
3
+ color: darkgray;
4
+ }
5
+ .turbo-request {
6
+ color: #359500;
7
+ }
8
+ .initial-request {
9
+ color: #8B0000;
10
+ font-weight: bold;
11
+ }
12
+ img, svg {
13
+ width: 32px;
14
+ height: 32px;
15
+ }
16
+ </style>
@@ -0,0 +1,37 @@
1
+ <%= render 'styles' %>
2
+ <style>
3
+ .svelte-component {
4
+ border: 1px solid lightgray;
5
+ padding: 10px;
6
+ margin: 10px;
7
+ width: 100px;
8
+ display: inline-block;
9
+ }
10
+ </style>
11
+
12
+ <%= render 'nav' %>
13
+
14
+
15
+ <h1>SSR / Client side rendering test</h1>
16
+ <p>On the always client side rendered components you will see a unpleasant «blink» on the initial request.</p>
17
+ <p>After rendering both, client and server side rendered components, must look similar for making sure that on ssr:
18
+ :auto (which is default)
19
+ no unpleasant «blink» is appearing.</p>
20
+ <hr>
21
+
22
+ <% components = ['SvgRawImport', 'JpgImport', 'PngImport', 'JavascriptImport', 'ParentWithChild'] %>
23
+
24
+ <h3>Always rendered server side</h3>
25
+ <div class="ssr-only">
26
+ <% components.each do |component| %>
27
+ <%= svelte_component(component, ssr: true, hydrate: false, title: component) %>
28
+ <% end %>
29
+ </div>
30
+
31
+ <hr>
32
+ <h3>Always rendered client side</h3>
33
+ <div class="client-only">
34
+ <% components.each do |component| %>
35
+ <%= svelte_component(component, ssr: false, hydrate: true, title: component) %>
36
+ <% end %>
37
+ </div>
@@ -1,5 +1,9 @@
1
- <p>Example view</p>
1
+ <%= render 'styles' %>
2
2
 
3
- <%= svelte_component "SvelteOnRailsHelloWorld", items: ['attributes', 'are', 'parsed'] %>
4
- <%= svelte_component "Pug", ssr: true, notice: "Pug is rendered server-side" %>
5
- <%= svelte_component "Pug", ssr: false, notice: "Pug is rendered client-side" %>
3
+
4
+ <%= render 'nav' %>
5
+
6
+
7
+ <h1>Svelte is here</h1>
8
+
9
+ <%= svelte_component "SvelteOnRailsHelloWorld", items: ['attributes', 'are', 'parsed'] %>
@@ -0,0 +1,26 @@
1
+ <%= render 'styles' %>
2
+ <style>
3
+ .svelte-component {
4
+ border: 1px solid lightgray;
5
+ padding: 10px;
6
+ margin: 10px;
7
+ width: 100px;
8
+ display: inline-block;
9
+ }
10
+ </style>
11
+
12
+ <%= render 'nav' %>
13
+
14
+
15
+ <h1>ssr: :auto (default)</h1>
16
+
17
+
18
+ <% components = ['SvgRawImport', 'JpgImport', 'PngImport', 'JavascriptImport', 'ParentWithChild'] %>
19
+
20
+ <h3>Rendered server side only on initial request</h3>
21
+ <div class="ssr-auto">
22
+ <% components.each do |component| %>
23
+ <%= svelte_component(component, title: component) %>
24
+ <% end %>
25
+ </div>
26
+
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.42
4
+ version: 0.0.43
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair
@@ -31,9 +31,6 @@ files:
31
31
  - README.md
32
32
  - lib/generators/svelte_on_rails/install/install_generator.rb
33
33
  - lib/svelte-on-rails.rb
34
- - lib/svelte_on_rails/compiler/compile.js
35
- - lib/svelte_on_rails/compiler/compiler.rb
36
- - lib/svelte_on_rails/compiler/customPlugins.js
37
34
  - lib/svelte_on_rails/configuration.rb
38
35
  - lib/svelte_on_rails/installer/gem_utils.rb
39
36
  - lib/svelte_on_rails/installer/haml.rb
@@ -52,18 +49,28 @@ files:
52
49
  - lib/svelte_on_rails/renderer/utils.js
53
50
  - lib/svelte_on_rails/view_helpers.rb
54
51
  - lib/tasks/svelte_on_rails_tasks.rake
52
+ - templates/config_base/app/frontend/initializers/svelte.js
53
+ - templates/config_base/app/frontend/ssr/ssr.js
54
+ - templates/config_base/config/svelte_on_rails.yml
55
+ - templates/config_base/vite-ssr.config.ts
55
56
  - templates/rails_vite_hello_world/app/controllers/svelte_on_rails_hello_world_controller.rb
56
- - templates/rails_vite_hello_world/app/frontend/images/atom.svg
57
- - templates/rails_vite_hello_world/app/frontend/images/check-circle-green.png
58
- - templates/rails_vite_hello_world/app/frontend/images/svg.svg
59
- - templates/rails_vite_hello_world/app/frontend/javascript/components/Pug.svelte
57
+ - templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-england.png
58
+ - templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-face-smile-wink.svg
59
+ - templates/rails_vite_hello_world/app/frontend/images/svelte-on-rails-hello-world-switzerland.jpg
60
+ - templates/rails_vite_hello_world/app/frontend/javascript/components/JavascriptImport.svelte
61
+ - templates/rails_vite_hello_world/app/frontend/javascript/components/JpgImport.svelte
62
+ - templates/rails_vite_hello_world/app/frontend/javascript/components/ParentWithChild.svelte
63
+ - templates/rails_vite_hello_world/app/frontend/javascript/components/PngImport.svelte
60
64
  - templates/rails_vite_hello_world/app/frontend/javascript/components/SvelteOnRailsHelloWorld.svelte
65
+ - templates/rails_vite_hello_world/app/frontend/javascript/components/SvgRawImport.svelte
61
66
  - templates/rails_vite_hello_world/app/frontend/javascript/components/sub/NestedComponent.svelte
62
67
  - templates/rails_vite_hello_world/app/frontend/javascript/nestedJavascript.js
63
68
  - templates/rails_vite_hello_world/app/frontend/javascript/nestedJavascriptToggled.js
69
+ - templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/_nav.html.erb
70
+ - templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/_styles.html.erb
71
+ - templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/backend_frontend_rendered.html.erb
64
72
  - templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/index.html.erb
65
- - templates/svelte_on_rails_vite_base/app/frontend/initializers/svelte.js
66
- - templates/svelte_on_rails_vite_base/config/svelte_on_rails.yml
73
+ - templates/rails_vite_hello_world/app/views/svelte_on_rails_hello_world/ssr_auto_rendered.html.erb
67
74
  homepage: https://gitlab.com/sedl/svelte-on-rails
68
75
  licenses:
69
76
  - MIT
@@ -1,123 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import path from 'path';
4
- import fs from 'fs/promises';
5
- import { rawResolver, stringPlugin, ssrExternals } from "./customPlugins.js";
6
-
7
- // Debugging: Log NODE_PATH and current working directory
8
- console.log('NODE_PATH:', process.env.NODE_PATH);
9
- console.log('Current Working Directory:', process.cwd());
10
-
11
- // Arguments: inputFile, outputDir, gemPath
12
- const args = process.argv.slice(2);
13
- const inputFile = args[0];
14
- const outputDir = args[1];
15
- const gemPath = args[2];
16
-
17
- // Pfad zu node_modules im Gem
18
- const gemNodeModules = path.join(gemPath, 'node_modules');
19
- // Path to node_modules in the Rails app (current working directory)
20
- const appNodeModules = path.join(process.cwd(), 'node_modules');
21
-
22
- // Dynamische Importe der Build-Tools aus dem Gem
23
- const { rollup } = await import(path.join(appNodeModules, 'rollup', 'dist', 'rollup.js'));
24
- const svelte = await import(path.join(appNodeModules, 'rollup-plugin-svelte', 'index.js')).then(m => m.default);
25
- const resolve = await import(path.join(appNodeModules, '@rollup', 'plugin-node-resolve', 'dist', 'es', 'index.js')).then(m => m.default);
26
- const commonjs = await import(path.join(appNodeModules, '@rollup/plugin-commonjs', 'dist', 'es', 'index.js')).then(m => m.default);
27
- const url = await import(path.join(appNodeModules, '@rollup', 'plugin-url', 'dist', 'es', 'index.js')).then(m => m.default);
28
- const css = await import(path.join(appNodeModules, 'rollup-plugin-css-only', 'dist', 'index.mjs')).then(m => m.default);
29
- const { sveltePreprocess } = await import(path.join(appNodeModules, 'svelte-preprocess', 'dist', 'index.js'));
30
-
31
-
32
- if (!inputFile || !outputDir || !gemPath) {
33
- console.error('Error: inputFile, outputDir, and gemPath must be specified.');
34
- console.error('Usage: node script.js <inputFile> <outputDir> <gemPath>');
35
- process.exit(1);
36
- }
37
-
38
- // Ensure that gemPath exists
39
- try {
40
- await fs.access(gemPath);
41
- } catch (error) {
42
- console.error(`Error: The gemPath "${gemPath}" does not exist.`);
43
- process.exit(1);
44
- }
45
-
46
- const fileName = path.basename(inputFile, '.svelte');
47
- const outputFile = path.join(outputDir, `${fileName}.js`);
48
- const outputCss = path.join(outputDir, `${fileName}.css`);
49
-
50
- const rollupConfig = {
51
- input: inputFile,
52
- output: {
53
- file: outputFile,
54
- format: 'esm',
55
- },
56
- external: [
57
- 'fs', 'path', 'url', 'module', 'http', 'https', 'zlib', 'stream', 'buffer',
58
- 'axios', 'form-data'
59
- ],
60
- onwarn(warning, warn) {
61
- if (warning.code === 'CIRCULAR_DEPENDENCY') return;
62
- if (warning.code === 'EMPTY_BUNDLE' || warning.code === 'UNUSED_EXTERNAL_IMPORT') return;
63
- warn(warning);
64
- },
65
- plugins: [
66
- svelte({
67
- preprocess: sveltePreprocess({
68
- pug: true,
69
- typescript: true
70
- }),
71
- compilerOptions: {
72
- generate: 'ssr',
73
- hydratable: false
74
- },
75
- onwarn(warning, handler) {
76
- if (warning.code === 'MISSING_EXPORT' && warning.exporter.includes('axios')) return;
77
- handler(warning);
78
- }
79
- }),
80
- rawResolver(),
81
- stringPlugin(),
82
- url({
83
- include: ['**/*.jpg', '**/*.png', '**/*.jpeg', '**/*.gif'],
84
- limit: 0,
85
- destDir: outputDir,
86
- publicPath: outputDir,
87
- emitFiles: true,
88
- }),
89
- css({ output: path.basename(outputCss) }),
90
- resolve({
91
- dedupe: ['svelte'],
92
- exportConditions: ['svelte', 'server'],
93
- // modulePaths: [appNodeModules, gemNodeModules], // Prioritize app's node_modules
94
- modulePaths: [appNodeModules], // Prioritize app's node_modules
95
- extensions: ['.mjs', '.js', '.cjs', '.ts'],
96
- mainFields: ['main', 'module']
97
- }),
98
- commonjs({
99
- include: [/node_modules/],
100
- transformMixedEsModules: true
101
- }),
102
- ssrExternals()
103
- ],
104
- };
105
-
106
- // Rollup build
107
- async function compile() {
108
- try {
109
- const startTime = performance.now();
110
- const bundle = await rollup(rollupConfig);
111
- await bundle.write(rollupConfig.output);
112
- await bundle.close();
113
- console.log(`Build successful: ${outputFile} and ${outputCss}`);
114
- const endTime = performance.now();
115
- const duration = (endTime - startTime).toFixed(2);
116
- console.log(`Execution time: ${duration} ms`);
117
- } catch (error) {
118
- console.error('Build failed:', error);
119
- process.exit(1);
120
- }
121
- }
122
-
123
- compile();
@@ -1,120 +0,0 @@
1
- module SvelteOnRails
2
-
3
- class Compiler
4
-
5
- require 'open3'
6
-
7
- def initialize(filename, base_path: SvelteOnRails::Configuration.instance.components_folder_full)
8
-
9
- utils = SvelteOnRails::Lib::Utils
10
- @files = utils.component_files(filename, base_path: base_path)
11
-
12
- end
13
-
14
- # compiler
15
- def compile_if_changes
16
-
17
- # letzte Änderung innerhalb des ordners ermitteln
18
-
19
- watch_changes = SvelteOnRails::Configuration.instance.watch_changes?
20
- ts_file = SvelteOnRails::Configuration.instance.dist_folder.join('reset_timestamp')
21
-
22
- have_changes = if watch_changes && File.exist?(ts_file)
23
-
24
- # compare last modification timestamp
25
- last_modification = 100.years.ago.to_time
26
- Dir.glob("#{SvelteOnRails::Configuration.instance.components_folder}**/*").each do |path|
27
- if File.file?(path)
28
- mtime = File.mtime(path)
29
- if mtime > last_modification
30
- last_modification = mtime
31
- end
32
- end
33
- end
34
- File.mtime(ts_file) < last_modification
35
-
36
- elsif watch_changes
37
- true
38
- elsif !File.exist?(ts_file)
39
- true
40
- else
41
- false
42
- end
43
-
44
- if have_changes || !File.exist?(compiled_js_file) || !File.exist?(compiled_css_file)
45
- if have_changes || (!File.exist?(ts_file) && watch_changes)
46
- self.class.reset_dist
47
- end
48
- compile
49
- end
50
- end
51
-
52
- def compiled_js_file
53
- @files[:compiled_file] + '.js'
54
- end
55
-
56
- def compiled_css_file
57
- @files[:compiled_file] + '.css'
58
- end
59
-
60
- def compiled_file
61
- @files[:compiled_file]
62
- end
63
-
64
- def compile
65
-
66
- start_time = Time.now
67
-
68
- cnf = SvelteOnRails::Configuration.instance
69
- subs = @files[:svelte_filename].split('/')[0..-2].join('/')
70
- dist = cnf.dist_folder + cnf.components_folder + subs
71
- utils = SvelteOnRails::Lib::Utils
72
-
73
- cmd = [
74
- 'node',
75
- utils.gem_app_dir + 'compiler/compile.js',
76
- @files[:svelte_file],
77
- dist,
78
- utils.gem_app_dir
79
- ].join(' ')
80
-
81
- Dir.chdir(cnf.rails_root) do
82
- env = { 'NODE_PATH' => File.join(cnf.rails_root, 'node_modules') }
83
-
84
- stdout, stderr, status = Open3.capture3(env, cmd, chdir: cnf.rails_root)
85
-
86
- unless status.to_s.match(/^pid [0-9]+ exit 0$/)
87
- raise "Compiling «#{@files[:svelte_filename]}» Server-side, script compile.js, executed within Rails.root:\n\n#{cmd}\n\n++++++\n\n#{stderr}\n\n++++++\n\n"
88
- end
89
- end
90
-
91
- time = Time.now - start_time
92
- Rails.logger.info " Compiled #{@files[:svelte_filename]}.js: #{time.round(3)}ms" rescue nil
93
- end
94
-
95
- def self.reset_dist
96
- unless Dir.exist?(SvelteOnRails::Configuration.instance.dist_folder)
97
- FileUtils.mkdir_p(SvelteOnRails::Configuration.instance.dist_folder)
98
- end
99
- FileUtils.rm_rf Dir.glob("#{SvelteOnRails::Configuration.instance.dist_folder}/*")
100
- FileUtils.touch SvelteOnRails::Configuration.instance.dist_folder.join('reset_timestamp')
101
- end
102
-
103
- def self.reset_and_compile_all
104
- reset_dist
105
- cnf = SvelteOnRails::Configuration.instance
106
- frontend_folder = cnf.frontend_folder_full
107
- files = Dir.glob(cnf.frontend_folder_full.join('**/*.svelte'))
108
- files.each_with_index do |file, ind|
109
- comp_name = file.to_s[(cnf.frontend_folder_full.to_s.length + 1)..-1]
110
-
111
- n = SvelteOnRails::Compiler.new(comp_name, base_path: frontend_folder)
112
- n.compile
113
-
114
- puts "compiled #{ind + 1}/#{files.length}: #{comp_name}"
115
-
116
- end
117
- end
118
-
119
- end
120
- end
@@ -1,60 +0,0 @@
1
- import path from 'path';
2
- import fs from 'fs/promises';
3
-
4
- // Plugin for raw imports
5
- export function rawResolver() {
6
- return {
7
- name: 'raw-resolver',
8
- resolveId(source, importer) {
9
- console.log(`Raw-Resolver: source=${source}, importer=${importer}`);
10
- if (source.endsWith('?raw')) {
11
- const actualSource = source.replace('?raw', '');
12
- const resolved = path.resolve(path.dirname(importer), actualSource);
13
- console.log(`Raw-Resolver: Resolving ${source} to ${resolved}?raw`);
14
- return `${resolved}?raw`;
15
- }
16
- return null;
17
- },
18
- };
19
- }
20
-
21
- export function stringPlugin() {
22
- return {
23
- name: 'string',
24
- async load(id) {
25
- if (id.endsWith('?raw')) {
26
- console.log(`String-Plugin: Loading ${id}`);
27
- const actualId = id.replace('?raw', '');
28
- try {
29
- const content = await fs.readFile(actualId, 'utf-8');
30
- console.log(`String-Plugin: Content of ${actualId} loaded (excerpt): ${content.substring(0, 50)}...`);
31
- return `export default ${JSON.stringify(content)};`;
32
- } catch (error) {
33
- console.error(`String-Plugin: Error loading ${actualId}:`, error);
34
- throw error;
35
- }
36
- }
37
- return null;
38
- },
39
- };
40
- }
41
-
42
- // Plugin to ignore browser-specific modules during SSR
43
- export function ssrExternals() {
44
- return {
45
- name: 'ssr-externals',
46
- resolveId(source) {
47
- // Ignore modules like axios during SSR
48
- if (source === 'axios' || source.includes('form-data')) {
49
- return { id: 'void 0', external: true };
50
- }
51
- return null;
52
- },
53
- load(id) {
54
- if (id === 'void 0') {
55
- return 'export default null;';
56
- }
57
- return null;
58
- }
59
- };
60
- }
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Pro 6.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path d="M256 224C238.3 224 223.1 238.4 223.1 256S238.3 288 256 288c17.62 0 32-14.38 32-32C287.9 238.4 273.6 224 256 224zM471.2 128c-15.62-27.75-49.62-41.38-93.11-41.38c-9.499 .125-18.87 .75-28.25 2C327.1 34.38 293.5 0 256 0S184.9 34.38 162.1 88.62c-9.374-1.25-18.75-1.875-28.25-2C90.39 86.62 56.4 100.2 40.78 128c-18.75 33.5-6.499 80.62 27.62 128c-34.12 47.38-46.37 94.5-27.62 128c15.62 27.75 49.62 41.38 93.11 41.38c9.499-.125 18.87-.75 28.25-2C184.9 477.6 218.5 512 256 512s71.12-34.38 93.86-88.63c9.374 1.25 18.75 1.875 28.25 2c43.49 0 77.49-13.62 93.11-41.38c18.75-33.5 6.499-80.63-27.62-128C477.7 208.6 489.1 161.5 471.2 128zM256 48c14.37 0 32.12 18.12 47.12 50C287.1 102.4 271.4 107.8 256 114.1c-15.37-6.375-31.12-11.76-47.12-16.14C223.9 66.12 241.6 48 256 48zM133.9 377.4c-26.5 0-46.74-6.625-52.74-17.38c-7.124-12.75-.5-37.63 18.62-66.63C111.4 305.2 123.8 316.5 136.8 327c2.25 16.5 5.374 33 9.624 49.13C142.3 376.4 137.9 377.4 133.9 377.4zM136.8 185C123.8 195.5 111.4 206.8 99.77 218.6C80.65 189.6 74.02 164.8 81.15 152c5.999-10.75 26.25-17.38 52.74-17.38c3.1 0 8.374 1 12.5 1.25C142.1 152 139 168.5 136.8 185zM256 464c-14.37 0-32.12-18.12-47.12-49.1C224.9 409.6 240.6 404.2 256 397.9c15.37 6.375 31.12 11.76 47.12 16.14C288.1 445.9 270.4 464 256 464zM256 352c-52.99 0-95.99-43-95.99-96S203 160 256 160s95.99 43 95.99 96S308.1 352 256 352zM430.9 360c-5.999 10.75-26.25 17.38-52.74 17.38c-3.1 0-8.374-1-12.5-1.25C369.9 360 372.1 343.5 375.2 327c12.1-10.5 25.37-21.75 36.1-33.63C431.4 322.4 437.1 347.2 430.9 360zM412.2 218.6C400.6 206.8 388.2 195.5 375.2 185C372.1 168.5 369.9 152 365.6 135.9c4.125-.25 8.499-1.25 12.5-1.25c26.5 0 46.74 6.625 52.74 17.38C437.1 164.8 431.4 189.6 412.2 218.6z"/></svg>
@@ -1,3 +0,0 @@
1
- <svg width="100" height="50" xmlns="http://www.w3.org/2000/svg">
2
- <text x="10" y="30" font-family="Arial, sans-serif" font-size="24" fill="black">svg</text>
3
- </svg>
@@ -1,14 +0,0 @@
1
- <script>
2
- export let notice
3
- let ary = ['item-1', 'item-2', 'item-3']
4
- </script>
5
-
6
- <template lang="pug">
7
- h1 Hello, Pug!
8
- p {notice}
9
- ul
10
- <!-- The | Syntax is a workaround for situations where pug integration in svelte is not working really well-->
11
- | {#each ary as item}
12
- | <li>{item}</li>
13
- | {/each}
14
- </template>