Skip to content

Commit 1311f96

Browse files
committed
Vendor react assets instead of including react-source
1 parent 6f7988b commit 1311f96

16 files changed

+57235
-14
lines changed

.bowerrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"directory" : "vendor/assets/"
3+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Gemfile.lock
44
test/dummy/tmp
55
gemfiles/*.lock
66
*.swp
7+
/lib/assets/react-source/

Rakefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ end
66

77
Bundler::GemHelper.install_tasks
88

9+
require 'pathname'
10+
require 'react/source'
11+
12+
namespace :react do
13+
task :copy_assets do
14+
assets_path = Pathname.new(__dir__).join('lib/assets/react-source/')
15+
copy_react_asset('react.js', assets_path.join('development/react.js'))
16+
copy_react_asset('react.min.js', assets_path.join('production/react.js'))
17+
copy_react_asset('react-with-addons.js', assets_path.join('development-with-addons/react.js'))
18+
copy_react_asset('react-with-addons.min.js', assets_path.join('production-with-addons/react.js'))
19+
end
20+
21+
def copy_react_asset(source, destination)
22+
vendor_path = Pathname.new(__dir__).join('vendor/react')
23+
FileUtils.mkdir_p(destination.dirname.to_s)
24+
FileUtils.cp(vendor_path.join(source), destination.to_s)
25+
end
26+
end
27+
28+
Rake::Task[:build].enhance(%w(react:copy_assets))
29+
930
require 'appraisal'
1031
require 'rake/testtask'
1132

@@ -16,5 +37,6 @@ Rake::TestTask.new(:test) do |t|
1637
t.verbose = ENV['TEST_VERBOSE'] == '1'
1738
t.warning = true
1839
end
40+
Rake::Task[:test].enhance(%w(react:copy_assets))
1941

2042
task default: :test

lib/react/rails/railtie.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ class Railtie < ::Rails::Railtie
2828
end
2929
end
3030

31-
initializer "react_rails.setup_vendor", group: :all do |app|
31+
config.after_initialize do |app|
3232
# We want to include different files in dev/prod. The development builds
3333
# contain console logging for invariants and logging to help catch
3434
# common mistakes. These are all stripped out in the production build.
3535

36-
directory = (app.config.react.variant || :production).to_s
37-
directory += 'with-addons' if app.config.react.addons
38-
app.assets.prepend_path ::React::Source.bundled_path_for(directory)
39-
app.assets.prepend_path ::React::Source.bundled_path_for('') # JSXTransformer.js
40-
end
36+
root_path = Pathname.new('../../../../').expand_path(__FILE__)
4137

38+
directory = app.config.react.variant == :production ? 'production' : 'development'
39+
directory += '-with-addons' if app.config.react.addons
40+
41+
app.assets.append_path(root_path.join('lib/assets/react-source/').join(directory).to_s)
42+
app.assets.append_path(root_path.join('vendor/react/').to_s)
4243

43-
config.after_initialize do |app|
4444
# Server Rendering
4545
# Concat component_filenames together for server rendering
4646
app.config.react.components_js = lambda {
@@ -59,6 +59,7 @@ class Railtie < ::Rails::Railtie
5959

6060
# Reload the JS VMs in dev when files change
6161
ActionDispatch::Reloader.to_prepare(&do_setup)
62+
6263
end
6364

6465

react-rails.gemspec

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ Gem::Specification.new do |s|
2323
s.add_development_dependency 'test-unit', '~> 2.5'
2424
s.add_development_dependency 'turbolinks', '>= 2.0.0'
2525

26-
2726
s.add_dependency 'coffee-script-source', '~>1.8'
2827
s.add_dependency 'connection_pool'
2928
s.add_dependency 'execjs'
3029
s.add_dependency 'rails', '>= 3.1'
31-
# If you change this, make sure to update VERSIONS.md:
32-
s.add_dependency 'react-source', '~> 0.13'
3330
s.add_dependency 'tilt'
3431

3532
s.files = Dir[

test/dummy/vendor/assets/javascripts/.gitkeep

Whitespace-only changes.

test/react_test.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
class ReactTest < ActionDispatch::IntegrationTest
55

66
test 'asset pipeline should deliver react file in a non-production variant' do
7-
actual_react_file_path = File.expand_path("../dummy/tmp/react-rails/react.js", __FILE__)
8-
actual_react_file_content = File.read actual_react_file_path
7+
app_react_file_path = File.expand_path("../dummy/vendor/assets/javascripts/react.js", __FILE__)
98

109
react_file_token = "'test_confirmation_token_react_content_non_production';\n";
11-
File.open(actual_react_file_path, 'w') {|f| f.write react_file_token}
10+
File.write(app_react_file_path, react_file_token)
1211

1312
get '/assets/react.js'
1413

15-
File.open(actual_react_file_path, 'w') {|f| f.write actual_react_file_content}
14+
File.unlink(app_react_file_path)
1615
FileUtils.rm_r CACHE_PATH if CACHE_PATH.exist?
1716

1817
assert_response :success

vendor/react/.bower.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "react",
3+
"version": "0.13.2",
4+
"main": "react.js",
5+
"ignore": [],
6+
"homepage": "https://github.com/facebook/react-bower",
7+
"_release": "0.13.2",
8+
"_resolution": {
9+
"type": "version",
10+
"tag": "v0.13.2",
11+
"commit": "a400da781e82ecac87d69169e4592f1771dee96c"
12+
},
13+
"_source": "git://github.com/facebook/react-bower.git",
14+
"_target": "~0.13.2",
15+
"_originalSource": "react",
16+
"_direct": true
17+
}

0 commit comments

Comments
 (0)