Skip to content

Commit 6ebc390

Browse files
committed
Change env_javascript_include_tag and env_stylesheet_link_tag API and rename application_prod and dev to static and hot
1 parent 902228b commit 6ebc390

14 files changed

+59
-34
lines changed

Diff for: Procfile.dev

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
# Development rails requires both rails and rails-assets
66
# (and rails-server-assets if server rendering)
7-
rails: rails s
7+
rails: REACT_ON_RAILS_ENV=HOT rails s
8+
89
# Run the hot reload server for client development
910
rails-assets: sh -c 'rm app/assets/webpack/* || true && HOT_RAILS_PORT=3500 npm run build:dev:client'
11+
1012
# Keep the JS fresh for server rendering. Remove if not server rendering
1113
rails-server-assets: sh -c 'npm run build:dev:server'
1214

1315
# If you don't keep this process going, you will rebuild the assets per spec run. This is configured
1416
# in rails_helper.rb.
15-
rails-spec: sh -c 'npm run build:test:client'
17+
rails-test: sh -c 'npm run build:test:client'
1618

1719
# Run an express server if you want to mock out your endpoints. No Rails involved!
1820
# Disable this if you are not using it.

Diff for: Procfile.express

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Basic procfile for express server dev work.
21
# Only run the express server!
32
# Use this if you want to mock out your endpoints.
43
# It's a great way to prototype UI especially with non-Rails developers!

Diff for: Procfile.rails renamed to Procfile.hot

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Basic procfile for dev work using Rails
1+
# Basic procfile for dev work.
2+
# Runs all processes. Development is faster if you pick one of the other Procfiles if you don't need
3+
# some of the processes: Procfile.rails or Procfile.express
4+
25
# Development rails requires both rails and rails-assets
36
# (and rails-server-assets if server rendering)
4-
# You can still run tests, and they will build the webpack file for each test run.
5-
# Start another shell and run the Procfile.spec to make test runs faster.
7+
rails: REACT_ON_RAILS_ENV=HOT rails s
68

7-
rails: rails s
89
# Run the hot reload server for client development
910
rails-assets: sh -c 'rm app/assets/webpack/* || true && HOT_RAILS_PORT=3500 npm run build:dev:client'
11+
1012
# Keep the JS fresh for server rendering. Remove if not server rendering
1113
rails-server-assets: sh -c 'npm run build:dev:server'

Diff for: Procfile.spec

-5
This file was deleted.

Diff for: Procfile.static

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Run Rails without hot reloading (static assets)
2+
rails: rails s
3+
4+
# Keep the JS fresh for server rendering. Remove if not server rendering
5+
rails-server-assets: sh -c 'npm run build:dev:server'

Diff for: Procfile.test

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# For keeping webpack bundles up-to-date during a testing workflow.
2+
# If you don't keep this process going, you will rebuild the assets per spec run. This is configured
3+
# in rails_helper.rb.
4+
rails-test: sh -c 'npm run build:test:client'

Diff for: README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ See package.json and Gemfile for versions
7575
1. Open a browser tab to http://localhost:3000 for the Rails app example.
7676
1. Open a browser tab to http://localhost:4000 for the Hot Module Replacement Example.
7777
1. With the browser open to any JSX file, such as [client/app/bundles/comments/components/CommentBox/CommentBox.jsx](client/app/bundles/comments/components/CommentBox/CommentBox.jsx) and you can change the JSX code, hit save, and you will see the sceen update without refreshing the window. This applies to port 3000 and port 4000.
78-
1. Try changing a `.scss` file, such as a color in [client/app/bundles/comments/components/CommentBox/CommentList/Comment/Comment.scss](client/app/bundles/comments/components/CommentBox/CommentList/Comment/Comment.scss). You can see port 3000 or 4000 update automatically.
78+
1. Try changing a `.scss` file, such as a color in [client/app/bundles/comments/components/CommentBox/CommentList/Comment/Comment.scss](client/app/bundles/comments/components/CommentBox/CommentList/Comment/Comment.scss). You can see port 3000 or 4000 update automatically.
7979

8080
# KEY COMMANDS
8181
1. Run all linters and tests: `rake ci`
@@ -95,15 +95,15 @@ Save a change to a JSX file and see it update immediately in the browser! Note,
9595
# Rails integration
9696

9797
## JS and CSS assets
98-
We're now using Webpack for all Sass and JavaScript assets so we can do CSS Modules within Rails!
98+
We're now using Webpack for all Sass and JavaScript assets so we can do CSS Modules within Rails!
9999

100-
1. **Production Deployment**: See [assets.rake](lib/tasks/assets.rake) for we modify the Rails precompile task to deploy assets for production.
100+
1. **Production Deployment**: See [assets.rake](lib/tasks/assets.rake) for we modify the Rails precompile task to deploy assets for production.
101101
2. **Development mode**: We modify the URL in [application.html.erb](app/views/layouts/application.html.erb) based on whether or not we're in production mode using the helpers `env_stylesheet_link_tag` and `env_javascript_include_tag`. *Development mode* uses the Webpack Dev server running on port 3500. Other modes (production/test) uses precompiled files.
102+
102103
```erb
103-
<%= env_stylesheet_link_tag 'application_prod', 'application_dev', media: 'all', 'data-turbolinks-track' => true %>
104-
<%= env_javascript_include_tag nil, 'http://localhost:3500/vendor-bundle.js' %>
105-
<%= env_javascript_include_tag nil, 'http://localhost:3500/app-bundle.js' %>
106-
<%= env_javascript_include_tag 'application_prod', 'application_dev', 'data-turbolinks-track' => true %>
104+
<%= env_stylesheet_link_tag(static: 'application_static', hot: 'application_non_webpack', options: { media: 'all', 'data-turbolinks-track' => true }) %>
105+
<%= env_javascript_include_tag(hot: ['http://localhost:3500/vendor-bundle.js', 'http://localhost:3500/app-bundle.js']) %>
106+
<%= env_javascript_include_tag(static: 'application_static', hot: 'application_non_webpack', options: { 'data-turbolinks-track' => true }) %>
107107
```
108108

109109
# Webpack configuration
@@ -142,7 +142,7 @@ export default class CommentBox extends React.Component {
142142
```
143143

144144
## Sass and fonts
145-
The tutorial makes use of a custom font OpenSans-Light. We're doing this to show how to add assets for the CSS processing. The font files are located under [client/app/assets/fonts](client/app/assets/fonts) and are loaded by both the Rails asset pipeline and the Webpack HMR server.
145+
The tutorial makes use of a custom font OpenSans-Light. We're doing this to show how to add assets for the CSS processing. The font files are located under [client/app/assets/fonts](client/app/assets/fonts) and are loaded by both the Rails asset pipeline and the Webpack HMR server.
146146

147147
# Process management
148148
Run the following command in your development environment to invoke both Webpack and Rails.
@@ -155,7 +155,7 @@ bundle exec foreman start -f Procfile.dev
155155

156156
Notable contributions include (please submit PR if I miss any!):
157157

158-
* [Justin Gordon](https://github.com/justin808/): Started this, leads this
158+
* [Justin Gordon](https://github.com/justin808/): Started this, leads this
159159
* [Alex Fedoseev](https://github.com/alexfedoseev): Added integration of Rails hot loading and CSS modules, plus much more
160160
* [Martin Breining](https://github.com/mbreining): For adding flux at first
161161
* [Dylan Grafmyre](https://github.com/Dgrafmyre): For ci setup

Diff for: app/assets/javascripts/application_prod.js renamed to app/assets/javascripts/application_static.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
//= require app-bundle
1111

1212
// Non-webpack assets incl turbolinks
13-
//= require application_dev
13+
//= require application_non_webpack

Diff for: app/assets/stylesheets/application_prod.css.scss renamed to app/assets/stylesheets/application_static.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
@import 'app-bundle';
44

55
// Non-webpack assets
6-
@import 'application_dev';
6+
@import 'application_non_webpack';

Diff for: app/helpers/application_helper.rb

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
module ApplicationHelper
22
# TODO: MOVE TO helper in react_on_rails
33
# See application.html.erb for usage example
4-
def env_javascript_include_tag(prod_asset, dev_asset, params = {})
5-
asset_file = !Rails.env.development? ? prod_asset : dev_asset
6-
return javascript_include_tag(asset_file, params) if asset_file
4+
def env_javascript_include_tag(args = {})
5+
send_tag_method(:javascript_include_tag, args)
76
end
87

98
# TODO: MOVE TO helper in react_on_rails
10-
def env_stylesheet_link_tag(prod_asset, dev_asset, params = {})
11-
asset_file = !Rails.env.development? ? prod_asset : dev_asset
12-
return stylesheet_link_tag(asset_file, params) if asset_file
9+
def env_stylesheet_link_tag(args = {})
10+
send_tag_method(:stylesheet_link_tag, args)
11+
end
12+
13+
def use_hot_reloading?
14+
ENV["REACT_ON_RAILS_ENV"] == "HOT"
15+
end
16+
17+
private
18+
19+
def send_tag_method(tag_method_name, args)
20+
asset_type = use_hot_reloading? ? :hot : :static
21+
assets = Array(args[asset_type])
22+
options = args.delete_if { |key, _value| %i(hot static).include?(key) }
23+
send(tag_method_name, *assets, options) unless assets.empty?
1324
end
1425
end

Diff for: app/views/layouts/application.html.erb

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
<html>
33
<head>
44
<title>RailsReactTutorial</title>
5-
<%= env_stylesheet_link_tag 'application_prod', 'application_dev', media: 'all', 'data-turbolinks-track' => true %>
6-
<%= env_javascript_include_tag nil, 'http://localhost:3500/vendor-bundle.js' %>
7-
<%= env_javascript_include_tag nil, 'http://localhost:3500/app-bundle.js' %>
8-
<%= env_javascript_include_tag 'application_prod', 'application_dev', 'data-turbolinks-track' => true %>
5+
<%= env_stylesheet_link_tag(static: 'application_static',
6+
hot: 'application_non_webpack',
7+
media: 'all',
8+
'data-turbolinks-track' => true) %>
9+
<!-- These do not use turbolinks -->
10+
<%= env_javascript_include_tag(hot: ['http://localhost:3500/vendor-bundle.js',
11+
'http://localhost:3500/app-bundle.js']) %>
12+
<!-- These do use turbolinks -->
13+
<%= env_javascript_include_tag(static: 'application_static',
14+
hot: 'application_non_webpack',
15+
'data-turbolinks-track' => true) %>
916
<%= csrf_meta_tags %>
1017
</head>
1118
<body>

Diff for: docs/jquery-with-react-on-rails.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
jQuery and jQuery-ujs are not required within `app/assets/javascript/application.js` and have been moved under`/client` and are managed by npm. The modules are exposed via entry point by [client/webpack.client.base.config.js](client/webpack.client.base.config.js) and, for `jquery-ujs`, in the [client/webpack.client.rails.build.config.js](client/webpack.client.rails.build.config.js) and the [client/webpack.client.rails.hot.config.js](client/webpack.client.rails.hot.config.js).
44

5-
In `application_dev.js` and `application_prod.js`, it's critical that any libraries that depend on jQuery come after the inclusion
5+
In `application_non_webpack.js` and `application_static.js`, it's critical that any libraries that depend on jQuery come after the inclusion
66
of the Webpack bundle.
77

88
Please refer to [Considerations for jQuery with Rails and Webpack](http://forum.shakacode.com/t/considerations-for-jquery-with-rails-and-webpack/344) for further info.

0 commit comments

Comments
 (0)