Skip to content

Commit 6bc8002

Browse files
committedDec 13, 2014
Merge pull request shakacode#3 from mbreining/master
Integrate npm bootstrap-sass; Remove bootstrap-sass gem; Update README
2 parents d56b717 + 223b549 commit 6bc8002

19 files changed

+397
-150
lines changed
 

‎Gemfile

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gem 'rails_12factor'
1212

1313
# Use SCSS for stylesheets
1414
gem 'sass-rails', '~> 5.0.0.beta1'
15+
gem 'bootstrap-sass', '~> 3.3.1'
1516
# Use Uglifier as compressor for JavaScript assets
1617
gem 'uglifier', '>= 1.3.0'
1718
# Use CoffeeScript for .js.coffee assets and views
@@ -37,6 +38,8 @@ gem 'rails-html-sanitizer', '~> 1.0'
3738
# Use Unicorn as the app server
3839
gem 'unicorn'
3940

41+
gem 'autoprefixer-rails'
42+
4043
# Use Capistrano for deployment
4144
# gem 'capistrano-rails', group: :development
4245

@@ -49,9 +52,7 @@ group :development, :test do
4952

5053
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
5154
gem 'spring'
52-
end
53-
54-
55-
gem 'bootstrap-sass', '~> 3.2.0'
56-
gem 'autoprefixer-rails'
5755

56+
# Manage application processes
57+
gem 'foreman'
58+
end

‎Gemfile.lock

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ GEM
3939
execjs
4040
binding_of_caller (0.7.3.pre1)
4141
debug_inspector (>= 0.0.1)
42-
bootstrap-sass (3.2.0.1)
42+
bootstrap-sass (3.3.1.0)
4343
sass (~> 3.2)
4444
builder (3.2.2)
4545
byebug (3.2.0)
@@ -55,8 +55,12 @@ GEM
5555
columnize (0.8.9)
5656
debug_inspector (0.0.2)
5757
debugger-linecache (1.2.0)
58+
dotenv (1.0.2)
5859
erubis (2.7.0)
5960
execjs (2.2.1)
61+
foreman (0.76.0)
62+
dotenv (~> 1.0.2)
63+
thor (~> 0.19.1)
6064
globalid (0.2.3)
6165
activesupport (>= 4.1.0)
6266
hike (1.2.3)
@@ -161,9 +165,10 @@ PLATFORMS
161165

162166
DEPENDENCIES
163167
autoprefixer-rails
164-
bootstrap-sass (~> 3.2.0)
168+
bootstrap-sass (~> 3.3.1)
165169
byebug
166170
coffee-rails (~> 4.0.0)
171+
foreman
167172
jbuilder (~> 2.0)
168173
jquery-rails
169174
pg

‎Procfile.dev

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
web: rails s -p 4000
2-
webpack: cd webpack && webpack -w --config webpack.rails.config.js
2+
webpack: cd webpack && $(npm bin)/webpack -w --config webpack.bundle.config.js

‎README.md

+100-35
Original file line numberDiff line numberDiff line change
@@ -8,88 +8,153 @@ Full tutorial can be found at: [Fast Rich Client Rails Development With Webpack
88

99
# Motivation
1010

11-
1. Enable development of a JS client separate from Rails.
12-
2. Enable easily retrofitting such a JS framework into an existing Rails app.
13-
3. Enable the use of the JavaScript es6 transpiler.
14-
4. Enable easily using npm modules with a Rails application.
11+
In no particular order:
12+
- Enable development of a JS client independently from Rails.
13+
- Easily enable use of npm modules with a Rails application.
14+
- Easily enable retrofitting such a JS framework into an existing Rails app.
15+
- Enable the use of the JavaScript ES6 transpiler.
1516

16-
# Example of the following technologies:
17+
# Technologies involved
1718

18-
1. react
19-
2. react-bootstrap
20-
3. webpack with hot-reload
21-
4. es6-loader (es6 transpiler)
22-
5. Simultaneously working with Rails 4.2
23-
6. Deployable to Heroku
19+
1. React 0.11 (for front-end app)
20+
2. React-bootstrap 0.12
21+
3. Webpack with hot-reload 1.4 (for local dev)
22+
4. ES6 transpiler (es6-loader) 0.2
23+
5. Rails 4.2 (for backend app)
24+
6. Heroku (for deployment)
2425

25-
# Running without Rails using Hot Reload
26+
# Javascript development without Rails using Hot Module Replacement (HMR)
2627

2728
Setup node and run the node server.
29+
2830
```
2931
npm install
3032
cd webpack && node server.js
3133
```
3234

33-
Point browser to [http://0.0.0.0:3000]().
35+
Point your browser to [http://0.0.0.0:3000]().
36+
3437

3538
Save a change to a JSX file and see it update immediately in the browser! Note,
3639
any browser state still exists, such as what you've typed in the comments box.
37-
That's totally different than "Live Reload" which refreshes the browser.
40+
That's totally different than [Live Reload](http://livereload.com/) which refreshes
41+
the browser.
42+
43+
# Rails integration
44+
45+
## Build JS/CSS bundles
46+
Run this command to have webpack build the JS/CSS bundles and have them saved in the Rails
47+
asset pipeline (app/assets).
48+
The Webpack ExtractTextPlugin can optionally be used to extract the CSS out of
49+
the JS bundle. The following bundles would then be generated:
50+
- rails-bundle.js which gets saved to app/assets/javascripts
51+
- bootstrap-and-customizations.css which gets saved in app/assets/stylesheets
52+
Observe how the bundles are automatically re-generated whenever your JSX changes.
3853

39-
# Rails
54+
```
55+
cd webpack
56+
webpack -w --config webpack.rails.config.js
57+
```
4058

59+
Make sure to invoke your local copy of the webpack executable as opposed
60+
to any globally installed webpack.
61+
See https://github.com/webpack/extract-text-webpack-plugin/blob/master/example/webpack.config.js
62+
If in doubt, run the following command:
4163
```
64+
$(npm bin)/webpack -w --config webpack.rails.config.js
65+
```
66+
67+
## Run Rails server
68+
69+
Once the JS/CSS bundled have been generated into the Rails asset pipeline, you can start
70+
the Rails server.
71+
72+
```
73+
cd <rails_project_name>
4274
bundle install
4375
rake db:setup
4476
rails s -p 4000
4577
```
46-
Point browser to [http://0.0.0.0:4000]().
78+
Point your browser to [http://0.0.0.0:4000]().
4779

48-
It's important to run the rails server on different port than the node server.
80+
It's important to run the Rails server on a different port than the node server.
4981

50-
## Automatically Building the rails-bundle.js
51-
Run this command to automatically build the rails-bundle.js file in the
52-
javascript directory whenever your jsx files change.
82+
# Webpack configuration
83+
- `webpack.hot.config.js`: Used by server.js to run the demo express server.
84+
- `webpack.rails.config.js`: Used to generate the Rails bundles.
85+
- `webpack.common.config.js`: Common configuration file to minimize code duplication.
5386

54-
```
55-
cd webpack
56-
webpack -w --config webpack.rails.config.js
57-
```
87+
# Bootstrap integration
88+
Notice that Bootstrap Sass is installed as both a gem and an npm package.
89+
When running the Rails app, the bootstrap-sass gem assets are loaded directly
90+
through the asset pipeline without passing through Webpack.
91+
See app/assets/application.css.scss.
92+
On the other hand when running the Webpack dev server, the bootrap-sass npm
93+
assets are loaded through Webpack (with help of the bootstrap-sass-loader).
94+
See webpack/webpack.hot.config.js.
95+
96+
97+
Bootstrap can be customized by hand-picking which modules to load and/or overwriting
98+
some of the Sass variables defined by the frameworks.
99+
100+
## Bootstrap modules customization
101+
102+
If you are not using all the Bootstrap modules then you'll likely want to customize
103+
it to avoid loading unused assets. This customization is done in separate files
104+
for the Rails app versus the Webpack dev server so it's important to keep these
105+
in-sync as you develop your app in parallel using the Rails and the Webpack HMR
106+
environments.
107+
108+
- Rails Bootstrap customization file: app/assets/stylesheets/_bootstrap-custom.scss
109+
- Webpack HMR Bootstrap customization file: webpack/bootstrap-sass.config.js
110+
111+
## Bootstrap variables customization
58112

59-
# Webpack Configuration
60-
`webpack.hot.config.js`: Used by server.js to run the demo server.
61-
`webpack.rails.config.js`: Used to generate the rails-bundle.js file
113+
If you need to customize some of the Sass variables defined in Bootstrap you
114+
can do so by overwriting these variables in a separate file and have it loaded
115+
before other Bootstrap modules.
62116

63-
# Notes on Rails Assets
117+
To avoid duplicating this customization between Rails and Webpack HMR,
118+
this custom code has been consolidated under Webpack in
119+
webpack/assets/stylesheets/_bootstrap-variables-customization.scss and the
120+
webpack/assets/stylesheets directory added to the Rails asset pipeline
121+
search path. See config config/application.rb. Keep that in mind as you
122+
customize the Bootstrap Sass variables.
123+
124+
# Notes on Rails assets
64125
## Javascript
65-
The `webpack.rails.config.js` file generates rails-bundle.js which is included
126+
The `webpack.rails.config.js` file generates rails-bundle.js which is then included
66127
by the Rails asset pipeline.
67128

68129
## Sass and images
69-
1. The Webpack server loads the images from the **symlink** of of the
130+
1. The Webpack server loads the images from the **symlink** of the
70131
app/assets/images directory.
71132
2. Since the images are not moved, Rails loads images via the normal asset
72133
pipeline features.
73134
3. The `image-url` sass helper takes care of mapping the correct directories for
74135
images. The image directory for the webpack server is configured by this
75136
line:
76137

138+
```
77139
{ test: /\.scss$/, loader: "style!css!sass?outputStyle=expanded&imagePath=/assets/images"}
140+
```
141+
142+
# Process management
143+
Run the following command in your development environment to invoke both Webpack and Rails.
144+
```
145+
bundle exec foreman start -f Procfile.dev
146+
```
78147

79148
# Source Maps
80149
They work for both Rails and the Webpack Server!
81150

82151
# Deploying to Heroku
83152

84-
In order to deploy to heroku, you'll need run this command once to set a custom
153+
In order to deploy to heroku, you'll need to run this command once to set a custom
85154
buildpack:
86155

87156
```
88157
heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
89158
```
90159

91160
This runs the two buildpacks in the `.buildpacks` directory.
92-
93-
# TO DO
94-
1. (Optionally) integrate twitter bootstrap assets into webpack build with way
95-
to configure same options for Rails and Webpack.

‎app/assets/javascripts/application.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
//= require jquery
1414
//= require jquery_ujs
1515

16+
//= require bootstrap-sprockets
17+
1618
// Important to import jquery_ujs before rails-bundle as that patches jquery xhr to use the authenticity token!
1719

18-
//= require rails-bundle
20+
//= require webpack-bundle
1921
//= require turbolinks
20-
//= require bootstrap-sprockets
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Customizations - needs to be imported first!
2+
@import "bootstrap-variables-customization";
3+
4+
// Core variables and mixins
5+
@import "bootstrap/variables";
6+
@import "bootstrap/mixins";
7+
8+
// Reset and dependencies
9+
@import "bootstrap/normalize";
10+
@import "bootstrap/print";
11+
@import "bootstrap/glyphicons";
12+
13+
// Core CSS
14+
@import "bootstrap/scaffolding";
15+
@import "bootstrap/type";
16+
@import "bootstrap/code";
17+
@import "bootstrap/grid";
18+
@import "bootstrap/tables";
19+
@import "bootstrap/forms";
20+
@import "bootstrap/buttons";
21+
22+
// Components
23+
@import "bootstrap/component-animations";
24+
@import "bootstrap/dropdowns";
25+
@import "bootstrap/button-groups";
26+
@import "bootstrap/input-groups";
27+
@import "bootstrap/navs";
28+
@import "bootstrap/navbar";
29+
@import "bootstrap/breadcrumbs";
30+
@import "bootstrap/pagination";
31+
@import "bootstrap/pager";
32+
@import "bootstrap/labels";
33+
@import "bootstrap/badges";
34+
//@import "bootstrap/jumbotron"; // excluding as an example
35+
@import "bootstrap/thumbnails";
36+
@import "bootstrap/alerts";
37+
//@import "bootstrap/progress-bars"; // excluding as an example
38+
@import "bootstrap/media";
39+
@import "bootstrap/list-group";
40+
@import "bootstrap/panels";
41+
@import "bootstrap/responsive-embed";
42+
@import "bootstrap/wells";
43+
@import "bootstrap/close";
44+
45+
// Components w/ JavaScript
46+
@import "bootstrap/modals"; // excluding as an example
47+
@import "bootstrap/tooltip";
48+
@import "bootstrap/popovers";
49+
@import "bootstrap/carousel"; // excluding as an example
50+
51+
// Utility classes
52+
@import "bootstrap/utilities";
53+
@import "bootstrap/responsive-utilities";
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
@import "bootstrap-sprockets";
2-
@import "bootstrap";
31
@import "../../../webpack/assets/stylesheets/test-stylesheet";
42
@import "../../../webpack/assets/stylesheets/test-sass-stylesheet";
3+
4+
@import "bootstrap-sprockets";
5+
@import "bootstrap-custom";

‎config/application.rb

+3
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ class Application < Rails::Application
2222

2323
# For not swallow errors in after_commit/after_rollback callbacks.
2424
config.active_record.raise_in_transactional_callbacks = true
25+
26+
# Add webpack/assets/stylesheets to asset pipeline's search path.
27+
config.assets.paths << Rails.root.join("webpack", "assets" ,"stylesheets")
2528
end
2629
end

‎package.json

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"node": "0.10.32"
88
},
99
"dependencies": {
10+
"bootstrap-sass": "~3.3.1",
11+
"bootstrap-sass-loader": "~0.0.4",
12+
"imports-loader": "^0.6.3",
1013
"body-parser": "^1.9.0",
1114
"bootstrap-webpack": "*",
1215
"es6-loader": "^0.2.0",

0 commit comments

Comments
 (0)
Please sign in to comment.