Skip to content

Commit e082973

Browse files
author
Nick Maher
committed
Add code option to Sprockets Render
This enables additional code to be added to the Exec JS context when initialised. Our app is consuming assets generated via Sprockets and Webpack, this option allows the Webpacked assets to injected along side the Sprockets assets for use in the ExecJS runtime.
1 parent e43aabb commit e082973

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

lib/react/server_rendering/sprockets_renderer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def initialize(options={})
1717
@replay_console = options.fetch(:replay_console, true)
1818
filenames = options.fetch(:files, ["react-server.js", "components.js"])
1919
js_code = CONSOLE_POLYFILL.dup
20+
js_code << options.fetch(:code, '')
2021

2122
filenames.each do |filename|
2223
js_code << asset_container.find_asset(filename)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compiled with Babel 6.14.0
2+
// const WithoutSprockets = ({label}) => <span>{label}</span>;
3+
4+
var WithoutSprockets = function WithoutSprockets(_ref) {
5+
var label = _ref.label;
6+
return React.createElement(
7+
"span",
8+
null,
9+
label
10+
);
11+
};

test/react/server_rendering/sprockets_renderer_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ class SprocketsRendererTest < ActiveSupport::TestCase
4848
assert_match(/\n/, err.to_s, "it includes the multi-line backtrace")
4949
end
5050

51+
test '.new accepts additional code to add to the JS context' do
52+
additional_code = File.read(File.expand_path("../../../helper_files/WithoutSprockets.js", __FILE__))
53+
54+
additional_renderer = React::ServerRendering::SprocketsRenderer.new(code: additional_code)
55+
56+
assert_match(/drink more caffeine<\/span>/, additional_renderer.render("WithoutSprockets", {label: "drink more caffeine"}, nil))
57+
end
58+
5159
test '.new accepts any filenames' do
5260
limited_renderer = React::ServerRendering::SprocketsRenderer.new(files: ["react-server.js", "components/Todo.js"])
5361
assert_match(/get a real job<\/li>/, limited_renderer.render("Todo", {todo: "get a real job"}, nil))

0 commit comments

Comments
 (0)