js_render 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b8a50324a1e29411b656887a42e712abc725525
4
- data.tar.gz: bbc79e9601ad177347554ffae6bad8f3b8bbb56a
3
+ metadata.gz: 931cdb27a1564cea7a6719b853ae02809767e3e1
4
+ data.tar.gz: 9c312aa181cc85b811fb42d1e5413c08031fe8c8
5
5
  SHA512:
6
- metadata.gz: 5ae2860584985861a352d3de82327b7da28006d2be451bb7350fe5576ba8c7f244b131652ffa5ec5d375d3ce8ea3b636bd5b2a2133e7725d7ea477b41b4c0d27
7
- data.tar.gz: 0f161814ea23b7f5d82d93eca445b1bbec78dcf9a2c13d94d6f2cd940c51a58ed38d7d1685abf31a7a14d6d60c4ed680e1ab12c5fd6e68b678b3ab8099e78879
6
+ metadata.gz: d0130b87cc4372619a790c24a53ba05905208a53e6faaa684e2e5b5828579064b8152176b08cd1e1c6ab791c52e6a1b968cd93e3b9cb660c6d1d774c97fbf2f2
7
+ data.tar.gz: 9d902e9d87c9a78fd2d834b572ed17f49b5c96024878af06aa36344d4b2b45189f58b18d23db93483a7fd68690ec0a4dbecd07b1bbbe92d69fa00a67269e4d0c
data/README.md CHANGED
@@ -133,6 +133,25 @@ If you want the component name to be included in the name dynamically, you can u
133
133
 
134
134
  > Defaults to `window.render*Client` (eg for `MyComponent`, `window.renderMyComponentClient`)
135
135
 
136
+ **asset_finder_class**
137
+
138
+ `JsRender::Renderer` uses the `JsRender::AssetFinder::Base` class to find and read JS asset files it needs to generate the server HTML (with the server render function). If `use_asset_pipeline` is true, it will use `JSRender::Rails::AssetFinder` instead, which just overrides the `read` method. The `asset_finder_class` configuration option provides a hook to override how the JS assets are found or read.
139
+
140
+ You can either provide a new class that provides a `#read_files` method (takes a component name and returns a string of JS from the files that component needs to render) or you can subclass `JsRender::AssetFinder::Base`.
141
+
142
+ Methods you can override if you subclass `JsRender::AssetFinder::Base`:
143
+
144
+ - `Base#find_files` takes the component name as a string and returns a list of paths to files.
145
+ - This method can be overridden to change how files are looked up, or to transform the pathnames (which might be useful if your JS assets are built outside of the asset pipeline or to another directory.
146
+
147
+ - `Base#read_files` takes the component name as a string and calls `Base#find_files` to get all the files. It then reads each of these files with `Base#read`, concatenates them together and returns a string. This is the method that is actually used directly by `JsRender::Renderer`.
148
+ - This method will rarely need to be overridden as you can change the behavior of `Base#find_files` and `Base#read`.
149
+
150
+ - `Base#read` takes a path as a string and returns the file contents as a string.
151
+ - This method can be overridden to apply a transformation on the contents of the file (like compiling ES2015 to ES5).
152
+
153
+ > Defaults to `nil`
154
+
136
155
  ## Development
137
156
 
138
157
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -149,6 +168,6 @@ The gem is available as open source under the terms of the [MIT License](http://
149
168
 
150
169
  ## Motivation and Thanks
151
170
 
152
- This project came out of the desire to support server side rendering of React components. The most popular existing solution, [`react-rails`](https://github.com/reactjs/react-rails) is much more opinionated, but also does a lot implicitly under the hood. If your focus is on React server side rendering, and your experience primarily lies in Ruby on Rails, or you do not mind writing your JavaScript within the boundaries and opinions of react-rails, I highly recommend you take a look at it. Another good React/Rails specific library for server side React rendering to check out is [react_on_rails](https://github.com/shakacode/react_on_rails). Many thanks to the contributors of these projects as they were both influential in creating JsRender.
171
+ This project came out of the desire to support server side rendering of React components. The most popular existing solution, [`react-rails`](https://github.com/reactjs/react-rails) is much more opinionated, but also does a lot implicitly under the hood. If your focus is on React server side rendering, and your experience primarily lies in Ruby on Rails, or you do not mind writing your JavaScript within the boundaries and opinions of react-rails, I highly recommend you take a look at it. Another good React/Rails specific library for server side React rendering to check out is [`react_on_rails`](https://github.com/shakacode/react_on_rails). Many thanks to the contributors of these projects as they were both influential in creating JsRender.
153
172
 
154
173
  The goal of this project is to be framework agnostic and support server side rendering for any JavaScript component library, popular or self-rolled. While Rails support is baked in, the ultimate goal of this project is to work anywhere where Ruby is used as the server side language.
@@ -17,3 +17,5 @@
17
17
  /tmp
18
18
 
19
19
  /node_modules
20
+
21
+ Gemfile.lock
@@ -15,3 +15,5 @@
15
15
  /log/*
16
16
  !/log/.keep
17
17
  /tmp
18
+
19
+ Gemfile.lock
@@ -1,12 +1,26 @@
1
1
  module JsRender
2
2
  module AssetFinder
3
3
  class Base
4
- def initialize
4
+ def find_files(component_name)
5
+ base_path = JsRender.config.base_path
6
+ paths = JsRender.config.component_paths
7
+ suffix = JsRender.config.component_suffix
8
+
9
+ paths.map do |path|
10
+ Dir[File.join(base_path, path)].select do |full_path|
11
+ full_path.match Regexp.new("/#{component_name}#{suffix}")
12
+ end
13
+ end.compact.flatten.uniq
14
+ end
15
+
16
+ def read_files(component_name)
17
+ files = find_files component_name
18
+ files.map { |file| read file }.join('')
5
19
  end
6
20
 
7
- def find(path)
21
+ def read(path)
8
22
  if File.file? path
9
- File.read(path)
23
+ File.read path
10
24
  else
11
25
  raise JsRender::Errors::AssetFileNotFound.new "Asset \"#{path}\" does not exist."
12
26
  end
@@ -21,7 +21,8 @@ module JsRender
21
21
  :component_suffix,
22
22
  :server_render_function,
23
23
  :client_render_function,
24
- :use_asset_pipeline
24
+ :use_asset_pipeline,
25
+ :asset_finder_class
25
26
 
26
27
  def initialize
27
28
  @base_path = 'app/assets/javascripts'
@@ -30,6 +31,7 @@ module JsRender
30
31
  @server_render_function = 'window.render*Server'
31
32
  @client_render_function = 'window.render*Client'
32
33
  @use_asset_pipeline = false
34
+ @asset_finder_class = nil
33
35
  end
34
36
  end
35
37
  end
@@ -2,11 +2,12 @@ module JsRender
2
2
  module Rails
3
3
  class AssetFinder < ::JsRender::AssetFinder::Base
4
4
  def initialize
5
+ super
5
6
  @environment = ::Rails.application.assets
6
7
  @manifest = ::Rails.application.assets_manifest
7
8
  end
8
9
 
9
- def find(path)
10
+ def read(path)
10
11
  logical_path = path.gsub('app/assets/javascripts/', '')
11
12
  if @environment
12
13
  @environment[logical_path].to_s
@@ -3,7 +3,7 @@ require 'securerandom'
3
3
 
4
4
  module JsRender
5
5
  class Renderer
6
- attr_reader :component_name, :json_data, :uuid
6
+ attr_reader :component_name, :json_data, :uuid, :asset_finder
7
7
 
8
8
  GLOBAL_CONTEXT = <<-JS
9
9
  var global = global || this;
@@ -33,7 +33,7 @@ module JsRender
33
33
  return '<span id="#{@uuid}">' + serverStr + '</span>';
34
34
  })()
35
35
  JS
36
- renderer_code = js_context(find_renderer_files)
36
+ renderer_code = asset_finder.read_files(@component_name)
37
37
  context = ::ExecJS.compile(GLOBAL_CONTEXT + renderer_code)
38
38
  context.eval(server_code)
39
39
  rescue ExecJS::RuntimeError, ExecJS::ProgramError => error
@@ -52,26 +52,15 @@ module JsRender
52
52
 
53
53
  private
54
54
 
55
- def find_renderer_files
56
- base_path = JsRender.config.base_path
57
- paths = JsRender.config.component_paths
58
- suffix = JsRender.config.component_suffix
59
-
60
- paths.map do |path|
61
- Dir[File.join(base_path, path)].select do |full_path|
62
- full_path.match Regexp.new("/#{@component_name}#{suffix}")
55
+ def asset_finder
56
+ @asset_finder ||=
57
+ if JsRender.config.asset_finder_class
58
+ JsRender.config.asset_finder_class.new
59
+ elsif defined?(::Rails) && JsRender.config.use_asset_pipeline
60
+ Rails::AssetFinder.new
61
+ else
62
+ AssetFinder::Base.new
63
63
  end
64
- end.compact.flatten.uniq
65
- end
66
-
67
- def js_context(paths)
68
- asset_finder = if defined?(::Rails) && JsRender.config.use_asset_pipeline
69
- Rails::AssetFinder.new
70
- else
71
- AssetFinder::Base.new
72
- end
73
- paths.map { |path| asset_finder.find path }.join('')
74
64
  end
75
65
  end
76
-
77
66
  end
@@ -1,3 +1,3 @@
1
1
  module JsRender
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Lehman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-11 00:00:00.000000000 Z
11
+ date: 2016-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,7 +85,6 @@ files:
85
85
  - bin/setup
86
86
  - examples/react/.gitignore
87
87
  - examples/react/Gemfile
88
- - examples/react/Gemfile.lock
89
88
  - examples/react/README.rdoc
90
89
  - examples/react/Rakefile
91
90
  - examples/react/app/assets/images/.keep
@@ -149,7 +148,6 @@ files:
149
148
  - examples/react/vendor/assets/stylesheets/.keep
150
149
  - examples/vanilla_js/.gitignore
151
150
  - examples/vanilla_js/Gemfile
152
- - examples/vanilla_js/Gemfile.lock
153
151
  - examples/vanilla_js/README.rdoc
154
152
  - examples/vanilla_js/Rakefile
155
153
  - examples/vanilla_js/app/assets/images/.keep
@@ -1,171 +0,0 @@
1
- PATH
2
- remote: ../../
3
- specs:
4
- js_render (0.1.1)
5
- execjs
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actionmailer (4.2.6)
11
- actionpack (= 4.2.6)
12
- actionview (= 4.2.6)
13
- activejob (= 4.2.6)
14
- mail (~> 2.5, >= 2.5.4)
15
- rails-dom-testing (~> 1.0, >= 1.0.5)
16
- actionpack (4.2.6)
17
- actionview (= 4.2.6)
18
- activesupport (= 4.2.6)
19
- rack (~> 1.6)
20
- rack-test (~> 0.6.2)
21
- rails-dom-testing (~> 1.0, >= 1.0.5)
22
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
- actionview (4.2.6)
24
- activesupport (= 4.2.6)
25
- builder (~> 3.1)
26
- erubis (~> 2.7.0)
27
- rails-dom-testing (~> 1.0, >= 1.0.5)
28
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
- activejob (4.2.6)
30
- activesupport (= 4.2.6)
31
- globalid (>= 0.3.0)
32
- activemodel (4.2.6)
33
- activesupport (= 4.2.6)
34
- builder (~> 3.1)
35
- activerecord (4.2.6)
36
- activemodel (= 4.2.6)
37
- activesupport (= 4.2.6)
38
- arel (~> 6.0)
39
- activesupport (4.2.6)
40
- i18n (~> 0.7)
41
- json (~> 1.7, >= 1.7.7)
42
- minitest (~> 5.1)
43
- thread_safe (~> 0.3, >= 0.3.4)
44
- tzinfo (~> 1.1)
45
- arel (6.0.3)
46
- binding_of_caller (0.7.2)
47
- debug_inspector (>= 0.0.1)
48
- browserify-rails (3.0.1)
49
- railties (>= 4.0.0, < 5.0)
50
- sprockets (>= 3.5.2)
51
- builder (3.2.2)
52
- byebug (8.2.5)
53
- coffee-rails (4.1.1)
54
- coffee-script (>= 2.2.0)
55
- railties (>= 4.0.0, < 5.1.x)
56
- coffee-script (2.4.1)
57
- coffee-script-source
58
- execjs
59
- coffee-script-source (1.10.0)
60
- concurrent-ruby (1.0.2)
61
- debug_inspector (0.0.2)
62
- erubis (2.7.0)
63
- execjs (2.6.0)
64
- globalid (0.3.6)
65
- activesupport (>= 4.1.0)
66
- i18n (0.7.0)
67
- jbuilder (2.4.1)
68
- activesupport (>= 3.0.0, < 5.1)
69
- multi_json (~> 1.2)
70
- jquery-rails (4.1.1)
71
- rails-dom-testing (>= 1, < 3)
72
- railties (>= 4.2.0)
73
- thor (>= 0.14, < 2.0)
74
- json (1.8.3)
75
- loofah (2.0.3)
76
- nokogiri (>= 1.5.9)
77
- mail (2.6.4)
78
- mime-types (>= 1.16, < 4)
79
- mime-types (3.0)
80
- mime-types-data (~> 3.2015)
81
- mime-types-data (3.2016.0221)
82
- mini_portile2 (2.0.0)
83
- minitest (5.8.4)
84
- multi_json (1.12.0)
85
- nokogiri (1.6.7.2)
86
- mini_portile2 (~> 2.0.0.rc2)
87
- rack (1.6.4)
88
- rack-test (0.6.3)
89
- rack (>= 1.0)
90
- rails (4.2.6)
91
- actionmailer (= 4.2.6)
92
- actionpack (= 4.2.6)
93
- actionview (= 4.2.6)
94
- activejob (= 4.2.6)
95
- activemodel (= 4.2.6)
96
- activerecord (= 4.2.6)
97
- activesupport (= 4.2.6)
98
- bundler (>= 1.3.0, < 2.0)
99
- railties (= 4.2.6)
100
- sprockets-rails
101
- rails-deprecated_sanitizer (1.0.3)
102
- activesupport (>= 4.2.0.alpha)
103
- rails-dom-testing (1.0.7)
104
- activesupport (>= 4.2.0.beta, < 5.0)
105
- nokogiri (~> 1.6.0)
106
- rails-deprecated_sanitizer (>= 1.0.1)
107
- rails-html-sanitizer (1.0.3)
108
- loofah (~> 2.0)
109
- railties (4.2.6)
110
- actionpack (= 4.2.6)
111
- activesupport (= 4.2.6)
112
- rake (>= 0.8.7)
113
- thor (>= 0.18.1, < 2.0)
114
- rake (11.1.2)
115
- rdoc (4.2.2)
116
- json (~> 1.4)
117
- sass (3.4.22)
118
- sass-rails (5.0.4)
119
- railties (>= 4.0.0, < 5.0)
120
- sass (~> 3.1)
121
- sprockets (>= 2.8, < 4.0)
122
- sprockets-rails (>= 2.0, < 4.0)
123
- tilt (>= 1.1, < 3)
124
- sdoc (0.4.1)
125
- json (~> 1.7, >= 1.7.7)
126
- rdoc (~> 4.0)
127
- spring (1.7.1)
128
- sprockets (3.6.0)
129
- concurrent-ruby (~> 1.0)
130
- rack (> 1, < 3)
131
- sprockets-rails (3.0.4)
132
- actionpack (>= 4.0)
133
- activesupport (>= 4.0)
134
- sprockets (>= 3.0.0)
135
- sqlite3 (1.3.11)
136
- thor (0.19.1)
137
- thread_safe (0.3.5)
138
- tilt (2.0.2)
139
- turbolinks (2.5.3)
140
- coffee-rails
141
- tzinfo (1.2.2)
142
- thread_safe (~> 0.1)
143
- uglifier (3.0.0)
144
- execjs (>= 0.3.0, < 3)
145
- web-console (2.3.0)
146
- activemodel (>= 4.0)
147
- binding_of_caller (>= 0.7.2)
148
- railties (>= 4.0)
149
- sprockets-rails (>= 2.0, < 4.0)
150
-
151
- PLATFORMS
152
- ruby
153
-
154
- DEPENDENCIES
155
- browserify-rails
156
- byebug
157
- coffee-rails (~> 4.1.0)
158
- jbuilder (~> 2.0)
159
- jquery-rails
160
- js_render!
161
- rails (= 4.2.6)
162
- sass-rails (~> 5.0)
163
- sdoc (~> 0.4.0)
164
- spring
165
- sqlite3
166
- turbolinks
167
- uglifier (>= 1.3.0)
168
- web-console (~> 2.0)
169
-
170
- BUNDLED WITH
171
- 1.12.1
@@ -1,167 +0,0 @@
1
- PATH
2
- remote: ../../
3
- specs:
4
- js_render (0.1.0)
5
- execjs
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actionmailer (4.2.6)
11
- actionpack (= 4.2.6)
12
- actionview (= 4.2.6)
13
- activejob (= 4.2.6)
14
- mail (~> 2.5, >= 2.5.4)
15
- rails-dom-testing (~> 1.0, >= 1.0.5)
16
- actionpack (4.2.6)
17
- actionview (= 4.2.6)
18
- activesupport (= 4.2.6)
19
- rack (~> 1.6)
20
- rack-test (~> 0.6.2)
21
- rails-dom-testing (~> 1.0, >= 1.0.5)
22
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
- actionview (4.2.6)
24
- activesupport (= 4.2.6)
25
- builder (~> 3.1)
26
- erubis (~> 2.7.0)
27
- rails-dom-testing (~> 1.0, >= 1.0.5)
28
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
- activejob (4.2.6)
30
- activesupport (= 4.2.6)
31
- globalid (>= 0.3.0)
32
- activemodel (4.2.6)
33
- activesupport (= 4.2.6)
34
- builder (~> 3.1)
35
- activerecord (4.2.6)
36
- activemodel (= 4.2.6)
37
- activesupport (= 4.2.6)
38
- arel (~> 6.0)
39
- activesupport (4.2.6)
40
- i18n (~> 0.7)
41
- json (~> 1.7, >= 1.7.7)
42
- minitest (~> 5.1)
43
- thread_safe (~> 0.3, >= 0.3.4)
44
- tzinfo (~> 1.1)
45
- arel (6.0.3)
46
- binding_of_caller (0.7.2)
47
- debug_inspector (>= 0.0.1)
48
- builder (3.2.2)
49
- byebug (8.2.5)
50
- coffee-rails (4.1.1)
51
- coffee-script (>= 2.2.0)
52
- railties (>= 4.0.0, < 5.1.x)
53
- coffee-script (2.4.1)
54
- coffee-script-source
55
- execjs
56
- coffee-script-source (1.10.0)
57
- concurrent-ruby (1.0.2)
58
- debug_inspector (0.0.2)
59
- erubis (2.7.0)
60
- execjs (2.6.0)
61
- globalid (0.3.6)
62
- activesupport (>= 4.1.0)
63
- i18n (0.7.0)
64
- jbuilder (2.4.1)
65
- activesupport (>= 3.0.0, < 5.1)
66
- multi_json (~> 1.2)
67
- jquery-rails (4.1.1)
68
- rails-dom-testing (>= 1, < 3)
69
- railties (>= 4.2.0)
70
- thor (>= 0.14, < 2.0)
71
- json (1.8.3)
72
- loofah (2.0.3)
73
- nokogiri (>= 1.5.9)
74
- mail (2.6.4)
75
- mime-types (>= 1.16, < 4)
76
- mime-types (3.0)
77
- mime-types-data (~> 3.2015)
78
- mime-types-data (3.2016.0221)
79
- mini_portile2 (2.0.0)
80
- minitest (5.8.4)
81
- multi_json (1.12.0)
82
- nokogiri (1.6.7.2)
83
- mini_portile2 (~> 2.0.0.rc2)
84
- rack (1.6.4)
85
- rack-test (0.6.3)
86
- rack (>= 1.0)
87
- rails (4.2.6)
88
- actionmailer (= 4.2.6)
89
- actionpack (= 4.2.6)
90
- actionview (= 4.2.6)
91
- activejob (= 4.2.6)
92
- activemodel (= 4.2.6)
93
- activerecord (= 4.2.6)
94
- activesupport (= 4.2.6)
95
- bundler (>= 1.3.0, < 2.0)
96
- railties (= 4.2.6)
97
- sprockets-rails
98
- rails-deprecated_sanitizer (1.0.3)
99
- activesupport (>= 4.2.0.alpha)
100
- rails-dom-testing (1.0.7)
101
- activesupport (>= 4.2.0.beta, < 5.0)
102
- nokogiri (~> 1.6.0)
103
- rails-deprecated_sanitizer (>= 1.0.1)
104
- rails-html-sanitizer (1.0.3)
105
- loofah (~> 2.0)
106
- railties (4.2.6)
107
- actionpack (= 4.2.6)
108
- activesupport (= 4.2.6)
109
- rake (>= 0.8.7)
110
- thor (>= 0.18.1, < 2.0)
111
- rake (11.1.2)
112
- rdoc (4.2.2)
113
- json (~> 1.4)
114
- sass (3.4.22)
115
- sass-rails (5.0.4)
116
- railties (>= 4.0.0, < 5.0)
117
- sass (~> 3.1)
118
- sprockets (>= 2.8, < 4.0)
119
- sprockets-rails (>= 2.0, < 4.0)
120
- tilt (>= 1.1, < 3)
121
- sdoc (0.4.1)
122
- json (~> 1.7, >= 1.7.7)
123
- rdoc (~> 4.0)
124
- spring (1.7.1)
125
- sprockets (3.6.0)
126
- concurrent-ruby (~> 1.0)
127
- rack (> 1, < 3)
128
- sprockets-rails (3.0.4)
129
- actionpack (>= 4.0)
130
- activesupport (>= 4.0)
131
- sprockets (>= 3.0.0)
132
- sqlite3 (1.3.11)
133
- thor (0.19.1)
134
- thread_safe (0.3.5)
135
- tilt (2.0.2)
136
- turbolinks (2.5.3)
137
- coffee-rails
138
- tzinfo (1.2.2)
139
- thread_safe (~> 0.1)
140
- uglifier (3.0.0)
141
- execjs (>= 0.3.0, < 3)
142
- web-console (2.3.0)
143
- activemodel (>= 4.0)
144
- binding_of_caller (>= 0.7.2)
145
- railties (>= 4.0)
146
- sprockets-rails (>= 2.0, < 4.0)
147
-
148
- PLATFORMS
149
- ruby
150
-
151
- DEPENDENCIES
152
- byebug
153
- coffee-rails (~> 4.1.0)
154
- jbuilder (~> 2.0)
155
- jquery-rails
156
- js_render!
157
- rails (= 4.2.6)
158
- sass-rails (~> 5.0)
159
- sdoc (~> 0.4.0)
160
- spring
161
- sqlite3
162
- turbolinks
163
- uglifier (>= 1.3.0)
164
- web-console (~> 2.0)
165
-
166
- BUNDLED WITH
167
- 1.12.1