lib/react/server_rendering/bundle_renderer.rb in react-rails-2.7.1 vs lib/react/server_rendering/bundle_renderer.rb in react-rails-3.0.0.rc.0

- old
+ new

@@ -1,29 +1,31 @@ -require 'react/server_rendering/environment_container' -require 'react/server_rendering/manifest_container' -require 'react/server_rendering/webpacker_manifest_container' -require 'react/server_rendering/yaml_manifest_container' +# frozen_string_literal: true +require "react/server_rendering/environment_container" +require "react/server_rendering/manifest_container" +require "react/server_rendering/yaml_manifest_container" +require "react/server_rendering/separate_server_bundle_container" + module React module ServerRendering # Extends ExecJSRenderer for the Rails environment - # - fetches JS code from the Rails app (webpacker or sprockets) + # - fetches JS code from the Rails app (Shakapacker or Sprockets) # - stringifies props # - implements console replay class BundleRenderer < ExecJSRenderer # Reimplement console methods for replaying on the client - CONSOLE_POLYFILL = File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_polyfill.js')) - CONSOLE_REPLAY = File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_replay.js')) - CONSOLE_RESET = File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_reset.js')) - TIMEOUT_POLYFILL = File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/timeout_polyfill.js')) + CONSOLE_POLYFILL = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_polyfill.js")) + CONSOLE_REPLAY = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_replay.js")) + CONSOLE_RESET = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_reset.js")) + TIMEOUT_POLYFILL = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/timeout_polyfill.js")) - def initialize(options={}) + def initialize(options = {}) @replay_console = options.fetch(:replay_console, true) - filenames = options.fetch(:files, ['server_rendering.js']) + filenames = options.fetch(:files, ["server_rendering.js"]) js_code = CONSOLE_POLYFILL.dup js_code << TIMEOUT_POLYFILL.dup - js_code << options.fetch(:code, '') + js_code << options.fetch(:code, "") filenames.each do |filename| js_code << asset_container.find_asset(filename) end @@ -38,16 +40,16 @@ t_options = prepare_options(prerender_options) t_props = prepare_props(props) super(component_name, t_props, t_options) end - def before_render(component_name, props, prerender_options) - @replay_console ? CONSOLE_RESET : '' + def before_render(_component_name, _props, _prerender_options) + @replay_console ? CONSOLE_RESET : "" end - def after_render(component_name, props, prerender_options) - @replay_console ? CONSOLE_REPLAY : '' + def after_render(_component_name, _props, _prerender_options) + @replay_console ? CONSOLE_REPLAY : "" end class << self attr_accessor :asset_container_class end @@ -69,24 +71,23 @@ private def prepare_options(options) r_func = render_function(options) opts = case options - when Hash then options - when TrueClass then {} - else - {} - end + when Hash then options + else + {} + end # This seems redundant to pass opts.merge(render_function: r_func) end def render_function(opts) if opts == :static - 'renderToStaticMarkup'.freeze + "renderToStaticMarkup" else - 'renderToString'.freeze + "renderToString" end end def prepare_props(props) props.is_a?(String) ? props : props.to_json @@ -98,25 +99,19 @@ # Detect what kind of asset system is in use and choose that container. # Or, if the user has provided {.asset_container_class}, use that. # @return [Class] suitable for {#asset_container} def asset_container_class - if self.class.asset_container_class.present? - self.class.asset_container_class - elsif WebpackerManifestContainer.compatible? - WebpackerManifestContainer - elsif assets_precompiled? - if ManifestContainer.compatible? - ManifestContainer - elsif YamlManifestContainer.compatible? - YamlManifestContainer - else - # Even though they are precompiled, we can't find them :S - EnvironmentContainer - end - else - EnvironmentContainer - end + return self.class.asset_container_class if self.class.asset_container_class.present? + return SeparateServerBundleContainer if SeparateServerBundleContainer.compatible? + + return EnvironmentContainer unless assets_precompiled? + + return ManifestContainer if ManifestContainer.compatible? + return YamlManifestContainer if YamlManifestContainer.compatible? + + # Even though they are precompiled, we can't find them :S + EnvironmentContainer end end end end