actionview-svelte-handler 0.1.0 → 0.3.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: 8a8d743d73bb7a9c2d20b16c9ba153ee83bb558800780f9913ed92edd6b9c3f5
4
- data.tar.gz: 2b506e0b2402d4c236e24caaf1d8ee09db301585568bfbbb3f5e68c90dd21e3a
3
+ metadata.gz: aa07b0a30bed3812a745c845a4e0906074e63d355e09812ebd138dde54416f21
4
+ data.tar.gz: 77410f77910319464178822fa8797415b8fe9851073518af6e4c0c07bc9a2bb0
5
5
  SHA512:
6
- metadata.gz: 37254f91eb9aded2426d894cf44047af5b0d31c73f99ec9946aff73518301b24d537c63888151e1a542fcd725360036de91551450893c83337f7dba3a87de1f0
7
- data.tar.gz: 1536e077c112bd44b1785792892f39f0cd5ea4057bf5a23ee6758cdcd94631ce93fd2ff787b4164a7d99648f64fd49f2c17064f32353069cfd995fe78006821c
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,44 +1,34 @@
1
+ require 'erb'
2
+
1
3
  module Svelte
2
- class Handler < ActionView::Template::Handlers::Raw
4
+ class Handler
3
5
  include ActionView::Helpers::JavaScriptHelper
4
-
5
- def call(template, source)
6
- file = Tempfile.new
7
- src = Tempfile.new
8
- src.write source.b
9
- src.rewind
10
-
11
- file.write <<-JS
12
- import { compile } from 'svelte/compiler';
13
-
14
- const src = await Bun.file("#{src.path}").text()
15
- const result = compile(src, {generate: "dom", name: "Component", css: "injected", sveltePath: "https://esm.sh/svelte"})
16
-
17
- process.stdout.write(JSON.stringify(result))
18
- JS
19
-
20
- file.rewind
21
-
22
- compiled = JSON.parse(%x(bun run #{file.path})).deep_symbolize_keys
23
-
24
- source = <<-HTML
25
- <is-land on:visible on:idle>
26
- <template data-island>
27
- <div id="svelte-app-client">
28
- </div>
29
-
30
- <script type="module">
31
- #{compiled.dig(:js, :code)}
32
-
33
- new Component({ target: document.getElementById("svelte-app-client") });
34
- </script>
35
-
36
- </template>
37
- </is-land>
38
- HTML
39
-
40
- super
6
+ def call(template, source)
7
+ return <<-RUBY
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
29
+ RUBY
41
30
  end
31
+
42
32
 
43
33
  def self.call(template, source)
44
34
  new.call(template, source)
@@ -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.1.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.1.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