/
base_generator.rb
206 lines (165 loc) · 7.76 KB
/
base_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
206
# frozen_string_literal: true
require "rails/generators"
require_relative "generator_messages"
require_relative "generator_helper"
module ReactOnRails
module Generators
class BaseGenerator < Rails::Generators::Base
include GeneratorHelper
Rails::Generators.hide_namespace(namespace)
source_root(File.expand_path("templates", __dir__))
# --redux
class_option :redux,
type: :boolean,
default: false,
desc: "Install Redux gems and Redux version of Hello World Example",
aliases: "-R"
def add_hello_world_route
route "get 'hello_world', to: 'hello_world#index'"
end
def create_react_directories
dirs = %w[components]
dirs.each { |name| empty_directory("app/javascript/bundles/HelloWorld/#{name}") }
end
def copy_base_files
base_path = "base/base/"
base_files = %w[app/controllers/hello_world_controller.rb
app/views/layouts/hello_world.html.erb
config/initializers/react_on_rails.rb
Procfile.dev
Procfile.dev-static]
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end
def copy_js_bundle_files
base_path = "base/base/"
base_files = %w[app/javascript/packs/server-bundle.js
app/javascript/bundles/HelloWorld/components/HelloWorldServer.js
app/javascript/bundles/HelloWorld/components/HelloWorld.module.css]
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end
def copy_webpack_config
puts "Adding Webpack config"
base_path = "base/base"
base_files = %w[babel.config.js
config/webpack/clientWebpackConfig.js
config/webpack/commonWebpackConfig.js
config/webpack/development.js
config/webpack/production.js
config/webpack/serverWebpackConfig.js
config/webpack/webpackConfig.js]
config = {
message: "// The source code including full typescript support is available at:"
}
base_files.each do |file|
template("#{base_path}/#{file}.tt", file, config)
end
end
def copy_webpacker_config
puts "Adding Webpacker v6 config"
base_path = "base/base/"
base_files = %w[config/webpacker.yml]
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end
def add_base_gems_to_gemfile
gem "mini_racer", "0.4.0", platforms: :ruby
run "bundle"
end
def add_yarn_dependencies
major_minor_patch_only = /\A\d+\.\d+\.\d+\z/.freeze
if ReactOnRails::VERSION.match?(major_minor_patch_only)
run "yarn add react-on-rails@#{ReactOnRails::VERSION} --exact"
else
# otherwise add latest
puts "Adding the lastest react-on-rails NPM module. Double check this is correct in package.json"
run "yarn add react-on-rails --exact"
end
puts "Adding React dependencies"
run "yarn add react react-dom @babel/preset-react prop-types babel-plugin-transform-react-remove-prop-types \
babel-plugin-macros"
puts "Adding CSS handlers"
run "yarn add css-loader css-minimizer-webpack-plugin mini-css-extract-plugin style-loader"
puts "Adding dev dependencies"
run "yarn add -D @pmmmwh/react-refresh-webpack-plugin react-refresh"
end
def append_to_spec_rails_helper
rails_helper = File.join(destination_root, "spec/rails_helper.rb")
if File.exist?(rails_helper)
add_configure_rspec_to_compile_assets(rails_helper)
else
spec_helper = File.join(destination_root, "spec/spec_helper.rb")
if File.exist?(spec_helper)
add_configure_rspec_to_compile_assets(spec_helper)
else
# rubocop:disable Layout/EmptyLinesAroundArguments
GeneratorMessages.add_info(
<<-MSG.strip_heredoc
We did not find a spec/rails_helper.rb or spec/spec_helper.rb to add
the React on Rails Test helper, which ensures that if we are running
js tests, then we are using latest webpack assets. You can later add
this to your rspec config:
# This will use the defaults of :js and :server_rendering meta tags
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
MSG
)
# rubocop:enable Layout/EmptyLinesAroundArguments
end
end
end
CONFIGURE_RSPEC_TO_COMPILE_ASSETS = <<-STR.strip_heredoc
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
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
STR
def self.helpful_message
<<-MSG.strip_heredoc
What to do next:
- See the documentation on https://github.com/rails/webpacker/blob/master/docs/webpack.md
for how to customize the default webpack configuration.
- Include your webpack assets to your application layout.
<%= javascript_pack_tag 'hello-world-bundle' %>
- Run `rails s` to start the Rails server and use Webpacker's default lazy compilation.
- Visit http://localhost:3000/hello_world and see your React On Rails app running!
- Run bin/webpack-dev-server to start the Webpack dev server for compilation of Webpack
assets as soon as you save. This default setup with the dev server does not work
for server rendering
- Alternately, you may turn off compile in config/webpacker.yml and run the foreman
command to start the rails server and run webpack in watch mode.
foreman start -f Procfile.dev-static
- To turn on HMR, edit config/webpacker.yml and set HMR to true. Restart the rails server
and bin/webpack-dev-server. Or use Procfile.dev.
- To server render, change this line app/views/hello_world/index.html.erb to
`prerender: true` to see server rendering (right click on page and select "view source").
<%= react_component("HelloWorldApp", props: @hello_world_props, prerender: true) %>
MSG
end
def print_helpful_message
GeneratorMessages.add_info(self.class.helpful_message)
end
private
# From https://github.com/rails/rails/blob/4c940b2dbfb457f67c6250b720f63501d74a45fd/railties/lib/rails/generators/rails/app/app_generator.rb
def app_name
@app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root))
.tr("\\", "").tr(". ", "_")
end
def defined_app_name
defined_app_const_base.underscore
end
def defined_app_const_base
Rails.respond_to?(:application) && defined?(Rails::Application) &&
Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, "")
end
alias defined_app_const_base? defined_app_const_base
def app_const_base
@app_const_base ||= defined_app_const_base || app_name.gsub(/\W/, "_").squeeze("_").camelize
end
def app_const
@app_const ||= "#{app_const_base}::Application"
end
def add_configure_rspec_to_compile_assets(helper_file)
search_str = "RSpec.configure do |config|"
gsub_file(helper_file, search_str, CONFIGURE_RSPEC_TO_COMPILE_ASSETS)
end
end
end
end