-
-
Notifications
You must be signed in to change notification settings - Fork 631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Support generator to build a template webpack setup #53
Conversation
Checking this out now! @rstudner can you give this a try? or any others on the team. I'll take a quick look as well. |
class DirectoryGenerator < Rails::Generators::Base | ||
source_root File.expand_path("../templates", __FILE__) | ||
|
||
def create_react_and_redux_dir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we, should we, have the flux system (redux) to be optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is okay if the project is 'opinionated' on Redux (and willing to switch to the most valuable Flux implementation in the future if it switches). Have an option, to 'opt-out' of having redux as the default. But it is okay if the generator provides 'react_on_rails' opinion on the best seed/starter setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @rstudner is correct, with an option to opt-out. 👍
For file: lib/generators/react_on_rails/templates/client/app/startup/clientGlobals.jsx and some of the other files, let's put in a few useful doc links on what is supposed to go in these files. We can provide:
|
|
||
def update_application_js | ||
insert_into_file "app/assets/javascripts/application.js", | ||
before: "//= require_tree ." do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no guarantee that require_tree .
will be in application.js
. I've never used that.
This is what we have in the tutorial:
// Need to be on top to allow Poltergeist test to work with React.
//= require es5-shim/es5-shim
//= require react_on_rails
// It is important that generated/vendor-bundle must be before bootstrap since it is exposing jQuery and jQuery-ujs
//= require generated/vendor-bundle
//= require generated/app-bundle
//= require bootstrap-sprockets
//= require turbolinks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I was basing this off a fresh rails project. I'll have to look through the Thor docs a little more to see what I can do to get it before bootstrap and before tree if tree exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 I think deleting require_tree in application.js is a first two-minutes operation for many projects :)
Wow! Great start! Let's consider getting this in v1.0.0, or very shortly thereafter. @aaronvb Don't hesitate to ask us if you have any questions. If you need any help with the suggestions, please let us know. This is very useful. |
@justin808 Thanks for the feedback. I'll be going through this over the next few days and will join you guys in Slack for any questions. A lot of what I have in here was extracted from the webpack tutorial, so I expected lots of changes and fixes. |
I've pushed updates to all the files noted above and added:
Install stays the same, except it doesn't create the redux folders or include the redux packages in package.json
Install with redux will do the same as the regular install, but create the redux directories and include redux in the package.json.
Please review the copy in both README files. TODO: Update the gem README with instructions on how to use the generator. |
Do we want to add a command line parameter for indicating 0.13 vs 0.14 setup (exposing ReactDOM and ReactDOMServer as appropriate) |
end | ||
|
||
def update_application_js | ||
append_to_file "app/assets/javascripts/application.js" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have one app that uses application.js.coffee and the generator breaks here. Also, one suggestion is to use prepend instead of append, because probably those requires should be on top.
bellow an attempt to add support for application.js.coffee and to show a message if no application.js/js.coffee file is found.
def update_application_js
data = "\n// It is important that generated/vendor-bundle must be before bootstrap since it is exposing jQuery and jQuery-ujs\n//= require generated/vendor-bundle\n//= require generated/app-bundle\n//= require react_on_rails\n\n"
if File.exists?("app/assets/javascripts/application.js")
prepend_to_file "app/assets/javascripts/application.js", data
elsif File.exists?("app/assets/javascripts/application.js.coffee")
prepend_to_file "app/assets/javascripts/application.js.coffee", data
else
puts "** app/assets/javascripts/application.js was not found. please add the following content to your main javascript file:\n\n#{data}\n\n"
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@derencius Awesome, thanks for the feedback. I'll will test this out now and update the method.
@rstudner @aaronvb Regarding any command line param for the old react, there's probably no need if we're doing a generator. As far as I can tell, React 0.14 gives warnings for the deprecations, so those are relatively easy to fix. That being said, there could be cross dependencies that might not yet like React 0.14. So if it's easy to add a param, then sure, but this is not a huge priority, at least not until at least one person really wants it. |
Correct -- is definitely not necessary. Just suggested it since some people get all upset when their chrome console has warnings in it they don't understand ;) |
@rstudner You mean terminal, right? |
@aaronvb would you say this is ready to go? I'll take a look at this tomorrow, and give it a spin. |
@justin808 In my opinion, yes. If all looks good to you, I will update the README.md in the gem with instructions on using the generate install. |
@justin808 Sorry I missed this in the email. yeah, I meant terminal sorry. If we didn't expose ReactDOMServer, then the gem would correctly fall back to just use React, which for 0.14 on the server side would generate terminal warnings. (0.15 they will remove the methods/deprecation warnings completely but that is in the future) |
@aaronvb I tested this PR on my project, using Rails 3.2.22/Ruby 2.2.2, it works very nice. Saving A LOT of boilerplate. Thanks for this 👍 I will report if I find anything useful. |
Does the Procfile.dev etc need to get created (is that part of the boilerplate) I do realize there are other ways to startup the rails & node servers in parallel etc. But since the react-webpack-rails-tutorial project has the 'opinion' to do Procfile.dev should this? |
Procfile.dev is very handy and well worth adding. |
I'm going to take this over for now. Thanks @aaronvb! |
I recommend setting the .babelrc stage to 2 or 1 as stage 0 is not considered recommended for production code. |
@gugl We're on the stage 0 mainly because of static properties. Moreover base libs (like |
Next up:
Per running steps: From directory of
Edit the Gemfile, adding these 2 lines: gem 'react_on_rails', path: '../react_on_rails'
gem 'therubyracer' Note the relative path to the react_on_rails gem. bundle
git commit -am "added react_on_rails gem"
rails generate react_on_rails:install Then you can see the changes that the generator did, ready for a commit. Then run: foreman start -f Procfile.dev Then visit ports 3000 and 4000. That's it! |
For extra credit:
However, we can do those in a separate PR. |
@aaronvb Let's run some basic sanity check that nvm, npm, node are installed and abort the generator with a link to get those installed. I think this is going to be the basic stumbling block for most of the ruby community when they first see this. |
Added linter gems to Gemfile. Added a simple react hello world component that demonstrates client rendering, server rendering, and hot reload. You can generate the hello world files by using Removed running webpack after initial install. Added a check for npm and node before installing, will abort with a message containing a url to the libraries. |
Fixing CI errors now. |
758477f
to
43d6b43
Compare
@aaronvb
Overall, very awesome! |
end | ||
|
||
task :clobber do | ||
rm_rf "#{RailsReactTutorial::Application.config.root}/app/assets/javascripts/generated/client-bundle.js" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is RailsReactTutorial
defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@necronet This needs to be updated, thanks for catching this. The assets.rake was pulled from https://github.com/shakacode/react-webpack-rails-tutorial - sorry for the confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@justin808 @aaronvb Changes made in tutorial on shakacode/react-webpack-rails-tutorial#129
|
|
||
server.listen(4000, 'localhost', function(err) { | ||
if (err) console.log(err); | ||
console.log('Listening at localhost:3000...'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldnt this be 'Listening at localhost:4000...' ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be:
console.log('Listening at localhost:' + this.address().port + '...');
rails generate react_on_rails:install rails generate react_on_rails:install --with-redux rails generate react_on_rails:install --with-hello-world-example rails generate react_on_rails:install --with-server-rendering rails generate react_on_rails:install --with-redux --with-server-rendering rails generate react_on_rails:install --with-hello-world-example --with-server-rendering
The work for this PR branch has been merged to a new branch on the original repo: https://github.com/shakacode/react_on_rails/tree/add-generators |
@justin808 I propose that we close this so as to ensure all future progress is made on #70, right? |
Created generators for:
I moved rails engine to its own file to be more explicit.
Regarding the npm package.json, I copied it from the tutorial repo. I'm not sure if all of those packages should be included in the gem, or if you want to slim it down to the barebones react libraries.