Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc updates, fix test helper #1093

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Changes since last non-beta release.

*Please add entries here for your pull requests that are not yet released.*

### [11.0.8] - 2018-05-22
#### Fixed
- Fix test helper determination of stale assets. [PR 1093](https://github.com/shakacode/react_on_rails/pull/1093) by [justin808](https://github.com/justin808).

#### Changed
- Document how to manually rehydrate XHR-substituted components on client side. [PR 1095](https://github.com/shakacode/react_on_rails/pull/1095) by [hchevalier](https://github.com/hchevalier).

Expand Down Expand Up @@ -768,7 +772,8 @@ Best done with Object destructing:
##### Fixed
- Fix several generator related issues.

[Unreleased]: https://github.com/shakacode/react_on_rails/compare/11.0.7...master
[Unreleased]: https://github.com/shakacode/react_on_rails/compare/11.0.8...master
[11.0.8]: https://github.com/shakacode/react_on_rails/compare/11.0.7...11.0.8
[11.0.7]: https://github.com/shakacode/react_on_rails/compare/11.0.6...11.0.7
[11.0.6]: https://github.com/shakacode/react_on_rails/compare/11.0.5...11.0.6
[11.0.5]: https://github.com/shakacode/react_on_rails/compare/11.0.4...11.0.5
Expand Down
8 changes: 7 additions & 1 deletion docs/additional-reading/rspec-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ The following `config/react_on_rails.rb` settings **must** match your setup:

# Define the files we need to check for webpack compilation when running tests.
# Generally, the manifest.json is good enough for this check if using webpacker
config.webpack_generated_files = %w( hello-world-bundle.js )
config.webpack_generated_files = %w( manifest.json )

# OR if you're not hashing the server-bundle.js, then you should include your server-bundle.js in the list.
# config.webpack_generated_files = %w( server-bundle.js manifest.json )

# OR if you're not using webpacker, your setup might look like.
# config.webpack_generated_files = %w( client-bundle.js server-bundle.js )

# If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
# with rspec then this controls what yarn command is run
Expand Down
10 changes: 7 additions & 3 deletions docs/basics/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ ReactOnRails.configure do |config|

# Define the files we need to check for webpack compilation when running tests.
# The default is `%w( manifest.json )` as will be sufficient for most webpacker builds.
# However, if you are generated a server bundle that is NOT hashed (present in manifest.json),
# then include the file in this list like this:
#
config.webpack_generated_files = %w( manifest.json )
config.webpack_generated_files = %w( server-bundle.js manifest.json )

# You can optionally add values to your rails_context. See example below for RenderingExtension
# config.rendering_extension = RenderingExtension
Expand All @@ -75,8 +77,10 @@ ReactOnRails.configure do |config|
# JavaScript execution instances which should handle any component requested.
#
# While you may configure this to be the same as your client bundle file, this file is typically
# different.
#
# different. Note, be sure to include the exact file name with the ".js" if you are not hashing this file.
# If you are hashing this file (supposing you are using the same file for client rendering), then
#
# you should include a name that matches your bundle name in your webpack config.
config.server_bundle_js_file = "server-bundle.js"

# If set to true, this forces Rails to reload the server bundle if it is modified
Expand Down
4 changes: 2 additions & 2 deletions docs/basics/upgrading-react-on-rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ Reason for doing this: This enables your webpack bundles to bypass the Rails ass
#### From version 7 or lower

##### ...while keeping your `client` directory
Unfortunately, this requires quite a few steps:
* `.gitignore`: add `/public/webpack/*`
* `Gemfile`: bump `react_on_rails` and add `webpacker`
* layout views: anything bundled by webpack will need to be requested by a `javascript_pack_tag` or `stylesheet_pack_tag`
* layout views: anything bundled by webpack will need to be requested by a `javascript_pack_tag` or `stylesheet_pack_tag`.
* Search your codebase for javascript_include_tag. Use the
* `config/initializers/assets.rb`: we no longer need to modify `Rails.application.config.assets.paths` or append anything to `Rails.application.config.assets.precompile`.
* `config/initializers/react_on_rails.rb`:
* Delete `config.generated_assets_dir`. Webpacker's config now supplies this information
Expand Down
255 changes: 129 additions & 126 deletions lib/react_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,138 +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

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 @@ -214,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
2 changes: 2 additions & 0 deletions lib/react_on_rails/prerender_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# rubocop:disable: Layout/IndentHeredoc
module ReactOnRails
class PrerenderError < ::ReactOnRails::Error
# TODO: Consider remove providing original `err` as already have access to `self.cause`
# http://blog.honeybadger.io/nested-errors-in-ruby-with-exception-cause/
attr_reader :component_name, :err, :props, :js_code, :console_messages

# err might be nil if JS caught the error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def find_most_recent_mtime
def all_compiled_assets
@all_compiled_assets ||= begin
webpack_generated_files = @webpack_generated_files.map do |bundle_name|
ReactOnRails::Utils.bundle_js_file_path(bundle_name)
if bundle_name == ReactOnRails.configuration.server_bundle_js_file
ReactOnRails::Utils.server_bundle_js_file_path
else
ReactOnRails::Utils.bundle_js_file_path(bundle_name)
end
end

if webpack_generated_files.present?
Expand Down
Loading