react_rails_webpack 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +58 -58
- data/lib/react_rails_webpack/app/views/react_example/greeting.html.erb +1 -0
- data/lib/react_rails_webpack/client/webpack.config.js +2 -1
- data/lib/react_rails_webpack/info_generator.rb +21 -0
- data/lib/react_rails_webpack/install_generator.rb +28 -9
- data/lib/react_rails_webpack/new_fork_generator.rb +24 -0
- data/lib/react_rails_webpack/version.rb +1 -1
- data/lib/react_rails_webpack/whats_next_generator.rb +35 -0
- data/lib/react_rails_webpack.rb +3 -0
- data/react_rails_webpack.gemspec +0 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d39753431cd013d16c4a744c9481404886093a7
|
4
|
+
data.tar.gz: 6a33e6cf4e7098edceb6089da732f435e4952f5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8fcb6fba5381eef13521677f268ce8b942ac17984437a819c10a37515740107a9a8fb706e6af409b4602cb3a57e2b139a075ced00ca5b892760d26d775b1da6
|
7
|
+
data.tar.gz: 8b0102de4210f267b4914e4ea67b0770c516850a404064a1557c0dd42fd68bc19bba18ddcaf6abfcd554b6f4a39281beebb2fa80647ae475191369fa57113942
|
data/README.md
CHANGED
@@ -4,9 +4,10 @@ A set of Rails generators that create all the necessary boilerplate code (with e
|
|
4
4
|
|
5
5
|
## Key Features
|
6
6
|
|
7
|
-
1.
|
8
|
-
2.
|
9
|
-
3.
|
7
|
+
1. One-command setup
|
8
|
+
2. Use any packages from the npm ecosystem
|
9
|
+
3. Automatically sets up working demo examples so you can jump into React immediately
|
10
|
+
4. Development server for React components will live update your changes to any device on your local network (including phones)
|
10
11
|
|
11
12
|
## Quick Start
|
12
13
|
|
@@ -15,15 +16,29 @@ A set of Rails generators that create all the necessary boilerplate code (with e
|
|
15
16
|
1. Make sure you have the [requirements](#requirements) installed
|
16
17
|
2. Add `gem 'react_rails_webpack'` to your Gemfile
|
17
18
|
3. Run `bundle install`
|
18
|
-
4. Run `rails g react_rails_webpack:install` ([further explanation of what this does](#
|
19
|
-
5. Run `npm run install`
|
20
|
-
6. Run `npm run build`
|
19
|
+
4. Run `rails g react_rails_webpack:install` ([further explanation of what this does](#how-does-this-work))
|
21
20
|
|
22
|
-
###
|
21
|
+
### Commands
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
#### `rails g react_rails_webpack:install`
|
24
|
+
|
25
|
+
Sets up the integration
|
26
|
+
|
27
|
+
#### `rails g react_rails_webpack:create_example_page`
|
28
|
+
|
29
|
+
Sets up an example page in your Rails app at [localhost:3000/greeting](http://localhost:3000/greeting) that displays the two example components.
|
30
|
+
|
31
|
+
#### `rails g react_rails_webpack:new_fork`
|
32
|
+
|
33
|
+
You must run this whenever you clone a repo with a react_rails_webpack integration setup. It generates [the environment.json file](lib/react_rails_webpack/client/environment.json) based on your computer's network name. Since the network name will be different for most computers, this file is gitignored and needs to be regenerated for each new computer the project is cloned on.
|
34
|
+
|
35
|
+
#### `rails g react_rails_webpack:info`
|
36
|
+
|
37
|
+
This command will tell you the url of your webpack development server on your local network.
|
38
|
+
|
39
|
+
#### `rails g react_rails_webpack:trailblazer_integration`
|
40
|
+
|
41
|
+
If you use the Trailblazer gem, use this command to add a [trailblazer](https://github.com/apotonick/trailblazer) cell for react components to your `lib` folder.
|
27
42
|
|
28
43
|
### Suggested Development Workflow
|
29
44
|
|
@@ -52,47 +67,6 @@ When I looked over the available gems for react/rails integrations, none of them
|
|
52
67
|
|
53
68
|
- This gem does not do server-side rendering, so if you need to do SEO stuff within your react components, it's probably the wrong choice
|
54
69
|
|
55
|
-
## Installing the Gem
|
56
|
-
|
57
|
-
Add this line to your application's Gemfile:
|
58
|
-
|
59
|
-
```ruby
|
60
|
-
gem 'react_rails_webpack'
|
61
|
-
```
|
62
|
-
|
63
|
-
And then execute:
|
64
|
-
|
65
|
-
$ bundle
|
66
|
-
|
67
|
-
## Requirements
|
68
|
-
|
69
|
-
- [Rails](http://rubyonrails.org/) 4.0+
|
70
|
-
- [node](https://nodejs.org/) (tested on 4.2.2 and 5.4.1)
|
71
|
-
- [npm](https://www.npmjs.com/) (tested on 3.4.0 and 3.5.3)
|
72
|
-
|
73
|
-
## Generating the Integration
|
74
|
-
|
75
|
-
Run the install generator like this:
|
76
|
-
|
77
|
-
$ rails g react_rails_webpack:install
|
78
|
-
|
79
|
-
This will setup a basic react integration with some example components (one standard react component and one using react with redux) under [a client folder](lib/react_rails_webpack/client) in your project's root. Once the generator's run is done, run the commands below (from your project's root):
|
80
|
-
|
81
|
-
**Note**: Make sure you have [node](https://nodejs.org/) and [npm](https://www.npmjs.com/) installed on your machine before running these.
|
82
|
-
|
83
|
-
$ npm run install # installs the needed npm packages
|
84
|
-
$ npm run build # uses webpack to compile your javascript code to your assets folder
|
85
|
-
|
86
|
-
Then set the dev server hostname ([instructions](#setting-the-dev-server-hostname)).
|
87
|
-
|
88
|
-
## Generating an Example Page
|
89
|
-
|
90
|
-
If you want to setup an example page in your Rails app that uses the provided React components, run:
|
91
|
-
|
92
|
-
$ rails g react_rails_webpack:create_example_page
|
93
|
-
|
94
|
-
Then run the rails server and go to [the greeting page](http://localhost:3000/greeting) to see everything in action.
|
95
|
-
|
96
70
|
## Creating and using your own components
|
97
71
|
|
98
72
|
Let's say you want to make a checkout form component with react. Here's what you'd need to do to use it from Rails:
|
@@ -131,29 +105,55 @@ export default {
|
|
131
105
|
render_component "CheckoutForm", { customerName: 'Harper' }
|
132
106
|
```
|
133
107
|
|
134
|
-
|
108
|
+
NOTE: Unless you're sure you know what you're doing, do not edit anything in the [client/app](lib/react_rails_webpack/client/app) folder except [the availableComponents file](lib/react_rails_webpack/client/app/availableComponents.js)). Those files are where the integration works it's magic (though of course feel free to read the files to check out how everything works).
|
135
109
|
|
136
|
-
|
110
|
+
## Working with the Webpack Dev Server
|
137
111
|
|
138
|
-
|
112
|
+
`npm run start` will start a webpack development server with hot reloading that is completely independent of your Rails app. You can see the output of this server on any computer or mobile device on your local network by going to the appropriate url (which you can find by running `rails g react_rails_webpack:info`). Any changes you make to your component files will be pushed immediately to all devices looking at the page.
|
139
113
|
|
140
|
-
|
114
|
+
## How does this work?
|
141
115
|
|
116
|
+
When you run the install generator like this:
|
142
117
|
|
143
|
-
|
118
|
+
$ rails g react_rails_webpack:install
|
119
|
+
|
120
|
+
This gem will setup a basic react integration with some example components (one standard react component and one using react with redux) under [a client folder](lib/react_rails_webpack/client) in your project's root.
|
144
121
|
|
145
|
-
|
122
|
+
The meat of the integration with Rails is in [the client/app folder](lib/react_rails_webpack/client/app), and [the app.js file](lib/react_rails_webpack/client/app.js). When your page loads in Rails, the `react_component` method creates divs that looks like this:
|
123
|
+
|
124
|
+
```html
|
125
|
+
<div class="react-component-target" data-componentname="ComponentName" data-componentprops="{myProp: 'some value'}">
|
126
|
+
<script>renderLastComponentDiv()</script>
|
127
|
+
</div>
|
128
|
+
```
|
129
|
+
|
130
|
+
That example is what it would look like if you called it like with:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
react_component "ComponentName", {myProp: 'some value'}
|
134
|
+
```
|
135
|
+
|
136
|
+
When your browser hits that `renderLastComponentDiv()` call, it grabs the component name ("ComponentName") from the parent div, then looks it up in [the availableComponents.js file](lib/react_rails_webpack/client/app/availableComponents.js).
|
137
|
+
|
138
|
+
NOTE: You MUST list all components that you will render with `react_component` in this file or it won't find anything.
|
139
|
+
|
140
|
+
Assuming it finds a component that matches the name, it will immediately render that component within that div, passing in props from the `data-componentprops` attribute. As the page loads, each successive div generated by `react_component` renders this way.
|
146
141
|
|
147
142
|
## Gotchas
|
148
143
|
|
149
144
|
### Forgetting to run `npm run build`
|
150
145
|
|
151
|
-
Remember, while your changes to components will hot reload when you use the webpack dev server, they will not show up at all in your Rails app until you run the `npm run build` command.
|
146
|
+
Remember, while your changes to components will hot reload when you use the webpack dev server, they will not show up at all in your Rails app until you run the `npm run build` command. And they do NOT hot-reload when run in the context of your Rails app.
|
152
147
|
|
153
148
|
### Forgetting to add components to the `client/src/app/availableComponents.js` file
|
154
149
|
|
155
150
|
Components will not be accessible from Rails if you forget to add them here.
|
156
151
|
|
152
|
+
### Forgetting to run `rails g react_rails_webpack:new_fork` when cloning your repo on a new machine
|
153
|
+
|
154
|
+
`rails g react_rails_webpack:new_fork` sets up [the environment.json file](lib/react_rails_webpack/client/environment.json), which is NOT included in your repo, because it contains information specific to each computer it is generated on.
|
155
|
+
|
156
|
+
|
157
157
|
## Development
|
158
158
|
|
159
159
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -13,7 +13,8 @@ var config = getConfig({
|
|
13
13
|
{
|
14
14
|
// title: 'Site Title'
|
15
15
|
// head: '<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>',
|
16
|
-
html: "<
|
16
|
+
html: "<aside>Find the source for this page in client/webpack.config.js</aside>" +
|
17
|
+
"<div class=\"react-component-target\" data-componentname=\"Hello\" data-componentprops='{ \"details\": \"My initial greeting was set with getInitialState in the client/src/components/Hello.jsx file.\" }'></div>" +
|
17
18
|
"<div class=\"react-component-target\" data-componentname=\"HelloWithRedux\" data-componentprops='{ \"details\": \"My initial greeting was set with initialState in the client/src/redux/reducer.js file.\" }'></div>"
|
18
19
|
}
|
19
20
|
)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
require 'colorize'
|
3
|
+
|
4
|
+
module ReactRailsWebpack
|
5
|
+
class InfoGenerator < Rails::Generators::Base
|
6
|
+
desc "This generator provides basic info about the react_rails_webpack integration."
|
7
|
+
|
8
|
+
source_root(File.join(File.expand_path(File.dirname(__FILE__))))
|
9
|
+
|
10
|
+
def print_info
|
11
|
+
puts
|
12
|
+
puts "-" * `tput cols`.to_i # print line of dashes
|
13
|
+
puts
|
14
|
+
puts "To run a live-reloading webpack server for you React \
|
15
|
+
components, run #{'npm run start'.white.bold}, then go to #{(`scutil --get LocalHostName`.strip + '.local:3000').blue.bold}"
|
16
|
+
puts
|
17
|
+
puts "-" * `tput cols`.to_i # print line of dashes
|
18
|
+
puts
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rails/generators/base'
|
2
2
|
require 'colorize'
|
3
|
+
require_relative 'new_fork_generator'
|
3
4
|
|
4
5
|
module ReactRailsWebpack
|
5
6
|
class InstallGenerator < Rails::Generators::Base
|
@@ -7,6 +8,24 @@ module ReactRailsWebpack
|
|
7
8
|
|
8
9
|
source_root(File.join(File.expand_path(File.dirname(__FILE__))))
|
9
10
|
|
11
|
+
def check_for_node
|
12
|
+
if `which node`.length == 0
|
13
|
+
puts "Looks like you don't have node installed yet."
|
14
|
+
puts "This generator cannot work without node."
|
15
|
+
puts "Go here to install it: #{'https://nodejs.org'.blue}"
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def check_for_npm
|
21
|
+
if `which npm`.length == 0
|
22
|
+
puts "Looks like you don't have npm installed yet."
|
23
|
+
puts "This generator cannot work without npm."
|
24
|
+
puts "Go here to install the latest version of node (which will include npm): #{'https://nodejs.org'.blue}"
|
25
|
+
exit
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
10
29
|
def setup_client_folder
|
11
30
|
puts 'Adding client folder...'
|
12
31
|
directory 'client'
|
@@ -32,13 +51,6 @@ module ReactRailsWebpack
|
|
32
51
|
end
|
33
52
|
end
|
34
53
|
|
35
|
-
def set_environment_hostname
|
36
|
-
localhost_name = `scutil --get LocalHostName`.strip
|
37
|
-
puts "Setting localhost name to #{localhost_name}..."
|
38
|
-
|
39
|
-
gsub_file 'client/environment.json', /<<<LOCALHOST_NAME>>>/, localhost_name
|
40
|
-
end
|
41
|
-
|
42
54
|
def add_webpack_asset_inclusion
|
43
55
|
puts 'Adding asset includes...'
|
44
56
|
# Add webpack folder to application asset paths
|
@@ -70,14 +82,21 @@ module ReactRailsWebpack
|
|
70
82
|
ensure_prepended "@import '../webpack/*\n", 'app/assets/stylesheets/application.sass' if File.exist?('app/assets/stylesheets/application.sass')
|
71
83
|
end
|
72
84
|
|
73
|
-
def
|
85
|
+
def run_new_fork_generator
|
86
|
+
Rails::Generators.invoke('react_rails_webpack:new_fork')
|
87
|
+
end
|
88
|
+
|
89
|
+
def run_npm_install_and_build_and_then_say_whats_next
|
74
90
|
puts
|
75
91
|
puts "-" * `tput cols`.to_i # print line of dashes
|
76
92
|
puts
|
77
|
-
puts "
|
93
|
+
puts "Integration files all setup. Now running #{'npm run install'.white.bold} and \
|
94
|
+
#{'npm run build'.white.bold} for you..."
|
78
95
|
puts
|
79
96
|
puts "-" * `tput cols`.to_i # print line of dashes
|
80
97
|
puts
|
98
|
+
|
99
|
+
exec('npm run install && npm run build; rails g react_rails_webpack:whats_next')
|
81
100
|
end
|
82
101
|
|
83
102
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
require 'colorize'
|
3
|
+
|
4
|
+
module ReactRailsWebpack
|
5
|
+
class NewForkGenerator < Rails::Generators::Base
|
6
|
+
desc "This generator sets up the environment.json file for react/rails integration in a Rails app."
|
7
|
+
|
8
|
+
source_root(File.join(File.expand_path(File.dirname(__FILE__))))
|
9
|
+
|
10
|
+
def copy_environment_json_file
|
11
|
+
inside 'client' do
|
12
|
+
copy_file 'environment.json'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_environment_hostname
|
17
|
+
|
18
|
+
localhost_name = `scutil --get LocalHostName`.strip
|
19
|
+
puts "Setting localhost name to #{localhost_name}..."
|
20
|
+
|
21
|
+
gsub_file 'client/environment.json', /<<<LOCALHOST_NAME>>>/, localhost_name
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
require 'colorize'
|
3
|
+
|
4
|
+
module ReactRailsWebpack
|
5
|
+
class WhatsNextGenerator < Rails::Generators::Base
|
6
|
+
desc "This generator instructs the user on what to do \
|
7
|
+
once the react_rails_webpack:install generator is done running."
|
8
|
+
|
9
|
+
source_root(File.join(File.expand_path(File.dirname(__FILE__))))
|
10
|
+
|
11
|
+
def say_whats_next
|
12
|
+
puts
|
13
|
+
puts "-" * `tput cols`.to_i # print line of dashes
|
14
|
+
puts
|
15
|
+
puts "DONE!"
|
16
|
+
puts
|
17
|
+
puts "Okay, #{'if you saw any errors'.red.bold} when the \
|
18
|
+
#{'npm run install'.white.bold} and #{'npm run build'.white.bold} commands \
|
19
|
+
were run just now, try running them again now manually."
|
20
|
+
puts
|
21
|
+
puts "#{"If you didn't get any errors".red.bold} you should be good to go, and \
|
22
|
+
you can start your live-reloading webpack development server for React \
|
23
|
+
by running the #{'npm run start'.white.bold} command and visiting \
|
24
|
+
#{(`scutil --get LocalHostName`.strip+'.local:3000').blue.bold} in your browser \
|
25
|
+
or on any other device on your local network (for example: your phone)."
|
26
|
+
puts
|
27
|
+
puts "#{'To generate an example page of React components inside your Rails app'.red.bold}, \
|
28
|
+
run the #{'rails g react_rails_webpack:create_example_page'.white.bold} command."
|
29
|
+
puts
|
30
|
+
puts "-" * `tput cols`.to_i # print line of dashes
|
31
|
+
puts
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/react_rails_webpack.rb
CHANGED
@@ -4,6 +4,9 @@ module ReactRailsWebpack
|
|
4
4
|
class ReactRailsWebpackRailtie < Rails::Railtie
|
5
5
|
generators do
|
6
6
|
require_relative 'react_rails_webpack/install_generator'
|
7
|
+
require_relative 'react_rails_webpack/new_fork_generator'
|
8
|
+
require_relative 'react_rails_webpack/whats_next_generator'
|
9
|
+
require_relative 'react_rails_webpack/info_generator'
|
7
10
|
require_relative 'react_rails_webpack/create_example_page_generator'
|
8
11
|
require_relative 'react_rails_webpack/trailblazer_integration_generator'
|
9
12
|
end
|
data/react_rails_webpack.gemspec
CHANGED
@@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
# spec.add_dependency("railties", ">= 4.1.0", "< 5.1")
|
23
22
|
spec.add_dependency("rails", ">= 4")
|
24
23
|
spec.add_dependency "colorize", ">= 0.7.7"
|
25
24
|
spec.add_development_dependency "bundler", "~> 1.11"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: react_rails_webpack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Neurodynamic
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -101,11 +101,14 @@ files:
|
|
101
101
|
- lib/react_rails_webpack/client/test/test_helper.js
|
102
102
|
- lib/react_rails_webpack/client/webpack.config.js
|
103
103
|
- lib/react_rails_webpack/create_example_page_generator.rb
|
104
|
+
- lib/react_rails_webpack/info_generator.rb
|
104
105
|
- lib/react_rails_webpack/install_generator.rb
|
105
106
|
- lib/react_rails_webpack/lib/react_cell.rb
|
107
|
+
- lib/react_rails_webpack/new_fork_generator.rb
|
106
108
|
- lib/react_rails_webpack/package.json
|
107
109
|
- lib/react_rails_webpack/trailblazer_integration_generator.rb
|
108
110
|
- lib/react_rails_webpack/version.rb
|
111
|
+
- lib/react_rails_webpack/whats_next_generator.rb
|
109
112
|
- react_rails_webpack.gemspec
|
110
113
|
homepage: https://github.com/neurodynamic/react_rails_webpack
|
111
114
|
licenses:
|