Class: ReactOnRails::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/react_on_rails/configuration.rb,
sig/react_on_rails/configuration.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender: nil, replay_console: nil, make_generated_server_bundle_the_entrypoint: nil, trace: nil, development_mode: nil, logging_on_server: nil, server_renderer_pool_size: nil, server_renderer_timeout: nil, raise_on_prerender_error: true, generated_assets_dir: nil, webpack_generated_files: nil, rendering_extension: nil, build_test_command: nil, build_production_command: nil, generated_component_packs_loading_strategy: nil, same_bundle_for_client_and_server: nil, i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil, i18n_yml_safe_load_options: nil, random_dom_id: nil, rendering_props_extension: nil, components_subdirectory: nil, stores_subdirectory: nil, auto_load_bundle: nil, component_registry_timeout: nil, server_bundle_output_path: nil, enforce_private_server_bundles: nil, check_database_on_dev_start: nil) ⇒ Configuration

rubocop:disable Metrics/AbcSize



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/react_on_rails/configuration.rb', line 92

def initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender: nil,
               replay_console: nil, make_generated_server_bundle_the_entrypoint: nil,
               trace: nil, development_mode: nil,
               logging_on_server: nil, server_renderer_pool_size: nil,
               server_renderer_timeout: nil, raise_on_prerender_error: true,
               generated_assets_dir: nil, webpack_generated_files: nil,
               rendering_extension: nil, build_test_command: nil,
               build_production_command: nil, generated_component_packs_loading_strategy: nil,
               same_bundle_for_client_and_server: nil,
               i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil, i18n_yml_safe_load_options: nil,
               random_dom_id: nil, rendering_props_extension: nil,
               components_subdirectory: nil, stores_subdirectory: nil, auto_load_bundle: nil,
               component_registry_timeout: nil, server_bundle_output_path: nil, enforce_private_server_bundles: nil,
               check_database_on_dev_start: nil)
  self.node_modules_location = node_modules_location.present? ? node_modules_location : Rails.root
  self.generated_assets_dir = generated_assets_dir
  self.build_test_command = build_test_command
  self.build_production_command = build_production_command
  self.i18n_dir = i18n_dir
  self.i18n_yml_dir = i18n_yml_dir
  self.i18n_output_format = i18n_output_format
  self.i18n_yml_safe_load_options = i18n_yml_safe_load_options

  self.random_dom_id = random_dom_id
  self.prerender = prerender
  self.replay_console = replay_console
  self.logging_on_server = logging_on_server
  self.development_mode = if development_mode.nil?
                            Rails.env.development?
                          else
                            development_mode
                          end
  self.trace = trace.nil? ? Rails.env.development? : trace
  self.raise_on_prerender_error = raise_on_prerender_error
  self.rendering_props_extension = rendering_props_extension
  self.component_registry_timeout = component_registry_timeout

  # Server rendering:
  self.server_bundle_js_file = server_bundle_js_file
  self.same_bundle_for_client_and_server = same_bundle_for_client_and_server
  self.server_renderer_pool_size = self.development_mode ? 1 : server_renderer_pool_size
  self.server_renderer_timeout = server_renderer_timeout # seconds

  self.webpack_generated_files = webpack_generated_files
  self.rendering_extension = rendering_extension

  self.components_subdirectory = components_subdirectory
  self.stores_subdirectory = stores_subdirectory
  self.auto_load_bundle = auto_load_bundle
  self.make_generated_server_bundle_the_entrypoint = make_generated_server_bundle_the_entrypoint
  self.generated_component_packs_loading_strategy = generated_component_packs_loading_strategy
  self.server_bundle_output_path = server_bundle_output_path
  self.enforce_private_server_bundles = enforce_private_server_bundles
  self.check_database_on_dev_start = check_database_on_dev_start.nil? ? true : check_database_on_dev_start
end

Instance Attribute Details

#auto_load_bundleBoolean

Returns the value of attribute auto_load_bundle.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def auto_load_bundle
  @auto_load_bundle
end

#build_production_commandString

Returns the value of attribute build_production_command.

Returns:

  • (String)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def build_production_command
  @build_production_command
end

#build_test_commandString

Returns the value of attribute build_test_command.

Returns:

  • (String)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def build_test_command
  @build_test_command
end

#check_database_on_dev_startObject

Returns the value of attribute check_database_on_dev_start.



75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def check_database_on_dev_start
  @check_database_on_dev_start
end

#component_registry_timeoutInteger

Returns the value of attribute component_registry_timeout.

Returns:

  • (Integer)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def component_registry_timeout
  @component_registry_timeout
end

#components_subdirectoryString?

Returns the value of attribute components_subdirectory.

Returns:

  • (String, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def components_subdirectory
  @components_subdirectory
end

#development_modeBoolean

Returns the value of attribute development_mode.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def development_mode
  @development_mode
end

#enforce_private_server_bundlesBoolean

Returns the value of attribute enforce_private_server_bundles.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def enforce_private_server_bundles
  @enforce_private_server_bundles
end

#generated_assets_dirString

Returns the value of attribute generated_assets_dir.

Returns:

  • (String)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def generated_assets_dir
  @generated_assets_dir
end

#generated_component_packs_loading_strategySymbol?

Returns the value of attribute generated_component_packs_loading_strategy.

Returns:

  • (Symbol, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def generated_component_packs_loading_strategy
  @generated_component_packs_loading_strategy
end

#i18n_dirString?

Returns the value of attribute i18n_dir.

Returns:

  • (String, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def i18n_dir
  @i18n_dir
end

#i18n_output_formatSymbol?

Returns the value of attribute i18n_output_format.

Returns:

  • (Symbol, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def i18n_output_format
  @i18n_output_format
end

#i18n_yml_dirString?

Returns the value of attribute i18n_yml_dir.

Returns:

  • (String, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def i18n_yml_dir
  @i18n_yml_dir
end

#i18n_yml_safe_load_optionsHash[Symbol, untyped]?

Returns the value of attribute i18n_yml_safe_load_options.

Returns:

  • (Hash[Symbol, untyped], nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def i18n_yml_safe_load_options
  @i18n_yml_safe_load_options
end

#logging_on_serverBoolean

Returns the value of attribute logging_on_server.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def logging_on_server
  @logging_on_server
end

#make_generated_server_bundle_the_entrypointBoolean

Returns the value of attribute make_generated_server_bundle_the_entrypoint.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def make_generated_server_bundle_the_entrypoint
  @make_generated_server_bundle_the_entrypoint
end

#node_modules_locationString?

Returns the value of attribute node_modules_location.

Returns:

  • (String, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def node_modules_location
  @node_modules_location
end

#prerenderBoolean

Returns the value of attribute prerender.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def prerender
  @prerender
end

#raise_on_prerender_errorBoolean

Returns the value of attribute raise_on_prerender_error.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def raise_on_prerender_error
  @raise_on_prerender_error
end

#random_dom_idBoolean

Returns the value of attribute random_dom_id.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def random_dom_id
  @random_dom_id
end

#rendering_extensionString?

Returns the value of attribute rendering_extension.

Returns:

  • (String, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def rendering_extension
  @rendering_extension
end

#rendering_props_extensionString?

Returns the value of attribute rendering_props_extension.

Returns:

  • (String, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def rendering_props_extension
  @rendering_props_extension
end

#replay_consoleBoolean

Returns the value of attribute replay_console.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def replay_console
  @replay_console
end

#same_bundle_for_client_and_serverBoolean

Returns the value of attribute same_bundle_for_client_and_server.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def same_bundle_for_client_and_server
  @same_bundle_for_client_and_server
end

#server_bundle_js_fileString

Returns the value of attribute server_bundle_js_file.

Returns:

  • (String)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def server_bundle_js_file
  @server_bundle_js_file
end

#server_bundle_output_pathString?

Returns the value of attribute server_bundle_output_path.

Returns:

  • (String, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def server_bundle_output_path
  @server_bundle_output_path
end

#server_renderer_pool_sizeInteger

Returns the value of attribute server_renderer_pool_size.

Returns:

  • (Integer)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def server_renderer_pool_size
  @server_renderer_pool_size
end

#server_renderer_timeoutInteger

Returns the value of attribute server_renderer_timeout.

Returns:

  • (Integer)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def server_renderer_timeout
  @server_renderer_timeout
end

#stores_subdirectoryString?

Returns the value of attribute stores_subdirectory.

Returns:

  • (String, nil)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def stores_subdirectory
  @stores_subdirectory
end

#traceBoolean

Returns the value of attribute trace.

Returns:

  • (Boolean)


75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def trace
  @trace
end

#webpack_generated_filesArray[String]

Returns the value of attribute webpack_generated_files.

Returns:



75
76
77
# File 'lib/react_on_rails/configuration.rb', line 75

def webpack_generated_files
  @webpack_generated_files
end

Instance Method Details

#adjust_precompile_taskvoid

This method returns an undefined value.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/react_on_rails/configuration.rb', line 281

def adjust_precompile_task
  skip_react_on_rails_precompile = %w[no false n f].include?(ENV.fetch("REACT_ON_RAILS_PRECOMPILE", nil))

  return if skip_react_on_rails_precompile || build_production_command.blank?

  raise(ReactOnRails::Error, compile_command_conflict_message) if ReactOnRails::PackerUtils.precompile?

  precompile_tasks = lambda {
    # Skip generate_packs if shakapacker has a precompile hook configured
    if ReactOnRails::PackerUtils.shakapacker_precompile_hook_configured?
      hook_value = ReactOnRails::PackerUtils.shakapacker_precompile_hook_value
      puts "Skipping react_on_rails:generate_packs (configured in shakapacker precompile hook: #{hook_value})"
    else
      Rake::Task["react_on_rails:generate_packs"].invoke
    end

    Rake::Task["react_on_rails:assets:webpack"].invoke

    # VERSIONS is per the shakacode/shakapacker clean method definition.
    # We set it very big so that it is not used, and then clean just
    # removes files older than 1 hour.
    versions = 100_000
    puts "Invoking task shakapacker:clean from React on Rails"
    Rake::Task["shakapacker:clean"].invoke(versions)
  }

  if Rake::Task.task_defined?("assets:precompile")
    Rake::Task["assets:precompile"].enhance do
      precompile_tasks.call
    end
  else
    Rake::Task.define_task("assets:precompile") do
      precompile_tasks.call
    end
  end
end

#check_autobundling_requirementsvoid

This method returns an undefined value.



273
274
275
276
277
278
279
# File 'lib/react_on_rails/configuration.rb', line 273

def check_autobundling_requirements
  raise_missing_components_subdirectory unless components_subdirectory.present?

  ReactOnRails::PackerUtils.raise_shakapacker_version_incompatible_for_autobundling unless
    ReactOnRails::PackerUtils.supports_autobundling?
  ReactOnRails::PackerUtils.raise_nested_entries_disabled unless ReactOnRails::PackerUtils.nested_entries?
end

#check_component_registry_timeoutvoid

This method returns an undefined value.



164
165
166
167
168
169
170
# File 'lib/react_on_rails/configuration.rb', line 164

def check_component_registry_timeout
  self.component_registry_timeout = DEFAULT_COMPONENT_REGISTRY_TIMEOUT if component_registry_timeout.nil?

  return if component_registry_timeout.is_a?(Integer) && component_registry_timeout >= 0

  raise ReactOnRails::Error, "component_registry_timeout must be a positive integer"
end

#compile_command_conflict_messageString

Returns:

  • (String)


373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/react_on_rails/configuration.rb', line 373

def compile_command_conflict_message
  <<~MSG

    React on Rails and Shakapacker error in configuration!
    In order to use config/react_on_rails.rb config.build_production_command,
    you must edit config/shakapacker.yml to include this value in the default configuration:
    'shakapacker_precompile: false'

    Alternatively, remove the config/react_on_rails.rb config.build_production_command and the
    default bin/shakapacker script will be used for assets:precompile.

    #{ReactOnRails::DOCTOR_RECOMMENDATION}
  MSG
end

#ensure_webpack_generated_files_existsvoid

This method returns an undefined value.



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/react_on_rails/configuration.rb', line 341

def ensure_webpack_generated_files_exists
  all_required_files = ["manifest.json", server_bundle_js_file]

  if ReactOnRails::Utils.react_on_rails_pro?
    pro_config = ReactOnRailsPro.configuration
    all_required_files << pro_config.rsc_bundle_js_file
    all_required_files << pro_config.react_client_manifest_file
    all_required_files << pro_config.react_server_client_manifest_file
  end

  all_required_files = all_required_files.compact_blank

  if webpack_generated_files.empty?
    self.webpack_generated_files = all_required_files
  else
    missing_files = all_required_files.reject { |file| webpack_generated_files.include?(file) }
    self.webpack_generated_files += missing_files if missing_files.any?
  end
end

#error_if_using_packer_and_generated_assets_dir_not_match_public_output_pathvoid

This method returns an undefined value.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/react_on_rails/configuration.rb', line 318

def error_if_using_packer_and_generated_assets_dir_not_match_public_output_path
  return if generated_assets_dir.blank?

  packer_public_output_path = ReactOnRails::PackerUtils.packer_public_output_path

  if File.expand_path(generated_assets_dir) == packer_public_output_path.to_s
    Rails.logger.warn("You specified generated_assets_dir in `config/initializers/react_on_rails.rb` " \
                      "with Shakapacker. " \
                      "Remove this line from your configuration file.")
  else
    msg = <<~MSG
      Error configuring /config/initializers/react_on_rails.rb: You are using Shakapacker
      and your specified value for generated_assets_dir = #{generated_assets_dir}
      that does not match the value for public_output_path specified in
      shakapacker.yml = #{packer_public_output_path}. You should remove the configuration
      value for "generated_assets_dir" from your config/initializers/react_on_rails.rb file.

      #{ReactOnRails::DOCTOR_RECOMMENDATION}
    MSG
    raise ReactOnRails::Error, msg
  end
end

#raise_missing_components_subdirectoryvoid

This method returns an undefined value.



361
362
363
364
365
366
367
368
369
370
371
# File 'lib/react_on_rails/configuration.rb', line 361

def raise_missing_components_subdirectory
  msg = <<~MSG
    **ERROR** ReactOnRails: auto_load_bundle is set to true, yet components_subdirectory is not configured.\
    Please set components_subdirectory to the desired directory.  For more information, please see \
    https://reactonrails.com/docs/core-concepts/auto-bundling/

    #{ReactOnRails::DOCTOR_RECOMMENDATION}
  MSG

  raise ReactOnRails::Error, msg
end

#react_client_manifest_fileString?

Returns:

  • (String, nil)


81
# File 'sig/react_on_rails/configuration.rbs', line 81

def react_client_manifest_file: () -> String?

#react_server_client_manifest_fileString?

Returns:

  • (String, nil)


82
# File 'sig/react_on_rails/configuration.rbs', line 82

def react_server_client_manifest_file: () -> String?

#rsc_bundle_js_fileString?

Returns:

  • (String, nil)


80
# File 'sig/react_on_rails/configuration.rbs', line 80

def rsc_bundle_js_file: () -> String?

#setup_config_valuesvoid

This method returns an undefined value.

on ReactOnRails



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/react_on_rails/configuration.rb', line 150

def setup_config_values
  check_autobundling_requirements if auto_load_bundle
  ensure_webpack_generated_files_exists
  error_if_using_packer_and_generated_assets_dir_not_match_public_output_path
  # check_deprecated_settings
  adjust_precompile_task
  check_component_registry_timeout
  validate_generated_component_packs_loading_strategy
  validate_enforce_private_server_bundles
  auto_detect_server_bundle_path_from_shakapacker
end

#validate_enforce_private_server_bundlesvoid

This method returns an undefined value.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/react_on_rails/configuration.rb', line 196

def validate_enforce_private_server_bundles
  return unless enforce_private_server_bundles

  # Check if server_bundle_output_path is nil
  if server_bundle_output_path.nil?
    raise ReactOnRails::Error, "enforce_private_server_bundles is set to true, but " \
                               "server_bundle_output_path is nil. Please set server_bundle_output_path " \
                               "to a directory outside of the public directory. " \
                               "#{ReactOnRails::DOCTOR_RECOMMENDATION}"
  end

  # Check if server_bundle_output_path is inside public directory
  # Skip validation if Rails.root is not available (e.g., in tests)
  return unless defined?(Rails) && Rails.root

  public_path = Rails.root.join("public").to_s
  server_output_path = File.expand_path(server_bundle_output_path, Rails.root.to_s)

  return unless server_output_path.start_with?(public_path)

  raise ReactOnRails::Error, "enforce_private_server_bundles is set to true, but " \
                             "server_bundle_output_path (#{server_bundle_output_path}) is inside " \
                             "the public directory. Please set it to a directory outside of public. " \
                             "#{ReactOnRails::DOCTOR_RECOMMENDATION}"
end

#validate_generated_component_packs_loading_strategyvoid

This method returns an undefined value.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/react_on_rails/configuration.rb', line 172

def validate_generated_component_packs_loading_strategy
  msg = <<~MSG
    ReactOnRails: Your current version of shakapacker \
    does not support async script loading. Please either:
    1. Use :defer or :sync loading strategy instead of :async
    2. Upgrade to Shakapacker v8.2.0 or above to enable async script loading
  MSG
  if PackerUtils.supports_async_loading?
    # Default based on Pro license: Pro users get :async, non-Pro users get :defer
    self.generated_component_packs_loading_strategy ||= (Utils.react_on_rails_pro? ? :async : :defer)
  elsif generated_component_packs_loading_strategy.nil?
    Rails.logger.warn("**WARNING** #{msg}")
    self.generated_component_packs_loading_strategy = :sync
  elsif generated_component_packs_loading_strategy == :async
    raise ReactOnRails::Error, "**ERROR** #{msg}\n\n#{ReactOnRails::DOCTOR_RECOMMENDATION}"
  end

  return if %i[async defer sync].include?(generated_component_packs_loading_strategy)

  raise ReactOnRails::Error,
        "generated_component_packs_loading_strategy must be either :async, :defer, or :sync. " \
        "#{ReactOnRails::DOCTOR_RECOMMENDATION}"
end