Skip to content

Commit 1ece366

Browse files
committed
Merge pull request shakacode#175 from shakacode/alex/client-babel-6-css-modules-rails-hot
Babel 6 / CSS Modules / Rails hot reloading
2 parents ee769cd + 816f3de commit 1ece366

File tree

97 files changed

+2186
-1201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+2186
-1201
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ vendor/ruby
2727
.ruby-gemset
2828

2929
# Generated js bundles
30-
/app/assets/javascripts/generated/*
30+
/app/assets/webpack/
3131

3232
# Rubymine/IntelliJ
3333
.idea

.scss-lint.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
scss_files:
44
- 'app/assets/stylesheets/**/*.scss'
5-
- 'client/assets/stylesheets/**/*.scss'
5+
- 'client/app/**/*.scss'
6+
7+
exclude: 'client/node_modules/**'
68

79
linters:
810
# BangFormat:
@@ -132,8 +134,8 @@ linters:
132134
# enabled: true
133135
# max_depth: 3
134136
#
135-
# SelectorFormat:
136-
# enabled: true
137+
SelectorFormat:
138+
enabled: false
137139
# convention: hyphenated_lowercase # or 'strict_BEM', or 'hyphenated_BEM', or 'snake_case', or 'camel_case', or a regex pattern
138140
#
139141
# Shorthand:

.travis.yml

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ install:
1818
- nvm install 5.0
1919
- nvm use 5.0
2020
- npm install
21-
- cd client && npm run build:client
22-
- npm run build:server
2321
before_script:
2422
- export DISPLAY=:99.0
2523
- sh -e /etc/init.d/xvfb start

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ end
8787
group :test do
8888
gem "coveralls", require: false
8989
gem "rspec-rails"
90+
gem "rspec-retry"
9091
gem "capybara"
9192
gem "capybara-screenshot"
9293
gem "selenium-webdriver"

Gemfile.lock

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ GEM
229229
rspec-expectations (~> 3.4.0)
230230
rspec-mocks (~> 3.4.0)
231231
rspec-support (~> 3.4.0)
232+
rspec-retry (0.4.5)
233+
rspec-core
232234
rspec-support (3.4.1)
233235
rubocop (0.35.1)
234236
astrolabe (~> 1.3)
@@ -350,6 +352,7 @@ DEPENDENCIES
350352
rails_12factor
351353
react_on_rails
352354
rspec-rails
355+
rspec-retry
353356
rubocop
354357
ruby-lint
355358
sass-rails

Procfile.dev

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
web: rails s
2-
client: sh -c 'rm app/assets/javascripts/generated/* || true && cd client && npm run build:dev:client'
2+
3+
# Run the hot reload server for client development
4+
client: sh -c 'rm app/assets/webpack/* || true && cd client && HOT_RAILS_PORT=3500 npm run build:dev:client'
5+
6+
# Keep the JS fresh for specs
7+
client-spec: sh -c 'cd client && npm run build:test:client'
8+
9+
# Keep the JS fresh for server rendering
310
server: sh -c 'cd client && npm run build:dev:server'
4-
hot: sh -c 'cd client && npm start'
11+
hot: sh -c 'cd client && HOT_PORT=4000 npm start'

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,14 @@ Note that it's important to run the Rails server on a different port than the no
127127

128128
# Webpack configuration
129129
## Config Files
130+
130131
- `webpack.client.base.config.js`: Common configuration file to minimize code duplication for client.rails and client.hot.
131-
- `webpack.client.rails.config.js`: Used to generate the Rails bundles for Rails use.
132-
- `webpack.client.hot.config.js`: Used by server.js to run the Webpack Dev server.
133-
- `webpack.server.rails.config.js`: Common configuration file to minimize code duplication
134-
between the HMR and Rails configurations.
132+
- `webpack.client.rails.build.config.js`: Client side js bundle.
133+
- `webpack.server.rails.build.config.js`: Server side js bundle
134+
135+
These are used for hot reloading (Webpack Dev Server):
136+
- `webpack.client.rails.hot.config.js`: Used to generate the Rails bundles for Rails use so you get hot reloading within your Rails app.
137+
- `webpack.client.hot.config.js`: Used by server.js to run the Webpack Dev server for stubbing the api end points
135138

136139
## Webpack Resources
137140
- Good overview: [Pete Hunt's Webpack Howto](https://github.com/petehunt/webpack-howto)

app/assets/javascripts/application.js renamed to app/assets/javascripts/application_dev.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
1111
// about supported directives.
1212

13-
// CRITICAL that generated/vendor-bundle must be BEFORE bootstrap-sprockets and turbolinks since it is
14-
// exposing jQuery and jQuery-ujs
15-
//= require generated/vendor-bundle
16-
//= require generated/app-bundle
13+
// All webpack assets in development will be loaded via webpack dev server
14+
// It's important to include them in layout view above this asset
15+
// b/c it exposes jQuery for turbolinks and another non-webpack JS (if any)
1716

18-
// Next two depend on jQuery
17+
// This one depends on jQuery
1918
//= require turbolinks
20-
//= require bootstrap-sprockets
2119

20+
// This will soon be removed as it will be in vendor-bundle with react_on_rails 2.0
2221
//= require react_on_rails
2322

2423
//= require rails_startup
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is used in production to server generated JS assets. In development mode, we use the Webpack Dev Server
2+
// to provide assets. This allows for hot reloading of the JS and CSS.
3+
// See app/helpers/application_helper.rb for how the correct assets file is picked based on the Rails environment.
4+
// Those helpers are used here: app/views/layouts/application.html.erb
5+
6+
// These assets are located in app/assets/webpack directory
7+
// CRITICAL that webpack/vendor-bundle must be BEFORE turbolinks
8+
// since it is exposing jQuery and jQuery-ujs
9+
//= require vendor-bundle
10+
//= require app-bundle
11+
12+
// Non-webpack assets incl turbolinks
13+
//= require application_dev

app/assets/stylesheets/_bootstrap-custom.scss

-59
This file was deleted.

app/assets/stylesheets/application.css.scss

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Any non webpack assets can be imported here
2+
// Others will be served via webpack-dev-server
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// These assets are located in app/assets/webpack directory
2+
@import 'vendor-bundle';
3+
@import 'app-bundle';
4+
5+
// Non-webpack assets
6+
@import 'application_dev';

app/assets/stylesheets/scaffolds.css.scss

-77
This file was deleted.

app/helpers/application_helper.rb

+12
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
module ApplicationHelper
2+
# TODO: MOVE TO helper in react_on_rails
3+
# 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
7+
end
8+
9+
# 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
13+
end
214
end

app/views/comments/show.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<div class="comment">
22
<p id="notice"><%= notice %></p>
33

4-
<p>
4+
<p class="js-comment-author">
55
<strong>Author:</strong>
66
<%= @comment.author %>
77
</p>
88

9-
<p>
9+
<p class="js-comment-text">
1010
<strong>Text:</strong>
1111
<%= @comment.text %>
1212
</p>

app/views/layouts/application.html.erb

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
<html>
33
<head>
44
<title>RailsReactTutorial</title>
5-
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6-
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
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 %>
79
<%= csrf_meta_tags %>
810
</head>
911
<body>

app/views/pages/no_router.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<%= render "header" %>
44

55
<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
6-
generator_function: true, prerender: true) %>
6+
generator_function: true, prerender: false) %>

client/.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"stage": 0
2+
"presets": ["es2015", "stage-0", "react"]
33
}

0 commit comments

Comments
 (0)