File tree Expand file tree Collapse file tree 5 files changed +52
-20
lines changed
Expand file tree Collapse file tree 5 files changed +52
-20
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11module 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
You can’t perform that action at this time.
0 commit comments