Class: ReactOnRails::Generators::BaseGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- ReactOnRails::Generators::BaseGenerator
show all
- Includes:
- GeneratorHelper, JsDependencyManager
- Defined in:
- lib/generators/react_on_rails/base_generator.rb
Constant Summary
collapse
- CONFIGURE_RSPEC_TO_COMPILE_ASSETS =
<<~STR
RSpec.configure do |config|
# Ensure that if we are running js tests, we are using latest webpack assets
# This will use the defaults of :js and :server_rendering meta tags
# Requires config.build_test_command in config/initializers/react_on_rails.rb.
# This is the default setup for React on Rails generated apps.
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
end
STR
- CONFIGURE_MINITEST_TO_COMPILE_ASSETS =
<<~STR
# Ensure that tests run against fresh webpack assets.
ActiveSupport::TestCase.setup do
ReactOnRails::TestHelper.ensure_assets_compiled
end
STR
JsDependencyManager::BABEL_REACT_DEPENDENCIES, JsDependencyManager::CSS_DEPENDENCIES, JsDependencyManager::DEV_DEPENDENCIES, JsDependencyManager::PRO_DEPENDENCIES, JsDependencyManager::REACT_DEPENDENCIES, JsDependencyManager::RSC_DEPENDENCIES, JsDependencyManager::RSPACK_DEPENDENCIES, JsDependencyManager::RSPACK_DEV_DEPENDENCIES, JsDependencyManager::SWC_DEPENDENCIES, JsDependencyManager::TYPESCRIPT_DEPENDENCIES
Instance Method Summary
collapse
#add_documentation_reference, #add_npm_dependencies, #component_extension, #copy_file_and_missing_parent_directories, #dest_dir_exists?, #dest_file_exists?, #destination_config_path, #detect_react_version, #empty_directory_with_keep_file, #gem_in_lockfile?, #keep_file, #mark_pro_gem_installed!, #package_json, #print_generator_messages, #pro_gem_installed?, #resolve_server_client_or_both_path, #setup_file_error, #shakapacker_version_9_or_higher?, #symlink_dest_file_to_dest_file, #use_pro?, #use_rsc?, #using_rspack?, #using_swc?
Instance Method Details
#add_base_gems_to_gemfile ⇒ Object
207
208
209
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 207
def add_base_gems_to_gemfile
run "bundle"
end
|
#add_hello_world_route ⇒ Object
101
102
103
104
105
106
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 101
def add_hello_world_route
return if use_rsc? && !options.redux?
route "get 'hello_world', to: 'hello_world#index'"
end
|
#append_to_spec_rails_helper ⇒ Object
231
232
233
234
235
236
237
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 231
def append_to_spec_rails_helper
rspec_helper = preferred_rspec_helper_file
add_configure_rspec_to_compile_assets(rspec_helper) if rspec_helper
test_helper = File.join(destination_root, "test/test_helper.rb")
add_configure_minitest_to_compile_assets(test_helper) if File.exist?(test_helper)
end
|
#copy_base_files ⇒ Object
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
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 115
def copy_base_files
base_path = "base/base/"
base_files = %w[Procfile.dev
Procfile.dev-static-assets
Procfile.dev-prod-assets
.dev-services.yml.example
.env.example
bin/shakapacker-precompile-hook]
base_files << "app/views/layouts/react_on_rails_default.html.erb"
base_files << "app/controllers/hello_world_controller.rb" unless use_rsc? && !options.redux?
base_templates = %w[config/initializers/react_on_rails.rb]
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
base_templates.each do |file|
template("#{base_path}/#{file}.tt", file)
end
if options[:pretend]
say_status :pretend, "Skipping chmod on shakapacker-precompile-hook in --pretend mode", :yellow
else
File.chmod(0o755, File.join(destination_root, "bin/shakapacker-precompile-hook"))
end
end
|
#copy_js_bundle_files ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 145
def copy_js_bundle_files
base_path = "base/base/"
base_files = %w[app/javascript/packs/server-bundle.js]
unless options.redux? || use_rsc?
base_files << "app/javascript/src/HelloWorld/ror_components/HelloWorld.module.css"
end
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end
|
#copy_packer_config ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 181
def copy_packer_config
base_path = "base/base/"
config = "config/shakapacker.yml"
if options.shakapacker_just_installed?
say "Replacing Shakapacker default config with React on Rails version"
template("#{base_path}#{config}.tt", config, force: true)
else
say "Adding Shakapacker #{ReactOnRails::PackerUtils.shakapacker_version} config"
template("#{base_path}#{config}.tt", config)
end
configure_rspack_in_shakapacker if using_rspack?
configure_precompile_hook_in_shakapacker
configure_private_output_path_in_shakapacker
end
|
#copy_webpack_config ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 157
def copy_webpack_config
cleanup_stale_webpack_config_dir_for_rspack
say "Adding #{using_rspack? ? 'Rspack' : 'Webpack'} config"
base_path = "base/base"
base_files = %w[babel.config.js
config/webpack/clientWebpackConfig.js
config/webpack/commonWebpackConfig.js
config/webpack/test.js
config/webpack/development.js
config/webpack/production.js
config/webpack/serverWebpackConfig.js
config/webpack/ServerClientOrBoth.js]
config = { message: DOCS_REFERENCE_MESSAGE }
base_files.each do |file|
template("#{base_path}/#{file}.tt", destination_config_path(file), config)
end
copy_webpack_main_config(base_path, config)
end
|
#create_react_directories ⇒ Object
108
109
110
111
112
113
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 108
def create_react_directories
return if options.redux? || use_rsc?
empty_directory("app/javascript/src/HelloWorld/ror_components")
end
|
#update_gitignore_for_auto_registration ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/generators/react_on_rails/base_generator.rb', line 211
def update_gitignore_for_auto_registration
gitignore_path = File.join(destination_root, ".gitignore")
return unless File.exist?(gitignore_path)
gitignore_content = File.read(gitignore_path)
additions = []
additions << "**/generated/**" unless gitignore_content.include?("**/generated/**")
additions << "ssr-generated" unless gitignore_content.include?("ssr-generated")
additions << ".env" unless gitignore_content.match?(/^\.env$/)
return if additions.empty?
append_to_file ".gitignore" do
lines = ["\n# React on Rails (generated and local files)"]
lines.concat(additions)
"#{lines.join("\n")}\n"
end
end
|