Skip to content

Commit

Permalink
combined webpack files into one webpack config. fixed corresponding t…
Browse files Browse the repository at this point in the history
…ests and code. cleaned up some documentation.
  • Loading branch information
jbhatab committed Apr 24, 2016
1 parent 4ccc0ce commit 8401c9d
Show file tree
Hide file tree
Showing 37 changed files with 207 additions and 720 deletions.
2 changes: 1 addition & 1 deletion app/helpers/react_on_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def server_rendered_react_component_html(props, react_component_name, dom_id,
# On server `location` option is added (`location = request.fullpath`)
# React Router needs this to match the current route

# Make sure that we use up-to-date server-bundle
# Make sure that we use up-to-date webpack-bundle
ReactOnRails::ServerRenderingPool.reset_pool_if_server_bundle_was_modified

# Since this code is not inserted on a web page, we don't need to escape props
Expand Down
97 changes: 2 additions & 95 deletions lib/generators/USAGE
Original file line number Diff line number Diff line change
@@ -1,99 +1,13 @@
Description:

The react_on_rails:install generator combined with the example pull requests of
generator runs will get you up and running efficiently. There's a fair bit of
setup involved when integrating Webpack with Rails. Defaults for options
are such that the default is for the flag to be off. For example, the default for
`-R` is that redux is off, and the default of -b is that skip-bootstrap is off.
The react_on_rails:install generator integrates webpack with rails with ease. You can pass the redux option if you'd like to have redux setup for you automatically.

* Redux

Passing the --redux generator option causes the generated Hello World example
to integrate the Redux state container framework. The necessary node modules
will be automatically included for you.

The generator uses 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. We recommend that
that each bundle will have it's own Redux store. If you have code that you
want to reuse across bundles, such as middleware or common utilities, place them
under `/client/app/lib`. You can then import them in your client code:
`import MyModule from 'lib/MyModule'`; since we have configured webpack to
automatically resolve the word lib to point to this folder.

* Using Images and Fonts

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.

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.

* Bootstrap Integration

React on Rails ships with Twitter Bootstrap already integrated into the build.
Note that the generator removes require_tree in both the application.js and
application.css.scss files. This is to ensure the correct load order for the
bootstrap integration, and is usually a good idea in general. You will therefore
need to explicitly require your files.

How the Bootstrap library is loaded depends upon whether one is using the Rails
server or the HMR development server.

1. Bootstrap via Rails Server

The Rails server loads bootstrap-sprockets, provided
by the bootstrap-sass ruby gem (added automatically to your Gemfile by
the generator), via the `app/assets/stylesheets/_bootstrap-custom.scss`
partial.

This allows for using Bootstrap in your regular Rails stylesheets. If you
wish to customize any of the Bootstrap variables, you can do so via the
`client/assets/stylesheets/_pre-bootstrap.scss` partial.

2. Bootstrap via Webpack Dev Server

The webpack dev server does not go through Rails but instead loads bootstrap
via the `bootstrap-sass-loader` webpack loader. You can configure the loader
via the `client/bootstrap-sass-config.js` file.

3. Keeping Custom Bootstrap Configurations Synced

Because the webpack dev server and Rails each load Bootstrap via a different
file (explained in the two sections immediately above), any changes to
the way components are loaded in one file must also be made to the other
file in order to keep styling consistent between the two. For example,
if an import is excluded in _bootstrap-custom.scss, the same import should
be excluded in `bootstrap-sass-config.js` so that styling in the Rails
server and the webpack dev server will be the same.

4. Skip Bootstrap Integration

Bootstrap integration is enabled by default, but can be disabled by passing
the --skip-bootstrap flag (alias -b). When you don't need Bootstrap in your
existing project, just skip it as needed.

* JavaScript Linters

JavaScript linters are enabled by default, but can be disabled by passing the
--skip-js-linters flag (alias j), and those that run in Node have been added to
`client/package.json` under devDependencies.

* Ruby Linters

Ruby linters are disabled by default, but can be enabled by passing the
`--ruby-linters` flag when generating. These linters have been added to your
Gemfile in addition to the the appropriate Rake tasks.

We really love using all the linters! Give them a try.

*******************************************************************************

After running the generator, you will want to:
Expand All @@ -102,14 +16,7 @@ After running the generator, you will want to:

Then you may run

npm run

And you will see several useful commands:

express-server
echo 'visit http://localhost:4000' && cd client && npm start
rails-server
echo 'visit http://localhost:3000/hello_world' && foreman start -f Procfile.dev
foreman start -f Procfile.dev

More Details:

Expand Down
39 changes: 3 additions & 36 deletions lib/generators/react_on_rails/base_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ class BaseGenerator < Rails::Generators::Base # rubocop:disable Metrics/ClassLen
default: false,
desc: "Install Redux gems and Redux version of Hello World Example",
aliases: "-R"
# --server-rendering
class_option :server_rendering,
type: :boolean,
default: false,
desc: "Configure for server-side rendering of webpack JavaScript",
aliases: "-S"

def add_hello_world_route
route "get 'hello_world', to: 'hello_world#index'"
Expand Down Expand Up @@ -48,11 +42,7 @@ def update_application_js
// DO NOT REQUIRE jQuery or jQuery-ujs in this file!
// DO NOT REQUIRE TREE!
// CRITICAL that vendor-bundle must be BEFORE bootstrap-sprockets and turbolinks
// since it is exposing jQuery and jQuery-ujs
//= require vendor-bundle
//= require app-bundle
//= require webpack-bundle
DATA

Expand Down Expand Up @@ -86,10 +76,7 @@ def copy_base_files
base_path = "base/base/"
base_files = %w(app/controllers/hello_world_controller.rb
client/.babelrc
client/webpack.client.base.config.js
client/webpack.client.rails.config.js
REACT_ON_RAILS.md
client/REACT_ON_RAILS_CLIENT_README.md)
client/webpack.config.js)
base_files.each { |file| copy_file(base_path + file, file) }
end

Expand All @@ -103,26 +90,6 @@ def template_base_files
client/package.json).each { |file| template(base_path + file + ".tt", file) }
end

def add_base_gems_to_gemfile
return unless options.server_rendering?
append_to_file("Gemfile", "\ngem 'therubyracer', platforms: :ruby\n")
end

def template_client_registration_file
filename = "clientRegistration.jsx"
location = "client/app/bundles/HelloWorld/startup"
template("base/base/#{location}/clientRegistration.jsx.tt", "#{location}/#{filename}")
end

def install_server_rendering_files_if_enabled
return unless options.server_rendering?
base_path = "base/server_rendering/"
%w(client/webpack.server.rails.config.js
client/app/bundles/HelloWorld/startup/serverRegistration.jsx).each do |file|
copy_file(base_path + file, file)
end
end

def template_assets_rake_file
template("base/base/lib/tasks/assets.rake.tt", "lib/tasks/assets.rake")
end
Expand All @@ -132,7 +99,7 @@ def template_assets_rake_file
# If you do not want to move existing images and fonts from your Rails app
# you could also consider creating symlinks there that point to the original
# rails directories. In that case, you would not add these paths here.
Rails.application.config.assets.precompile += %w( server-bundle.js )
Rails.application.config.assets.precompile += %w( webpack-bundle.js )
# Add folder with webpack generated assets to assets.paths
Rails.application.config.assets.paths << Rails.root.join("app", "assets", "webpack")
Expand Down
18 changes: 17 additions & 1 deletion lib/generators/react_on_rails/dev_tests_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class DevTestsGenerator < Rails::Generators::Base
Rails::Generators.hide_namespace(namespace)
source_root(File.expand_path("../templates/dev_tests", __FILE__))

# --example-server-rendering
class_option :example_server_rendering,
type: :boolean,
default: false,
desc: "Setup prerender true for server rendered examples"

def copy_rspec_files
%w(spec/spec_helper.rb
spec/rails_helper.rb
Expand Down Expand Up @@ -40,7 +46,7 @@ def change_webpack_client_base_config_to_include_fallback
plugins: [
TEXT
sentinel = /^\s\s},\n\s\splugins: \[\n/
config = File.join(destination_root, "client", "webpack.client.base.config.js")
config = File.join(destination_root, "client", "webpack.config.js")
old_contents = File.read(config)
new_contents = old_contents.gsub(sentinel, text)
File.open(config, "w+") { |f| f.puts new_contents }
Expand All @@ -53,6 +59,16 @@ def add_test_related_gems_to_gemfile
gem("coveralls", require: false)
gem("poltergeist")
end

def gsub_prerender_if_server_rendering
return if !options.example_server_rendering
hello_world_index = File.join(destination_root, "app", "views", "hello_world", "index.html.erb")
hello_world_contents = File.read(hello_world_index)
new_hello_world_contents = hello_world_contents.gsub(/\) %>/,
', prerender: true) %>')

File.open(hello_world_index, "w+") { |f| f.puts new_hello_world_contents }
end
end
end
end
6 changes: 0 additions & 6 deletions lib/generators/react_on_rails/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ class InstallGenerator < Rails::Generators::Base
default: false,
desc: "Install Redux gems and Redux version of Hello World Example. Default: false",
aliases: "-R"
# --server-rendering
class_option :server_rendering,
type: :boolean,
default: false,
desc: "Add necessary files and configurations for server-side rendering. Default: false",
aliases: "-S"

# --ignore-warnings
class_option :ignore_warnings,
Expand Down
20 changes: 3 additions & 17 deletions lib/generators/react_on_rails/react_no_redux_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,16 @@ class ReactNoReduxGenerator < Rails::Generators::Base
Rails::Generators.hide_namespace(namespace)
source_root(File.expand_path("../templates", __FILE__))

# --server-rendering
class_option :server_rendering,
type: :boolean,
default: false,
desc: "Configure for server-side rendering of webpack JavaScript",
aliases: "-S"

def copy_base_files
base_path = "no_redux/base/"
file = "client/app/bundles/HelloWorld/containers/HelloWorld.jsx"
copy_file(base_path + file, file)
end

def copy_server_rendering_files_if_appropriate
return unless options.server_rendering?
base_path = "no_redux/server_rendering/"
file = "client/app/bundles/HelloWorld/startup/HelloWorldAppServer.jsx"
copy_file(base_path + file, file)
end

def template_appropriate_version_of_hello_world_app_client
filename = "HelloWorldAppClient.jsx"
def template_appropriate_version_of_hello_world_app
filename = "HelloWorldApp.jsx"
location = "client/app/bundles/HelloWorld/startup"
template("no_redux/base/#{location}/HelloWorldAppClient.jsx.tt", "#{location}/#{filename}")
template("no_redux/base/#{location}/HelloWorldApp.jsx.tt", "#{location}/#{filename}")
end
end
end
Expand Down
25 changes: 4 additions & 21 deletions lib/generators/react_on_rails/react_with_redux_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@ class ReactWithReduxGenerator < Rails::Generators::Base
Rails::Generators.hide_namespace(namespace)
source_root(File.expand_path("../templates", __FILE__))

# --server-rendering
class_option :server_rendering,
type: :boolean,
default: false,
desc: "Configure for server-side rendering of webpack JavaScript",
aliases: "-S"

def create_redux_directories
dirs = %w(actions constants reducers store)
dirs.each { |name| empty_directory("client/app/bundles/HelloWorld/#{name}") }

empty_directory("client/app/lib/middlewares")
end

def copy_base_redux_files
Expand All @@ -27,23 +18,15 @@ def copy_base_redux_files
client/app/bundles/HelloWorld/constants/helloWorldConstants.jsx
client/app/bundles/HelloWorld/reducers/helloWorldReducer.jsx
client/app/bundles/HelloWorld/reducers/index.jsx
client/app/bundles/HelloWorld/store/helloWorldStore.jsx
client/app/lib/middlewares/loggerMiddleware.js).each do |file|
client/app/bundles/HelloWorld/store/helloWorldStore.jsx).each do |file|
copy_file(base_path + file, file)
end
end

def copy_server_rendering_redux_files
return unless options.server_rendering?
base_path = "redux/server_rendering/"
file = "client/app/bundles/HelloWorld/startup/HelloWorldAppServer.jsx"
copy_file(base_path + file, file)
end

def template_appropriate_version_of_hello_world_app_client
filename = "HelloWorldAppClient.jsx"
def template_appropriate_version_of_hello_world_app
filename = "HelloWorldApp.jsx"
location = "client/app/bundles/HelloWorld/startup"
template("redux/base/#{location}/HelloWorldAppClient.jsx.tt", "#{location}/#{filename}")
template("redux/base/#{location}/HelloWorldApp.jsx.tt", "#{location}/#{filename}")
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
web: rails s
client: sh -c 'rm app/assets/webpack/* || true && cd client && npm run build:dev:client'
<%- if options.server_rendering? %>server: sh -c 'cd client && npm run build:dev:server'<%- end %>
client: sh -c 'rm app/assets/webpack/* || true && cd client && npm run build:dev'

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<h1 class="alert alert-info this-works"><%= options.server_rendering? ? "Server Rendering" : "Client Rendering" %></h1>
<%%= react_component("HelloWorldApp",
props: @hello_world_props,
prerender: <%= options.server_rendering? %>) %>
<h1 class="alert alert-info this-works">Hello World</h1>
<%%= react_component("HelloWorldApp", props: @hello_world_props) %>

This file was deleted.

Loading

0 comments on commit 8401c9d

Please sign in to comment.