actionview-svelte-handler 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9167cd64c58c539f35cd795e5c6b4f753f2c66ba7abee1b83f9846dc064498a2
4
- data.tar.gz: fe6da9a5f4de7c21411aaefccd5847505be84b2944db2a42f9c475873729aabf
3
+ metadata.gz: aa07b0a30bed3812a745c845a4e0906074e63d355e09812ebd138dde54416f21
4
+ data.tar.gz: 77410f77910319464178822fa8797415b8fe9851073518af6e4c0c07bc9a2bb0
5
5
  SHA512:
6
- metadata.gz: 18eac635e7134c55e4d3907d44635558ec7f53e2e831339f48e1d9f32c971c0f910999daf41975f63c75aeaafc8f35cca1f7e66bdd34014e469e25bc9a7575fb
7
- data.tar.gz: 42633207280f2c079fc378ce90ac1594110d997419db831315508b15522b6f31c0d2a3138de1f86e6ad6c14ae65b6e1a1706f673adf1bdef16a2db54ad18956f
6
+ metadata.gz: 726d5a198e94ceea5e1e5eaff320b2393a7cc106f1d807409c30c9e06ead56f033ca69c502d85a51ca72b3d0f3d0ef765f47f02c5241f3b39e240c90d759589b
7
+ data.tar.gz: 3f89592a576d8d6aa87b05ac9dd527e5f356c2803405a91d92bfdff6a76ad3162607743d3713f2707fd4a5fd0835e865b67ff761cbdf544bf737e74904ddca82
data/README.md CHANGED
@@ -1,12 +1,17 @@
1
- # actionview-svelte-handler
1
+ # Action View Svelte Handler
2
2
 
3
- Create `.svelte` views seamlessly in Rails applications.
3
+ [![Copyleft License: LGPL-3.0-or-later](https://img.shields.io/badge/%F0%9F%84%AF_copyleft-LGPL--3.0--or--later-black)](https://spdx.org/licenses/LGPL-3.0-or-later.html)
4
+ [![Gem Version](https://img.shields.io/gem/v/actionview-svelte-handler)](https://rubygems.org/gems/actionview-svelte-handler)
5
+
6
+ `actionview-svelte-handler` is a template handler for Action View that allows you to create [Svelte](https://svelte.dev) views in Rails applications with ease.
4
7
 
5
8
  ## Usage
6
9
 
7
10
  Add `.html.svelte` views to your application instead of `.html.erb`. Note that ERB helpers will cease to be available.
8
11
 
9
- To pass props, use the `Svelte.props` object in your controller then access it like a store with `$props`:
12
+ To pass props, use the `Svelte.props` object in your controller, and then access it as a store with `$props`.
13
+
14
+ ### Example
10
15
 
11
16
  `users_controller.rb`:
12
17
 
@@ -34,24 +39,40 @@ end
34
39
  </style>
35
40
  ```
36
41
 
42
+ ### Server-side rendering
43
+
44
+ Server-side rendering is enabled by default, but if you need to disable it for any reason, pass the `svelte[:ssr]` local to `render` like so:
45
+
46
+ ```ruby
47
+ render "view", locals: { svelte: { ssr: false } }
48
+ ```
49
+
37
50
  ## Installation
38
51
 
39
- 1. Ensure you have the [Bun](https://bun.sh) JavaScript runtime in your `$PATH`
52
+ 1. Ensure you have [Node.js >=v12.16.0](https://nodejs.org) and [NPM](https://npmjs.com) in your `$PATH`
40
53
 
41
54
  3. Execute:
42
55
 
43
56
  ```bash
44
- bundle add actionview-svelte-helper
57
+ bundle add actionview-svelte-handler
45
58
  ```
46
59
 
47
60
  4. And then add the `svelte_tags` helper at the end of the `<head>` in `application.html.erb`:
48
61
 
49
- ```erb
62
+ ```html
50
63
  <head>
51
64
  <!-- other head content -->
52
65
  <%= svelte_tags %>
53
66
  </head>
54
67
  ```
55
68
 
56
- ## License
57
- The gem is available as open source under the terms of the [LGPL License](https://opensource.org/license/lgpl-3-0).
69
+ ## Copyright
70
+
71
+ Copyright (C) 2024 [Software Freedom Conservancy](https://sfconservancy.org/assignment/603092cf-aeeb-4ee3-a5e0-903bd14805a8/) and Action View Svelte Handler contributors
72
+
73
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
74
+
75
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
76
+
77
+ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see https://www.gnu.org/licenses/.
78
+
@@ -1,53 +1,37 @@
1
+ require 'erb'
2
+
1
3
  module Svelte
2
4
  class Handler
3
5
  include ActionView::Helpers::JavaScriptHelper
4
- def call(template, source)
5
- file = Tempfile.new
6
- src = Tempfile.new
7
- src.write source.b
8
- src.rewind
9
-
10
- file.write <<-JS
11
- import { compile } from 'svelte/compiler';
12
-
13
- const src = await Bun.file("#{src.path}").text()
14
- const result = compile(src, {generate: "dom", name: "Component", css: "injected", sveltePath: "https://esm.sh/svelte"})
15
-
16
- process.stdout.write(JSON.stringify(result))
17
- JS
18
-
19
- file.rewind
20
-
21
- compiled = JSON.parse(%x(bun run #{file.path})).deep_symbolize_keys
22
-
6
+ def call(template, source)
23
7
  return <<-RUBY
24
- Svelte::Handler.generate_island(#{(compiled.dig(:js, :code) || "").dump}, local_assigns.to_json.presence)
8
+ locals = j(local_assigns.to_json).presence
9
+ props = j(Svelte.props.to_json).presence
10
+ source = j('#{source}')
11
+ ssr = local_assigns.dig(:svelte, :ssr) != nil ? local_assigns.dig(:svelte, :ssr) : Svelte.ssr
12
+
13
+ assembler = Tempfile.new(['assembler', '.mjs'], "#{Svelte.gem_dir}/tmp")
14
+ assembler.write(ERB.new(File.read("#{Svelte.gem_dir}/lib/svelte/templates/assembler.js.erb")).result_with_hash({
15
+ source: source,
16
+ locals: local_assigns,
17
+ ssr: ssr
18
+ }))
19
+ assembler.rewind
20
+
21
+ result = JSON.parse(`NODE_NO_WARNINGS=1 node --experimental-vm-modules \#{assembler.path}`).deep_symbolize_keys
22
+
23
+ island = ERB.new(File.read("#{Svelte.gem_dir}/lib/svelte/templates/island.html.erb")).result_with_hash({ result: result, locals: local_assigns })
24
+
25
+ assembler.close
26
+ assembler.unlink
27
+
28
+ return island
25
29
  RUBY
26
30
  end
31
+
27
32
 
28
33
  def self.call(template, source)
29
34
  new.call(template, source)
30
35
  end
31
-
32
- def self.generate_island(js, props)
33
- return <<-HTML
34
- <is-land on:visible on:idle>
35
- <template data-island>
36
- <div id="svelte-app-client">
37
- </div>
38
-
39
- <script type="module">
40
- #{js}
41
-
42
- new Component({
43
- target: document.getElementById("svelte-app-client"),
44
- props: JSON.parse(#{props.to_json.presence || "{}"})
45
- });
46
- </script>
47
-
48
- </template>
49
- </is-land>
50
- HTML
51
- end
52
36
  end
53
37
  end
@@ -3,11 +3,13 @@ require "svelte/helpers"
3
3
 
4
4
  module Svelte
5
5
  class Railtie < ::Rails::Railtie
6
- initializer "svelte.register_actionview" do
6
+ initializer "svelte" do
7
7
  ActiveSupport.on_load :action_view do
8
8
  ActionView::Template.register_template_handler :svelte, Svelte::Handler
9
- ActionView::Base.send :include, Svelte::Helpers
9
+ ActionView::Base.send :include, Svelte::Helpers
10
10
  end
11
- end
11
+
12
+ `npm install #{Svelte.gem_dir} --install-links --save=false`
13
+ end
12
14
  end
13
15
  end
@@ -1,3 +1,3 @@
1
1
  module Svelte
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/svelte.rb CHANGED
@@ -5,7 +5,13 @@ require "svelte/version"
5
5
  require "active_support/isolated_execution_state"
6
6
 
7
7
  module Svelte
8
+ mattr_accessor :ssr, default: true
9
+
8
10
  def self.props
9
11
  ActiveSupport::IsolatedExecutionState[:svelte_props] ||= {}
10
12
  end
13
+
14
+ def self.gem_dir
15
+ Gem::Specification.find_by_name("actionview-svelte-handler").gem_dir
16
+ end
11
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionview-svelte-handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - reesericci
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-27 00:00:00.000000000 Z
11
+ date: 2024-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails