tilt-react 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ deploy:
5
+ provider: rubygems
6
+ api_key:
7
+ gem: tilt-react
8
+ on:
9
+ tags: true
10
+ repo: jphastings/tilt-react
11
+ addons:
12
+ code_climate:
13
+ secure: "NLJ1KBW8QcBQLY2R8qYgr+M4MgaGr1NZFBBffPvsQf/fqHo+n26B+z42SoMQIUaGm40Y9fDB3FgloumG5GGCNsdQ+6mIec2948Fjh9BsBy8ZDdpOypcsX5cAtBwHAhOa1tUv/nOU1udMEExT9dFQ7FCae+Z/ivw2EvpgaZuOd5bDwZWvnPiDAybVwlwusTWOPxQhTR3OEd1y0WnXfoVtdT0ACdp6mLxzy7ERs8uEuoOBHq/Q4Ai807beseNtZs9H+2K4L48fi3u65/p7c9WpyzCUVeEpUXPBjLl43NzWXqC8Zj6sOL5gRzj4gV3y7a+NTBzDyOHEAqNvnNe83AHS38AMLidvbmnLWSf2GvKBHr9oSdiQzyBxiHVdZXgP/1FjrVu2JC7MAYVKz5va9Pri4Ey38qjdT1JrR+9meXCq9nTMyPVWxdiO6AysoXBG+7YaNqZjvvxpT60UfKc9pzYf+YxHNEgmYvK0AbjiY2A+wX7f5+mT/BgwKIr6LHSy136Y9IuduUKfuhsdeKn1LBD68ox6pmB24oz2XNJPJ2cLPgIARGMj55ZYg/3zrccaqa/sHBp/XvQ7r+MsR6gCZ7Kedfhevq7JimSsbN6FkNrzIPlzAMxOIQCuSLtwc52FVMMrznmihbN2TmWssdBvAyua5dkqlwqgOUYKDP69m3gI2Bc="
@@ -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, 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 tilt-react.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 JP Hastings-Spital
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.
@@ -0,0 +1,43 @@
1
+ # Tilt::React
2
+
3
+ Use react components as view templates in Sinatra and other frameworks using Tilt.
4
+
5
+ Bonus feature: RSpec::React, a helpful framework for writing unit tests for the server generated HTML your components produce.
6
+
7
+ This is proof-of-concept code, there are some limitations:
8
+
9
+ * Only server-side rendering is supported at the moment
10
+ * There's some hacky code around the place (search for FIXME)
11
+ * There aren't any tests for the actual library…
12
+
13
+ There's an example of using this library with Sinatra in the `examples` directory.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'tilt-react'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install tilt-react
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/jphastings/tilt-react/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tilt/react"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ /node_modules/
2
+ /public/js/components.js
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format progress
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'sinatra', '~> 1.4'
4
+ gem 'capybara', '~> 2.6'
5
+ gem 'rspec', '~> 3.4'
6
+ gem 'rspec-its', '~> 1.2'
7
+ gem 'rspec-as_fixture', '~> 0.1'
8
+ gem 'tilt-react', path: '../..'
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: ../..
3
+ specs:
4
+ tilt-react (0.1.0)
5
+ babel-transpiler (~> 0.7)
6
+ commonjs (~> 0.2)
7
+ therubyracer (~> 0.12)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.4.0)
13
+ babel-source (5.8.35)
14
+ babel-transpiler (0.7.0)
15
+ babel-source (>= 4.0, < 6)
16
+ execjs (~> 2.0)
17
+ capybara (2.6.2)
18
+ addressable
19
+ mime-types (>= 1.16)
20
+ nokogiri (>= 1.3.3)
21
+ rack (>= 1.0.0)
22
+ rack-test (>= 0.5.4)
23
+ xpath (~> 2.0)
24
+ commonjs (0.2.7)
25
+ diff-lcs (1.2.5)
26
+ execjs (2.6.0)
27
+ libv8 (3.16.14.13)
28
+ mime-types (3.0)
29
+ mime-types-data (~> 3.2015)
30
+ mime-types-data (3.2016.0221)
31
+ mini_portile2 (2.0.0)
32
+ nokogiri (1.6.7.2)
33
+ mini_portile2 (~> 2.0.0.rc2)
34
+ rack (1.6.4)
35
+ rack-protection (1.5.3)
36
+ rack
37
+ rack-test (0.6.3)
38
+ rack (>= 1.0)
39
+ ref (2.0.0)
40
+ rspec (3.4.0)
41
+ rspec-core (~> 3.4.0)
42
+ rspec-expectations (~> 3.4.0)
43
+ rspec-mocks (~> 3.4.0)
44
+ rspec-as_fixture (0.1.2)
45
+ rspec (~> 3.4)
46
+ rspec-core (3.4.3)
47
+ rspec-support (~> 3.4.0)
48
+ rspec-expectations (3.4.0)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.4.0)
51
+ rspec-its (1.2.0)
52
+ rspec-core (>= 3.0.0)
53
+ rspec-expectations (>= 3.0.0)
54
+ rspec-mocks (3.4.1)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.4.0)
57
+ rspec-support (3.4.1)
58
+ sinatra (1.4.7)
59
+ rack (~> 1.5)
60
+ rack-protection (~> 1.4)
61
+ tilt (>= 1.3, < 3)
62
+ therubyracer (0.12.2)
63
+ libv8 (~> 3.16.14.0)
64
+ ref
65
+ tilt (2.0.2)
66
+ xpath (2.0.0)
67
+ nokogiri (~> 1.3)
68
+
69
+ PLATFORMS
70
+ ruby
71
+
72
+ DEPENDENCIES
73
+ capybara (~> 2.6)
74
+ rspec (~> 3.4)
75
+ rspec-as_fixture (~> 0.1)
76
+ rspec-its (~> 1.2)
77
+ sinatra (~> 1.4)
78
+ tilt-react!
@@ -0,0 +1,19 @@
1
+ # An example Sinatra App
2
+
3
+ This example app demonstrates how to use React Components as view templates in your sinatra app.
4
+
5
+ ## Go go gadget example app
6
+
7
+ You'll need to download React in order to get this example to work. [Install NPM](https://docs.npmjs.com/getting-started/installing-node), install the npm dependencies, then run the app:
8
+
9
+ ```bash
10
+ npm install
11
+ rackup
12
+ ```
13
+ You should be set! Try visiting http://127.0.0.1:9292/?name=world
14
+
15
+ ## Writing unit tests for your components
16
+
17
+ There are also simple tests demonstrating how to write unit tests for the HTML your components produce.
18
+
19
+ If you declare `type: :component` on the root `describe` block of your test suite, then that component is loaded, rendered with the `props` given, and the subject of the test becomes a Nokogiri HTML object, which allows easy assertion with `rspec-its` etc.
@@ -0,0 +1,10 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/react'
3
+
4
+ class ExampleApp < Sinatra::Base
5
+ register Sinatra::React
6
+
7
+ get '/' do
8
+ react :home_page, locals: { name: params['name'] || 'you' }
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ import React, { PropTypes } from 'react';
2
+
3
+ class HomePage extends React.Component {
4
+ render() {
5
+ return (
6
+ <h1>Hello {this.props.name || 'you'}</h1>
7
+ );
8
+ }
9
+ }
10
+
11
+ HomePage.propTypes = { name: PropTypes.string };
12
+
13
+ export default HomePage;
@@ -0,0 +1,3 @@
1
+ require File.join(__dir__, 'app.rb')
2
+
3
+ run ExampleApp
@@ -0,0 +1,7 @@
1
+ {
2
+ "dependencies": {
3
+ "fbjs": "^0.7",
4
+ "react": "^0.14",
5
+ "react-dom": "^0.14"
6
+ }
7
+ }
@@ -0,0 +1,3 @@
1
+ - title: when addressing the world
2
+ props:
3
+ name: world
@@ -0,0 +1,24 @@
1
+ require 'rspec/its'
2
+ require 'rspec/react'
3
+ require 'rspec/as_fixture'
4
+
5
+ RSpec.describe 'HomePage', type: :component do
6
+ context 'when addressing the world', :as_fixture do
7
+ its(:content) { should include('Hello world') }
8
+ end
9
+
10
+ context 'when no props are given' do
11
+ let(:props) { {} }
12
+
13
+ its(:content) { should include('Hello you') }
14
+ end
15
+
16
+ context 'when invalid props are sent' do
17
+ let(:props) { { name: -1 } }
18
+
19
+ # It would be nice if React's PropTypes worked in a way that meant
20
+ # I could capture props of incorrect types and raise errors for them
21
+ # but I haven't yet figured out how to do this.
22
+ xit { expect { rendered_component }.to raise_error(ArgumentError) }
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec/as_fixture'
2
+ require 'rspec/react'
3
+
4
+ # This is the default, here only to be explicit
5
+ RSpec::React.js_libs = ['node_modules']
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1">
7
+ <title>Tilt::React Example</title>
8
+ </head>
9
+ <body>
10
+ <%= yield %>
11
+ <script src="js/components.js"></script>
12
+ </body>
13
+ </html>
File without changes
@@ -0,0 +1,12 @@
1
+ React = require('react');
2
+ DOMServer = require('react-dom/server');
3
+
4
+ var tiltReact = {
5
+ render: function(componentName, props) {
6
+ var component = React.createFactory(tiltReact.components[componentName]);
7
+ return DOMServer.renderToString(component(props));
8
+ },
9
+ components: {}
10
+ };
11
+
12
+ module.exports = tiltReact;
@@ -0,0 +1,12 @@
1
+ module CommonJS
2
+ class Environment
3
+ def require_string(module_id, js)
4
+ raise "#{module_id} has already been loaded" if @modules[module_id]
5
+ load_js = "( function(module, require, exports) {\n#{js}\n} )"
6
+ load = @runtime.eval(load_js)
7
+ @modules[module_id] = mod = Module.new(module_id, self)
8
+ load.call(mod, mod.require_function, mod.exports)
9
+ mod.exports
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ require "rspec/react/component_helpers"
2
+
3
+ RSpec.configure do |config|
4
+ config.include RSpec::React::ComponentHelpers, type: :component
5
+ end
@@ -0,0 +1,65 @@
1
+ require 'tilt/react'
2
+ require 'capybara/rspec'
3
+
4
+ module RSpec
5
+ module React
6
+ class << self
7
+ attr_writer :js_libs, :components
8
+
9
+ def js_libs
10
+ @js_libs || 'node_modules'
11
+ end
12
+
13
+ def components
14
+ @components || 'components'
15
+ end
16
+ end
17
+
18
+ module ComponentHelpers
19
+ def self.included(klass)
20
+ include Capybara::RSpecMatchers
21
+
22
+ Tilt::ReactTemplate.load_context(RSpec::React.js_libs)
23
+
24
+ klass.around do |example|
25
+ group = example.metadata[:example_group]
26
+ loop do
27
+ break if group[:parent_example_group].nil?
28
+ group = group[:parent_example_group]
29
+ end
30
+
31
+ component = group[:description]
32
+ snake_cased = component.gsub(/::/, '/').
33
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
34
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
35
+ tr("-", "_").
36
+ downcase
37
+ filename = File.expand_path("#{snake_cased}.jsx", RSpec::React.components)
38
+
39
+ Tilt::ReactTemplate.compile(filename)
40
+ @template = Tilt::ReactTemplate.new(filename)
41
+
42
+ example.run
43
+ end
44
+
45
+ klass.subject(:rendered_component) { html_from_component(props) }
46
+ end
47
+
48
+ # Renders the component defined in the describe block using the given props blob.
49
+ #
50
+ # This returns a Nokogiri element, so you can do the following to make your tests clean:
51
+ #
52
+ # let(:props) { { name: 'JP' } }
53
+ # subject { html_from_component(props) }
54
+ # its(:content) { should include('JP') }
55
+ #
56
+ # @return [Nokogiri::XML::Element] The (necessarily singular) root HTML element that the component returns as a parsed XML object
57
+ def html_from_component(props = {})
58
+ fragment = Nokogiri::HTML.fragment(@template.render(nil, props))
59
+
60
+ # React components can only ever have one top-level element, so we just return that
61
+ fragment.children.first
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,47 @@
1
+ require 'sinatra/base'
2
+ require 'tilt/react'
3
+
4
+ module Sinatra
5
+ module React
6
+ module Helpers
7
+ def react(*args)
8
+ render :jsx, *args
9
+ end
10
+
11
+ def find_template(views, name, engine, &block)
12
+ if engine == Tilt::ReactTemplate
13
+ views = settings.components
14
+ end
15
+ super(views, name, engine, &block)
16
+ end
17
+ end
18
+
19
+ def self.registered(app)
20
+ app.helpers(Helpers)
21
+ app.set :components, Proc.new { root && File.join(root, 'components') }
22
+ app.set :js_libs, Proc.new { root && File.join(root, 'node_modules') }
23
+ app.set :component_bundles, { '*.jsx' => 'js/components.js' }
24
+ app.set :jsx, layout_engine: :erb
25
+
26
+ app.configure do |config|
27
+ # FIXME: I don't like this following line. Apparently this block gets called
28
+ # multiple times, once without the settings above returning nil, and a second
29
+ # time with them being what they should be. Because this block configures the
30
+ # JS context calling it without js_libs means no libraries are loaded, so we
31
+ # must wait until js_libs has content.
32
+ next unless config.settings.js_libs
33
+
34
+ Tilt::ReactTemplate.js_libs = config.settings.js_libs
35
+ Tilt::ReactTemplate.load_context
36
+ Tilt::ReactTemplate.compile_bundles(config.settings.component_bundles.map { |glob, bundle|
37
+ [
38
+ File.expand_path(glob, config.settings.components),
39
+ File.expand_path(bundle, config.settings.public_folder)
40
+ ]
41
+ })
42
+ end
43
+ end
44
+ end
45
+
46
+ register React
47
+ end