react-rails 2.2.1 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,6 @@ module React
2
2
  module Rails
3
3
  # If you change this, make sure to update VERSIONS.md
4
4
  # and republish the UJS by updating package.json and `bundle exec rake ujs:publish`
5
- VERSION = "2.2.1"
5
+ VERSION = '2.3.0'
6
6
  end
7
7
  end
@@ -13,7 +13,7 @@ module React
13
13
  # This will clear all state such as loaded code, JS VM state, or options.
14
14
  # @return [void]
15
15
  def self.reset_pool
16
- options = {size: pool_size, timeout: pool_timeout}
16
+ options = { size: pool_size, timeout: pool_timeout }
17
17
  @@pool = ConnectionPool.new(options) { self.renderer.new(self.renderer_options) }
18
18
  end
19
19
 
@@ -1,7 +1,7 @@
1
- require "react/server_rendering/environment_container"
2
- require "react/server_rendering/manifest_container"
3
- require "react/server_rendering/webpacker_manifest_container"
4
- require "react/server_rendering/yaml_manifest_container"
1
+ require 'react/server_rendering/environment_container'
2
+ require 'react/server_rendering/manifest_container'
3
+ require 'react/server_rendering/webpacker_manifest_container'
4
+ require 'react/server_rendering/yaml_manifest_container'
5
5
 
6
6
  module React
7
7
  module ServerRendering
@@ -11,14 +11,14 @@ module React
11
11
  # - implements console replay
12
12
  class BundleRenderer < ExecJSRenderer
13
13
  # Reimplement console methods for replaying on the client
14
- CONSOLE_POLYFILL = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_polyfill.js"))
15
- CONSOLE_REPLAY = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_replay.js"))
16
- CONSOLE_RESET = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_reset.js"))
17
- TIMEOUT_POLYFILL = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/timeout_polyfill.js"))
14
+ CONSOLE_POLYFILL = File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_polyfill.js'))
15
+ CONSOLE_REPLAY = File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_replay.js'))
16
+ CONSOLE_RESET = File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_reset.js'))
17
+ TIMEOUT_POLYFILL = File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/timeout_polyfill.js'))
18
18
 
19
19
  def initialize(options={})
20
20
  @replay_console = options.fetch(:replay_console, true)
21
- filenames = options.fetch(:files, ["server_rendering.js"])
21
+ filenames = options.fetch(:files, ['server_rendering.js'])
22
22
  js_code = CONSOLE_POLYFILL.dup
23
23
  js_code << TIMEOUT_POLYFILL.dup
24
24
  js_code << options.fetch(:code, '')
@@ -41,11 +41,11 @@ module React
41
41
  end
42
42
 
43
43
  def before_render(component_name, props, prerender_options)
44
- @replay_console ? CONSOLE_RESET : ""
44
+ @replay_console ? CONSOLE_RESET : ''
45
45
  end
46
46
 
47
47
  def after_render(component_name, props, prerender_options)
48
- @replay_console ? CONSOLE_REPLAY : ""
48
+ @replay_console ? CONSOLE_REPLAY : ''
49
49
  end
50
50
 
51
51
  class << self
@@ -9,7 +9,7 @@ module React
9
9
  attr_reader :context
10
10
 
11
11
  def initialize(options={})
12
- js_code = options[:code] || raise("Pass `code:` option to instantiate a JS context!")
12
+ js_code = options[:code] || raise('Pass `code:` option to instantiate a JS context!')
13
13
  @context = ExecJS.compile(GLOBAL_WRAPPER + js_code)
14
14
  end
15
15
 
@@ -23,8 +23,8 @@ module React
23
23
  end
24
24
 
25
25
  # Hooks for inserting JS before/after rendering
26
- def before_render(component_name, props, prerender_options); ""; end
27
- def after_render(component_name, props, prerender_options); ""; end
26
+ def before_render(component_name, props, prerender_options); ''; end
27
+ def after_render(component_name, props, prerender_options); ''; end
28
28
 
29
29
  # Handle Node.js & other ExecJS contexts
30
30
  GLOBAL_WRAPPER = <<-JS
@@ -40,7 +40,7 @@ module React
40
40
  end
41
41
 
42
42
  def main_render(component_name, props, prerender_options)
43
- render_function = prerender_options.fetch(:render_function, "renderToString")
43
+ render_function = prerender_options.fetch(:render_function, 'renderToString')
44
44
  "this.ReactRailsUJS.serverRender('#{render_function}', '#{component_name}', #{props})"
45
45
  end
46
46
 
@@ -11,7 +11,7 @@ module React
11
11
 
12
12
  def find_asset(logical_path)
13
13
  asset_path = @manifest.assets[logical_path] || raise("No compiled asset for #{logical_path}, was it precompiled?")
14
- asset_full_path = ::Rails.root.join("public", @manifest.dir, asset_path)
14
+ asset_full_path = ::Rails.root.join('public', @manifest.dir, asset_path)
15
15
  File.read(asset_full_path)
16
16
  end
17
17
 
@@ -1,4 +1,4 @@
1
- require "open-uri"
1
+ require 'open-uri'
2
2
 
3
3
  module React
4
4
  module ServerRendering
@@ -7,27 +7,87 @@ module React
7
7
  # - webpack-dev-server
8
8
  # - compiled pack
9
9
  class WebpackerManifestContainer
10
+
11
+ begin
12
+ MAJOR, MINOR, PATCH, _ = Bundler.locked_gems.specs.find { |gem_spec| gem_spec.name == 'webpacker' }.version.segments
13
+ rescue
14
+ MAJOR, MINOR, PATCH, _ = [0,0,0]
15
+ end
16
+
10
17
  # This pattern matches the code that initializes the dev-server client.
11
18
  CLIENT_REQUIRE = %r{__webpack_require__\(.*webpack-dev-server\/client\/index\.js.*\n}
12
19
 
13
- def find_asset(logical_path)
14
- # raises if not found
15
- asset_path = Webpacker::Manifest.lookup(logical_path).to_s
16
- if asset_path.start_with?("http")
17
- # Get a file from the webpack-dev-server
18
- dev_server_asset = open(asset_path).read
19
- # Remove `webpack-dev-server/client/index.js` code which causes ExecJS to 💥
20
- dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
21
- dev_server_asset
22
- else
23
- # Read the already-compiled pack:
24
- full_path = Webpacker::Manifest.lookup_path(logical_path).to_s
25
- File.read(full_path)
20
+ def self.compatible?
21
+ !!defined?(Webpacker)
22
+ end
23
+
24
+ if MAJOR < 3
25
+ def find_asset(logical_path)
26
+ # raises if not found
27
+ asset_path = manifest.lookup(logical_path).to_s
28
+ if asset_path.start_with?('http')
29
+ # Get a file from the webpack-dev-server
30
+ dev_server_asset = open(asset_path).read
31
+ # Remove `webpack-dev-server/client/index.js` code which causes ExecJS to 💥
32
+ dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
33
+ dev_server_asset
34
+ else
35
+ # Read the already-compiled pack:
36
+ full_path = file_path(logical_path).to_s
37
+ File.read(full_path)
38
+ end
39
+ end
40
+ else
41
+ def find_asset(logical_path)
42
+ asset_path = Webpacker.manifest.lookup(logical_path).to_s
43
+ if Webpacker.dev_server.running?
44
+ ds = Webpacker.dev_server
45
+ dev_server_asset = open("#{ds.protocol}://#{ds.host_with_port}#{asset_path}").read
46
+ dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
47
+ dev_server_asset
48
+ else
49
+ File.read(file_path(logical_path))
50
+ end
26
51
  end
27
52
  end
28
53
 
29
- def self.compatible?
30
- !!defined?(Webpacker)
54
+ if MAJOR < 3
55
+ def manifest
56
+ Webpacker::Manifest
57
+ end
58
+ else
59
+ def manifest
60
+ Webpacker.manifest
61
+ end
62
+ end
63
+
64
+ if MAJOR < 3
65
+ def config
66
+ Webpacker::Configuration
67
+ end
68
+ else
69
+ def config
70
+ Webpacker.config
71
+ end
72
+ end
73
+
74
+ if (MAJOR == 1 && MINOR >= 2) || MAJOR == 2
75
+ def file_path path
76
+ manifest.lookup_path(path)
77
+ end
78
+ elsif MAJOR == 3
79
+ def file_path path
80
+ ::Rails.root.join('public', manifest.lookup(path)[1..-1])
81
+ end
82
+ else # 1.0 and 1.1 support
83
+ def file_path path
84
+ File.join(output_path, manifest.lookup(path).split('/')[2..-1])
85
+ end
86
+ end
87
+
88
+ def output_path
89
+ # Webpack1 /:output/:entry, Webpack3 /public/:output
90
+ config.respond_to?(:output_path) ? config.output_path : 'public'
31
91
  end
32
92
  end
33
93
  end
@@ -6,7 +6,7 @@ module React
6
6
  # but sometimes, they're compiled to other directories (or other servers)
7
7
  class YamlManifestContainer
8
8
  def initialize
9
- @assets = YAML.load_file(public_asset_path("manifest.yml"))
9
+ @assets = YAML.load_file(public_asset_path('manifest.yml'))
10
10
  end
11
11
 
12
12
  def find_asset(logical_path)
@@ -21,7 +21,7 @@ module React
21
21
  private
22
22
 
23
23
  def public_asset_path(asset_name)
24
- asset_path = File.join("public", ::Rails.application.config.assets.prefix, asset_name)
24
+ asset_path = File.join('public', ::Rails.application.config.assets.prefix, asset_name)
25
25
  ::Rails.root.join(asset_path)
26
26
  end
27
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: react-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul O’Shannessy
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-06-30 00:00:00.000000000 Z
12
+ date: 2017-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: appraisal
@@ -326,7 +326,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
326
326
  version: '0'
327
327
  requirements: []
328
328
  rubyforge_project:
329
- rubygems_version: 2.5.2
329
+ rubygems_version: 2.5.1
330
330
  signing_key:
331
331
  specification_version: 4
332
332
  summary: React integration for Ruby on Rails