Skip to content

Commit a16f39a

Browse files
committed
feat(WebpackerManifestContainer) use webpack-dev-server if detected
1 parent 0f4700c commit a16f39a

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

lib/react/server_rendering/sprockets_renderer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ def assets_precompiled?
9494
def asset_container_class
9595
if self.class.asset_container_class.present?
9696
self.class.asset_container_class
97+
elsif WebpackerManifestContainer.compatible?
98+
WebpackerManifestContainer
9799
elsif assets_precompiled?
98-
if WebpackerManifestContainer.compatible?
99-
WebpackerManifestContainer
100-
elsif ManifestContainer.compatible?
100+
if ManifestContainer.compatible?
101101
ManifestContainer
102102
elsif YamlManifestContainer.compatible?
103103
YamlManifestContainer

lib/react/server_rendering/webpacker_manifest_container.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1+
require "open-uri"
2+
13
module React
24
module ServerRendering
35
# Get a compiled file from Webpacker
46
class WebpackerManifestContainer
57
def find_asset(logical_path)
68
asset_path = Webpacker::Manifest.lookup(logical_path) # raises if not found
7-
full_path = File.join(
8-
# TODO: using `.parent` here won't work for nonstandard configurations
9-
Webpacker::Configuration.output_path.parent,
10-
asset_path
11-
)
12-
# TODO: webpacker-dev-server points these at localhost:8080
13-
File.read(full_path)
9+
if asset_path.start_with?("http")
10+
# TODO: this includes webpack-dev-server code which causes ExecJS to 💥
11+
dev_server_asset = open(asset_path).read
12+
else
13+
full_path = File.join(
14+
# TODO: using `.parent` here won't work for nonstandard configurations
15+
Webpacker::Configuration.output_path.parent,
16+
asset_path
17+
)
18+
File.read(full_path)
19+
end
1420
end
1521

1622
def self.compatible?
17-
defined?(Webpacker)
23+
!!defined?(Webpacker)
1824
end
1925
end
2026
end

test/react/rails/webpacker_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ReactRailsWebpackerTest < ActionDispatch::IntegrationTest
66

77
setup do
88
Capybara.current_driver = Capybara.javascript_driver
9+
WebpackerHelpers.compile
910
end
1011

1112
teardown do
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
require "test_helper"
2+
require "open-uri"
23

34
WebpackerHelpers.when_webpacker_available do
45
class WebpackerManifestContainerTest < ActiveSupport::TestCase
6+
setup do
7+
WebpackerHelpers.clear_webpacker_packs
8+
end
9+
510
test "it loads JS from the webpacker container" do
11+
WebpackerHelpers.compile
12+
container = React::ServerRendering::WebpackerManifestContainer.new
13+
js_file = container.find_asset("application.js")
14+
# Main file:
15+
assert_includes js_file, "ReactRailsUJS.loadContext(ctx)"
16+
# Bundled dependencies:
17+
assert_includes js_file, "ExportDefaultComponent"
18+
end
19+
20+
def test_it_loads_from_webpack_dev_server
21+
webpack_dev_server = fork do
22+
Dir.chdir("test/dummy") do
23+
exec "./bin/webpack-dev-server RAILS_ENV=development"
24+
end
25+
end
26+
27+
detected_dev_server = false
28+
20.times do |i|
29+
begin
30+
# Make sure that the manifest has been updated:
31+
Webpacker::Manifest.load("./test/dummy/public/packs/manifest.json")
32+
webpack_manifest = Webpacker::Manifest.instance.data
33+
example_asset_path = webpack_manifest.values.first
34+
assert_includes example_asset_path, "http://localhost:8080"
35+
# Make sure the dev server is up:
36+
open("http://localhost:8080/application.js")
37+
detected_dev_server = true
38+
break
39+
rescue StandardError => err
40+
sleep 0.5
41+
end
42+
end
43+
44+
# If we didn't hook up with a dev server after 10s,
45+
# fail loudly.
46+
assert detected_dev_server
47+
648
container = React::ServerRendering::WebpackerManifestContainer.new
749
js_file = container.find_asset("application.js")
850
# Main file:
951
assert_includes js_file, "ReactRailsUJS.loadContext(ctx)"
1052
# Bundled dependencies:
1153
assert_includes js_file, "ExportDefaultComponent"
54+
ensure
55+
Process.kill(9, webpack_dev_server)
56+
Process.wait
1257
end
1358
end
1459
end

test/support/webpacker_helpers.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ module WebpackerHelpers
33

44
def when_webpacker_available
55
if defined?(Webpacker)
6-
clear_webpacker_packs
7-
Dir.chdir("./test/dummy") do
8-
Rake::Task['webpacker:compile'].invoke
9-
end
10-
# Reload cached JSON manifest:
11-
Webpacker::Manifest.load
126
yield
137
end
148
end
159

10+
def compile
11+
clear_webpacker_packs
12+
Dir.chdir("./test/dummy") do
13+
Rake::Task['webpacker:compile'].invoke
14+
end
15+
# Reload cached JSON manifest:
16+
Webpacker::Manifest.load
17+
end
18+
1619
def clear_webpacker_packs
1720
packs_directory = File.expand_path("../dummy/public/packs", __FILE__)
1821
FileUtils.rm_rf(packs_directory)

0 commit comments

Comments
 (0)