Skip to content

Commit c89ac91

Browse files
authored
Merge pull request #678 from reactjs/webpack-ujs
refactor(UJS) make it webpack-friendly
2 parents d62f518 + 47873d4 commit c89ac91

Some content is hidden

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

63 files changed

+7649
-301
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
Gemfile.lock
33
*.log
44
test/dummy/tmp
5+
test/dummy/public/packs
56
gemfiles/*.lock
67
*.swp
78
/vendor/react
8-
react-builds/node_modules
9+
**/node_modules
910
react-builds/build
1011
coverage/

.travis.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
language: ruby
22
sudo: required
33
dist: trusty
4-
cache: bundler
4+
cache:
5+
bundler: true
6+
yarn: true
57
rvm:
68
- 2.3.0
79
- 2.1
@@ -60,3 +62,8 @@ before_install:
6062
- tar -xvf $PWD/travis-phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 -C $PWD/travis-phantomjs
6163
- export PATH=$PWD/travis-phantomjs/phantomjs-2.1.1-linux-x86_64/bin:$PATH
6264
- phantomjs --version
65+
# Repo for Yarn
66+
- sudo apt-key adv --fetch-keys http://dl.yarnpkg.com/debian/pubkey.gpg
67+
- echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
68+
- sudo apt-get update -qq
69+
- sudo apt-get install -y -qq yarn

Appraisals

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ appraise "rails-4.2-sprockets_4" do
4343
gem 'rails', '~> 4.2.1'
4444
gem "sprockets", "~> 4.0.x"
4545
gem "turbolinks", "~> 2.5.0"
46+
gem "webpacker", github: "rails/webpacker"
4647
end
4748

4849
appraise "rails-5" do
4950
gem 'rails', '~> 5.0.0'
5051
gem "turbolinks", "~> 5.0.0"
52+
gem "webpacker", github: "rails/webpacker"
5153
end
5254

5355
appraise "rails-5-no_sprockets" do

Rakefile

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ def copy_react_asset(webpack_file, destination_file)
1212
FileUtils.cp(full_webpack_path, full_destination_path)
1313
end
1414

15+
# Move to `dirname` and execute `yarn {cmd}`
16+
def yarn_run_in(dirname, cmd)
17+
Dir.chdir(dirname) do
18+
`yarn #{cmd}`
19+
end
20+
end
21+
1522
namespace :react do
1623
desc "Run the JS build process to put files in the gem source"
1724
task update: [:install, :build, :copy]
1825

1926
desc "Build the JS bundles with Webpack"
2027
task :build do
21-
Dir.chdir("react-builds") do
22-
`yarn run build`
23-
end
28+
yarn_run_in("react-builds", "build")
2429
end
2530

2631
desc "Copy browser-ready JS files to the gem's asset paths"
@@ -39,8 +44,36 @@ namespace :react do
3944

4045
desc "Install the JavaScript dependencies"
4146
task :install do
42-
Dir.chdir("react-builds") do
43-
`yarn upgrade`
47+
yarn_run_in("react-builds", "upgrade")
48+
end
49+
end
50+
51+
namespace :ujs do
52+
desc "Run the JS build process to put files in the gem source"
53+
task update: [:install, :build, :copy]
54+
55+
desc "Install the JavaScript dependencies"
56+
task :install do
57+
yarn_run_in("react_ujs", "upgrade")
58+
end
59+
60+
61+
desc "Build the JS bundles with Webpack"
62+
task :build do
63+
yarn_run_in("react_ujs", "build")
64+
end
65+
66+
desc "Copy browser-ready JS files to the gem's asset paths"
67+
task :copy do
68+
full_webpack_path = File.expand_path("../react_ujs/dist/react_ujs.js", __FILE__)
69+
full_destination_path = File.expand_path("../lib/assets/javascripts/react_ujs.js", __FILE__)
70+
FileUtils.cp(full_webpack_path, full_destination_path)
71+
end
72+
73+
desc "Publish the package in ./react_ujs/ to npm as `react_ujs`"
74+
task :publish do
75+
Dir.chdir("react_ujs") do
76+
`npm publish`
4477
end
4578
end
4679
end
@@ -57,3 +90,11 @@ Rake::TestTask.new(:test) do |t|
5790
end
5891

5992
task default: :test
93+
94+
task :test_setup do
95+
Dir.chdir("./test/dummy") do
96+
`yarn install`
97+
end
98+
end
99+
100+
task test: :test_setup

gemfiles/rails_4.2_sprockets_4.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ source "http://rubygems.org"
55
gem "rails", "~> 4.2.1"
66
gem "sprockets", "~> 4.0.x"
77
gem "turbolinks", "~> 2.5.0"
8+
gem "webpacker", :github => "rails/webpacker"
89

910
gemspec :path => "../"

gemfiles/rails_5.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ source "http://rubygems.org"
44

55
gem "rails", "~> 5.0.0"
66
gem "turbolinks", "~> 5.0.0"
7+
gem "webpacker", :github => "rails/webpacker"
78

89
gemspec :path => "../"

0 commit comments

Comments
 (0)