Skip to content

Commit 010c14b

Browse files
committed
feat(ServerRendering) add WebpackerManifestContainer
1 parent ce68d57 commit 010c14b

File tree

5 files changed

+61
-15
lines changed

5 files changed

+61
-15
lines changed

lib/react/server_rendering/sprockets_renderer.rb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "react/server_rendering/environment_container"
22
require "react/server_rendering/manifest_container"
3+
require "react/server_rendering/webpacker_manifest_container"
34
require "react/server_rendering/yaml_manifest_container"
45

56
module React
@@ -57,19 +58,7 @@ class << self
5758
#
5859
# @return [#find_asset(logical_path)] An object that returns asset contents by logical path
5960
def asset_container
60-
@asset_container ||= if self.class.asset_container_class.present?
61-
self.class.asset_container_class.new
62-
elsif assets_precompiled? && ManifestContainer.compatible?
63-
ManifestContainer.new
64-
elsif assets_precompiled? && YamlManifestContainer.compatible?
65-
YamlManifestContainer.new
66-
else
67-
EnvironmentContainer.new
68-
end
69-
end
70-
71-
def assets_precompiled?
72-
!::Rails.application.config.assets.compile
61+
@asset_container ||= asset_container_class.new
7362
end
7463

7564
private
@@ -97,6 +86,29 @@ def render_function(opts)
9786
def prepare_props(props)
9887
props.is_a?(String) ? props : props.to_json
9988
end
89+
90+
def assets_precompiled?
91+
!::Rails.application.config.assets.compile
92+
end
93+
94+
def asset_container_class
95+
if self.class.asset_container_class.present?
96+
self.class.asset_container_class
97+
elsif assets_precompiled?
98+
if WebpackerManifestContainer.compatible?
99+
WebpackerManifestContainer
100+
elsif ManifestContainer.compatible?
101+
ManifestContainer
102+
elsif YamlManifestContainer.compatible?
103+
YamlManifestContainer
104+
else
105+
# Even though they are precompiled, we can't find them :S
106+
EnvironmentContainer
107+
end
108+
else
109+
EnvironmentContainer
110+
end
111+
end
100112
end
101113
end
102114
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module React
2+
module ServerRendering
3+
# Get a compiled file from Webpacker
4+
class WebpackerManifestContainer
5+
def find_asset(logical_path)
6+
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)
14+
end
15+
16+
def self.compatible?
17+
defined?(Webpacker)
18+
end
19+
end
20+
end
21+
end

lib/react/server_rendering/yaml_manifest_container.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module React
22
module ServerRendering
3-
# Get asset content by reading the compiled file from disk using the generated maniftest.yml file
3+
# Get asset content by reading the compiled file from disk using the generated manifest.yml file
44
#
55
# This is good for Rails production when assets are compiled to public/assets
66
# but sometimes, they're compiled to other directories (or other servers)

test/react/rails/webpacker_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'test_helper'
22

3-
43
WebpackerHelpers.when_webpacker_available do
54
class ReactRailsWebpackerTest < ActionDispatch::IntegrationTest
65
include Capybara::DSL
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "test_helper"
2+
3+
WebpackerHelpers.when_webpacker_available do
4+
class WebpackerManifestContainerTest < ActiveSupport::TestCase
5+
test "it loads JS from the webpacker container" do
6+
container = React::ServerRendering::WebpackerManifestContainer.new
7+
js_file = container.find_asset("application.js")
8+
# Main file:
9+
assert_includes js_file, "ReactRailsUJS.loadContext(ctx)"
10+
# Bundled dependencies:
11+
assert_includes js_file, "ExportDefaultComponent"
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)