Skip to content

Babel 6 / CSS Modules / Rails hot reloading #175

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

Merged
merged 11 commits into from
Dec 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ vendor/ruby
.ruby-gemset

# Generated js bundles
/app/assets/javascripts/generated/*
/app/assets/webpack/

# Rubymine/IntelliJ
.idea
8 changes: 5 additions & 3 deletions .scss-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

scss_files:
- 'app/assets/stylesheets/**/*.scss'
- 'client/assets/stylesheets/**/*.scss'
- 'client/app/**/*.scss'

exclude: 'client/node_modules/**'

linters:
# BangFormat:
Expand Down Expand Up @@ -132,8 +134,8 @@ linters:
# enabled: true
# max_depth: 3
#
# SelectorFormat:
# enabled: true
SelectorFormat:
enabled: false
# convention: hyphenated_lowercase # or 'strict_BEM', or 'hyphenated_BEM', or 'snake_case', or 'camel_case', or a regex pattern
#
# Shorthand:
Expand Down
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ install:
- nvm install 5.0
- nvm use 5.0
- npm install
- cd client && npm run build:client
- npm run build:server
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ end
group :test do
gem "coveralls", require: false
gem "rspec-rails"
gem "rspec-retry"
gem "capybara"
gem "capybara-screenshot"
gem "selenium-webdriver"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ GEM
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-retry (0.4.5)
rspec-core
rspec-support (3.4.1)
rubocop (0.35.1)
astrolabe (~> 1.3)
Expand Down Expand Up @@ -350,6 +352,7 @@ DEPENDENCIES
rails_12factor
react_on_rails
rspec-rails
rspec-retry
rubocop
ruby-lint
sass-rails
Expand Down
11 changes: 9 additions & 2 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
web: rails s
client: sh -c 'rm app/assets/javascripts/generated/* || true && cd client && npm run build:dev:client'

# Run the hot reload server for client development
client: sh -c 'rm app/assets/webpack/* || true && cd client && HOT_RAILS_PORT=3500 npm run build:dev:client'

# Keep the JS fresh for specs
client-spec: sh -c 'cd client && npm run build:test:client'

# Keep the JS fresh for server rendering
server: sh -c 'cd client && npm run build:dev:server'
hot: sh -c 'cd client && npm start'
hot: sh -c 'cd client && HOT_PORT=4000 npm start'
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,14 @@ Note that it's important to run the Rails server on a different port than the no

# Webpack configuration
## Config Files

- `webpack.client.base.config.js`: Common configuration file to minimize code duplication for client.rails and client.hot.
- `webpack.client.rails.config.js`: Used to generate the Rails bundles for Rails use.
- `webpack.client.hot.config.js`: Used by server.js to run the Webpack Dev server.
- `webpack.server.rails.config.js`: Common configuration file to minimize code duplication
between the HMR and Rails configurations.
- `webpack.client.rails.build.config.js`: Client side js bundle.
- `webpack.server.rails.build.config.js`: Server side js bundle

These are used for hot reloading (Webpack Dev Server):
- `webpack.client.rails.hot.config.js`: Used to generate the Rails bundles for Rails use so you get hot reloading within your Rails app.
- `webpack.client.hot.config.js`: Used by server.js to run the Webpack Dev server for stubbing the api end points

## Webpack Resources
- Good overview: [Pete Hunt's Webpack Howto](https://github.com/petehunt/webpack-howto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.

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

// Next two depend on jQuery
// This one depends on jQuery
//= require turbolinks
//= require bootstrap-sprockets

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

//= require rails_startup
13 changes: 13 additions & 0 deletions app/assets/javascripts/application_prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file is used in production to server generated JS assets. In development mode, we use the Webpack Dev Server
// to provide assets. This allows for hot reloading of the JS and CSS.
// See app/helpers/application_helper.rb for how the correct assets file is picked based on the Rails environment.
// Those helpers are used here: app/views/layouts/application.html.erb

// These assets are located in app/assets/webpack directory
// CRITICAL that webpack/vendor-bundle must be BEFORE turbolinks
// since it is exposing jQuery and jQuery-ujs
//= require vendor-bundle
//= require app-bundle

// Non-webpack assets incl turbolinks
//= require application_dev
59 changes: 0 additions & 59 deletions app/assets/stylesheets/_bootstrap-custom.scss

This file was deleted.

12 changes: 0 additions & 12 deletions app/assets/stylesheets/application.css.scss

This file was deleted.

2 changes: 2 additions & 0 deletions app/assets/stylesheets/application_dev.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Any non webpack assets can be imported here
// Others will be served via webpack-dev-server
6 changes: 6 additions & 0 deletions app/assets/stylesheets/application_prod.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// These assets are located in app/assets/webpack directory
@import 'vendor-bundle';
@import 'app-bundle';

// Non-webpack assets
@import 'application_dev';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this partial located?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right next to current file.

77 changes: 0 additions & 77 deletions app/assets/stylesheets/scaffolds.css.scss

This file was deleted.

12 changes: 12 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
module ApplicationHelper
# TODO: MOVE TO helper in react_on_rails
# See application.html.erb for usage example
def env_javascript_include_tag(prod_asset, dev_asset, params = {})
asset_file = !Rails.env.development? ? prod_asset : dev_asset
return javascript_include_tag(asset_file, params) if asset_file
end

# TODO: MOVE TO helper in react_on_rails
def env_stylesheet_link_tag(prod_asset, dev_asset, params = {})
asset_file = !Rails.env.development? ? prod_asset : dev_asset
return stylesheet_link_tag(asset_file, params) if asset_file
end
end
4 changes: 2 additions & 2 deletions app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div class="comment">
<p id="notice"><%= notice %></p>

<p>
<p class="js-comment-author">
<strong>Author:</strong>
<%= @comment.author %>
</p>

<p>
<p class="js-comment-text">
<strong>Text:</strong>
<%= @comment.text %>
</p>
Expand Down
6 changes: 4 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<html>
<head>
<title>RailsReactTutorial</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= env_stylesheet_link_tag 'application_prod', 'application_dev', media: 'all', 'data-turbolinks-track' => true %>
<%= env_javascript_include_tag nil, 'http://localhost:3500/vendor-bundle.js' %>
<%= env_javascript_include_tag nil, 'http://localhost:3500/app-bundle.js' %>
<%= env_javascript_include_tag 'application_prod', 'application_dev', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion app/views/pages/no_router.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<%= render "header" %>

<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
generator_function: true, prerender: true) %>
generator_function: true, prerender: false) %>
2 changes: 1 addition & 1 deletion client/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"stage": 0
"presets": ["es2015", "stage-0", "react"]
}
Loading