Skip to content

Commit

Permalink
Improve React on Rails Configuration setup
Browse files Browse the repository at this point in the history
  • Loading branch information
justin808 committed Jun 6, 2018
1 parent d2fa7d7 commit 571dd45
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 141 deletions.
257 changes: 129 additions & 128 deletions lib/react_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,140 +3,13 @@
module ReactOnRails
def self.configure
yield(configuration)
setup_config_values
configuration.setup_config_values
end

DEFAULT_GENERATED_ASSETS_DIR = File.join(%w[public webpack], Rails.env).freeze
DEFAULT_SERVER_RENDER_TIMEOUT = 20
DEFAULT_POOL_SIZE = 1

# TODO: Move this inside of the class Configuration so as not to put all these methods
# on ReactOnRails
def self.setup_config_values
ensure_webpack_generated_files_exists
configure_generated_assets_dirs_deprecation
configure_skip_display_none_deprecation
ensure_generated_assets_dir_present
ensure_server_bundle_js_file_has_no_path
check_i18n_directory_exists
check_i18n_yml_directory_exists
check_server_render_method_is_only_execjs
error_if_using_webpacker_and_generated_assets_dir_not_match_public_output_path
end

def self.error_if_using_webpacker_and_generated_assets_dir_not_match_public_output_path
return unless ReactOnRails::WebpackerUtils.using_webpacker?

return if @configuration.generated_assets_dir.blank?

webpacker_public_output_path = ReactOnRails::WebpackerUtils.webpacker_public_output_path

if File.expand_path(@configuration.generated_assets_dir) == webpacker_public_output_path.to_s
Rails.logger.warn("You specified /config/initializers/react_on_rails.rb generated_assets_dir "\
"with Webpacker. Remove this line from your configuration file.")
else
msg = <<-MSG.strip_heredoc
Error configuring /config/initializers/react_on_rails.rb: You are using webpacker
and your specified value for generated_assets_dir = #{@configuration.generated_assets_dir}
that does not match the value for public_output_path specified in
webpacker.yml = #{webpacker_public_output_path}. You should remove the configuration
value for "generated_assets_dir" from your config/initializers/react_on_rails.rb file.
MSG
raise ReactOnRails::Error, msg
end
end

def self.check_server_render_method_is_only_execjs
return if @configuration.server_render_method.blank? ||
@configuration.server_render_method == "ExecJS"

msg = <<-MSG.strip_heredoc
Error configuring /config/initializers/react_on_rails.rb: invalid value for `config.server_render_method`.
If you wish to use a server render method other than ExecJS, contact justin@shakacode.com
for details.
MSG
raise ReactOnRails::Error, msg
end

def self.check_i18n_directory_exists
return if @configuration.i18n_dir.nil?
return if Dir.exist?(@configuration.i18n_dir)

msg = <<-MSG.strip_heredoc
Error configuring /config/initializers/react_on_rails.rb: invalid value for `config.i18n_dir`.
Directory does not exist: #{@configuration.i18n_dir}. Set to value to nil or comment it
out if not using the React on Rails i18n feature.
MSG
raise ReactOnRails::Error, msg
end

def self.check_i18n_yml_directory_exists
return if @configuration.i18n_yml_dir.nil?
return if Dir.exist?(@configuration.i18n_yml_dir)

msg = <<-MSG.strip_heredoc
Error configuring /config/initializers/react_on_rails.rb: invalid value for `config.i18n_yml_dir`.
Directory does not exist: #{@configuration.i18n_yml_dir}. Set to value to nil or comment it
out if not using this i18n with React on Rails, or if you want to use all translation files.
MSG
raise ReactOnRails::Error, msg
end

def self.ensure_generated_assets_dir_present
return if @configuration.generated_assets_dir.present? || ReactOnRails::WebpackerUtils.using_webpacker?

@configuration.generated_assets_dir = DEFAULT_GENERATED_ASSETS_DIR
Rails.logger.warn "ReactOnRails: Set generated_assets_dir to default: #{DEFAULT_GENERATED_ASSETS_DIR}"
end

def self.configure_generated_assets_dirs_deprecation
return if @configuration.generated_assets_dirs.blank?

if ReactOnRails::WebpackerUtils.using_webpacker?
webpacker_public_output_path = ReactOnRails::WebpackerUtils.webpacker_public_output_path
Rails.logger.warn "Error configuring config/initializers/react_on_rails. Define neither the "\
"generated_assets_dirs no the generated_assets_dir when using Webpacker. This is defined by "\
"public_output_path specified in webpacker.yml = #{webpacker_public_output_path}."
return
end

Rails.logger.warn "[DEPRECATION] ReactOnRails: Use config.generated_assets_dir rather than "\
"generated_assets_dirs"
if @configuration.generated_assets_dir.blank?
@configuration.generated_assets_dir = @configuration.generated_assets_dirs
else
Rails.logger.warn "[DEPRECATION] ReactOnRails. You have both generated_assets_dirs and "\
"generated_assets_dir defined. Define ONLY generated_assets_dir if NOT using Webpacker"\
" and define neither if using Webpacker"
end
end

def self.ensure_webpack_generated_files_exists
return unless @configuration.webpack_generated_files.empty?

files = ["hello-world-bundle.js"]
files << @configuration.server_bundle_js_file if @configuration.server_bundle_js_file.present?

@configuration.webpack_generated_files = files
end

def self.ensure_server_bundle_js_file_has_no_path
return unless @configuration.server_bundle_js_file.include?(File::SEPARATOR)

assets_dir = ReactOnRails::Utils.generated_assets_full_path
@configuration.server_bundle_js_file = File.basename(@configuration.server_bundle_js_file)

Rails.logger_warn do
"[DEPRECATION] ReactOnRails: remove path from server_bundle_js_file in configuration. "\
"All generated files must go in #{assets_dir}. Using file basename #{@configuration.server_bundle_js_file}"
end
end

def self.configure_skip_display_none_deprecation
return if @configuration.skip_display_none.nil?
Rails.logger.warn "[DEPRECATION] ReactOnRails: remove skip_display_none from configuration."
end

def self.configuration
@configuration ||= Configuration.new(
node_modules_location: nil,
Expand Down Expand Up @@ -216,5 +89,133 @@ def initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender
self.server_render_method = server_render_method
self.symlink_non_digested_assets_regex = symlink_non_digested_assets_regex
end

# on ReactOnRails
def setup_config_values
ensure_webpack_generated_files_exists
configure_generated_assets_dirs_deprecation
configure_skip_display_none_deprecation
ensure_generated_assets_dir_present
ensure_server_bundle_js_file_has_no_path
check_i18n_directory_exists
check_i18n_yml_directory_exists
check_server_render_method_is_only_execjs
error_if_using_webpacker_and_generated_assets_dir_not_match_public_output_path
end

private

def error_if_using_webpacker_and_generated_assets_dir_not_match_public_output_path
return unless ReactOnRails::WebpackerUtils.using_webpacker?

return if generated_assets_dir.blank?

webpacker_public_output_path = ReactOnRails::WebpackerUtils.webpacker_public_output_path

if File.expand_path(generated_assets_dir) == webpacker_public_output_path.to_s
Rails.logger.warn("You specified /config/initializers/react_on_rails.rb generated_assets_dir "\
"with Webpacker. Remove this line from your configuration file.")
else
msg = <<-MSG.strip_heredoc
Error configuring /config/initializers/react_on_rails.rb: You are using webpacker
and your specified value for generated_assets_dir = #{generated_assets_dir}
that does not match the value for public_output_path specified in
webpacker.yml = #{webpacker_public_output_path}. You should remove the configuration
value for "generated_assets_dir" from your config/initializers/react_on_rails.rb file.
MSG
raise ReactOnRails::Error, msg
end
end

def check_server_render_method_is_only_execjs
return if server_render_method.blank? ||
server_render_method == "ExecJS"

msg = <<-MSG.strip_heredoc
Error configuring /config/initializers/react_on_rails.rb: invalid value for `config.server_render_method`.
If you wish to use a server render method other than ExecJS, contact justin@shakacode.com
for details.
MSG
raise ReactOnRails::Error, msg
end

def check_i18n_directory_exists
return if i18n_dir.nil?
return if Dir.exist?(i18n_dir)

msg = <<-MSG.strip_heredoc
Error configuring /config/initializers/react_on_rails.rb: invalid value for `config.i18n_dir`.
Directory does not exist: #{i18n_dir}. Set to value to nil or comment it
out if not using the React on Rails i18n feature.
MSG
raise ReactOnRails::Error, msg
end

def check_i18n_yml_directory_exists
return if i18n_yml_dir.nil?
return if Dir.exist?(i18n_yml_dir)

msg = <<-MSG.strip_heredoc
Error configuring /config/initializers/react_on_rails.rb: invalid value for `config.i18n_yml_dir`.
Directory does not exist: #{i18n_yml_dir}. Set to value to nil or comment it
out if not using this i18n with React on Rails, or if you want to use all translation files.
MSG
raise ReactOnRails::Error, msg
end

def ensure_generated_assets_dir_present
return if generated_assets_dir.present? || ReactOnRails::WebpackerUtils.using_webpacker?

self.generated_assets_dir = DEFAULT_GENERATED_ASSETS_DIR
Rails.logger.warn "ReactOnRails: Set generated_assets_dir to default: #{DEFAULT_GENERATED_ASSETS_DIR}"
end

def configure_generated_assets_dirs_deprecation
return if generated_assets_dirs.blank?

if ReactOnRails::WebpackerUtils.using_webpacker?
webpacker_public_output_path = ReactOnRails::WebpackerUtils.webpacker_public_output_path
Rails.logger.warn "Error configuring config/initializers/react_on_rails. Define neither the "\
"generated_assets_dirs no the generated_assets_dir when using Webpacker. This is defined by "\
"public_output_path specified in webpacker.yml = #{webpacker_public_output_path}."
return
end

Rails.logger.warn "[DEPRECATION] ReactOnRails: Use config.generated_assets_dir rather than "\
"generated_assets_dirs"
if generated_assets_dir.blank?
self.generated_assets_dir = generated_assets_dirs
else
Rails.logger.warn "[DEPRECATION] ReactOnRails. You have both generated_assets_dirs and "\
"generated_assets_dir defined. Define ONLY generated_assets_dir if NOT using Webpacker"\
" and define neither if using Webpacker"
end
end

def ensure_webpack_generated_files_exists
return unless webpack_generated_files.empty?

files = ["hello-world-bundle.js"]
files << server_bundle_js_file if server_bundle_js_file.present?

self.webpack_generated_files = files
end

def ensure_server_bundle_js_file_has_no_path
return unless server_bundle_js_file.include?(File::SEPARATOR)

assets_dir = ReactOnRails::Utils.generated_assets_full_path
self.server_bundle_js_file = File.basename(server_bundle_js_file)

Rails.logger_warn do
"[DEPRECATION] ReactOnRails: remove path from server_bundle_js_file in configuration. "\
"All generated files must go in #{assets_dir}. Using file basename #{server_bundle_js_file}"
end
end

def configure_skip_display_none_deprecation
return if skip_display_none.nil?
Rails.logger.warn "[DEPRECATION] ReactOnRails: remove skip_display_none from configuration."
end
end
end
6 changes: 3 additions & 3 deletions lib/react_on_rails/version_syntax_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def rubygem_to_npm(rubygem_version = ReactOnRails::VERSION)

def npm_to_rubygem(npm_version)
match = npm_version
.tr("-", ".")
.strip
.match(/(\d.*)/)
.tr("-", ".")
.strip
.match(/(\d.*)/)
match.present? ? match[0] : nil
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
require "webpacker"
allow(ReactOnRails::WebpackerUtils).to receive(:manifest_exists?).and_return(true)
allow(ReactOnRails::Utils).to receive(:bundle_js_file_path)
.with("manifest.json")
.and_return(File.join(generated_assets_full_path, "manifest.json"))
.with("manifest.json")
.and_return(File.join(generated_assets_full_path, "manifest.json"))
allow(ReactOnRails::Utils).to receive(:bundle_js_file_path)
.with("server-bundle.js")
.and_return(File.join(generated_assets_full_path, "server-bundle.js"))
.with("server-bundle.js")
.and_return(File.join(generated_assets_full_path, "server-bundle.js"))
touch_files_in_dir(generated_assets_full_path)
end

Expand All @@ -64,14 +64,14 @@
require "webpacker"
allow(ReactOnRails::WebpackerUtils).to receive(:manifest_exists?).and_return(true)
allow(ReactOnRails::WebpackerUtils).to receive(:webpacker_public_output_path)
.and_return(generated_assets_full_path)
.and_return(generated_assets_full_path)
allow(ReactOnRails.configuration).to receive(:server_bundle_js_file).and_return("server-bundle.js")
allow(ReactOnRails::Utils).to receive(:bundle_js_file_path)
.with("manifest.json")
.and_return(File.join(generated_assets_full_path, "manifest.json"))
.with("manifest.json")
.and_return(File.join(generated_assets_full_path, "manifest.json"))
allow(ReactOnRails::Utils).to receive(:bundle_js_file_path)
.with("server-bundle.js")
.and_raise(Webpacker::Manifest::MissingEntryError)
.with("server-bundle.js")
.and_raise(Webpacker::Manifest::MissingEntryError)
touch_files_in_dir(generated_assets_full_path)
end

Expand Down Expand Up @@ -99,7 +99,6 @@
specify { expect(checker.stale_generated_webpack_files).to eq([]) }
end


context "when compiled assets don't exist" do
let(:fixture_dirname) { "assets_no_exist" }
specify do
Expand Down

0 comments on commit 571dd45

Please sign in to comment.