rails-react-views 0.0.1 → 0.0.7

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: a028f3d9ae7fd27788a2562640ed586391a6ad570f7819dd3b856fb17ede47fd
4
- data.tar.gz: 69c77a908ef892d597c51ca3c147d2d1cc8484569c4459fa2d9e26774a5759f7
3
+ metadata.gz: b023ad6f9bf11a32bac03938ad40d5799b1b547765533797f2d14d725efd4950
4
+ data.tar.gz: 38d49a56d77f70d888069b0f414f6e8aff79a48eed689704432fa6285dd5d094
5
5
  SHA512:
6
- metadata.gz: ece3d02d088046ce4a5d3fb9fe7553307e18d06782ef1f9c51de8b06a4369ad788cbbe1bfbb01b2347cd380ad08f387cdff499bc11c2d8312edfd1a168f2cc8b
7
- data.tar.gz: e18d0f3068ee08ac8d975b7f838734cd9c265f9308829e32cab2cf49608ac7d609c77813e2f404895f690e7f9f7b39988a311f8970ae702724091a023c7261e8
6
+ metadata.gz: 3f234735d6ab4fb12dc6ef3303bbbe04d64a153002cf9983e1a1ddf4af99ff6bf0c78fd56d74928e274919cd5b0b279ce4fd6175d74d940e8f5fc7b3e652b8c3
7
+ data.tar.gz: 8df9deb60085b227388fa595689e59b711a441c46b85187c8ff2c308d87df226b672f5968f0ff8f0fbdd511df8d1eb44d4f5df2ca8638d05af943b00060b7daa
@@ -3,6 +3,7 @@ require 'rails_react_views/config'
3
3
  require 'rails_react_views/digest'
4
4
  require 'rails_react_views/prerenderer'
5
5
  require 'rails_react_views/railtie'
6
+ require 'rails_react_views/shell'
6
7
  require 'rails_react_views/version'
7
8
 
8
9
  module RailsReactViews
@@ -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.path)
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/**/*')).reject { |file| File.directory?(file) }
14
- yarn_digest = Digest::MD5.file(Rails.root.join('yarn.lock'))
15
- digests = js_files.map { |file| Digest::MD5.file(file).to_s } << yarn_digest
19
+ js_files = Dir.glob(Rails.root.join('app/javascript/**/*'))
20
+ files_digest([*js_files, yarn_file])
21
+ end
16
22
 
17
- Digest::MD5.hexdigest(digests.to_s)
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.ends_with?('.json')
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
@@ -1,3 +1,3 @@
1
1
  module RailsReactViews
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.7"
3
3
  end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-react-views
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Geerts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-08 00:00:00.000000000 Z
11
+ date: 2022-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: railties
14
+ name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5'
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: '5'
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: '5'
54
+ version: '0'
41
55
  description: A library for full integration of React.js views in Ruby on Rails.
42
56
  email:
43
57
  executables: []
@@ -52,11 +66,15 @@ 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: {}
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
60
78
  post_install_message:
61
79
  rdoc_options: []
62
80
  require_paths: