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 +4 -4
- data/README.md +29 -8
- data/lib/svelte/handler.rb +28 -38
- data/lib/svelte/railtie.rb +5 -3
- data/lib/svelte/version.rb +1 -1
- data/lib/svelte.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa07b0a30bed3812a745c845a4e0906074e63d355e09812ebd138dde54416f21
|
4
|
+
data.tar.gz: 77410f77910319464178822fa8797415b8fe9851073518af6e4c0c07bc9a2bb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 726d5a198e94ceea5e1e5eaff320b2393a7cc106f1d807409c30c9e06ead56f033ca69c502d85a51ca72b3d0f3d0ef765f47f02c5241f3b39e240c90d759589b
|
7
|
+
data.tar.gz: 3f89592a576d8d6aa87b05ac9dd527e5f356c2803405a91d92bfdff6a76ad3162607743d3713f2707fd4a5fd0835e865b67ff761cbdf544bf737e74904ddca82
|
data/README.md
CHANGED
@@ -1,12 +1,17 @@
|
|
1
|
-
#
|
1
|
+
# Action View Svelte Handler
|
2
2
|
|
3
|
-
|
3
|
+
[](https://spdx.org/licenses/LGPL-3.0-or-later.html)
|
4
|
+
[](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
|
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
|
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-
|
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
|
-
```
|
62
|
+
```html
|
50
63
|
<head>
|
51
64
|
<!-- other head content -->
|
52
65
|
<%= svelte_tags %>
|
53
66
|
</head>
|
54
67
|
```
|
55
68
|
|
56
|
-
##
|
57
|
-
|
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
|
+
|
data/lib/svelte/handler.rb
CHANGED
@@ -1,44 +1,34 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
1
3
|
module Svelte
|
2
|
-
class Handler
|
4
|
+
class Handler
|
3
5
|
include ActionView::Helpers::JavaScriptHelper
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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)
|
data/lib/svelte/railtie.rb
CHANGED
@@ -3,11 +3,13 @@ require "svelte/helpers"
|
|
3
3
|
|
4
4
|
module Svelte
|
5
5
|
class Railtie < ::Rails::Railtie
|
6
|
-
initializer "svelte
|
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
|
-
|
11
|
+
|
12
|
+
`npm install #{Svelte.gem_dir} --install-links --save=false`
|
13
|
+
end
|
12
14
|
end
|
13
15
|
end
|
data/lib/svelte/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2024-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|