react_on_rails 13.0.2 → 13.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -4
  3. data/Gemfile.development_dependencies +1 -3
  4. data/README.md +42 -19
  5. data/docs/additional-details/migrating-from-react-rails.md +1 -1
  6. data/docs/api/view-helpers-api.md +2 -1
  7. data/docs/{guides/getting-started.md → getting-started.md} +4 -1
  8. data/docs/guides/client-vs-server-rendering.md +4 -2
  9. data/docs/guides/configuration.md +18 -4
  10. data/docs/guides/file-system-based-automated-bundle-generation.md +188 -0
  11. data/docs/guides/i18n.md +71 -83
  12. data/docs/guides/tutorial.md +9 -3
  13. data/docs/home.md +1 -1
  14. data/docs/javascript/server-rendering-tips.md +0 -3
  15. data/lib/generators/react_on_rails/base_generator.rb +0 -1
  16. data/lib/generators/react_on_rails/templates/base/base/app/views/layouts/hello_world.html.erb +1 -1
  17. data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +13 -0
  18. data/lib/react_on_rails/configuration.rb +11 -33
  19. data/lib/react_on_rails/helper.rb +37 -2
  20. data/lib/react_on_rails/locales/base.rb +24 -1
  21. data/lib/react_on_rails/packs_generator.rb +298 -0
  22. data/lib/react_on_rails/react_component/render_options.rb +4 -0
  23. data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +0 -6
  24. data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +2 -0
  25. data/lib/react_on_rails/test_helper/webpack_assets_status_checker.rb +19 -3
  26. data/lib/react_on_rails/version.rb +1 -1
  27. data/lib/react_on_rails/webpacker_utils.rb +14 -0
  28. data/lib/react_on_rails.rb +1 -0
  29. data/lib/tasks/generate_packs.rake +11 -0
  30. data/package.json +2 -2
  31. data/yarn.lock +5 -5
  32. metadata +6 -3
data/docs/guides/i18n.md CHANGED
@@ -1,99 +1,87 @@
1
- # I18n
1
+ # Internationalization
2
2
 
3
- Here's a summary of adding the I18n functionality.
3
+ You can use [Rails internationalization (i18n)](https://guides.rubyonrails.org/i18n.html) in your client code.
4
4
 
5
- 1. Add `config.i18n_dir` in `config/initializers/react_on_rails.rb`
5
+ 1. Set `config.i18n_dir` in `config/initializers/react_on_rails.rb`:
6
6
 
7
- React on Rails will generate `translations.json` & `default.json` automatically (see #3) after you configured your `config.i18n_dir` in `config/initializers/react_on_rails.rb`.
7
+ ```ruby
8
+ # Replace the following line by the directory containing your translation.js and default.js files.
9
+ config.i18n_dir = Rails.root.join("PATH_TO", "YOUR_JS_I18N_FOLDER")
10
+ ```
8
11
 
9
- ```ruby
10
- # Replace the following line to the location where you keep translation.js & default.js.
11
- config.i18n_dir = Rails.root.join("PATH_TO", "YOUR_JS_I18N_FOLDER")
12
- ```
12
+ If you do not want to use the YAML files from `Rails.root.join("config", "locales")` and installed gems, you can also set `config.i18n_yml_dir`:
13
+ ```ruby
14
+ # Replace the following line by the location of your client i18n yml files
15
+ # Without this option, all YAML files from Rails.root.join("config", "locales") and installed gems are loaded
16
+ config.i18n_yml_dir = Rails.root.join("PATH_TO", "YOUR_YAML_I18N_FOLDER")
17
+ ```
13
18
 
14
- Optionally you can also set `config.i18n_yml_dir` if you do not what to use all the locale files from rails.
15
- ```ruby
16
- # Replace the following line to the location where you keep your client i18n yml files
17
- # By default(without this option), all yaml files from Rails.root.join("config", "locales") and installed gems are loaded
18
- config.i18n_yml_dir = Rails.root.join("PATH_TO", "YOUR_YAML_I18N_FOLDER")
19
- ```
19
+ 2. Add that directory (or just the generated files `translations.json` and `default.json`) to your `.gitignore`.
20
20
 
21
- `translations.json`: All your locales in json format.
22
- `default.json`: Default settings in json format.
21
+ 3. The locale files must be generated before `yarn build` using `rake react_on_rails:locale`.
23
22
 
24
- 2. Add `translations.json` and `default.json` to your `.gitignore`.
23
+ For development, you should adjust your startup scripts (`Procfile`s) so that they run `bundle exec rake react_on_rails:locale` before running any webpack watch process (`yarn run build:development`).
25
24
 
26
- 3. Javascript locale files must be generated before `yarn build`.
25
+ If you are not using the React on Rails test helper,
26
+ you may need to configure your CI to run `bundle exec rake react_on_rails:locale` before any webpack process as well.
27
27
 
28
- Once you setup `config.i18n_dir` as in the previous step, you will need to make sure `rake react_on_rails:locale` runs before webpack.
29
-
30
- For development, you should adjust your startup scripts (Procfiles) so that they run **`bundle exec rake react_on_rails:locale`** before running any webpack watch process (`yarn run build:development`).
31
-
32
- You may need to configure your CI to run **`bundle exec rake react_on_rails:locale`** before any webpack process if you are not using the React on Rails test helper.
33
-
34
- Note, if you are try to lint before running tests, and you are depending on the test helper to build your locales, your linting will fail because the translations won't be built yet.
35
-
36
- The fix is either to
37
- 1) run the rake task to build the translations before running the lint command or
38
- 2) to run the tests first.
28
+ Note: if you try to lint before running tests, and you depend on the test helper to build your locales, linting will fail because the translations won't be built yet.
39
29
 
40
- # Generate locales with react-intl support
30
+ The fix is either to
31
+ 1) run the rake task to build the translations before running the lint command or
32
+ 2) to run the tests first.
41
33
 
42
- By default the locales generated in json format. If you need to generate files in the prior way
43
- with `react-intl` supported via js files:
34
+ By default, the locales are generated as JSON, but you can also generate them as JavaScript with [`react-intl`](https://formatjs.io/docs/getting-started/installation/) support:
44
35
 
45
- 1. Specify i18n output format in `react_on_rails.rb`:
46
- ```rb
47
- config.i18n_output_format = 'js'
48
- ```
36
+ 1. Specify the i18n output format in `config/initializers/react_on_rails.rb`:
37
+ ```rb
38
+ config.i18n_output_format = "js"
39
+ ```
49
40
 
50
- 2. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle && yarn install`.
51
- Versions should be newer than these:
41
+ 2. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle install && yarn install`. The minimum supported versions are:
52
42
 
53
- ```js
54
- "dependencies": {
43
+ ```js
44
+ "dependencies": {
45
+ ...
46
+ "intl": "^1.2.5",
47
+ "react-intl": "^2.1.5",
48
+ ...
49
+ }
50
+ ```
51
+
52
+ 3. In React, you need to initialize `react-intl`, and set its parameters:
53
+
54
+ ```js
55
+ ...
56
+ import { addLocaleData } from 'react-intl';
57
+ import en from 'react-intl/locale-data/en';
58
+ import de from 'react-intl/locale-data/de';
59
+ import { translations } from 'path_to/i18n/translations';
60
+ import { defaultLocale } from 'path_to/i18n/default';
61
+ ...
62
+ // Initizalize all locales for react-intl.
63
+ addLocaleData([...en, ...de]);
64
+ ...
65
+ // set locale and messages for IntlProvider.
66
+ const locale = method_to_get_current_locale() || defaultLocale;
67
+ const messages = translations[locale];
55
68
  ...
56
- "intl": "^1.2.5",
57
- "react-intl": "^2.1.5",
69
+ return (
70
+ <IntlProvider locale={locale} key={locale} messages={messages}>
71
+ <CommentScreen {...{ actions, data }} />
72
+ </IntlProvider>
73
+ )
74
+ ```
75
+ ```js
76
+ // In your component.
77
+ import { defaultMessages } from 'path_to/i18n/default';
58
78
  ...
59
- }
60
- ```
61
-
62
- 3. Add `translations.js` and `default.js` to your `.gitignore` and `.eslintignore`.
63
-
64
- 4. In React, you need to initialize `react-intl`, and set parameters for it.
65
-
66
- ```js
67
- ...
68
- import { addLocaleData } from 'react-intl';
69
- import en from 'react-intl/locale-data/en';
70
- import de from 'react-intl/locale-data/de';
71
- import { translations } from 'path_to/i18n/translations';
72
- import { defaultLocale } from 'path_to/i18n/default';
73
- ...
74
- // Initizalize all locales for react-intl.
75
- addLocaleData([...en, ...de]);
76
- ...
77
- // set locale and messages for IntlProvider.
78
- const locale = method_to_get_current_locale() || defaultLocale;
79
- const messages = translations[locale];
80
- ...
81
- return (
82
- <IntlProvider locale={locale} key={locale} messages={messages}>
83
- <CommentScreen {...{ actions, data }} />
84
- </IntlProvider>
85
- )
86
- ```
87
- ```js
88
- // In your component.
89
- import { defaultMessages } from 'path_to/i18n/default';
90
- ...
91
- return (
92
- { formatMessage(defaultMessages.yourLocaleKeyInCamelCase) }
93
- )
94
- ```
95
-
96
- # Notes
97
- * See why using JSON could be better compare to JS if amount of data is hure [ https://v8.dev/blog/cost-of-javascript-2019#json]( https://v8.dev/blog/cost-of-javascript-2019#json).
98
- * See [Support for Rails' i18n pluralization #1000](https://github.com/shakacode/react_on_rails/issues/1000) for a discussion of issues around pluralization.
99
- * *Outdated:* You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) and [PR #340](https://github.com/shakacode/react-webpack-rails-tutorial/pull/340), [commmited](https://github.com/shakacode/react-webpack-rails-tutorial/commit/ef369ed9d922aea5116ca7e50208169fd7831389) for a complete example.
79
+ return (
80
+ { formatMessage(defaultMessages.yourLocaleKeyInCamelCase) }
81
+ )
82
+ ```
83
+
84
+ # Notes
85
+ * See why using JSON can perform better compared to JS for large amounts of data [https://v8.dev/blog/cost-of-javascript-2019#json](https://v8.dev/blog/cost-of-javascript-2019#json).
86
+ * See [Support for Rails' i18n pluralization #1000](https://github.com/shakacode/react_on_rails/issues/1000) for a discussion of issues around pluralization.
87
+ * *Outdated:* You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) and [PR #340](https://github.com/shakacode/react-webpack-rails-tutorial/pull/340), [commmited](https://github.com/shakacode/react-webpack-rails-tutorial/commit/ef369ed9d922aea5116ca7e50208169fd7831389) for a complete example.
@@ -62,7 +62,7 @@ _Use the latest version for `react_on_rails` and `shakapacker`._
62
62
 
63
63
  ```
64
64
  gem 'react_on_rails', '13.0.1' # prefer exact gem version to match npm version
65
- gem 'shakapacker', '6.1.1' # prefer exact gem version to match npm version
65
+ gem 'shakapacker', '6.4.0' # prefer exact gem version to match npm version
66
66
 
67
67
  ```
68
68
 
@@ -91,8 +91,6 @@ Install React on Rails: `rails generate react_on_rails:install`. You need to fir
91
91
  Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
92
92
  If you want the redux install: `rails generate react_on_rails:install --redux`
93
93
 
94
- The generator will add `mini_racer`'s latest version. If you're using linux & encounter issues installing `libv8`, here's [a common solution](https://github.com/rubyjs/mini_racer/issues/218).
95
-
96
94
  ```
97
95
  bundle exec rails generate react_on_rails:install
98
96
  ```
@@ -100,6 +98,14 @@ bundle exec rails generate react_on_rails:install
100
98
  Enter `a` to replace all configuration files required by the project. You can check the diffs
101
99
  before you commit to see what changed.
102
100
 
101
+ ## Setting up your environment variables
102
+
103
+ Add the following variable to your environment:
104
+
105
+ ```
106
+ EXECJS_RUNTIME=Node
107
+ ```
108
+
103
109
  Then run the server with one of the following options:
104
110
 
105
111
  ## Running with HMR
data/docs/home.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## Details
4
4
  1. [Overview](https://www.shakacode.com/react-on-rails/docs/guides/react-on-rails-overview/)
5
- 1. [Getting Started](https://www.shakacode.com/react-on-rails/docs/guides/getting-started/)
5
+ 1. [Getting Started](https://www.shakacode.com/react-on-rails/docs/getting-started/)
6
6
  1. [How React on Rails Works](https://www.shakacode.com/react-on-rails/docs/guides/how-react-on-rails-works/)
7
7
  1. [Webpack Configuration](https://www.shakacode.com/react-on-rails/docs/guides/webpack-configuration/)
8
8
  1. [View Helpers API](https://www.shakacode.com/react-on-rails/docs/api/view-helpers-api/)
@@ -2,9 +2,6 @@
2
2
 
3
3
  For the best performance with Server Rendering, consider using [React on Rails Pro]
4
4
 
5
- Be sure to use mini_racer. See [issues/428](https://github.com/shakacode/react_on_rails/issues/428).
6
-
7
-
8
5
 
9
6
  ## General Tips
10
7
  - Your code can't reference `document`. Server side JS execution does not have access to `document`,
@@ -73,7 +73,6 @@ module ReactOnRails
73
73
  end
74
74
 
75
75
  def add_base_gems_to_gemfile
76
- gem "mini_racer", platforms: :ruby
77
76
  run "bundle"
78
77
  end
79
78
 
@@ -1,7 +1,7 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
- <title>ReactOnRailsWithWebpacker</title>
4
+ <title>ReactOnRailsWithShakapacker</title>
5
5
  <%= csrf_meta_tags %>
6
6
  <%= javascript_pack_tag 'hello-world-bundle' %>
7
7
  <%= stylesheet_pack_tag 'hello-world-bundle' %>
@@ -42,4 +42,17 @@ ReactOnRails.configure do |config|
42
42
  # React components.
43
43
  #
44
44
  config.server_bundle_js_file = "server-bundle.js"
45
+
46
+ ################################################################################
47
+ ################################################################################
48
+ # FILE SYSTEM BASED COMPONENT REGISTRY
49
+ ################################################################################
50
+ # `components_subdirectory` is the name of the matching directories that contain automatically registered components
51
+ # for use in the Rails views. The default is nil, you can enable the feature by updating it in the next line.
52
+ # config.components_subdirectory = "ror_components"
53
+ #
54
+ # For automated component registry, `render_component` view helper method tries to load bundle for component from
55
+ # generated directory. default is false, you can pass option at the time of individual usage or update the default
56
+ # in the following line
57
+ config.auto_load_bundle = false
45
58
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/ClassLength
4
-
5
3
  module ReactOnRails
6
4
  def self.configure
7
5
  yield(configuration)
@@ -9,6 +7,7 @@ module ReactOnRails
9
7
  end
10
8
 
11
9
  DEFAULT_GENERATED_ASSETS_DIR = File.join(%w[public webpack], Rails.env).freeze
10
+ DEFAULT_COMPONENTS_SUBDIRECTORY = nil
12
11
  DEFAULT_SERVER_RENDER_TIMEOUT = 20
13
12
  DEFAULT_POOL_SIZE = 1
14
13
  DEFAULT_RANDOM_DOM_ID = true # for backwards compatability
@@ -21,6 +20,7 @@ module ReactOnRails
21
20
  generated_assets_dir: "",
22
21
  server_bundle_js_file: "",
23
22
  prerender: false,
23
+ auto_load_bundle: false,
24
24
  replay_console: true,
25
25
  logging_on_server: true,
26
26
  raise_on_prerender_error: Rails.env.development?,
@@ -38,7 +38,8 @@ module ReactOnRails
38
38
  build_production_command: "",
39
39
  random_dom_id: DEFAULT_RANDOM_DOM_ID,
40
40
  same_bundle_for_client_and_server: false,
41
- i18n_output_format: nil
41
+ i18n_output_format: nil,
42
+ components_subdirectory: DEFAULT_COMPONENTS_SUBDIRECTORY
42
43
  )
43
44
  end
44
45
 
@@ -51,8 +52,8 @@ module ReactOnRails
51
52
  :webpack_generated_files, :rendering_extension, :build_test_command,
52
53
  :build_production_command,
53
54
  :i18n_dir, :i18n_yml_dir, :i18n_output_format,
54
- :server_render_method, :random_dom_id,
55
- :same_bundle_for_client_and_server, :rendering_props_extension
55
+ :server_render_method, :random_dom_id, :auto_load_bundle,
56
+ :same_bundle_for_client_and_server, :rendering_props_extension, :components_subdirectory
56
57
 
57
58
  # rubocop:disable Metrics/AbcSize
58
59
  def initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender: nil,
@@ -66,7 +67,8 @@ module ReactOnRails
66
67
  build_production_command: nil,
67
68
  same_bundle_for_client_and_server: nil,
68
69
  i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil,
69
- random_dom_id: nil, server_render_method: nil, rendering_props_extension: nil)
70
+ random_dom_id: nil, server_render_method: nil, rendering_props_extension: nil,
71
+ components_subdirectory: nil, auto_load_bundle: nil)
70
72
  self.node_modules_location = node_modules_location.present? ? node_modules_location : Rails.root
71
73
  self.generated_assets_dirs = generated_assets_dirs
72
74
  self.generated_assets_dir = generated_assets_dir
@@ -100,6 +102,8 @@ module ReactOnRails
100
102
  self.rendering_extension = rendering_extension
101
103
 
102
104
  self.server_render_method = server_render_method
105
+ self.components_subdirectory = components_subdirectory
106
+ self.auto_load_bundle = auto_load_bundle
103
107
  end
104
108
  # rubocop:enable Metrics/AbcSize
105
109
 
@@ -109,8 +113,6 @@ module ReactOnRails
109
113
  configure_generated_assets_dirs_deprecation
110
114
  configure_skip_display_none_deprecation
111
115
  ensure_generated_assets_dir_present
112
- check_i18n_directory_exists
113
- check_i18n_yml_directory_exists
114
116
  check_server_render_method_is_only_execjs
115
117
  error_if_using_webpacker_and_generated_assets_dir_not_match_public_output_path
116
118
  # check_deprecated_settings
@@ -129,6 +131,7 @@ module ReactOnRails
129
131
  ENV["WEBPACKER_PRECOMPILE"] = "false"
130
132
 
131
133
  precompile_tasks = lambda {
134
+ Rake::Task["react_on_rails:generate_packs"].invoke
132
135
  Rake::Task["react_on_rails:assets:webpack"].invoke
133
136
  puts "Invoking task webpacker:clean from React on Rails"
134
137
 
@@ -184,30 +187,6 @@ module ReactOnRails
184
187
  raise ReactOnRails::Error, msg
185
188
  end
186
189
 
187
- def check_i18n_directory_exists
188
- return if i18n_dir.nil?
189
- return if Dir.exist?(i18n_dir)
190
-
191
- msg = <<-MSG.strip_heredoc
192
- Error configuring /config/initializers/react_on_rails.rb: invalid value for `config.i18n_dir`.
193
- Directory does not exist: #{i18n_dir}. Set to value to nil or comment it
194
- out if not using the React on Rails i18n feature.
195
- MSG
196
- raise ReactOnRails::Error, msg
197
- end
198
-
199
- def check_i18n_yml_directory_exists
200
- return if i18n_yml_dir.nil?
201
- return if Dir.exist?(i18n_yml_dir)
202
-
203
- msg = <<-MSG.strip_heredoc
204
- Error configuring /config/initializers/react_on_rails.rb: invalid value for `config.i18n_yml_dir`.
205
- Directory does not exist: #{i18n_yml_dir}. Set to value to nil or comment it
206
- out if not using this i18n with React on Rails, or if you want to use all translation files.
207
- MSG
208
- raise ReactOnRails::Error, msg
209
- end
210
-
211
190
  def ensure_generated_assets_dir_present
212
191
  return if generated_assets_dir.present? || ReactOnRails::WebpackerUtils.using_webpacker?
213
192
 
@@ -253,4 +232,3 @@ module ReactOnRails
253
232
  end
254
233
  end
255
234
  end
256
- # rubocop:enable Metrics/ClassLength
@@ -57,6 +57,7 @@ module ReactOnRails
57
57
  internal_result = internal_react_component(component_name, options)
58
58
  server_rendered_html = internal_result[:result]["html"]
59
59
  console_script = internal_result[:result]["consoleReplayScript"]
60
+ render_options = internal_result[:render_options]
60
61
 
61
62
  case server_rendered_html
62
63
  when String
@@ -64,7 +65,7 @@ module ReactOnRails
64
65
  server_rendered_html: server_rendered_html,
65
66
  component_specification_tag: internal_result[:tag],
66
67
  console_script: console_script,
67
- render_options: internal_result[:render_options]
68
+ render_options: render_options
68
69
  )
69
70
  when Hash
70
71
  msg = <<~MSG
@@ -90,6 +91,26 @@ module ReactOnRails
90
91
  end
91
92
  end
92
93
 
94
+ def load_pack_for_component(component_name)
95
+ component_pack_file = generated_components_pack(component_name)
96
+ is_component_pack_present = File.exist?("#{component_pack_file}.jsx")
97
+ is_development = ENV["RAILS_ENV"] == "development"
98
+
99
+ if is_development && !is_component_pack_present
100
+ ReactOnRails::PacksGenerator.generate
101
+ raise_generated_missing_pack_warning(component_name)
102
+ end
103
+
104
+ ReactOnRails::PacksGenerator.raise_nested_enteries_disabled unless ReactOnRails::WebpackerUtils.nested_entries?
105
+
106
+ append_javascript_pack_tag "generated/#{component_name}"
107
+ append_stylesheet_pack_tag "generated/#{component_name}"
108
+ end
109
+
110
+ def generated_components_pack(component_name)
111
+ "#{ReactOnRails::WebpackerUtils.webpacker_source_entry_path}/generated/#{component_name}"
112
+ end
113
+
93
114
  # react_component_hash is used to return multiple HTML strings for server rendering, such as for
94
115
  # adding meta-tags to a page.
95
116
  # It is exactly like react_component except for the following:
@@ -112,6 +133,7 @@ module ReactOnRails
112
133
  internal_result = internal_react_component(component_name, options)
113
134
  server_rendered_html = internal_result[:result]["html"]
114
135
  console_script = internal_result[:result]["consoleReplayScript"]
136
+ render_options = internal_result[:render_options]
115
137
 
116
138
  if server_rendered_html.is_a?(String) && internal_result[:result]["hasErrors"]
117
139
  server_rendered_html = { COMPONENT_HTML_KEY => internal_result[:result]["html"] }
@@ -122,7 +144,7 @@ module ReactOnRails
122
144
  server_rendered_html: server_rendered_html,
123
145
  component_specification_tag: internal_result[:tag],
124
146
  console_script: console_script,
125
- render_options: internal_result[:render_options]
147
+ render_options: render_options
126
148
  )
127
149
  else
128
150
  msg = <<~MSG
@@ -425,6 +447,8 @@ module ReactOnRails
425
447
  # Create the HTML rendering part
426
448
  result = server_rendered_react_component(render_options)
427
449
 
450
+ load_pack_for_component react_component_name if render_options.auto_load_bundle
451
+
428
452
  {
429
453
  render_options: render_options,
430
454
  tag: component_specification_tag,
@@ -528,6 +552,17 @@ module ReactOnRails
528
552
  result
529
553
  end
530
554
 
555
+ def raise_generated_missing_pack_warning(component_name)
556
+ msg = <<~MSG
557
+ **ERROR** ReactOnRails: Generated missing pack for Component: #{component_name}. Please refresh the webpage \
558
+ once webpack has finished generating the bundles. If the problem persists
559
+ 1. Verify `components_subdirectory` is configured in `config/initializers/react_on_rails`.
560
+ 2. Component: #{component_name} is placed inside the configured `components_subdirectory`.
561
+ MSG
562
+
563
+ raise ReactOnRails::Error, msg
564
+ end
565
+
531
566
  def replay_console_option(val)
532
567
  val.nil? ? ReactOnRails.configuration.replay_console : val
533
568
  end
@@ -5,13 +5,36 @@ require "erb"
5
5
  module ReactOnRails
6
6
  module Locales
7
7
  def self.compile
8
- if ReactOnRails.configuration.i18n_output_format&.downcase == "js"
8
+ config = ReactOnRails.configuration
9
+ check_config_directory_exists(
10
+ directory: config.i18n_dir, key_name: "config.i18n_dir",
11
+ remove_if: "not using the React on Rails i18n feature"
12
+ )
13
+ check_config_directory_exists(
14
+ directory: config.i18n_yml_dir, key_name: "config.i18n_yml_dir",
15
+ remove_if: "not using this i18n with React on Rails, or if you want to use all translation files"
16
+ )
17
+ if config.i18n_output_format&.downcase == "js"
9
18
  ReactOnRails::Locales::ToJs.new
10
19
  else
11
20
  ReactOnRails::Locales::ToJson.new
12
21
  end
13
22
  end
14
23
 
24
+ def self.check_config_directory_exists(directory:, key_name:, remove_if:)
25
+ return if directory.nil?
26
+ return if Dir.exist?(directory)
27
+
28
+ msg = <<~MSG
29
+ Error configuring /config/initializers/react_on_rails.rb: invalid value for `#{key_name}`.
30
+ Directory does not exist: #{directory}. Set to value to nil or comment it
31
+ out if #{remove_if}.
32
+ MSG
33
+ raise ReactOnRails::Error, msg
34
+ end
35
+
36
+ private_class_method :check_config_directory_exists
37
+
15
38
  class Base
16
39
  def initialize
17
40
  return if i18n_dir.nil?