Skip to content

Commit f15b255

Browse files
committed
Use Webpackers native dev_server detection
1 parent 4aa297f commit f15b255

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

lib/react/server_rendering/webpacker_manifest_container.rb

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,33 @@ def self.compatible?
2222
!!defined?(Webpacker)
2323
end
2424

25-
def find_asset(logical_path)
26-
# raises if not found
27-
asset_path = manifest.lookup(logical_path).to_s
28-
if asset_path.start_with?("http")
29-
# Get a file from the webpack-dev-server
30-
dev_server_asset = open(asset_path).read
31-
# Remove `webpack-dev-server/client/index.js` code which causes ExecJS to 💥
32-
dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
33-
dev_server_asset
34-
else
35-
# Read the already-compiled pack:
36-
full_path = file_path(logical_path).to_s
37-
File.read(full_path)
25+
if MAJOR < 3
26+
def find_asset(logical_path)
27+
# raises if not found
28+
asset_path = manifest.lookup(logical_path).to_s
29+
if asset_path.start_with?("http")
30+
# Get a file from the webpack-dev-server
31+
dev_server_asset = open(asset_path).read
32+
# Remove `webpack-dev-server/client/index.js` code which causes ExecJS to 💥
33+
dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
34+
dev_server_asset
35+
else
36+
# Read the already-compiled pack:
37+
full_path = file_path(logical_path).to_s
38+
File.read(full_path)
39+
end
40+
end
41+
else
42+
def find_asset(logical_path)
43+
asset_path = Webpacker.manifest.lookup(logical_path).to_s
44+
if Webpacker.dev_server.running?
45+
ds = Webpacker.dev_server
46+
dev_server_asset = open("#{ds.protocol}://#{ds.host_with_port}#{asset_path}").read
47+
dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
48+
dev_server_asset
49+
else
50+
File.read(file_path(logical_path))
51+
end
3852
end
3953
end
4054

test/support/webpacker_helpers.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ def with_dev_server
106106
else
107107
# Webpacker proxies the dev server when Rails is running in Webpacker 3
108108
# so the manifest doens't have absolute paths anymore..
109-
file = open("http://localhost:8080#{example_asset_path}")
110-
if ! file
111-
raise "Webpack Dev Server hasn't started yet"
112-
end
109+
# Reload webpacker config.
110+
old_env = ENV["NODE_ENV"]
111+
ENV["NODE_ENV"] = 'development'
112+
Webpacker.instance.instance_variable_set(:@config, nil)
113+
Webpacker.config
114+
ENV["NODE_ENV"] = old_env
115+
raise "Webpack Dev Server hasn't started yet" unless Webpacker.dev_server.running?
113116
end
114117

115118
detected_dev_server = true

0 commit comments

Comments
 (0)