Skip to content

Commit 3b94282

Browse files
committed
reworked rails-webpacker.md to include prerendering
1 parent b361afe commit 3b94282

File tree

4 files changed

+75
-23
lines changed

4 files changed

+75
-23
lines changed

docs/installation/installation.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ And if you just added webpacker make sure to run `bundle exec rails webpacker:in
8989

9090
### Update the `application.js` file
9191

92-
The `hyperstack-loader` is a dynamically generated asset manifest that will load all your client side Ruby code. Make sure it is the last require in `app/assets/javascripts/application.js` file.
92+
The `hyperstack-loader` is a dynamically generated asset manifest that will load all your client side Ruby code. Make sure it is the last require in `app/assets/javascripts/application.js` file. That is it should be just before the final `require_tree` directive
9393

9494
`jQuery` is very nicely integrated with Hyperstack, and provides a well
9595
documented uniform interface to the DOM. To use it require it and its Rails
@@ -99,6 +99,7 @@ counter part in `application.js` before the `hyperstack-loader`
9999
//= require jquery
100100
//= require jquery_ujs
101101
//= require hyperstack-loader
102+
//= require_tree .
102103
```
103104
> Note check to make sure jquery is not already being required.
104105

install/rails-webpacker.rb

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Hyperstack
2+
WITH_PRERENDERING = true
23

34
# ----------------------------------- Commit so we have good history of these changes
45

@@ -9,7 +10,7 @@
910
# ----------------------------------- Add the gems
1011

1112
gem 'webpacker'
12-
gem 'rails-hyperstack', '~> 1.0.alpha'
13+
gem 'rails-hyperstack', '~> 1.0.alpha1.0'
1314
gem_group :development do
1415
gem 'foreman'
1516
end
@@ -116,36 +117,86 @@ class Hyperstack::ApplicationPolicy
116117
run 'yarn add react'
117118
run 'yarn add react-dom'
118119
run 'yarn add react-router'
120+
if WITH_PRERENDERING
121+
run 'yarn add react-router-dom'
122+
run 'yarn add history'
123+
run 'yarn add react_ujs'
124+
run 'yarn add jquery'
125+
end
119126

120-
# ----------------------------------- Create hyperstack.js
127+
if !WITH_PRERENDERING
128+
# ----------------------------------- Create hyperstack.js
121129

122-
file 'app/javascript/packs/hyperstack.js', <<-CODE
123-
// Import all the modules
124-
import React from 'react';
125-
import ReactDOM from 'react-dom';
130+
file 'app/javascript/packs/hyperstack.js', <<-CODE
131+
// Import all the modules
132+
import React from 'react';
133+
import ReactDOM from 'react-dom';
126134
127-
// for opal/hyperstack modules to find React and others they must explicitly be saved
128-
// to the global space, otherwise webpack will encapsulate them locally here
129-
global.React = React;
130-
global.ReactDOM = ReactDOM;
131-
CODE
135+
// for opal/hyperstack modules to find React and others they must explicitly be saved
136+
// to the global space, otherwise webpack will encapsulate them locally here
137+
global.React = React;
138+
global.ReactDOM = ReactDOM;
139+
CODE
132140

133-
# ----------------------------------- View template
141+
# ----------------------------------- View template
134142

135-
inject_into_file 'app/views/layouts/application.html.erb', before: %r{<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>} do
136-
<<-CODE
137-
<%= javascript_pack_tag 'hyperstack' %>
138-
CODE
139-
end
143+
inject_into_file 'app/views/layouts/application.html.erb', before: %r{<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>} do
144+
<<-CODE
145+
<%= javascript_pack_tag 'hyperstack' %>
146+
CODE
147+
end
140148

141-
# ----------------------------------- application.js
149+
# ----------------------------------- application.js
142150

143-
inject_into_file 'app/assets/javascripts/application.js', before: %r{//= require_tree .} do
151+
inject_into_file 'app/assets/javascripts/application.js', before: %r{//= require_tree .} do
144152
<<-CODE
145153
//= require jquery
146154
//= require jquery_ujs
147155
//= require hyperstack-loader
148156
CODE
157+
end
158+
else
159+
# ----------------------------------- Create client_and_server.js
160+
161+
file 'app/javascript/packs/client_and_server.js', <<-CODE
162+
//app/javascript/packs/client_and_server.js
163+
// these packages will be loaded both during prerendering and on the client
164+
React = require('react'); // react-js library
165+
History = require('history'); // react-router history library
166+
ReactRouter = require('react-router'); // react-router js library
167+
ReactRouterDOM = require('react-router-dom'); // react-router DOM interface
168+
ReactRailsUJS = require('react_ujs'); // interface to react-rails
169+
// to add additional NPM packages call run yarn package-name@version
170+
// then add the require here.
171+
CODE
172+
173+
# ----------------------------------- Create client_only.js
174+
175+
file 'app/javascript/packs/client_only.js', <<-CODE
176+
//app/javascript/packs/client_only.js
177+
// add any requires for packages that will run client side only
178+
ReactDOM = require('react-dom'); // react-js client side code
179+
jQuery = require('jquery');
180+
// to add additional NPM packages call run yarn package-name@version
181+
// then add the require here.
182+
CODE
183+
184+
# ----------------------------------- add asset paths
185+
186+
# note before this was just public/packs now its public/paths/js. WHY???
187+
append_file 'config/initializers/assets.rb' do
188+
<<-RUBY
189+
Rails.application.config.assets.paths << Rails.root.join('public', 'packs', 'js').to_s
190+
RUBY
191+
end
192+
193+
# ----------------------------------- application.js
194+
195+
inject_into_file 'app/assets/javascripts/application.js', before: %r{//= require_tree .} do
196+
<<-CODE
197+
//= require hyperstack-loader
198+
CODE
199+
end
149200
end
150201

151202
# ----------------------------------- Procfile

ruby/hyper-component/lib/hyperstack/ext/component/element.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def self.find(selector)
33
if `typeof #{selector}['$respond_to?'] == 'function'` && selector.respond_to?(:dom_node)
44
selector = selector.dom_node
55
end
6-
`$(#{selector})`
6+
`jQuery(#{selector})`
77
end
88

99
def self.[](selector)
@@ -35,7 +35,7 @@ def self.[](selector)
3535
# see react-rails documentation for more details
3636

3737
%x{
38-
$.fn.mount_components = function() {
38+
jQuery.fn.mount_components = function() {
3939
this.each(function(e) { ReactRailsUJS.mountComponents(e[0]) })
4040
return this;
4141
}

ruby/hyper-model/spec/batch7/poly_assoc_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class Testing123 < HyperComponent
308308
evaluate_ruby do
309309
Uzer.find(1).groups << Group.find(1) # client side
310310
end
311-
wait_for_ajax
311+
wait_for_ajax # this is required
312312
@group1.reload
313313
compare_to_server @group1, 'uzers.collect(&:id)', [@uzer1.id], false # server side
314314
end

0 commit comments

Comments
 (0)