Skip to content

Commit 379147e

Browse files
committed
feat(ComponentGenerator) output components to Webpacker source dir if present
1 parent 865f80e commit 379147e

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed

lib/generators/react/component_generator.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
44
source_root File.expand_path '../../templates', __FILE__
55
desc <<-DESC.strip_heredoc
66
Description:
7-
Scaffold a react component into app/assets/javascripts/components.
7+
Scaffold a React component into `components/` of your Webpacker source or asset pipeline.
88
The generated component will include a basic render function and a PropTypes
99
hash to help with development.
1010
@@ -90,17 +90,29 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
9090
}
9191

9292
def create_component_file
93-
extension = case
94-
when options[:es6]
95-
'es6.jsx'
96-
when options[:coffee]
97-
'js.jsx.coffee'
98-
else
99-
'js.jsx'
100-
end
101-
102-
file_path = File.join('app/assets/javascripts/components', "#{file_name}.#{extension}")
103-
template("component.#{extension}", file_path)
93+
template_extension = case
94+
when options[:es6]
95+
'es6.jsx'
96+
when options[:coffee]
97+
'js.jsx.coffee'
98+
else
99+
'js.jsx'
100+
end
101+
102+
# Prefer webpacker to sprockets:
103+
if defined?(Webpacker)
104+
extension = options[:coffee] ? "coffee" : "js"
105+
target_dir = Webpacker::Configuration.source_path
106+
.join("components")
107+
.relative_path_from(::Rails.root)
108+
.to_s
109+
else
110+
extension = template_extension
111+
target_dir = 'app/assets/javascripts/components'
112+
end
113+
114+
file_path = File.join(target_dir, "#{file_name}.#{extension}")
115+
template("component.#{template_extension}", file_path)
104116
end
105117

106118
private

test/generators/coffee_component_generator_test.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ class CoffeeComponentGeneratorTest < Rails::Generators::TestCase
66
setup :prepare_destination
77
tests React::Generators::ComponentGenerator
88

9-
def filename
10-
'app/assets/javascripts/components/generated_component.js.jsx.coffee'
9+
if WebpackerHelpers.available?
10+
def filename
11+
"app/javascript/components/generated_component.coffee"
12+
end
13+
else
14+
def filename
15+
'app/assets/javascripts/components/generated_component.js.jsx.coffee'
16+
end
1117
end
12-
1318
def class_name
1419
'GeneratedComponent'
1520
end

test/generators/component_generator_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ class ComponentGeneratorTest < Rails::Generators::TestCase
66
setup :prepare_destination
77
tests React::Generators::ComponentGenerator
88

9-
def filename
10-
'app/assets/javascripts/components/generated_component.js.jsx'
9+
if WebpackerHelpers.available?
10+
def filename
11+
"app/javascript/components/generated_component.js"
12+
end
13+
else
14+
def filename
15+
'app/assets/javascripts/components/generated_component.js.jsx'
16+
end
1117
end
1218

1319
test "creates the component file" do

test/generators/es6_component_generator_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ class Es6ComponentGeneratorTest < Rails::Generators::TestCase
66
setup :prepare_destination
77
tests React::Generators::ComponentGenerator
88

9-
def filename
10-
'app/assets/javascripts/components/generated_component.es6.jsx'
9+
if WebpackerHelpers.available?
10+
def filename
11+
"app/javascript/components/generated_component.js"
12+
end
13+
else
14+
def filename
15+
'app/assets/javascripts/components/generated_component.es6.jsx'
16+
end
1117
end
1218

1319
def class_name

test/support/webpacker_helpers.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module WebpackerHelpers
22
module_function
3+
def available?
4+
defined?(Webpacker)
5+
end
36

47
def when_webpacker_available
5-
if defined?(Webpacker)
8+
if available?
69
clear_webpacker_packs
710
Dir.chdir("./test/dummy") do
811
Rake::Task['webpacker:compile'].invoke

0 commit comments

Comments
 (0)