Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d60a3c

Browse files
committed
fix(ComponentGenerated) include module-related code when generating for webpack
1 parent 6c70ee8 commit 4d60a3c

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

‎lib/generators/react/component_generator.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def create_component_file
100100
end
101101

102102
# Prefer webpacker to sprockets:
103-
if defined?(Webpacker)
103+
if webpacker?
104104
new_file_name = file_name.camelize
105105
extension = options[:coffee] ? "coffee" : "js"
106106
target_dir = Webpacker::Configuration.source_path
@@ -119,6 +119,30 @@ def create_component_file
119119

120120
private
121121

122+
def component_name
123+
file_name.camelize
124+
end
125+
126+
def file_header
127+
if webpacker?
128+
%|var React = require("react")\n|
129+
else
130+
""
131+
end
132+
end
133+
134+
def file_footer
135+
if webpacker?
136+
%|module.exports = #{component_name}|
137+
else
138+
""
139+
end
140+
end
141+
142+
def webpacker?
143+
defined?(Webpacker)
144+
end
145+
122146
def parse_attributes!
123147
self.attributes = (attributes || []).map do |attr|
124148
name, type, options = "", "", ""

‎lib/generators/templates/component.es6.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class <%= file_name.camelize %> extends React.Component {
1+
<%=file_header%>class <%= component_name %> extends React.Component {
22
render () {
33
<% if attributes.size > 0 -%>
44
return (
@@ -21,3 +21,4 @@ class <%= file_name.camelize %> extends React.Component {
2121
<% end -%>
2222
};
2323
<% end -%>
24+
<%= file_footer %>

‎lib/generators/templates/component.js.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var <%= file_name.camelize %> = React.createClass({
1+
<%=file_header%>var <%= component_name %> = React.createClass({
22
<% if attributes.size > 0 -%>
33
propTypes: {
44
<% attributes.each_with_index do |attribute, idx| -%>
@@ -21,3 +21,4 @@ var <%= file_name.camelize %> = React.createClass({
2121
<% end -%>
2222
}
2323
});
24+
<%= file_footer %>

‎lib/generators/templates/component.js.jsx.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class @<%= file_name.camelize %> extends React.Component
1+
class @<%= component_name %> extends React.Component
22
<% if attributes.size > 0 -%>
33
@propTypes =
44
<% attributes.each do |attribute| -%>
@@ -15,4 +15,4 @@ class @<%= file_name.camelize %> extends React.Component
1515
</div>`
1616
<% else -%>
1717
`<div />`
18-
<% end -%>
18+
<% end -%>

‎test/dummy/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"rails-erb-loader": "^4.0.0",
2222
"react": "^15.4.2",
2323
"react-dom": "^15.4.2",
24-
"react_ujs": "^2.0.2",
24+
"react_ujs": "^2.1.0",
2525
"sass-loader": "^6.0.3",
2626
"style-loader": "^0.16.1",
2727
"webpack": "^2.3.3",

‎test/dummy/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,9 +3450,9 @@ react@^15.4.2:
34503450
loose-envify "^1.1.0"
34513451
object-assign "^4.1.0"
34523452

3453-
react_ujs@^2.0.2:
3454-
version "2.0.2"
3455-
resolved "https://registry.yarnpkg.com/react_ujs/-/react_ujs-2.0.2.tgz#0020f38fa2e47a04c03faa080325324bfa6d1f0b"
3453+
react_ujs@^2.1.0:
3454+
version "2.1.0"
3455+
resolved "https://registry.yarnpkg.com/react_ujs/-/react_ujs-2.1.0.tgz#bef224350b09b82b01fc678718a257192d5bbf1f"
34563456

34573457
read-cache@^1.0.0:
34583458
version "1.0.0"

‎test/generators/component_generator_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ def filename
1919
test "creates the component file" do
2020
run_generator %w(GeneratedComponent)
2121

22-
assert_file filename
22+
assert_file filename do |contents|
23+
if WebpackerHelpers.available?
24+
assert_match /^var React = require\("react"\)/, contents
25+
assert_match /module\.exports = GeneratedComponent\n$/m, contents
26+
end
27+
end
2328
end
2429

2530
test "creates the component file with a node argument" do

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /