Add support to have ReactStyleFileName#32
Conversation
| **This is ALPHA Software** | ||
|
|
||
| This was built as a prototype to evaluate using react inside of our Ember apps. We are not yet using it in production. PRs and constructive questions and comments via [GitHub issues](https://github.com/AltSchool/ember-cli-react/issues/new) are highly encouraged. | ||
| This was built as a prototype to evaluate using react inside of our Ember apps. |
There was a problem hiding this comment.
Most of the changes were caused by Prettier (apparently I missed this file last time)
| highly recommended to have clean React component tree whenever possible for best | ||
| performance. | ||
|
|
||
| ## Using File Name Convention for React |
There was a problem hiding this comment.
This section is the real content addition
There was a problem hiding this comment.
This seems like a good explanation of why, but doesn't really make it clear what ember-cli-react is providing in terms of file name support. I think it should make it clear that 1) you can name your jsx files in PascalCase and 2) either import them in templates using standard dasherized syntax or refer to them there using PascalCase with the aid of react-component.
alexgb
left a comment
There was a problem hiding this comment.
Thanks for the detailed PR explanation. Your reasoning makes sense to me. I've left a few comments.
| }); | ||
|
|
||
| it('supports React-style component file name', function() { | ||
| this.render(hbs`{{react-component "react-style-file-name"}}`); |
There was a problem hiding this comment.
I think we should have a test for `{{react-component "ReactStyleFileName"}} as well
There was a problem hiding this comment.
Oh right, that's a valid test
| // A React-style file name is capitalized camel-cased. | ||
| resolveReactStyleFile(parsedName) { | ||
| const originalName = parsedName.fullNameWithoutType; | ||
| parsedName.fullNameWithoutType = Ember.String.classify(originalName); |
There was a problem hiding this comment.
Instead of mutating parsedName can we copy it?
const parsedNameWithReactStyle = Object.assign(
{},
parsedName,
{ fullNameWithoutType: Ember.String.classify(originalName) }
);This should mean you can get rid of line 51.
| highly recommended to have clean React component tree whenever possible for best | ||
| performance. | ||
|
|
||
| ## Using File Name Convention for React |
There was a problem hiding this comment.
This seems like a good explanation of why, but doesn't really make it clear what ember-cli-react is providing in terms of file name support. I think it should make it clear that 1) you can name your jsx files in PascalCase and 2) either import them in templates using standard dasherized syntax or refer to them there using PascalCase with the aid of react-component.
|
@alexgb I have added a lot of tests to cover several test cases, mainly around the cases when conflict occurs. The basis of all conflict resolution is "prefer React component over Ember component". Scenarios:
Ember component can never have PascalCase file name so only 3 scenarios exist. This behaviour was chosen to provide a simple resolution strategy for the following use cases: We can try to be smart and determine the right file by the different use case above, but I think it is overly complicated and not unnecessary. |
alexgb
left a comment
There was a problem hiding this comment.
Looks good, left one small comment.
I also worked on simplifying the Readme a bit, including some edits to the text here about file naming. I'll open a separate PR for these changes, perhaps as part of bringing to 1.0. cfc52a5
|
|
||
| // This resolver method attempt to find a file with React-style file name. | ||
| // A React-style file name is in PascalCase. | ||
| resolveReactStyleFile(parsedName) { |
There was a problem hiding this comment.
I believe the resolve* methods on the resolver determined at runtime based on factory types. So naming a method this way also adds a new factory type of react-style-file:*. Since I don't think that's the intention here I would suggest marking as a private method _resolveReactStyleFile.
There was a problem hiding this comment.
Ah great catch. I'll update this part :)
|
@pswai thanks for the great PR writeup on this one |
The file name convention for Ember components is
snake-case, however in React seems to bePascalCase. This PR adds support for ReactStyleFileName.Problem
Let's we at the 2 major use cases of using React in Ember:
For the first scenario, using Ember's convention makes sense because Ember and React are going to coexist. Having 2 file name conventions is confusing to developers.
However, for the second scenario, having 2 file name conventions becomes a good thing, because it shows clearly which components haven't been migrated yet. In fact, using Ember's convention for React components becomes awkward when importing components:
Also, it is painful at the end of the project to have to rename all React components to PascalCase and update all the imports.
Proposed Solution
With support for ReactStyleFileName, users for the second scenario can write their React components this way:
When the migration is done, no renaming is required since we have everything in place already. (Ok not yet, the
npm:prefix can be removed with #27).Caveat
Unfortunately we still can't use single-worded component in Handlebars:
However we can use it with the
react-componentcomponent: