render_react 1.0.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: 1f1199329df44373786d8f5377fe6f4895ff2973
4
+ data.tar.gz: 79f01a0f8ca990c0e352aab45c1a4a5d922575ce
5
+ SHA512:
6
+ metadata.gz: 769e5d7d47459c76ca3cfaa92b5af1af0f1d69bb5b57903e2d1a960aea67cd485bae9e90c977df7170607241696bfa4e9f2f3d0a2f6b46e51c3283cd555a86a3
7
+ data.tar.gz: 9619a216f48a4cc90402d87db1e83e17beac59c74c94332f6eef2213c9f500eff08b186f3361db74dabf8083fc15f550044787ce367b66e1ea59f2495059f5e2
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ /pkg
data/.travis.yml ADDED
@@ -0,0 +1,22 @@
1
+ sudo: false
2
+ language: ruby
3
+
4
+ rvm:
5
+ - 2.4.1
6
+ - 2.3.4
7
+ - 2.2
8
+ - 2.1
9
+ - 2.0
10
+ - jruby-9.1.12.0
11
+ - ruby-head
12
+ - jruby-head
13
+
14
+ cache:
15
+ - bundler
16
+
17
+ matrix:
18
+ allow_failures:
19
+ - rvm: jruby-head
20
+ - rvm: ruby-head
21
+ - rvm: 2.0
22
+ # fast_finish: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## CHANGELOG
2
+
3
+ ### 1.0.0
4
+
5
+ * Initial release
6
+
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at opensource@janlelis.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'minitest'
6
+ gem 'minitest-reporters'
7
+ gem 'rake'
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 Jan Lelis, mail@janlelis.de
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,127 @@
1
+ # RenderReact [![[version]](https://badge.fury.io/rb/render_react.svg)](http://badge.fury.io/rb/render_react) [![[travis]](https://travis-ci.org/janlelis/render_react.svg)](https://travis-ci.org/janlelis/render_react)
2
+
3
+ A lo-fi way to render client- and server-side React components from Ruby:
4
+
5
+ ```js
6
+ class ExampleComponent extends React.Component {
7
+ render() {
8
+ return <marquee>Hello Ruby { this.props.example }</marquee>
9
+ }
10
+ }
11
+ ```
12
+
13
+ ```html
14
+ RenderReact.on_client_and_server("ExampleComponent", { example: "prop" })
15
+ # =>
16
+ <div id="RenderReact-caac405e-1714-495e-aeb4-77b42be42291">
17
+ <marquee data-reactroot="" data-reactid="1" data-react-checksum="441921122">
18
+ <!-- react-text: 2 -->Hello Ruby <!-- /react-text --><!-- react-text: 3 -->!<!-- /react-text -->
19
+ </marquee>
20
+ </div>
21
+ <script>
22
+ RenderReact.ReactDOM.render(RenderReact.React.createElement(RenderReact.components.ExampleComponent, {"example":"!"}),
23
+ document.getElementById('RenderReact-caac405e-1714-495e-aeb4-77b42be42291'))
24
+ </script>
25
+ ```
26
+
27
+ It is *bring your own tooling*: React is not included, nor any ES6 transpilers or module bundlers. It expects you to prepare the JavaScript bundle file in a specific format, which must contain React and all of your components.
28
+
29
+ If you are looking for higher-level alternatives, checkout [react_on_rails](https://github.com/shakacode/react_on_rails) or [react-rails](https://github.com/reactjs/react-rails).
30
+
31
+ ## Setup
32
+
33
+ Add to your `Gemfile`:
34
+
35
+ ```ruby
36
+ gem 'render_react'
37
+ ```
38
+
39
+ ### JavaScript Source Preparation
40
+
41
+ **RenderReact** expects the JavaScript bundle to include a global variable called `RenderReact` with the following contents:
42
+
43
+ ```javascript
44
+ {
45
+ React: [variable which contains React, see first paragraph of https://facebook.github.io/react/docs/react-api.html],
46
+ ReactDOM: [variable which contains ReactDOM, see first paragraph of https://facebook.github.io/react/docs/react-dom.html]
47
+ ReactDOMServer: [variable which contains ReactDOMServer, see first paragraph of https://facebook.github.io/react/docs/react-dom-server.html],
48
+ components: {
49
+ ComponentIdentifier1: [variable which contains the component 1],
50
+ ComponentIdentifier2: [variable which contains the component 2],
51
+ ...
52
+ }
53
+ }
54
+ ```
55
+
56
+ You can have two different javascript bundle files - one for server rendering, and one for client-rendering.
57
+
58
+ - The client bundle has to be included into your application by a method of your choice. You may skip passing in `ReactDOMServer` for the client bundle
59
+ - The server bundle has to be passed to `RenderReact` (see Usage section). You may skip passing in `ReactDOM` for the server bundle
60
+
61
+ #### Example (With ES6 Modules)
62
+
63
+ ```javascript
64
+ import React from 'react'
65
+ import ReactDOM from 'react-dom'
66
+ import ReactDOMServer from 'react-dom/server'
67
+
68
+ import ExampleComponent from './components/example_component'
69
+
70
+ export default {
71
+ React: React,
72
+ ReactDOM: ReactDOM,
73
+ ReactDOMServer: ReactDOMServer,
74
+ components: {
75
+ ExampleComponent: ExampleComponent
76
+ }
77
+ }
78
+ ```
79
+
80
+ (gets later imported as `RenderReact`)
81
+
82
+ #### Example (With Browser Globals)
83
+
84
+ ```javascript
85
+ window.RenderReact {
86
+ React: React,
87
+ ReactDOM: ReactDOM,
88
+ ReactDOMServer: ReactDOMServer,
89
+ components: {
90
+ ExampleComponent: ExampleComponent
91
+ }
92
+ }
93
+ ```
94
+
95
+ ## Usage
96
+
97
+ Create a **RenderReact** context by passing your server-side JavaScript bundle:
98
+
99
+ ```ruby
100
+ RenderReact.create_context! File.read('path/to/your/server-bundle.js'), mode: :client_and_server
101
+ ```
102
+
103
+ The optional `mode:` keyword argument can have one of the following values
104
+
105
+ - `:client_and_server` (default) - component will be rendered server-side and mounted in the client
106
+ - `:client` - component will be mounted in the client
107
+ - `:server` - component will be render statically
108
+
109
+ You can then render a component with
110
+
111
+ ```ruby
112
+ RenderReact.("ExampleComponent", { example: "prop" })
113
+ ```
114
+
115
+ It is possible to overwrite the context-rendering-mode by using specfic render methods:
116
+
117
+ ```ruby
118
+ RenderReact.on_client_and_server("ExampleComponent") # server- and client-side
119
+ RenderReact.on_client("ExampleComponent") # only client-side
120
+ RenderReact.on_server("ExampleComponent") # only static
121
+ ```
122
+
123
+ ## MIT License
124
+
125
+ Copyright (C) 2017 Jan Lelis <http://janlelis.com>. Released under the MIT license.
126
+
127
+ React is BSD licensed.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ # # #
2
+ # Get gemspec info
3
+
4
+ gemspec_file = Dir['*.gemspec'].first
5
+ gemspec = eval File.read(gemspec_file), binding, gemspec_file
6
+ info = "#{gemspec.name} | #{gemspec.version} | " \
7
+ "#{gemspec.runtime_dependencies.size} dependencies | " \
8
+ "#{gemspec.files.size} files"
9
+
10
+ # # #
11
+ # Gem build and install task
12
+
13
+ desc info
14
+ task :gem do
15
+ puts info + "\n\n"
16
+ print " "; sh "gem build #{gemspec_file}"
17
+ FileUtils.mkdir_p 'pkg'
18
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
19
+ puts; sh %{gem install --no-document pkg/#{gemspec.name}-#{gemspec.version}.gem}
20
+ end
21
+
22
+ # # #
23
+ # Start an IRB session with the gem loaded
24
+
25
+ desc "#{gemspec.name} | IRB"
26
+ task :irb do
27
+ sh "irb -I ./lib -r #{gemspec.name.gsub '-','/'}"
28
+ end
29
+
30
+ # # #
31
+ # Run specs
32
+
33
+ desc "#{gemspec.name} | Spec"
34
+ task :spec do
35
+ sh "for file in spec/*_spec.rb; do ruby $file; done"
36
+ end
37
+ task default: :spec
38
+
@@ -0,0 +1,35 @@
1
+ require_relative "render_react/version"
2
+ require_relative "render_react/context"
3
+
4
+ module RenderReact
5
+ class << self
6
+ def context
7
+ @context or raise ArgumentError, "Create a RenderReact::Context via RenderReact.create_context! first!"
8
+ end
9
+
10
+ def create_context!(*args)
11
+ @context = Context.new(*args)
12
+ end
13
+
14
+ def render_react(*args)
15
+ context.render_react(*args)
16
+ end
17
+ alias call render_react
18
+
19
+ def on_server(*args)
20
+ context.on_server(*args)
21
+ end
22
+
23
+ def on_client(*args)
24
+ context.on_client(*args)
25
+ end
26
+
27
+ def on_client_and_server(*args)
28
+ context.on_client_and_server(*args)
29
+ end
30
+
31
+ def on_server_and_client(*args)
32
+ context.on_server_and_client(*args)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ require "execjs"
2
+ require "securerandom"
3
+ require "json"
4
+
5
+ module RenderReact
6
+ class Context
7
+ attr_reader :app, :mode
8
+
9
+ def initialize(javascript_source = "", mode: :client_and_server)
10
+ @app = ExecJS.compile(javascript_source)
11
+ @mode = mode
12
+ end
13
+
14
+ def render_react(*args)
15
+ case mode
16
+ when :client
17
+ on_client(*args)
18
+ when :server
19
+ on_server(*args)
20
+ when :client_and_server, :server_and_client
21
+ on_client_and_server(*args)
22
+ else
23
+ raise ArgumentError, "unknown render mode"
24
+ end
25
+ end
26
+ alias call render_react
27
+
28
+ def on_client(component_name, props_hash = {})
29
+ component_uuid = SecureRandom.uuid
30
+ props_json = JSON.dump(props_hash)
31
+ "<div id=\"RenderReact-#{component_uuid}\"></div><script>#{client_script(component_name, props_json, component_uuid)}</script>"
32
+ end
33
+
34
+ def on_server(component_name, props_hash = {})
35
+ props_json = JSON.dump(props_hash)
36
+ app.eval(server_script(component_name, props_json, true))
37
+ end
38
+
39
+ def on_client_and_server(component_name, props_hash = {})
40
+ component_uuid = SecureRandom.uuid
41
+ props_json = JSON.dump(props_hash)
42
+ server_rendered = app.eval(server_script(component_name, props_json))
43
+ "<div id=\"RenderReact-#{component_uuid}\">#{server_rendered}</div><script>#{client_script(component_name, props_json, component_uuid)}</script>"
44
+ end
45
+ alias on_server_and_client on_client_and_server
46
+
47
+ private
48
+
49
+ def client_script(component_name, props_json, uuid)
50
+ "RenderReact.ReactDOM.render(RenderReact.React.createElement(RenderReact.components.#{component_name}, #{props_json}), document.getElementById('RenderReact-#{uuid}'))"
51
+ end
52
+
53
+ def server_script(component_name, props_json, static = false)
54
+ render_method = static ? 'renderToStaticMarkup' : 'renderToString'
55
+ "RenderReact.ReactDOMServer.#{render_method}(RenderReact.React.createElement(RenderReact.components.#{component_name}, #{props_json}))"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,4 @@
1
+ module RenderReact
2
+ VERSION = "1.0.0".freeze
3
+ end
4
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + "/lib/render_react/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "render_react"
7
+ gem.version = RenderReact::VERSION
8
+ gem.summary = "Lo-fi way of rendering React components"
9
+ gem.description = "Lo-fi way of rendering React components."
10
+ gem.authors = ["Jan Lelis"]
11
+ gem.email = ["mail@janlelis.de"]
12
+ gem.homepage = "https://github.com/janlelis/render_react"
13
+ gem.license = "MIT"
14
+
15
+ gem.files = Dir["{**/}{.*,*}"].select{ |path| File.file?(path) && path !~ /^(pkg|spec)/ }
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.required_ruby_version = "~> 2.0"
21
+ gem.add_dependency "execjs", "~> 2.7"
22
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: render_react
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Lelis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: execjs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ description: Lo-fi way of rendering React components.
28
+ email:
29
+ - mail@janlelis.de
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".travis.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - MIT-LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/render_react.rb
44
+ - lib/render_react/context.rb
45
+ - lib/render_react/version.rb
46
+ - render_react.gemspec
47
+ homepage: https://github.com/janlelis/render_react
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '2.0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.6.11
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Lo-fi way of rendering React components
71
+ test_files: []