react_on_rails 9.0.0.beta.11 → 9.0.0.beta.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintignore +2 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +5 -0
  5. data/.travis.yml +11 -0
  6. data/CHANGELOG.md +24 -9
  7. data/Gemfile +2 -2
  8. data/Gemfile.rails32 +74 -0
  9. data/README.md +17 -12
  10. data/Rakefile +8 -1
  11. data/app/helpers/react_on_rails_helper.rb +1 -1
  12. data/docs/additional-reading/asset-pipeline.md +20 -0
  13. data/docs/additional-reading/hot-reloading-rails-development.md +0 -75
  14. data/docs/additional-reading/rspec-configuration.md +6 -23
  15. data/{lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb.tt → docs/basics/configuration.md} +78 -41
  16. data/docs/basics/generator.md +0 -5
  17. data/docs/basics/installation-overview.md +5 -4
  18. data/docs/tutorial.md +25 -13
  19. data/lib/generators/USAGE +0 -4
  20. data/lib/generators/react_on_rails/base_generator.rb +41 -81
  21. data/lib/generators/react_on_rails/dev_tests_generator.rb +7 -5
  22. data/lib/generators/react_on_rails/install_generator.rb +5 -10
  23. data/lib/generators/react_on_rails/react_no_redux_generator.rb +10 -9
  24. data/lib/generators/react_on_rails/react_with_redux_generator.rb +27 -17
  25. data/lib/generators/react_on_rails/templates/.eslintrc +2 -0
  26. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +4 -0
  27. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-server +5 -0
  28. data/lib/generators/react_on_rails/templates/base/base/{client/app → app/javascript}/bundles/HelloWorld/components/HelloWorld.jsx +0 -0
  29. data/lib/generators/react_on_rails/templates/base/base/{client/app/bundles/HelloWorld/startup/registration.jsx.tt → app/javascript/packs/registration.js.tt} +0 -0
  30. data/lib/generators/react_on_rails/templates/base/base/app/views/hello_world/index.html.erb.tt +0 -1
  31. data/lib/generators/react_on_rails/templates/base/base/app/views/layouts/{hello_world.html.erb.tt → hello_world.html.erb} +3 -3
  32. data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +41 -0
  33. data/lib/generators/react_on_rails/templates/dev_tests/.eslintrc +25 -0
  34. data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +0 -1
  35. data/lib/generators/react_on_rails/templates/dev_tests/spec/spec_helper.rb +2 -0
  36. data/lib/generators/react_on_rails/templates/redux/base/{client/app/bundles/HelloWorld/actions/helloWorldActionCreators.jsx → app/javascript/bundles/HelloWorld/actions/helloWorldActionCreators.js} +0 -0
  37. data/lib/generators/react_on_rails/templates/redux/base/{client/app → app/javascript}/bundles/HelloWorld/components/HelloWorld.jsx +0 -0
  38. data/lib/generators/react_on_rails/templates/redux/base/{client/app/bundles/HelloWorld/constants/helloWorldConstants.jsx → app/javascript/bundles/HelloWorld/constants/helloWorldConstants.js} +0 -0
  39. data/lib/generators/react_on_rails/templates/redux/base/{client/app/bundles/HelloWorld/containers/HelloWorldContainer.jsx → app/javascript/bundles/HelloWorld/containers/HelloWorldContainer.js} +0 -0
  40. data/lib/generators/react_on_rails/templates/redux/base/{client/app/bundles/HelloWorld/reducers/helloWorldReducer.jsx → app/javascript/bundles/HelloWorld/reducers/helloWorldReducer.js} +0 -0
  41. data/lib/generators/react_on_rails/templates/redux/base/{client/app → app/javascript}/bundles/HelloWorld/startup/HelloWorldApp.jsx +0 -0
  42. data/lib/generators/react_on_rails/templates/redux/base/{client/app/bundles/HelloWorld/store/helloWorldStore.jsx → app/javascript/bundles/HelloWorld/store/helloWorldStore.js} +0 -0
  43. data/lib/react_on_rails/configuration.rb +15 -10
  44. data/lib/react_on_rails/server_rendering_pool/exec.rb +21 -8
  45. data/lib/react_on_rails/test_helper.rb +6 -6
  46. data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +1 -1
  47. data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +3 -1
  48. data/lib/react_on_rails/test_helper/webpack_assets_status_checker.rb +10 -6
  49. data/lib/react_on_rails/utils.rb +34 -3
  50. data/lib/react_on_rails/version.rb +1 -1
  51. data/lib/react_on_rails/version_checker.rb +1 -1
  52. data/lib/tasks/assets.rake +10 -2
  53. data/package.json +6 -2
  54. data/rakelib/dummy_apps.rake +9 -0
  55. data/rakelib/example_type.rb +8 -19
  56. data/rakelib/examples.rake +3 -5
  57. data/rakelib/run_rspec.rake +17 -0
  58. data/yarn.lock +29 -0
  59. metadata +19 -23
  60. data/lib/generators/react_on_rails/node_generator.rb +0 -24
  61. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev.tt +0 -4
  62. data/lib/generators/react_on_rails/templates/base/base/client/.babelrc +0 -3
  63. data/lib/generators/react_on_rails/templates/base/base/client/REACT_ON_RAILS_CLIENT_README.md +0 -9
  64. data/lib/generators/react_on_rails/templates/base/base/client/package.json.tt +0 -36
  65. data/lib/generators/react_on_rails/templates/base/base/client/webpack.config.js +0 -78
  66. data/lib/generators/react_on_rails/templates/base/base/config/webpacker.yml +0 -28
  67. data/lib/generators/react_on_rails/templates/base/base/package.json.tt +0 -8
  68. data/lib/generators/react_on_rails/templates/node/base/client/node/package.json +0 -10
  69. data/lib/generators/react_on_rails/templates/node/base/client/node/server.js +0 -105
@@ -5,7 +5,6 @@ ReactOnRails provides a helper method called `ReactOnRails::TestHelper.configure
5
5
 
6
6
  ```ruby
7
7
  RSpec.configure do |config|
8
- # Next line will ensure that assets are built if webpack -w is not running to build the bundles
9
8
  ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
10
9
  ```
11
10
 
@@ -24,7 +23,9 @@ If you are using Webpack to build CSS assets, you should do something like this
24
23
  ```
25
24
 
26
25
  Please take note of the following:
27
- - This utility uses your `npm_build_test_command` to build the static generated files. This command **must** not include the `--watch` option. If you have different server and client bundle files, this command **must** create all the bundles.
26
+ - This utility uses your `build_test_command` to build the static generated files. This command **must not** include the `--watch` option. If you have different server and client bundle files, this command **must** create all the bundles. If you are using webpacker, the default value will come from the `config/webpacker.yml` value for the `public_output_path` and the `source_path`
27
+ - If you add an older file to your source files, that is already older than the produced output files, no new recompilation is done. The solution to this issue is to clear out your directory of webpack generated files when adding new source files that may have older dates. This is actually a common occurrence when you've built your test generated files and then you sync up your repository files.
28
+
28
29
  - By default, the webpack processes look for the `app/assets/webpack` folders (configured as setting `webpack_generated_files` in the `config/react_on_rails.rb`. If this folder is missing, is empty, or contains files in the `config.webpack_generated_files` list with `mtime`s older than any of the files in your `client` folder, the helper will recompile your assets. You can override the location of these files inside of `config/initializers/react_on_rails.rb` by passing a filepath (relative to the root of the app) to the `generated_assets_dir` configuration option.
29
30
 
30
31
  The following `config/react_on_rails.rb` settings **must** match your setup:
@@ -35,37 +36,19 @@ The following `config/react_on_rails.rb` settings **must** match your setup:
35
36
  config.generated_assets_dir = File.join(%w[public webpack], Rails.env)
36
37
 
37
38
  # Define the files we need to check for webpack compilation when running tests.
39
+ # Generally, the manifest.json is good enough for this check if using webpacker
38
40
  config.webpack_generated_files = %w( hello-world-bundle.js )
39
41
 
40
42
  # If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
41
43
  # with rspec then this controls what yarn command is run
42
44
  # to automatically refresh your webpack assets on every test run.
43
- config.npm_build_test_command = "yarn run build:test"
45
+ config.build_test_command = "yarn run build:test"
44
46
  ```
45
47
 
46
- If you want to speed up the re-compiling process, you can call `yarn run build:development` (per below script) to have webpack run in "watch" mode and recompile these files in the background, which will be much faster when making incremental changes than compiling from scratch, assuming you have your setup like this:
47
-
48
- ```
49
- "scripts": {
50
- "build:test": "webpack --config webpack.config.js",
51
- "build:production": "NODE_ENV=production webpack --config webpack.config.js",
52
- "build:development": "webpack -w --config webpack.config.js"
53
- },
54
- ```
48
+ If you want to speed up the re-compiling process so you don't wait to run your tests to build the files, you can run your test compilation with the "watch" flags.
55
49
 
56
50
  [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy) contains examples of how to set the proc files for this purpose.
57
51
 
58
- ## Hot Reloading
59
- If you're using the hot reloading setup, you'll be running a Webpack development server to provide the JavaScript and mabye CSS assets to your Rails app. If you're doing server rendering, you'll also have a webpack watch process to refresh the server rendering file. **If your server and client bundles are different**, and you run specs, the client bundle files will not be created until React on Rails detects it's out of date. Then your script to create all bundle files will redo the server bundle file. There's a few simple remedies for this situation:
60
-
61
- 1. Switch to using static compilation of the webpack bundle files. This way, you're running a Webpack watch process to generate the same files used for tests as well as developmentment. You should have a separate Procfile for doing development using a "static" setup, rather than a "hot" setup.
62
- 2. Change your Procfile for starting the hot reloading server to also run a webpack process to automatically rebuild a the client assets while also doing hot reloading for browser testing.
63
-
64
- Note, none of these issues matter if you're using the same file for both server and client side rendering.
65
-
66
- ## Using RubyMine and other IDEs that save files right before invoking tests
67
- We had previously tried to allow using a process check to see if a Webpack watch process would already be automatically compiling the files. However, we removed this as it proved to be fragile with various setups. Thus, you want to hit the save keystroke when you save a JavaScript file, and then run your tests. Yes, you might not do this in time, and the test runner may be building the same files as you Webpack watch processes. This will probably happen infrequently, but if this does become an issue, we can look into bring back this functionality ([#398](https://github.com/shakacode/react_on_rails/pull/398)).
68
-
69
52
  If you want to use a testing framework other than RSpec, please submit let us know on the changes you need to do and we'll update the docs.
70
53
 
71
54
  ![2016-01-27_02-36-43](https://cloud.githubusercontent.com/assets/1118459/12611951/7c56d070-c4a4-11e5-8a80-9615f99960d9.png)
@@ -1,54 +1,92 @@
1
- # Shown below are the defaults for configuration
2
- ReactOnRails.configure do |config|
3
- # Client bundles are configured in application.js
1
+ Here is the full set of config options.
4
2
 
5
- # Directory where your generated assets go. All generated assets must go to the same directory.
6
- # Configure this in your webpack config files. This relative to your Rails root directory.
7
- config.generated_assets_dir = File.join(%w[public webpack], Rails.env)
3
+ ```yaml
4
+ # frozen_string_literal: true
8
5
 
9
- # Define the files we need to check for webpack compilation when running tests.
10
- config.webpack_generated_files = %w( hello-world-bundle.js )
6
+ # NOTE: you typically will leave the commented out configurations set to their defaults.
7
+ # Thus, you only need to pay careful attention to the non-commented settings in this file.
11
8
 
12
- # This is the file used for server rendering of React when using `(prerender: true)`
13
- # If you are never using server rendering, you may set this to "".
14
- # If you are using the same file for client and server rendering, having this set probably does
15
- # not affect performance.
16
- config.server_bundle_js_file = "hello-world-bundle.js"
17
-
18
- # If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
19
- # with rspec then this controls what yarn command is run
20
- # to automatically refresh your webpack assets on every test run.
21
- config.npm_build_test_command = "yarn run build:test"
9
+ ReactOnRails.configure do |config|
10
+ # defaults to "" (top level)
11
+ #
12
+ config.node_modules_location = ""
22
13
 
23
14
  # This configures the script to run to build the production assets by webpack. Set this to nil
24
15
  # if you don't want react_on_rails building this file for you.
25
- config.npm_build_production_command = "yarn run build:production"
16
+ config.npm_build_production_command = "RAILS_ENV=production bin/webpack"
26
17
 
27
18
  ################################################################################
28
- # CLIENT RENDERING OPTIONS
29
- # Below options can be overriden by passing options to the react_on_rails
30
- # `render_component` view helper method.
31
19
  ################################################################################
32
- # default is false
33
- config.prerender = false
20
+ # TEST CONFIGURATION OPTIONS
21
+ # Below options are used with the use of this test helper:
22
+ # ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
23
+ ################################################################################
34
24
 
35
- # default is true for development, off otherwise
36
- config.trace = Rails.env.development?
25
+ # If you are using this in your spec_helper.rb (or rails_helper.rb):
26
+ #
27
+ # ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
28
+ #
29
+ # with rspec then this controls what yarn command is run
30
+ # to automatically refresh your webpack assets on every test run.
31
+ #
32
+ config.build_test_command = "RAILS_ENV=test bin/webpack"
37
33
 
34
+ # Directory where your generated assets go. All generated assets must go to the same directory.
35
+ # If you are using webpacker, this value will come from your config/webpacker.yml file.
36
+ # This is the default in webpacker.yml:
37
+ # public_output_path: packs-test
38
+ # which means files in /public/packs-test
39
+ #
40
+ # Alternately, you may configure this. It is relative to your Rails root directory.
41
+ # A custom, non-webpacker, config might use something like:
42
+ #
43
+ config.generated_assets_dir = File.join(%w[public webpack], Rails.env)
44
+
45
+ # The test helper needs to know where your JavaScript files exist. The default is configured
46
+ # by your config/webpacker.yml soure_path:
47
+ # source_path: app/javascript
48
+ #
49
+ # If you have a non-default `node_modules_location`, that is assumed to be the location of your source
50
+ # files.
51
+
52
+ # Define the files we need to check for webpack compilation when running tests.
53
+ # The default is `%w( manifest.json )` as will be sufficient for most webpacker builds.
54
+ #
55
+ config.webpack_generated_files = %w( manifest.json )
56
+
57
+ ################################################################################
38
58
  ################################################################################
39
59
  # SERVER RENDERING OPTIONS
40
60
  ################################################################################
61
+ # This is the file used for server rendering of React when using `(prerender: true)`
62
+ # If you are never using server rendering, you should set this to "".
63
+ # Note, there is only one server bundle, unlike JavaScript where you want to minimize the size
64
+ # of the JS sent to the client. For the server rendering, React on Rails creates a pool of
65
+ # JavaScript execution instances which should handle any component requested.
66
+ #
67
+ # While you may configure this to be the same as your client bundle file, this file is typically
68
+ # different.
69
+ config.server_bundle_js_file = "server-bundle.js"
70
+
41
71
  # If set to true, this forces Rails to reload the server bundle if it is modified
72
+ # Default value is Rails.env.development?
73
+ #
42
74
  config.development_mode = Rails.env.development?
43
75
 
44
76
  # For server rendering. This can be set to false so that server side messages are discarded.
45
77
  # Default is true. Be cautious about turning this off.
78
+ # Default value is true
79
+ #
46
80
  config.replay_console = true
47
81
 
48
82
  # Default is true. Logs server rendering messages to Rails.logger.info
83
+ #
49
84
  config.logging_on_server = true
50
85
 
51
- config.raise_on_prerender_error = false # change to true to raise exception on server if the JS code throws
86
+ # Default is to false to NOT raise exception on server if the JS code throws.
87
+ # Reason is that it's easier to debug this when you get the error over to the client.
88
+ #
89
+ config.raise_on_prerender_error = false
52
90
 
53
91
  # Server rendering only (not for render_component helper)
54
92
  # You can configure your pool of JS virtual machines and specify where it should load code:
@@ -70,22 +108,21 @@ ReactOnRails.configure do |config|
70
108
  #
71
109
  # Replace the following line to the location where you keep your client i18n yml files
72
110
  # that will source for automatic generation on translations.js & default.js
73
- # By default(without this option) all yaml files from Rails.root.join("config", "locales") and installed gems are loaded
74
- # config.i18n_yml_dir = Rails.root.join("config", "locales", "client")
111
+ # By default(without this option) all yaml files from Rails.root.join("config", "locales")
112
+ # and installed gems are loaded
113
+ config.i18n_yml_dir = Rails.root.join("config", "locales", "client")
75
114
 
76
115
  ################################################################################
77
- # MISCELLANEOUS OPTIONS
78
116
  ################################################################################
117
+ # CLIENT RENDERING OPTIONS
118
+ # Below options can be overriden by passing options to the react_on_rails
119
+ # `render_component` view helper method.
120
+ ################################################################################
121
+ # default is false
122
+ config.prerender = false
79
123
 
80
- # The server render method - either ExecJS or NodeJS
81
- <%- if options.node? -%>
82
- config.server_render_method = "NodeJS"
83
- <%- else -%>
84
- config.server_render_method = "ExecJS"
85
- <%- end -%>
86
-
87
- # If you want to use webpack for CSS and images, and still use the asset pipeline,
88
- # see https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/rails-assets.md
89
- # And you will use a setting like this.
90
- # config.symlink_non_digested_assets_regex = /\.(png|jpg|jpeg|gif|tiff|woff|ttf|eot|svg|map)/
124
+ # default is true for development, off otherwise
125
+ config.trace = Rails.env.development?
91
126
  end
127
+
128
+ ```
@@ -13,7 +13,6 @@ Usage:
13
13
 
14
14
  Options:
15
15
  -R, [--redux], [--no-redux] # Install Redux gems and Redux version of Hello World Example. Default: false
16
- -N, [--node], [--no-node] # Sets up node as a server rendering option. Default: false
17
16
  [--ignore-warnings], [--no-ignore-warnings] # Skip warnings. Default: false
18
17
 
19
18
  Runtime options:
@@ -70,7 +69,3 @@ If you have used the `--redux` generator option, you will notice the familiar ad
70
69
 
71
70
  Note the organizational paradigm of "bundles". These are like application domains and are used for grouping your code into webpack bundles, in case you decide to create different bundles for deployment. This is also useful for separating out logical parts of your application. The concept is that each bundle will have it's own Redux store. If you have code that you want to reuse across bundles, including components and reducers, place them under `/client/app/lib`.
72
71
 
73
- ### Using Images and Fonts
74
- The generator has amended the folders created in `client/assets/` to Rails's asset path. We recommend that if you have any existing assets that you want to use with your client code, you should move them to these folders and use webpack as normal. This allows webpack's development server to have access to your assets, as it will not be able to see any assets in the default Rails directories which are above the `/client` directory.
75
-
76
- Alternatively, if you have many existing assets and don't wish to move them, you could consider creating symlinks from client/assets that point to your Rails assets folders inside of `app/assets/`. The assets there will then be visible to both Rails and webpack.
@@ -4,11 +4,14 @@ Here's an overview of installation if you're not using the generator.
4
4
 
5
5
  Note, the best way to understand how to use ReactOnRails is to study the examples:
6
6
 
7
- 1. [spec/dummy](../../spec/dummy): Simple, no DB example.
8
- 2. [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial): Full featured example.
7
+ 1. Run the generator per the [Tutorial](../../tutorial.md).
8
+ 2. [spec/dummy](../../spec/dummy): Simple, no DB example.
9
+ 3. [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial): Full featured example.
9
10
 
10
11
  ## Configure the `/client` Directory
11
12
 
13
+ Note, version 9.0 of React on Rails removed the requirement to place all client side files in the `/client` directory.
14
+
12
15
  This directory has no references to Rails outside of the destination directory for the files created by the various Webpack config files.
13
16
 
14
17
  The only requirements within this directory for basic React on Rails integration are:
@@ -27,8 +30,6 @@ The default path: `public/webpack` can be loaded with webpackConfigLoader as sho
27
30
  1. Configure the `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [spec/dummy/config/initializers/react_on_rails.rb](../../spec/dummy/config/initializers/react_on_rails.rb) for a detailed example of configuration, including comments on the different values to configure.
28
31
  1. Configure your Procfiles per the example apps. These are at the root of your Rails installation.
29
32
  1. Configure your top level JavaScript files for inclusion in your layout. You'll want a version that you use for static assets, and you want a file for any files in your setup that are not part of your webpack build. The reason for this is for use with hot-reloading. If you are not using hot reloading, then you only need to configure your `application.js` file to include your Webpack generated files. For more information on hot reloading, see [Hot Reloading of Assets For Rails Development](../additional-reading/hot-reloading-rails-development.md)
30
-
31
- 1. Configure your `lib/tasks/assets.rake` file to run webpack during asset precompilation.
32
33
  1. If you are deploying to Heroku, see [heroku-deployment.md](/docs/additional-reading/heroku-deployment.md)
33
34
 
34
35
  If I missed anything, please submit a PR or file an issue.
@@ -1,6 +1,6 @@
1
1
  # React on Rails Basic Tutorial
2
2
 
3
- This tutorial setups up a new Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering. It is updated to 8.0.0.
3
+ This tutorial setups up a new Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering. It is updated to 9.0.0.
4
4
 
5
5
  After finishing this tutorial you will get an application that can do the following (live on Heroku):
6
6
 
@@ -28,47 +28,59 @@ nvm list # check
28
28
 
29
29
  brew install yarn # you can use other installer if desired
30
30
 
31
- rvm install 2.3.1 # download and install latest stable Ruby (update to exact version)
32
- rvm use 2.3.1 --default # use it and make it default
31
+ rvm install 2.4.1 # download and install latest stable Ruby (update to exact version)
32
+ rvm use 2.4.1 --default # use it and make it default
33
33
  rvm list # check
34
34
 
35
35
  gem install rails # download and install latest stable Rails
36
36
  gem install foreman # download and install Foreman
37
37
  ```
38
38
 
39
- Then we need to create a fresh Rails application as following:
39
+ Then we need to create a fresh Rails application with webpacker react support as following:
40
40
 
41
41
  ```
42
- cd <basic directory where you want to create your new Rails app>
42
+ cd <directory where you want to create your new Rails app>
43
43
 
44
- rails new test-react-on-rails # any name you like
44
+ # any name you like for the rails app
45
+ rails new test-react-on-rails --webpack=react
45
46
 
46
47
  cd test-react-on-rails
47
48
  ```
48
49
 
50
+ Note: you can do the following two commands in an existing Rails app:
51
+
52
+ ```
53
+ bundle exec rails webpacker:install
54
+ bundle exec rails webpacker:install:react
55
+ ```
56
+
57
+
49
58
  ![01](https://cloud.githubusercontent.com/assets/20628911/17464917/3c29e55a-5cf2-11e6-8754-046ba3ee92d9.png)
50
59
 
51
- Add **React On Rails** gem to your Gemfile (`vim Gemfile` or `nano Gemfile` or in IDE):
60
+
61
+ Add the **React On Rails** gem to your Gemfile:
52
62
 
53
63
  ```
54
- gem 'react_on_rails', '8.0.0' # use latest gem version, prefer exact version
64
+ gem 'react_on_rails', '9.0.0' # use latest gem version, prefer exact version
55
65
  ```
56
66
 
67
+
57
68
  ![02](https://cloud.githubusercontent.com/assets/20628911/17464919/3c2d74c2-5cf2-11e6-8704-a84958832fbb.png)
58
69
 
59
- put everything under git repository (or `rails generate` will not work properly)
70
+ Then run `bundle` and commit the git repository (or `rails generate` will not work properly)
60
71
 
61
72
  ```
62
- # Here are git commands to make a new git repo and commit everything
63
- git init
73
+ bundle
74
+
75
+ # Here are git commands to make a new git repo and commit everything.
76
+ # Newer versions of Rails create the git repo by default.
64
77
  git add -A
65
78
  git commit -m "Initial commit"
66
79
  ```
67
80
 
68
- update dependencies and generate empty app via `react_on_rails:install` or `react_on_rails:install --redux`. You need to first git commit your files before running the generator, or else it will generate an error.
81
+ Install React on Rails: `rails generator react_on_rails:install` or `rails generate react_on_rails:install --redux`. You need to first git commit your files before running the generator, or else it will generate an error.
69
82
 
70
83
  ```
71
- bundle
72
84
  rails generate react_on_rails:install
73
85
  bundle && yarn
74
86
  ```
@@ -9,10 +9,6 @@ can pass the redux option if you'd like to have redux setup for you automaticall
9
9
  to integrate the Redux state container framework. The necessary node modules
10
10
  will be automatically included for you.
11
11
 
12
- * Node
13
-
14
- Passing the --node generator option sets up the necessary files for node to render the react_components.
15
-
16
12
  *******************************************************************************
17
13
 
18
14
  After running the generator, you will want to:
@@ -22,63 +22,28 @@ module ReactOnRails
22
22
  route "get 'hello_world', to: 'hello_world#index'"
23
23
  end
24
24
 
25
- def update_git_ignore
26
- data = <<-DATA.strip_heredoc
27
- # React on Rails
28
- npm-debug.log*
29
- node_modules
30
-
31
- # Generated js bundles
32
- /public/webpack/*
33
- DATA
34
-
35
- if dest_file_exists?(".gitignore")
36
- append_to_file(".gitignore", data)
37
- else
38
- GeneratorMessages.add_error(setup_file_error(".gitignore", data))
39
- end
40
- end
41
-
42
25
  def create_react_directories
43
- dirs = %w[components containers startup]
44
- dirs.each { |name| empty_directory("client/app/bundles/HelloWorld/#{name}") }
26
+ dirs = %w[components]
27
+ dirs.each { |name| empty_directory("app/javascript/bundles/HelloWorld/#{name}") }
45
28
  end
46
29
 
47
30
  def copy_base_files
48
31
  base_path = "base/base/"
49
32
  base_files = %w[app/controllers/hello_world_controller.rb
50
- config/webpacker.yml
51
- client/.babelrc
52
- client/webpack.config.js
53
- client/REACT_ON_RAILS_CLIENT_README.md]
33
+ app/views/layouts/hello_world.html.erb
34
+ config/initializers/react_on_rails.rb
35
+ Procfile.dev
36
+ Procfile.dev-server]
54
37
  base_files.each { |file| copy_file("#{base_path}#{file}", file) }
55
38
  end
56
39
 
57
- def template_base_files
58
- base_path = "base/base/"
59
- %w[app/views/layouts/hello_world.html.erb
60
- config/initializers/react_on_rails.rb
61
- Procfile.dev
62
- client/package.json].each { |file| template("#{base_path}#{file}.tt", file) }
63
- end
64
-
65
- def template_package_json
66
- if dest_file_exists?("package.json")
67
- add_yarn_postinstall_script_in_package_json
68
- else
69
- template("base/base/package.json", "package.json")
70
- end
40
+ def add_base_gems_to_gemfile
41
+ gem "mini_racer", platforms: :ruby
42
+ run "bundle"
71
43
  end
72
44
 
73
- def add_base_gems_to_gemfile
74
- gems = <<-GEMS.strip_heredoc
75
-
76
- gem 'mini_racer', platforms: :ruby
77
- gem 'webpacker', git: "https://github.com/shakacode/webpacker.git",
78
- branch: "issue-464-merge-webpacker-lite-into-webpacker-v5"
79
- GEMS
80
- append_to_file("Gemfile",
81
- gems)
45
+ def add_yarn_dependencies
46
+ run "yarn add react-on-rails"
82
47
  end
83
48
 
84
49
  def append_to_spec_rails_helper
@@ -92,10 +57,14 @@ module ReactOnRails
92
57
  else
93
58
  GeneratorMessages.add_info(
94
59
  <<-MSG.strip_heredoc
95
- Did not find spec/rails_helper.rb or spec/spec_helper.rb to add
96
- # Ensure that if we are running js tests, we are using latest webpack assets
97
- # This will use the defaults of :js and :server_rendering meta tags
98
- ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
60
+
61
+ We did not find a spec/rails_helper.rb or spec/spec_helper.rb to add
62
+ the React on Rails Test helper, which ensures that if we are running
63
+ js tests, then we are using latest webpack assets. You can later add
64
+ this to your rspec config:
65
+
66
+ # This will use the defaults of :js and :server_rendering meta tags
67
+ ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
99
68
  MSG
100
69
  )
101
70
  end
@@ -109,8 +78,8 @@ module ReactOnRails
109
78
  ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
110
79
  STR
111
80
 
112
- def print_helpful_message
113
- message = <<-MSG.strip_heredoc
81
+ def self.helpful_message
82
+ <<-MSG.strip_heredoc
114
83
 
115
84
  What to do next:
116
85
 
@@ -118,45 +87,36 @@ module ReactOnRails
118
87
 
119
88
  <%= javascript_pack_tag 'hello-world-bundle' %>
120
89
 
121
- - Ensure your bundle and yarn installs of dependencies are up to date.
122
-
123
- bundle && yarn
124
-
125
90
  - Run the foreman command to start the rails server and run webpack in watch mode.
126
91
 
127
92
  foreman start -f Procfile.dev
128
93
 
94
+ - Change this line app/views/hello_world/index.html.erb to `prerender: true` to see
95
+ server rendering (right click on page and select "view source").
96
+
97
+ <%= react_component("HelloWorldApp", props: @hello_world_props, prerender: true) %>
98
+
99
+ - Run the foreman command to start the rails server and run webpack in HMR mode. Be sure
100
+ to change your development/dev_server/hmr setting to true to see HMR in action.
101
+ Note, you cannot use the default Procfile.dev-server setup with server rendering.
102
+
103
+ foreman start -f Procfile.dev-server
104
+
105
+ - You may run the commands in the Procfiles in separate shells rather than using foreman.
106
+
107
+ - See the documentation on https://github.com/rails/webpacker for how the default webpack
108
+ configuration works.
109
+
129
110
  - Visit http://localhost:3000/hello_world and see your React On Rails app running!
130
111
  MSG
131
- GeneratorMessages.add_info(message)
132
112
  end
133
113
 
134
- private
135
-
136
- def add_yarn_postinstall_script_in_package_json
137
- client_package_json = File.join(destination_root, "package.json")
138
- contents = File.read(client_package_json)
139
- postinstall = %("postinstall": "cd client && yarn install")
140
- if contents =~ /"scripts" *:/
141
- replacement = <<-STRING
142
- "scripts": {
143
- #{postinstall},
144
- STRING
145
- regexp = / {2}"scripts": {/
146
- else
147
- regexp = /^{/
148
- replacement = <<-STRING.strip_heredoc
149
- {
150
- "scripts": {
151
- #{postinstall}
152
- },
153
- STRING
154
- end
155
-
156
- contents.gsub!(regexp, replacement)
157
- File.open(client_package_json, "w+") { |f| f.puts contents }
114
+ def print_helpful_message
115
+ GeneratorMessages.add_info(self.class.helpful_message)
158
116
  end
159
117
 
118
+ private
119
+
160
120
  # From https://github.com/rails/rails/blob/4c940b2dbfb457f67c6250b720f63501d74a45fd/railties/lib/rails/generators/rails/app/app_generator.rb
161
121
  def app_name
162
122
  @app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root))