Skip to content

Commit f3489ec

Browse files
authored
update react_on_rails to beta version (#475)
This PR demonstrates how to use the rails/webpacker bin/webpack to call a custom webpack configuration for tests and production.
1 parent 0152e91 commit f3489ec

13 files changed

+114
-56
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11
1+
12

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ruby-2.5.3
1+
ruby-2.7.1

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ gemfile: Gemfile
2121
env:
2222
global:
2323
- RAILS_ENV=test
24+
- NODE_ENV=test
2425
- DRIVER=selenium_chrome
2526
- CHROME_BIN=/usr/bin/google-chrome
2627
- USE_COVERALLS=TRUE
@@ -34,7 +35,7 @@ before_install:
3435

3536
install:
3637
- travis_retry gem install bundler -v '<2' # Ruby 2.2 and Rails 3.2 & 4.2 depend on bundler 1.x.
37-
- travis_retry nvm install 11.7.0
38+
- travis_retry nvm install 12
3839
- node -v
3940
- travis_retry npm i -g yarn
4041
- travis_retry bundle install

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
source "https://rubygems.org"
44
ruby "2.7.1"
55

6-
gem "react_on_rails", "12.0.0-beta.1"
6+
gem "react_on_rails", github: "shakacode/react_on_rails", branch: "justin808/remove-precompile-rake"
77
gem "webpacker"
88

99
# Bundle edge Rails instead: gem "rails", github: "rails/rails"
@@ -96,12 +96,12 @@ end
9696
group :test do
9797
gem "capybara", "2.18.0"
9898
gem "capybara-screenshot"
99-
gem "chromedriver-helper"
10099
gem "coveralls", require: false
101100
gem "database_cleaner"
102101
gem "generator_spec"
103102
gem "launchy"
104103
gem "rails_best_practices"
105104
gem "rspec-rails", "3.7.2"
106105
gem "selenium-webdriver"
106+
gem "webdrivers", "~> 4.0"
107107
end

Gemfile.lock

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
GIT
2+
remote: https://github.com/shakacode/react_on_rails.git
3+
revision: 38403e66bba1c3fde7edd4fb46c7f7daaaa111a6
4+
branch: justin808/remove-precompile-rake
5+
specs:
6+
react_on_rails (12.0.0.pre.beta.2)
7+
addressable
8+
connection_pool
9+
execjs (~> 2.5)
10+
rails (>= 3.2)
11+
rainbow (~> 3.0)
12+
webpacker (>= 4.0)
13+
114
GEM
215
remote: https://rubygems.org/
316
specs:
@@ -44,8 +57,6 @@ GEM
4457
tzinfo (~> 1.1)
4558
addressable (2.7.0)
4659
public_suffix (>= 2.0.2, < 5.0)
47-
archive-zip (0.12.0)
48-
io-like (~> 0.3.0)
4960
arel (9.0.0)
5061
ast (2.4.1)
5162
autoprefixer-rails (9.7.6)
@@ -71,9 +82,6 @@ GEM
7182
capybara (>= 1.0, < 4)
7283
launchy
7384
childprocess (3.0.0)
74-
chromedriver-helper (2.1.1)
75-
archive-zip (~> 0.10)
76-
nokogiri (~> 1.8)
7785
code_analyzer (0.5.1)
7886
sexp_processor
7987
coderay (1.1.3)
@@ -115,7 +123,6 @@ GEM
115123
i18n (1.8.3)
116124
concurrent-ruby (~> 1.0)
117125
interception (0.5)
118-
io-like (0.3.1)
119126
jbuilder (2.10.0)
120127
activesupport (>= 5.0.0)
121128
json (2.3.0)
@@ -209,12 +216,6 @@ GEM
209216
rb-inotify (0.10.1)
210217
ffi (~> 1.0)
211218
rdoc (6.2.1)
212-
react_on_rails (12.0.0.pre.beta.1)
213-
addressable
214-
connection_pool
215-
execjs (~> 2.5)
216-
rails (>= 3.2)
217-
rainbow (~> 3.0)
218219
redis (3.3.3)
219220
regexp_parser (1.7.1)
220221
require_all (3.0.0)
@@ -310,6 +311,10 @@ GEM
310311
activemodel (>= 5.0)
311312
bindex (>= 0.4.0)
312313
railties (>= 5.0)
314+
webdrivers (4.4.1)
315+
nokogiri (~> 1.6)
316+
rubyzip (>= 1.3.0)
317+
selenium-webdriver (>= 3.0, < 4.0)
313318
webpacker (5.1.1)
314319
activesupport (>= 5.2)
315320
rack-proxy (>= 0.6.1)
@@ -332,7 +337,6 @@ DEPENDENCIES
332337
bundler-audit
333338
capybara (= 2.18.0)
334339
capybara-screenshot
335-
chromedriver-helper
336340
coffee-rails
337341
coveralls
338342
database_cleaner
@@ -355,7 +359,7 @@ DEPENDENCIES
355359
rails-html-sanitizer
356360
rails_best_practices
357361
rainbow
358-
react_on_rails (= 12.0.0.pre.beta.1)
362+
react_on_rails!
359363
redis (= 3.3.3)
360364
rspec-rails (= 3.7.2)
361365
rubocop
@@ -368,6 +372,7 @@ DEPENDENCIES
368372
spring-commands-rspec
369373
uglifier
370374
web-console
375+
webdrivers (~> 4.0)
371376
webpacker
372377

373378
RUBY VERSION

bin/webpack

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env ruby
2+
3+
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
4+
ENV["NODE_ENV"] ||= "development"
5+
6+
require "pathname"
7+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
8+
Pathname.new(__FILE__).realpath)
9+
10+
require "bundler/setup"
11+
require "webpacker"
12+
require "webpacker/webpack_runner"
13+
14+
APP_ROOT = File.expand_path("..", __dir__)
15+
16+
puts "bin/webpack is running #{APP_ROOT}/bin/webpack.sh"
17+
exec "#{APP_ROOT}/bin/webpack.sh"

bin/webpack.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
5+
__root="$(dirname $(dirname $(realpath $0)))"
6+
cd $__root
7+
8+
# echo BEFORE
9+
# echo RAILS_ENV is ${RAILS_ENV:-UNDEFINED}
10+
# echo NODE_ENV is ${NODE_ENV:-UNDEFINED}
11+
12+
# From default for bin/webpack from @rails/webpacker
13+
# ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
14+
# ENV["NODE_ENV"] ||= "development"
15+
16+
RAILS_ENV=${RAILS_ENV:-${RACK_ENV:-development}}
17+
NODE_ENV=${NODE_ENV:-development}
18+
19+
echo Running $0
20+
echo RAILS_ENV is $RAILS_ENV, NODE_ENV is $NODE_ENV
21+
22+
23+
#if [[ $RAILS_ENV = "production" ]] ; then
24+
# yarn run build:production
25+
#fi
26+
27+
#if [[ $RAILS_ENV = "test" ]] ; then
28+
# yarn run build:test
29+
#fi
30+
31+
bundle exec rake react_on_rails:locale
32+
33+
# In case you have your build scripts in the /client subdirectory
34+
cd client
35+
36+
case $NODE_ENV in
37+
test)
38+
# rm -rf public/webpack/development || true
39+
yarn run build:test "$@"
40+
;;
41+
production)
42+
yarn run build:production "$@"
43+
;;
44+
*)
45+
yarn run build:dev "$@"
46+
esac

client/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Code from the React Webpack tutorial.",
55
"main": "server-express.js",
66
"engines": {
7-
"node": "^7.8.0 || ^8.0.0 || ^9.2.0 || ^10.0.0 || ^11.0.0",
7+
"node": "12",
88
"yarn": "^1.0.0"
99
},
1010
"repository": {
@@ -31,6 +31,7 @@
3131
"build:production:client": "NODE_ENV=production webpack -p --config webpack.client.rails.build.config.js",
3232
"build:production:server": "NODE_ENV=production webpack -p --config webpack.server.rails.build.config.js",
3333
"build:client": "webpack --config webpack.client.rails.build.config.js",
34+
"build:dev": "yarn run build:client && yarn run build:server",
3435
"build:dev:client": "NODE_ENV=development webpack -w --config webpack.client.rails.build.config.js",
3536
"build:dev:server": "NODE_ENV=development webpack -w --config webpack.server.rails.build.config.js",
3637
"build:server": "webpack --config webpack.server.rails.build.config.js",
@@ -116,7 +117,7 @@
116117
"pug": "^2.0.0-rc.4",
117118
"react-transform-hmr": "^1.0.4",
118119
"regenerator-runtime": "^0.12.1",
119-
"sleep": "^5.1.1",
120+
"sleep": "^6.2.0",
120121
"webpack-dev-server": "^2.9.4"
121122
},
122123
"jest": {

client/yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6086,7 +6086,7 @@ [email protected]:
60866086
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
60876087
integrity sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=
60886088

6089-
nan@>=2.12.1, nan@^2.12.1, nan@^2.13.2:
6089+
nan@^2.12.1, nan@^2.13.2:
60906090
version "2.14.1"
60916091
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.1.tgz#d7be34dfa3105b91494c3147089315eff8874b01"
60926092
integrity sha512-isWHgVjnFjh2x2yuJ/tj3JbwoHu3UC2dX5G/88Cm24yB6YopVgxvBObDY7n5xW6ExmFhJpSEQqFPvq9zaXc8Jw==
@@ -8333,12 +8333,12 @@ slash@^1.0.0:
83338333
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
83348334
integrity sha1-xB8vbDn8FtHNF61LXYlhFK5HDVU=
83358335

8336-
sleep@^5.1.1:
8337-
version "5.2.4"
8338-
resolved "https://registry.yarnpkg.com/sleep/-/sleep-5.2.4.tgz#d1922209f61a1f29bc910495d406e3697dd730ec"
8339-
integrity sha512-SoltvxayTifWOgOGD6CTh+djcp5TaOa/zdbaA38wEH1ahF2azmiLOh8CPt6ExHf0pAJAsA9OCHTS7zK24Ym4yA==
8336+
sleep@^6.2.0:
8337+
version "6.2.0"
8338+
resolved "https://registry.yarnpkg.com/sleep/-/sleep-6.2.0.tgz#88029df49529b8aa3ea5b5a510d44921101579e5"
8339+
integrity sha512-W4Rpfx82IGEa3lMQQPBbew0lvb0u3WY5geYzeVmIvNuc/y3YUcx2c02zZBOk+R8E318KAtYU/Ee5tqZwrKqZHg==
83408340
dependencies:
8341-
nan ">=2.12.1"
8341+
nan "^2.13.2"
83428342

83438343
83448344
version "1.0.0"

config/initializers/react_on_rails.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Shown below are the defaults for configuration
22
ReactOnRails.configure do |config|
3-
config.node_modules_location = "client"
4-
53
# Define the files for we need to check for webpack compilation when running tests
64
config.webpack_generated_files = %w[ app-bundle.js vendor-bundle.js app-bundle.css
75
vendor-bundle.css server-bundle.js ]
@@ -12,15 +10,6 @@
1210
# not affect performance.
1311
config.server_bundle_js_file = "server-bundle.js"
1412

15-
# If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
16-
# with rspec then this controls what npm command is run
17-
# to automatically refresh your webpack assets on every test run.
18-
config.build_test_command = "yarn run build:test"
19-
20-
# This configures the script to run to build the production assets by webpack. Set this to nil
21-
# if you don't want react_on_rails building this file for you.
22-
config.build_production_command = "yarn run build:production"
23-
2413
################################################################################
2514
# CLIENT RENDERING OPTIONS
2615
# Below options can be overriden by passing options to the react_on_rails

config/webpacker.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ default: &default
99
# Reload manifest.json on all requests so we reload latest compiled packs
1010
cache_manifest: false
1111

12-
# Important to set this to false if not using the webpacker webpack setup
1312
compile: false
1413

14+
compile_output: true
15+
1516
# default to building the extracted CSS file
1617
extract_css: true
1718

1819
development:
1920
<<: *default
21+
compile: true
2022

2123
dev_server:
2224
host: localhost
@@ -28,6 +30,7 @@ development:
2830

2931
test:
3032
<<: *default
33+
compile: true
3134

3235
# Compile test packs to a separate directory
3336
public_output_path: webpack-test

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
"description": "ShakaCode's example of React on Rails plus React Native",
55
"main": "server-express.js",
66
"engines": {
7-
"node": "^7.8.0 || ^8.0.0 || ^9.2.0 || ^10.0.0 || ^11.0.0",
7+
"node": "12",
88
"yarn": "^1.0.0"
99
},
1010
"scripts": {
1111
"postinstall": "cd client && yarn install",
1212
"test": "rspec && yarn run test:client && yarn run lint",
13-
"test:client": "(cd client && yarn run --silent test)",
14-
"lint": "(cd client && yarn run --silent lint)",
13+
"test:client": "(cd client && yarn run test)",
14+
"lint": "(cd client && yarn run lint)",
1515
"build:clean": "rm app/assets/webpack/*",
16-
"build:production": "(cd client && yarn run --silent build:production)",
17-
"build:production:client": "(cd client && yarn run --silent build:production:client)",
18-
"build:production:server": "(cd client && yarn run --silent build:production:server)",
19-
"build:client": "(cd client && yarn run --silent build:client)",
20-
"build:server": "(cd client && yarn run --silent build:server)",
21-
"build:dev:client": "(cd client && yarn run --silent build:dev:client)",
22-
"build:dev:server": "(cd client && yarn run --silent build:dev:server)",
16+
"build:production": "(cd client && yarn run build:production)",
17+
"build:production:client": "(cd client && yarn run build:production:client)",
18+
"build:production:server": "(cd client && yarn run build:production:server)",
19+
"build:client": "(cd client && yarn run build:client)",
20+
"build:server": "(cd client && yarn run build:server)",
21+
"build:dev:client": "(cd client && yarn run build:dev:client)",
22+
"build:dev:server": "(cd client && yarn run build:dev:server)",
2323
"hot-assets": "(cd client && yarn run hot-assets)",
24-
"start": "(cd client && yarn run --silent start)"
24+
"start": "(cd client && yarn run start)"
2525
},
2626
"repository": {
2727
"url": "https://github.com/shakacode/react-webpack-rails-tutorial.git",

spec/rails_helper.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This file is copied to spec/ when you run "rails generate rspec:install"
22
ENV["RAILS_ENV"] ||= "test"
3+
ENV["NODE_ENV"] ||= "test"
34
require "coveralls"
45
Coveralls.wear!("rails") # must occur before any of your application code is required
56
require "spec_helper"
@@ -8,6 +9,7 @@
89
require "rspec/rails"
910
require "capybara/rspec"
1011
require "capybara-screenshot/rspec"
12+
require "webdrivers"
1113

1214
## Add additional requires below this line. Rails is not loaded until this point!
1315

@@ -35,12 +37,6 @@
3537
Dir[Rails.root.join("spec", "support", "**", "*.rb")].each { |f| require f }
3638

3739
RSpec.configure do |config|
38-
# Ensure that if we are running js tests, we are using latest webpack assets
39-
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config, :requires_webpack_assets)
40-
config.define_derived_metadata(file_path: %r{spec/(system|requests)}) do |metadata|
41-
metadata[:requires_webpack_assets] = true
42-
end
43-
4440
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
4541
config.fixture_path = "#{::Rails.root}/spec/fixtures"
4642

0 commit comments

Comments
 (0)