Skip to content

Commit 001aa9b

Browse files
committed
generator: refactor templates to default to auto registration
1 parent 18b6020 commit 001aa9b

File tree

9 files changed

+68
-15
lines changed

9 files changed

+68
-15
lines changed

lib/generators/react_on_rails/base_generator.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def add_hello_world_route
2222
end
2323

2424
def create_react_directories
25-
dirs = %w[components]
26-
dirs.each { |name| empty_directory("app/javascript/bundles/HelloWorld/#{name}") }
25+
# Create auto-registration directory structure
26+
empty_directory("app/javascript/src/HelloWorld/ror_components")
2727
end
2828

2929
def copy_base_files
@@ -44,8 +44,8 @@ def copy_base_files
4444
def copy_js_bundle_files
4545
base_path = "base/base/"
4646
base_files = %w[app/javascript/packs/server-bundle.js
47-
app/javascript/bundles/HelloWorld/components/HelloWorldServer.js
48-
app/javascript/bundles/HelloWorld/components/HelloWorld.module.css]
47+
app/javascript/src/HelloWorld/HelloWorldServer.js
48+
app/javascript/src/HelloWorld/HelloWorld.module.css]
4949
base_files.each { |file| copy_file("#{base_path}#{file}", file) }
5050
end
5151

@@ -60,7 +60,7 @@ def copy_webpack_config
6060
config/webpack/production.js
6161
config/webpack/serverWebpackConfig.js
6262
config/webpack/webpack.config.js
63-
config/webpack/webpackConfig.js]
63+
config/webpack/generateWebpackConfigs.js]
6464
config = {
6565
message: "// The source code including full typescript support is available at:"
6666
}
@@ -114,6 +114,22 @@ def add_js_dependencies
114114
], type: :dev)
115115
end
116116

117+
def update_gitignore_for_auto_registration
118+
gitignore_path = File.join(destination_root, ".gitignore")
119+
return unless File.exist?(gitignore_path)
120+
121+
gitignore_content = File.read(gitignore_path)
122+
return if gitignore_content.include?("**/generated/**")
123+
124+
append_to_file ".gitignore" do
125+
<<~GITIGNORE
126+
127+
# Generated React on Rails packs
128+
**/generated/**
129+
GITIGNORE
130+
end
131+
end
132+
117133
def append_to_spec_rails_helper
118134
rails_helper = File.join(destination_root, "spec/rails_helper.rb")
119135
if File.exist?(rails_helper)

lib/generators/react_on_rails/react_no_redux_generator.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ class ReactNoReduxGenerator < Rails::Generators::Base
1212

1313
def copy_base_files
1414
base_js_path = "base/base"
15-
base_files = %w[app/javascript/bundles/HelloWorld/components/HelloWorld.jsx]
15+
base_files = %w[app/javascript/src/HelloWorld/ror_components/HelloWorld.jsx]
1616
base_files.each { |file| copy_file("#{base_js_path}/#{file}", file) }
1717
end
1818

1919
def create_appropriate_templates
2020
base_path = "base/base"
2121
config = {
22-
component_name: "HelloWorld",
23-
app_relative_path: "../bundles/HelloWorld/components/HelloWorld"
22+
component_name: "HelloWorld"
2423
}
2524

26-
template("#{base_path}/app/javascript/packs/registration.js.tt",
27-
"app/javascript/packs/hello-world-bundle.js", config)
25+
# Only create the view template - no manual bundle needed for auto registration
2826
template("#{base_path}/app/views/hello_world/index.html.erb.tt",
2927
"app/views/hello_world/index.html.erb", config)
3028
end

lib/generators/react_on_rails/templates/base/base/app/javascript/packs/server-bundle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ReactOnRails from 'react-on-rails';
22

3-
import HelloWorld from '../bundles/HelloWorld/components/HelloWorldServer';
3+
import HelloWorld from '../src/HelloWorld/HelloWorldServer';
44

55
// This is how react_on_rails can see the HelloWorld in the browser.
66
ReactOnRails.register({
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.bright {
2+
color: green;
3+
font-weight: bold;
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import HelloWorld from './ror_components/HelloWorld';
2+
// This could be specialized for server rendering
3+
// For example, if using React Router, we'd have the SSR setup here.
4+
5+
export default HelloWorld;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import PropTypes from 'prop-types';
2+
import React, { useState } from 'react';
3+
import * as style from '../HelloWorld.module.css';
4+
5+
const HelloWorld = (props) => {
6+
const [name, setName] = useState(props.name);
7+
8+
return (
9+
<div>
10+
<h3>Hello, {name}!</h3>
11+
<hr />
12+
<form>
13+
<label className={style.bright} htmlFor="name">
14+
Say hello to:
15+
<input id="name" type="text" value={name} onChange={(e) => setName(e.target.value)} />
16+
</label>
17+
</form>
18+
</div>
19+
);
20+
};
21+
22+
HelloWorld.propTypes = {
23+
name: PropTypes.string.isRequired, // this is passed from the Rails view
24+
};
25+
26+
export default HelloWorld;

lib/generators/react_on_rails/templates/base/base/app/views/layouts/hello_world.html.erb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
<head>
44
<title>ReactOnRailsWithShakapacker</title>
55
<%= csrf_meta_tags %>
6-
<%= javascript_pack_tag 'hello-world-bundle' %>
7-
<%= stylesheet_pack_tag 'hello-world-bundle' %>
6+
7+
<!-- Empty pack tags - React on Rails injects component CSS/JS here -->
8+
<%= stylesheet_pack_tag %>
9+
<%= javascript_pack_tag %>
810
</head>
911

1012
<body>

lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ ReactOnRails.configure do |config|
5454
# For automated component registry, `render_component` view helper method tries to load bundle for component from
5555
# generated directory. default is false, you can pass option at the time of individual usage or update the default
5656
# in the following line
57-
config.auto_load_bundle = false
57+
config.auto_load_bundle = true
5858
end

lib/generators/react_on_rails/templates/base/base/config/shakapacker.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ default: &default
77
public_output_path: packs
88
cache_path: tmp/shakapacker
99
webpack_compile_output: true
10+
11+
# Enable nested entry points for auto-registration
1012
nested_entries: true
1113

1214
# Additional paths webpack should lookup modules
@@ -23,9 +25,9 @@ development:
2325

2426
# Reference: https://webpack.js.org/configuration/dev-server/
2527
dev_server:
26-
https: false
2728
host: localhost
2829
port: 3035
30+
server: http
2931
# Hot Module Replacement updates modules while the application is running without a full reload
3032
hmr: true
3133
client:

0 commit comments

Comments
 (0)