react_on_rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3d6a3770f22b5c124f17556203f1b1a74fd550b
4
+ data.tar.gz: 779c9ccbca94e36754c0fa90d60ab12aa8bf4785
5
+ SHA512:
6
+ metadata.gz: 47688d10f95c09bc2b9dcdb86744319c9e15b071d7c10de1c204c3a1815b793089ef2da7699a15a4bb7f314796e8d8dd7f608bdc43bc401497e2f9e61d239806
7
+ data.tar.gz: 5701bc9218429a92987a9160de14cd37a8965b5298bc2535eb632adb1639087afde627136a7a11f938bfa7cfa898b89631094f334dd25b5bb29a34ae5365c61a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+
12
+ /spec/dummy/client/node_modules
13
+ /spec/dummy/app/assets/javascripts/generated/
14
+
15
+ # RVM
16
+ .ruby-version
17
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in react_on_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Justin Gordon
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # ReactOnRails
2
+
3
+ Published: https://rubygems.org/gems/react_on_rails
4
+
5
+ Supports:
6
+
7
+ 1. Rails
8
+ 2. Webpack
9
+ 3. React
10
+ 4. Redux
11
+ 5. Turbolinks
12
+ 6. Server side rendering with fragment caching
13
+
14
+ # Authors
15
+ The Shaka Code team!
16
+
17
+ 1. [Justin Gordon](https://github.com/justin808/)
18
+ 2. [Samnang Chhun](https://github.com/samnang)
19
+ 3. [Alex Fedoseev](https://github.com/alexfedoseev)
20
+
21
+ And based on the work of the [react-rails gem](https://github.com/reactjs/react-rails)
22
+
23
+ # Key Info
24
+ Currently in proof of concept phase.
25
+
26
+ 1. https://github.com/justin808/react-webpack-rails-tutorial/
27
+ 2. http://www.railsonmaui.com/blog/2014/10/03/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/
28
+ 3. http://forum.railsonmaui.com
29
+ 4. Lots of work to do in terms of docs, tests
30
+ 5. If this project is interesting to you, email me at justin@shakacode.com. We're looking for great
31
+ developers that want to work with Rails + React with a distributed, worldwide team.
32
+
33
+
34
+ # Try it out!
35
+ Contributions and pull requests welcome!
36
+
37
+ 1. Setup and run the test app. There's no database.
38
+ ```bash
39
+ cd spec/dummy
40
+ bundle
41
+ npm i
42
+ foreman start
43
+ ```
44
+ 2. Caching is turned for development mode. Open the console and run `Rails.cache.clear` to clear
45
+ the cache. Note, even if you stop the server, you'll still have the cache entries around.
46
+ 3. Visit http://localhost:3000
47
+ 4. Notice that the first time you hit the page, you'll see a message that server is rendering.
48
+ See `spec/dummy/app/views/pages/index.html.erb:17` for the generation of that message.
49
+ 5. Open up the browser console and see some tracing.
50
+ 6. Open up the source for the page and see the server rendered code.
51
+ 7. If you want to turn off server caching, run the server like:
52
+ `export RAILS_USE_CACHE=N && foreman start`
53
+ 8. If you click back and forth between the about and react page links, you can see the rails console
54
+ log as well as the browser console to see what's going on with regards to server rendering and
55
+ caching.
56
+
57
+ # Key Tips
58
+ 1. See sample app in `spec/dummy` for how to set this up.
59
+ 2. The file used for server rendering is hard coded as generated/server.js
60
+ (assets/javascripts/generated/server.js)
61
+ 3. The default for rendering right now is `prerender: true`. **NOTE:** This does not work for
62
+ components, namely react-router, that use an async setup for server rendering.
63
+ 4. The API for objects exposed differs from the react-rails gem in that you expose a function that
64
+ returns a react component. We'll be changing that to take either a function or a React component.
65
+
66
+ ## References
67
+ * [Making the helper for server side rendering work with JS created by Webpack] (https://github.com/reactjs/react-rails/issues/301#issuecomment-133098974)
68
+ * [Add Demonstration of Server Side Rendering](https://github.com/justin808/react-webpack-rails-tutorial/issues/2)
69
+ * [Charlie Marsh's article "Rendering React Components on the Server"](http://www.crmarsh.com/react-ssr/)
70
+ * [Node globals](https://nodejs.org/api/globals.html#globals_global)
71
+
72
+ ## Installation
73
+
74
+ Add this line to your application's Gemfile:
75
+
76
+ ```ruby
77
+ gem 'react_on_rails'
78
+ ```
79
+
80
+ And then execute:
81
+
82
+ $ bundle
83
+
84
+ Or install it yourself as:
85
+
86
+ $ gem install react_on_rails
87
+
88
+ ## Usage
89
+
90
+ PENDING. See `spec/dummy` for the sample app.
91
+
92
+ ## Development
93
+
94
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
95
+
96
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shakacode/react_on_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
101
+
102
+
103
+ ## License
104
+
105
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
106
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,135 @@
1
+ require 'react_on_rails/react_renderer'
2
+
3
+ module ReactOnRailsHelper
4
+
5
+ # component_name: React component name
6
+ # props: Ruby Hash which contains the properties to pass to the react object
7
+ #
8
+ # Naming Conventions:
9
+ # Suppose your component is named "App" (can be anything)
10
+ # 1. Inside app/startup/ServerApp.jsx, setup the component for server rendering. This is used by
11
+ # the global exports in step 2
12
+ # 2. Component_name is changed to CamelizedUpper for the exposed component,
13
+ # so we have App as the exposed global (see app/startup/serverGlobals.jsx)
14
+ # 3. Inside app/startup/ClientApp.jsx, you want to export a function that generates
15
+ # your component, and when you generate the component, you must use this naming convention
16
+ # to get the data.
17
+ # App(__appData__)
18
+ # That way it gets the data you pass from the Rails helper.
19
+ #
20
+ # options:
21
+ # prerender: <true/false> defaults to true
22
+ # trace: <true/false> defaults to false, set to true to print additional info
23
+ def react_component(component_name, props = {}, options = {})
24
+ # Create the JavaScript setup of the global to initialize the client rendering
25
+ # (re-hydrate the data). This enables react rendered on the client to see that the
26
+ # server has already rendered the HTML.
27
+ @react_component_index ||= 0
28
+
29
+ prerender = options[:prerender].nil? ? true : options[:prerender]
30
+ trace = options[:trace].nil? ? false : options[:trace]
31
+
32
+ dataVariable = "__#{component_name.camelize(:lower)}Data#{@react_component_index}__"
33
+ reactComponent = component_name.camelize
34
+ domId = "#{component_name}-react-component-#{@react_component_index}"
35
+ @react_component_index += 1
36
+
37
+ turbolinks_loaded = Object.const_defined?(:Turbolinks)
38
+ install_render_events = turbolinks_loaded ? turbolinks_bootstrap(domId) : non_turbolinks_bootstrap
39
+
40
+ page_loaded_js = <<-JS
41
+ (function() {
42
+ window.#{dataVariable} = #{props.to_json};
43
+ #{define_render_if_dom_node_present(reactComponent, dataVariable, domId, trace)}
44
+ #{install_render_events}
45
+ })();
46
+ JS
47
+
48
+ data_from_server_script_tag = javascript_tag(page_loaded_js)
49
+
50
+ # Create the HTML rendering part
51
+ if prerender
52
+
53
+ render_js_expression = <<-JS
54
+ renderReactComponent(this.#{reactComponent}, #{props.to_json})
55
+ JS
56
+ server_rendered_react_component_html = render_js(render_js_expression)
57
+ else
58
+ server_rendered_react_component_html = ""
59
+ end
60
+
61
+ rendered_output = content_tag(:div,
62
+ server_rendered_react_component_html,
63
+ id: domId)
64
+
65
+ <<-HTML.strip_heredoc.html_safe
66
+ #{data_from_server_script_tag}
67
+ #{rendered_output}
68
+ HTML
69
+ end
70
+
71
+ # Takes javascript code and returns the output from it. This is called by react_component, which
72
+ # sets up the JS code for rendering a react component.
73
+ # This method could be used by itself to render the output of any javascript that returns a
74
+ # string of proper HTML.
75
+ def render_js(js_expression)
76
+ ReactOnRails::ReactRenderer.new.render_js(js_expression).html_safe
77
+ end
78
+
79
+ private
80
+
81
+ def debug_js(react_component, data_variable, dom_id, trace)
82
+ if trace
83
+ <<-JS.strip_heredoc
84
+ console.log("CLIENT SIDE RENDERED #{react_component} with dataVariable #{data_variable} to dom node with id: #{dom_id}");
85
+ JS
86
+ else
87
+ ""
88
+ end
89
+ end
90
+
91
+ def define_render_if_dom_node_present(react_component, data_variable, dom_id, trace)
92
+ <<-JS.strip_heredoc
93
+ var renderIfDomNodePresent = function() {
94
+ var domNode = document.getElementById('#{dom_id}');
95
+ if (domNode) {
96
+ #{debug_js(react_component, data_variable, dom_id, trace)}
97
+ var reactComponent = #{react_component}(#{data_variable});
98
+ React.render(reactComponent, domNode);
99
+ }
100
+ }
101
+ JS
102
+ end
103
+
104
+ def non_turbolinks_bootstrap
105
+ <<-JS.strip_heredoc
106
+ document.addEventListener("DOMContentLoaded", function(event) {
107
+ console.log("DOMContentLoaded event fired");
108
+ renderIfDomNodePresent();
109
+ });
110
+ JS
111
+ end
112
+
113
+ def turbolinks_bootstrap(dom_id)
114
+ <<-JS.strip_heredoc
115
+ var turbolinksInstalled = typeof(Turbolinks) !== 'undefined';
116
+ if (!turbolinksInstalled) {
117
+ console.warn("WARNING: NO TurboLinks detected in JS, but it's in your Gemfile");
118
+ #{non_turbolinks_bootstrap}
119
+ } else {
120
+ function onPageChange(event) {
121
+ var removePageChangeListener = function() {
122
+ document.removeEventListener("page:change", onPageChange);
123
+ document.removeEventListener("page:before-unload", removePageChangeListener);
124
+ var domNode = document.getElementById('#{dom_id}');
125
+ React.unmountComponentAtNode(domNode);
126
+ };
127
+ document.addEventListener("page:before-unload", removePageChangeListener);
128
+
129
+ renderIfDomNodePresent();
130
+ }
131
+ document.addEventListener("page:change", onPageChange);
132
+ }
133
+ JS
134
+ end
135
+ end
@@ -0,0 +1,6 @@
1
+ require "react_on_rails/version"
2
+
3
+ module ReactOnRails
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,44 @@
1
+ module ReactOnRails
2
+ class ReactRenderer
3
+
4
+ # "this" does not need a closure as it refers to the "this" defined by the
5
+ # calling the calling context which is the "this" in the execJs environment.
6
+ def render_js_react_component
7
+ <<-JS.strip_heredoc
8
+ function renderReactComponent(componentClass, props) {
9
+ return this.React.renderToString(
10
+ componentClass(props)
11
+ );
12
+ }
13
+ JS
14
+ end
15
+
16
+ def initialize
17
+ js_code = "#{bundle_js_code};\n#{render_js_react_component}"
18
+ @context = ExecJS.compile(js_code)
19
+ end
20
+
21
+ # Returns a html_safe string of HTML for direct insertion on the page by evaluating a js_code.
22
+ # Note, js_code does not have to be based on React.
23
+ def render_js(js_code)
24
+ js_code_wrapper = <<-JS.strip_heredoc
25
+ (function () {
26
+ return #{js_code}
27
+ })()
28
+ JS
29
+
30
+ # This is the html from the JavaScript. Whatever calls this will probably call 'html_safe'
31
+ # on the string.
32
+ @context.eval(js_code_wrapper)
33
+ end
34
+
35
+ private
36
+
37
+ def bundle_js_code
38
+ # Calling to_s to get contents of sprockets.
39
+
40
+ # TODO: Make this file name configurable
41
+ Rails.application.assets['generated/server.js'].to_s
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module ReactOnRails
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'react_on_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "react_on_rails"
8
+ spec.version = ReactOnRails::VERSION
9
+ spec.authors = ["Justin Gordon"]
10
+ spec.email = ["justin@shakacode.com"]
11
+
12
+ spec.summary = %q{Rails with react server rendering with webpack. }
13
+ spec.description = %q{See README.md}
14
+ spec.homepage = "https://github.com/shakacode/react_on_rails"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rails", "~> 4.2"
23
+ spec.add_dependency "execjs", "~> 2.5"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: react_on_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Gordon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: execjs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: See README.md
84
+ email:
85
+ - justin@shakacode.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - app/helpers/react_on_rails_helper.rb
99
+ - lib/react_on_rails.rb
100
+ - lib/react_on_rails/react_renderer.rb
101
+ - lib/react_on_rails/version.rb
102
+ - react_on_rails.gemspec
103
+ homepage: https://github.com/shakacode/react_on_rails
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Rails with react server rendering with webpack.
127
+ test_files: []