rails-react-views 0.0.1 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +3 -0
- data/lib/rails-react-views.rb +1 -0
- data/lib/rails_react_views/concern.rb +1 -1
- data/lib/rails_react_views/config.rb +1 -7
- data/lib/rails_react_views/digest.rb +12 -4
- data/lib/rails_react_views/prerenderer.rb +28 -32
- data/lib/rails_react_views/shell.rb +24 -0
- data/lib/rails_react_views/version.rb +1 -1
- metadata +34 -16
- data/LICENSE +0 -1
- data/README.md +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a7ac05bf80cec2829fa8e6fc98c31919844a9a2daaaf5e36631b219d2d48fd0
|
4
|
+
data.tar.gz: d442e398115c05441883fb873ef73d43b1841348ca198288377e1297347079ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8821fb6c3c987078c23ba2b9d44f76358bf9fabeaead3dec41313e1e01a50222901f8128753dda657f297598d572d1841818fb3344d5415e39d172f90186d533
|
7
|
+
data.tar.gz: d72ba94b7b765c13a42fe77f8942fa4da4dbcad477ee1f16a650084d8016e90b06e5e937624c756b67b2a042cfdbffba56e46888c35aeeb4d88add2d1981474c
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 Nick Geerts
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
data/lib/rails-react-views.rb
CHANGED
@@ -5,7 +5,7 @@ module RailsReactViews
|
|
5
5
|
def render_react_view(view: nil, props: {}, prerender: true, cache: true, layout: nil)
|
6
6
|
view ||= "#{controller_path}/#{action_name}"
|
7
7
|
layout ||= RailsReactViews::config[:layout]
|
8
|
-
context = RailsReactViews::Prerenderer.build_context(view: view, props: props, path: request.
|
8
|
+
context = RailsReactViews::Prerenderer.build_context(view: view, props: props, path: request.fullpath)
|
9
9
|
|
10
10
|
respond_to do |format|
|
11
11
|
format.html do
|
@@ -1,13 +1,7 @@
|
|
1
1
|
module RailsReactViews
|
2
2
|
def self.config
|
3
3
|
@@config ||= begin
|
4
|
-
config =
|
5
|
-
if Rails.env.production?
|
6
|
-
`NODE_ENV=production node node_modules/rails-react-views/dist/cjs/server/scripts/config.js`
|
7
|
-
else
|
8
|
-
`BABEL_ENV=test node_modules/.bin/babel-node -x '.js,.jsx,.ts,.tsx' node_modules/rails-react-views/dist/cjs/server/scripts/config.js`
|
9
|
-
end
|
10
|
-
|
4
|
+
config = RailsReactViews::Shell.config
|
11
5
|
JSON.parse(config).with_indifferent_access
|
12
6
|
end
|
13
7
|
end
|
@@ -7,13 +7,21 @@ module RailsReactViews
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
def self.files_digest(files)
|
11
|
+
actual_files = files.compact.reject { |file| File.directory?(file) }
|
12
|
+
digests = actual_files.map { |file| Digest::MD5.file(file).to_s }
|
13
|
+
Digest::MD5.hexdigest(digests.to_s)
|
14
|
+
end
|
15
|
+
|
10
16
|
private
|
11
17
|
|
12
18
|
def self.calculate_digest
|
13
|
-
js_files = Dir.glob(Rails.root.join('app/javascript/**/*'))
|
14
|
-
|
15
|
-
|
19
|
+
js_files = Dir.glob(Rails.root.join('app/javascript/**/*'))
|
20
|
+
files_digest([*js_files, yarn_file])
|
21
|
+
end
|
16
22
|
|
17
|
-
|
23
|
+
def self.yarn_file
|
24
|
+
path = Rails.root.join('yarn.lock')
|
25
|
+
File.exists?(path) ? path : nil
|
18
26
|
end
|
19
27
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'zlib'
|
2
|
+
|
1
3
|
module RailsReactViews
|
2
4
|
class Prerenderer
|
3
5
|
class << self
|
@@ -17,12 +19,34 @@ module RailsReactViews
|
|
17
19
|
|
18
20
|
def build_context(props: {}, view:, path:)
|
19
21
|
{
|
20
|
-
props: props,
|
21
22
|
view: view,
|
22
|
-
path: base_path(path)
|
23
|
+
path: base_path(path),
|
24
|
+
props: props
|
23
25
|
}
|
24
26
|
end
|
25
27
|
|
28
|
+
def encode_text(text)
|
29
|
+
gzipped_text = Zlib::Deflate.deflate(text)
|
30
|
+
Base64.strict_encode64(gzipped_text)
|
31
|
+
end
|
32
|
+
|
33
|
+
def decode_text(text)
|
34
|
+
decoded_text = Base64.strict_decode64(text)
|
35
|
+
Zlib::Inflate.inflate(decoded_text).force_encoding('utf-8')
|
36
|
+
end
|
37
|
+
|
38
|
+
def split_html(html = '')
|
39
|
+
head_start = html.index('<head>')
|
40
|
+
head_end = html.index('</head>')
|
41
|
+
head = (head_start && head_end ? html[head_start + 6..head_end - 1] : '').html_safe
|
42
|
+
|
43
|
+
body_start = html.index('<body>')
|
44
|
+
body_end = html.index('</body>')
|
45
|
+
body = (body_start && body_end ? html[body_start + 6..body_end - 1] : '').html_safe
|
46
|
+
|
47
|
+
{ head: head, body: body }
|
48
|
+
end
|
49
|
+
|
26
50
|
private
|
27
51
|
|
28
52
|
def process_prerender(context_json)
|
@@ -49,47 +73,19 @@ module RailsReactViews
|
|
49
73
|
|
50
74
|
def cmd_prerender(context_json)
|
51
75
|
encoded_context = encode_text(context_json)
|
52
|
-
encoded_html =
|
53
|
-
if Rails.env.production?
|
54
|
-
`NODE_ENV=production node node_modules/rails-react-views/dist/cjs/server/scripts/cmd.js #{encoded_context}`
|
55
|
-
else
|
56
|
-
`BABEL_ENV=test node_modules/.bin/babel-node -x '.js,.jsx,.ts,.tsx' node_modules/rails-react-views/dist/cjs/server/scripts/cmd.js #{encoded_context}`
|
57
|
-
end
|
58
|
-
|
76
|
+
encoded_html = RailsReactViews::Shell.cmd(encoded_context)
|
59
77
|
html = decode_text(encoded_html)
|
60
78
|
split_html(html)
|
61
79
|
end
|
62
80
|
|
63
|
-
def encode_text(text)
|
64
|
-
gzipped_text = Zlib::Deflate.deflate(text)
|
65
|
-
Base64.strict_encode64(gzipped_text)
|
66
|
-
end
|
67
|
-
|
68
|
-
def decode_text(text)
|
69
|
-
decoded_text = Base64.strict_decode64(text)
|
70
|
-
Zlib::Inflate.inflate(decoded_text).force_encoding('utf-8')
|
71
|
-
end
|
72
|
-
|
73
81
|
def base_path(path)
|
74
|
-
if path.
|
82
|
+
if path.end_with?('.json')
|
75
83
|
path[0..-6]
|
76
84
|
else
|
77
85
|
path
|
78
86
|
end
|
79
87
|
end
|
80
88
|
|
81
|
-
def split_html(html = '')
|
82
|
-
head_start = html.index('<head>')
|
83
|
-
head_end = html.index('</head>')
|
84
|
-
head = (head_start && head_end ? html[head_start + 6..head_end - 1] : '').html_safe
|
85
|
-
|
86
|
-
body_start = html.index('<body>')
|
87
|
-
body_end = html.index('</body>')
|
88
|
-
body = (body_start && body_end ? html[body_start + 6..body_end - 1] : '').html_safe
|
89
|
-
|
90
|
-
{ head: head, body: body }
|
91
|
-
end
|
92
|
-
|
93
89
|
def server?
|
94
90
|
RailsReactViews::config[:server]
|
95
91
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RailsReactViews
|
2
|
+
class Shell
|
3
|
+
class << self
|
4
|
+
def cmd(encoded_context)
|
5
|
+
`#{node} node_modules/rails-react-views/dist/cjs/server/scripts/cmd.js #{encoded_context}`
|
6
|
+
end
|
7
|
+
|
8
|
+
def config
|
9
|
+
`#{node} node_modules/rails-react-views/dist/cjs/server/scripts/config.js`
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def node
|
15
|
+
if Rails.env.production?
|
16
|
+
'NODE_ENV=production node'
|
17
|
+
else
|
18
|
+
# Using BABEL_ENV=test to target CommonJS syntax
|
19
|
+
"BABEL_ENV=test node_modules/.bin/babel-node -x '.js,.jsx,.ts,.tsx'"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,45 +1,59 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-react-views
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Geerts
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: webpacker
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '5'
|
33
|
+
version: '5.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '0'
|
41
55
|
description: A library for full integration of React.js views in Ruby on Rails.
|
42
|
-
email:
|
56
|
+
email:
|
43
57
|
executables: []
|
44
58
|
extensions: []
|
45
59
|
extra_rdoc_files: []
|
@@ -52,12 +66,16 @@ files:
|
|
52
66
|
- lib/rails_react_views/digest.rb
|
53
67
|
- lib/rails_react_views/prerenderer.rb
|
54
68
|
- lib/rails_react_views/railtie.rb
|
69
|
+
- lib/rails_react_views/shell.rb
|
55
70
|
- lib/rails_react_views/version.rb
|
56
71
|
homepage: https://rubygems.org/gems/rails-react-views
|
57
72
|
licenses:
|
58
73
|
- MIT
|
59
|
-
metadata:
|
60
|
-
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://rubygems.org/gems/rails-react-views
|
76
|
+
source_code_uri: https://github.com/njjkgeerts/rails-react-views
|
77
|
+
changelog_uri: https://github.com/njjkgeerts/rails-react-views/releases
|
78
|
+
post_install_message:
|
61
79
|
rdoc_options: []
|
62
80
|
require_paths:
|
63
81
|
- lib
|
@@ -72,8 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
90
|
- !ruby/object:Gem::Version
|
73
91
|
version: '0'
|
74
92
|
requirements: []
|
75
|
-
rubygems_version: 3.
|
76
|
-
signing_key:
|
93
|
+
rubygems_version: 3.2.33
|
94
|
+
signing_key:
|
77
95
|
specification_version: 4
|
78
96
|
summary: React.js views for Ruby on Rails
|
79
97
|
test_files: []
|
data/LICENSE
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
../LICENSE
|
data/README.md
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
../README.md
|