humid 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +27 -49
- data/lib/humid/version.rb +1 -1
- data/lib/humid.rb +8 -41
- metadata +6 -82
- data/spec/controller_runtime_spec.rb +0 -79
- data/spec/log_subscriber_spec.rb +0 -54
- data/spec/render_spec.rb +0 -188
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8422854ba8184e3afc75af501ec45002304538e9f756ea3ee3bdd58b15d98de
|
4
|
+
data.tar.gz: 8e8025cba53a2ed0e54aecb85701bcb65a7d39798f241ef4e5d3aeea391367aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 404f369b35a24b2b313bf2a5d572746b1d4262df0b5ee4988f69540a878502045d046279714432d77b367366e98031818f6c3f3c26747f67e916581335b4004f
|
7
|
+
data.tar.gz: 38feb90293fd0340155314e6300e5a3ff70a8ee5f2d2e7fb081c0bf89ea8b7826a7297d52b15888d05f447ff64738da454cc4ede07ff9a152236ae7318fd5380
|
data/README.md
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
# Humid
|
2
|
-
[](https://circleci.com/gh/thoughtbot/humid)
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+

|
4
|
+
|
5
|
+
Humid is a lightweight wrapper around [mini_racer] used to generate Server
|
6
|
+
Side Rendered (SSR) pages from your js-bundling builds. While it was built
|
7
|
+
for React, it can work with any JS function that returns a HTML string.
|
9
8
|
|
10
9
|
## Caution
|
11
10
|
|
@@ -34,16 +33,14 @@ Add an initializer to configure
|
|
34
33
|
|
35
34
|
```ruby
|
36
35
|
Humid.configure do |config|
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
|
41
|
-
config.server_rendering_pack = "server_rendering.js"
|
36
|
+
# Path to your build file located in `app/assets/build/`. You should use a
|
37
|
+
# separate build apart from your `application.js`.
|
38
|
+
# Required
|
39
|
+
config.application_path = "app/assets/build/server_rendering.js"
|
42
40
|
|
43
|
-
#
|
44
|
-
#
|
45
|
-
|
46
|
-
config.use_source_map = true
|
41
|
+
# Path to your source map file
|
42
|
+
# Optional
|
43
|
+
config.source_map_path = "app/assets/build/server_rendering.js.map"
|
47
44
|
|
48
45
|
# Raise errors if JS rendering failed. If false, the error will be
|
49
46
|
# logged out to Rails log and Humid.render will return an empty string
|
@@ -78,8 +75,8 @@ end
|
|
78
75
|
```
|
79
76
|
|
80
77
|
If you'd like support for source map support, you will need to
|
81
|
-
1.
|
82
|
-
2.
|
78
|
+
1. Add the following to your entry file, e.g, `server_rendering.js`.
|
79
|
+
2. set `config.source_map_path`.
|
83
80
|
|
84
81
|
```javascript
|
85
82
|
require("source-map-support").install({
|
@@ -91,6 +88,7 @@ require("source-map-support").install({
|
|
91
88
|
}
|
92
89
|
});
|
93
90
|
```
|
91
|
+
A [sample] webpack.config is available for reference.
|
94
92
|
|
95
93
|
## The mini_racer environment.
|
96
94
|
|
@@ -112,7 +110,8 @@ respective methods on the configured logger.
|
|
112
110
|
|
113
111
|
## Usage
|
114
112
|
|
115
|
-
|
113
|
+
In your entry file, e.g, `server_rendering.js`, pass your HTML render function
|
114
|
+
to `setHumidRenderer`. There is no need to require the function.
|
116
115
|
|
117
116
|
```javascript
|
118
117
|
// Set a factory function that will create a new instance of our app
|
@@ -156,41 +155,20 @@ end
|
|
156
155
|
|
157
156
|
### Server-side libraries that detect node.js envs.
|
158
157
|
You may need webpacker to create aliases for server friendly libraries that can
|
159
|
-
not detect the `mini_racer` environment.
|
158
|
+
not detect the `mini_racer` environment. For example, in your `webpack.config.js`.
|
160
159
|
|
161
160
|
```diff
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
+const path = require('path')
|
170
|
-
+const ConfigObject = require('@rails/webpacker/package/config_types/config
|
171
|
-
|
172
|
-
-module.exports = environment.toWebpackConfig()
|
173
|
-
+const webConfig = environment.toWebpackConfig()
|
174
|
-
+const ssrConfig = new ConfigObject(webConfig.toObject())
|
175
|
-
+
|
176
|
-
+ssrConfig.delete('entry')
|
177
|
-
+ssrConfig.merge({
|
178
|
-
+ entry: {
|
179
|
-
+ server_rendering: webConfig.entry.server_rendering
|
180
|
-
+ },
|
181
|
-
+ resolve: {
|
182
|
-
+ alias: {
|
183
|
-
+ 'html-dom-parser': path.resolve(__dirname, '../../node_modules/html-dom-parser/lib/html-to-dom-server')
|
184
|
-
+ }
|
185
|
-
+ }
|
186
|
-
+})
|
187
|
-
+
|
188
|
-
+delete webConfig.entry.server_rendering
|
189
|
-
+module.exports = [ssrConfig, webConfig]
|
161
|
+
...
|
162
|
+
resolve: {
|
163
|
+
alias: {
|
164
|
+
'html-dom-parser': path.resolve(__dirname, '../../node_modules/html-dom-parser/lib/html-to-dom-server')
|
165
|
+
}
|
166
|
+
}
|
167
|
+
...
|
190
168
|
```
|
191
169
|
|
192
170
|
## Writing universal code
|
193
|
-
[Vue has
|
171
|
+
[Vue has a resource][vue_ssr] on how to write universal code. Below
|
194
172
|
are a few highlights that are important to keep in mind.
|
195
173
|
|
196
174
|
### State
|
@@ -241,5 +219,5 @@ See [our other projects][community] or
|
|
241
219
|
[community]: https://thoughtbot.com/community?utm_source=github
|
242
220
|
[hire]: https://thoughtbot.com?utm_source=github
|
243
221
|
[mini_racer]: https://github.com/rubyjs/mini_racer
|
244
|
-
[webpacker]: https://github.com/rails/webpacker
|
245
222
|
[vue_ssr]: https://ssr.vuejs.org/
|
223
|
+
[sample]: ./webpack.config.js
|
data/lib/humid/version.rb
CHANGED
data/lib/humid.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require "mini_racer"
|
2
2
|
require "logger"
|
3
|
-
require "webpacker"
|
4
3
|
require "active_support"
|
5
4
|
require "active_support/core_ext"
|
6
5
|
require "humid/log_subscriber"
|
@@ -19,20 +18,15 @@ module Humid
|
|
19
18
|
|
20
19
|
@@context = nil
|
21
20
|
|
22
|
-
config_accessor :
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
config_accessor :use_source_map do
|
27
|
-
false
|
28
|
-
end
|
21
|
+
config_accessor :application_path
|
22
|
+
config_accessor :source_map_path
|
29
23
|
|
30
24
|
config_accessor :raise_render_errors do
|
31
25
|
true
|
32
26
|
end
|
33
27
|
|
34
28
|
config_accessor :logger do
|
35
|
-
Logger.new(
|
29
|
+
Logger.new($stdout)
|
36
30
|
end
|
37
31
|
|
38
32
|
config_accessor :context_options do
|
@@ -63,27 +57,7 @@ module Humid
|
|
63
57
|
JS
|
64
58
|
end
|
65
59
|
|
66
|
-
def handle_stale_files
|
67
|
-
if Webpacker.compiler.stale?
|
68
|
-
Webpacker.compiler.compile
|
69
|
-
end
|
70
|
-
|
71
|
-
public_path = Webpacker.config.public_path
|
72
|
-
server_rendering_pack = config.server_rendering_pack
|
73
|
-
source_path = public_path.join(Webpacker.manifest.lookup(server_rendering_pack)[1..-1])
|
74
|
-
filename = File.basename(source_path.to_s)
|
75
|
-
|
76
|
-
if @@current_filename != filename
|
77
|
-
dispose
|
78
|
-
create_context
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
60
|
def context
|
83
|
-
if @@context && Webpacker.env.development?
|
84
|
-
handle_stale_files
|
85
|
-
end
|
86
|
-
|
87
61
|
@@context
|
88
62
|
end
|
89
63
|
|
@@ -95,7 +69,7 @@ module Humid
|
|
95
69
|
end
|
96
70
|
|
97
71
|
def create_context
|
98
|
-
ctx = MiniRacer::Context.new(config.context_options)
|
72
|
+
ctx = MiniRacer::Context.new(**config.context_options)
|
99
73
|
ctx.attach("console.log", proc { |err| logger.debug(err.to_s) })
|
100
74
|
ctx.attach("console.info", proc { |err| logger.info(err.to_s) })
|
101
75
|
ctx.attach("console.error", proc { |err| logger.error(err.to_s) })
|
@@ -106,20 +80,13 @@ module Humid
|
|
106
80
|
js << renderer
|
107
81
|
ctx.eval(js)
|
108
82
|
|
109
|
-
|
110
|
-
|
111
|
-
webpack_source_file = Webpacker.manifest.lookup(config.server_rendering_pack)
|
112
|
-
if webpack_source_file.nil?
|
113
|
-
raise FileNotFound.new("Humid could not find a built pack for #{config.server_rendering_pack}")
|
114
|
-
end
|
83
|
+
source_path = config.application_path
|
84
|
+
map_path = config.source_map_path
|
115
85
|
|
116
|
-
if
|
117
|
-
webpack_source_map = Webpacker.manifest.lookup("#{config.server_rendering_pack}.map")
|
118
|
-
map_path = public_path.join(webpack_source_map[1..-1])
|
86
|
+
if map_path
|
119
87
|
ctx.attach("readSourceMap", proc { File.read(map_path) })
|
120
88
|
end
|
121
89
|
|
122
|
-
source_path = public_path.join(webpack_source_file[1..-1])
|
123
90
|
filename = File.basename(source_path.to_s)
|
124
91
|
@@current_filename = filename
|
125
92
|
ctx.eval(File.read(source_path), filename: filename)
|
@@ -131,7 +98,7 @@ module Humid
|
|
131
98
|
ActiveSupport::Notifications.instrument("render.humid") do
|
132
99
|
context.call("__renderer", *args)
|
133
100
|
rescue MiniRacer::RuntimeError => e
|
134
|
-
message = ([e.message] + e.backtrace.filter {|x| x.starts_with? "JavaScript"}).join("\n")
|
101
|
+
message = ([e.message] + e.backtrace.filter { |x| x.starts_with? "JavaScript" }).join("\n")
|
135
102
|
render_error = Humid::RenderError.new(message)
|
136
103
|
|
137
104
|
if config.raise_render_errors
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: humid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johny Ho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: webpacker
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '4.0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '4.0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: mini_racer
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,70 +30,14 @@ dependencies:
|
|
44
30
|
requirements:
|
45
31
|
- - ">="
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
33
|
+
version: '7.0'
|
48
34
|
type: :runtime
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
38
|
- - ">="
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rake
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '12.0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '12.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: '3.8'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '3.8'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: byebug
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '9.0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '9.0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: rails
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '6.0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '6.0'
|
40
|
+
version: '7.0'
|
111
41
|
description: Javascript SSR rendering for Rails
|
112
42
|
email: jho406@gmail.com
|
113
43
|
executables: []
|
@@ -119,9 +49,6 @@ files:
|
|
119
49
|
- lib/humid/controller_runtime.rb
|
120
50
|
- lib/humid/log_subscriber.rb
|
121
51
|
- lib/humid/version.rb
|
122
|
-
- spec/controller_runtime_spec.rb
|
123
|
-
- spec/log_subscriber_spec.rb
|
124
|
-
- spec/render_spec.rb
|
125
52
|
homepage: https://github.com/thoughtbot/humid/
|
126
53
|
licenses:
|
127
54
|
- MIT
|
@@ -141,11 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
68
|
- !ruby/object:Gem::Version
|
142
69
|
version: '0'
|
143
70
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
71
|
+
rubygems_version: 3.4.6
|
145
72
|
signing_key:
|
146
73
|
specification_version: 4
|
147
74
|
summary: Javascript SSR rendering for Rails
|
148
|
-
test_files:
|
149
|
-
- spec/render_spec.rb
|
150
|
-
- spec/log_subscriber_spec.rb
|
151
|
-
- spec/controller_runtime_spec.rb
|
75
|
+
test_files: []
|
@@ -1,79 +0,0 @@
|
|
1
|
-
require_relative "./support/helper"
|
2
|
-
|
3
|
-
describe Humid::ControllerRuntime do
|
4
|
-
controller_runtime = Humid::ControllerRuntime
|
5
|
-
|
6
|
-
def set_metric value
|
7
|
-
Humid::LogSubscriber.runtime = value
|
8
|
-
end
|
9
|
-
|
10
|
-
def clear_metric!
|
11
|
-
Humid::LogSubscriber.reset_runtime = 0
|
12
|
-
end
|
13
|
-
|
14
|
-
reference_controller_class = Class.new {
|
15
|
-
def process_action *_
|
16
|
-
@process_action = true
|
17
|
-
end
|
18
|
-
|
19
|
-
def cleanup_view_runtime *_
|
20
|
-
@cleanup_view_runtime.call
|
21
|
-
end
|
22
|
-
|
23
|
-
def append_info_to_payload *_
|
24
|
-
@append_info_to_payload = true
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.log_process_action *_
|
28
|
-
@log_process_action.call
|
29
|
-
end
|
30
|
-
}
|
31
|
-
|
32
|
-
controller_class = Class.new reference_controller_class do
|
33
|
-
include controller_runtime
|
34
|
-
|
35
|
-
def logger
|
36
|
-
Logger.new(STDOUT)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
let(:controller) { controller_class.new }
|
41
|
-
|
42
|
-
it "resets the metric before each action" do
|
43
|
-
set_metric 42
|
44
|
-
controller.send(:process_action, "foo")
|
45
|
-
expect(Humid::LogSubscriber.runtime).to be(0)
|
46
|
-
expect(controller.instance_variable_get("@process_action")).to be(true)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "strips the metric of other sources of the runtime" do
|
50
|
-
set_metric 1
|
51
|
-
controller.instance_variable_set "@cleanup_view_runtime", -> {
|
52
|
-
controller.instance_variable_set "@cleanup_view_runtime", true
|
53
|
-
set_metric 13
|
54
|
-
42
|
55
|
-
}
|
56
|
-
returned = controller.send :cleanup_view_runtime
|
57
|
-
expect(controller.instance_variable_get("@cleanup_view_runtime")).to be(true)
|
58
|
-
expect(controller.humid_runtime).to eq(14)
|
59
|
-
expect(returned).to be(29)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "appends the metric to payload" do
|
63
|
-
payload = {}
|
64
|
-
set_metric 42
|
65
|
-
controller.send :append_info_to_payload, payload
|
66
|
-
expect(controller.instance_variable_get("@append_info_to_payload")).to be(true)
|
67
|
-
expect(payload[:humid_runtime]).to eq(42)
|
68
|
-
end
|
69
|
-
|
70
|
-
it "adds metric to log message" do
|
71
|
-
controller_class.instance_variable_set "@log_process_action", -> {
|
72
|
-
controller_class.instance_variable_set "@log_process_action", true
|
73
|
-
[]
|
74
|
-
}
|
75
|
-
messages = controller_class.log_process_action humid_runtime: 42.101
|
76
|
-
expect(controller_class.instance_variable_get("@log_process_action")).to be(true)
|
77
|
-
expect(messages).to eq(["Humid SSR: 42.1ms"])
|
78
|
-
end
|
79
|
-
end
|
data/spec/log_subscriber_spec.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require_relative "./support/helper"
|
2
|
-
require "active_support/log_subscriber/test_helper"
|
3
|
-
require "byebug"
|
4
|
-
|
5
|
-
RSpec.describe Humid::LogSubscriber do
|
6
|
-
around(:each) do |example|
|
7
|
-
app_path = File.expand_path("./testapp", File.dirname(__FILE__))
|
8
|
-
Dir.chdir(app_path) do
|
9
|
-
example.run
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
before(:each) do
|
14
|
-
Humid::LogSubscriber.reset_runtime
|
15
|
-
end
|
16
|
-
|
17
|
-
context ".runtime" do
|
18
|
-
it "is returns the runtime from the thread local" do
|
19
|
-
expect(Humid::LogSubscriber.runtime).to eql 0
|
20
|
-
key = "attr_Humid::LogSubscriber_humid_runtime"
|
21
|
-
Thread.current[key] = 3
|
22
|
-
expect(Humid::LogSubscriber.runtime).to eql 3
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context ".runtime=" do
|
27
|
-
it "sets the runtime in a thread-safe manner" do
|
28
|
-
expect(Humid::LogSubscriber.runtime).to eql 0
|
29
|
-
Humid::LogSubscriber.runtime = 3
|
30
|
-
key = "attr_Humid::LogSubscriber_humid_runtime"
|
31
|
-
expect(Thread.current[key]).to eql 3
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context ".reset_runtime" do
|
36
|
-
it "resets the runtime" do
|
37
|
-
Humid::LogSubscriber.runtime = 3
|
38
|
-
key = "attr_Humid::LogSubscriber_humid_runtime"
|
39
|
-
expect(Thread.current[key]).to eql 3
|
40
|
-
|
41
|
-
Humid::LogSubscriber.reset_runtime
|
42
|
-
expect(Thread.current[key]).to eql 0
|
43
|
-
expect(Humid::LogSubscriber.runtime).to eql 0
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it "is attached" do
|
48
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
49
|
-
Humid.create_context
|
50
|
-
expect(Humid::LogSubscriber.runtime).to eql(0)
|
51
|
-
Humid.render
|
52
|
-
expect(Humid::LogSubscriber.runtime).to be > 0
|
53
|
-
end
|
54
|
-
end
|
data/spec/render_spec.rb
DELETED
@@ -1,188 +0,0 @@
|
|
1
|
-
require_relative "./support/helper"
|
2
|
-
|
3
|
-
RSpec.describe "Humid" do
|
4
|
-
around(:each) do |example|
|
5
|
-
app_path = File.expand_path("./testapp", File.dirname(__FILE__))
|
6
|
-
Dir.chdir(app_path) do
|
7
|
-
example.run
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "create_context" do
|
12
|
-
after(:each) do
|
13
|
-
Humid.dispose
|
14
|
-
end
|
15
|
-
|
16
|
-
it "creates a context with initial js" do
|
17
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
18
|
-
Humid.create_context
|
19
|
-
|
20
|
-
expect(Humid.context).to be_kind_of(MiniRacer::Context)
|
21
|
-
end
|
22
|
-
|
23
|
-
context "When the file can not be found" do
|
24
|
-
it "raises" do
|
25
|
-
allow(Humid.config).to receive("server_rendering_pack") { "does_not_exist.js" }
|
26
|
-
|
27
|
-
expect {
|
28
|
-
Humid.create_context
|
29
|
-
}.to raise_error(Humid::FileNotFound, "Humid could not find a built pack for does_not_exist.js")
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
it "does not have timeouts, immediates, and intervals" do
|
34
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
35
|
-
|
36
|
-
Humid.create_context
|
37
|
-
|
38
|
-
expect {
|
39
|
-
Humid.context.eval("setTimeout()")
|
40
|
-
}.to raise_error(MiniRacer::RuntimeError, "ReferenceError: setTimeout is not defined")
|
41
|
-
expect {
|
42
|
-
Humid.context.eval("setInterval()")
|
43
|
-
}.to raise_error(MiniRacer::RuntimeError, "ReferenceError: setInterval is not defined")
|
44
|
-
expect {
|
45
|
-
Humid.context.eval("clearTimeout()")
|
46
|
-
}.to raise_error(MiniRacer::RuntimeError, "ReferenceError: clearTimeout is not defined")
|
47
|
-
expect {
|
48
|
-
Humid.context.eval("setImmediate()")
|
49
|
-
}.to raise_error(MiniRacer::RuntimeError, "ReferenceError: setImmediate is not defined")
|
50
|
-
expect {
|
51
|
-
Humid.context.eval("clearImmediate()")
|
52
|
-
}.to raise_error(MiniRacer::RuntimeError, "ReferenceError: clearImmediate is not defined")
|
53
|
-
end
|
54
|
-
|
55
|
-
it "proxies to Rails logger" do
|
56
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
57
|
-
Humid.create_context
|
58
|
-
expect(Humid.logger).to receive(:info).with("hello")
|
59
|
-
|
60
|
-
Humid.context.eval("console.info('hello')")
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe "context" do
|
65
|
-
it "returns the created context" do
|
66
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
67
|
-
|
68
|
-
Humid.create_context
|
69
|
-
|
70
|
-
expect(Humid.context).to be_kind_of(MiniRacer::Context)
|
71
|
-
end
|
72
|
-
|
73
|
-
context "when the js is stale and env is NOT dev`" do
|
74
|
-
it "does not recompile the JS" do
|
75
|
-
allow(Webpacker).to receive_message_chain("env.development?") { false }
|
76
|
-
allow(Webpacker).to receive_message_chain("compiler.stale?") { true }
|
77
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
78
|
-
|
79
|
-
Humid.create_context
|
80
|
-
prev_context = Humid.context
|
81
|
-
expect(prev_context).to be_kind_of(MiniRacer::Context)
|
82
|
-
|
83
|
-
allow(Webpacker).to receive_message_chain("compiler.stale?") { true }
|
84
|
-
|
85
|
-
next_context = Humid.context
|
86
|
-
|
87
|
-
expect(prev_context).to eql(next_context)
|
88
|
-
expect(next_context).to be_kind_of(MiniRacer::Context)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context "when the env is development" do
|
93
|
-
it "compiles the JS when stale" do
|
94
|
-
allow(Webpacker).to receive_message_chain("env.development?") { true }
|
95
|
-
allow(Webpacker).to receive_message_chain("compiler.stale?") { true }
|
96
|
-
allow(Webpacker).to receive_message_chain("compiler.compile")
|
97
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
98
|
-
|
99
|
-
Humid.create_context
|
100
|
-
prev_context = Humid.context
|
101
|
-
expect(prev_context).to be_kind_of(MiniRacer::Context)
|
102
|
-
|
103
|
-
allow(Webpacker).to receive_message_chain("compiler.stale?") { true }
|
104
|
-
# This simulates a changing file
|
105
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple_changed.js" }
|
106
|
-
|
107
|
-
next_context = Humid.context
|
108
|
-
|
109
|
-
expect(prev_context).to_not eql(next_context)
|
110
|
-
expect(next_context).to be_kind_of(MiniRacer::Context)
|
111
|
-
end
|
112
|
-
|
113
|
-
it "creates a new context when webpack-devserver already handled JS staleness" do
|
114
|
-
allow(Webpacker).to receive_message_chain("env.development?") { true }
|
115
|
-
allow(Webpacker).to receive_message_chain("compiler.stale?") { true }
|
116
|
-
allow(Webpacker).to receive_message_chain("compiler.compile")
|
117
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
118
|
-
|
119
|
-
Humid.create_context
|
120
|
-
prev_context = Humid.context
|
121
|
-
expect(Humid.render).to eql("hello")
|
122
|
-
expect(prev_context).to be_kind_of(MiniRacer::Context)
|
123
|
-
|
124
|
-
allow(Webpacker).to receive_message_chain("compiler.stale?") { false }
|
125
|
-
# This simulates a changing file
|
126
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple_changed.js" }
|
127
|
-
|
128
|
-
next_context = Humid.context
|
129
|
-
|
130
|
-
expect(prev_context).to_not eql(next_context)
|
131
|
-
expect(next_context).to be_kind_of(MiniRacer::Context)
|
132
|
-
expect(Humid.render).to eql("hello changed")
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
describe "render" do
|
138
|
-
it "returns a js output" do
|
139
|
-
allow(Humid.config).to receive("server_rendering_pack") { "simple.js" }
|
140
|
-
Humid.create_context
|
141
|
-
|
142
|
-
expect(Humid.render).to eql("hello")
|
143
|
-
end
|
144
|
-
|
145
|
-
it "applys args to the func" do
|
146
|
-
allow(Humid.config).to receive("server_rendering_pack") { "args.js" }
|
147
|
-
Humid.create_context
|
148
|
-
|
149
|
-
args = ["a", 1, 2, [], {}]
|
150
|
-
|
151
|
-
expect(Humid.render(*args)).to eql({"0" => "a", "1" => 1, "2" => 2, "3" => [], "4" => {}})
|
152
|
-
end
|
153
|
-
|
154
|
-
it "can use source maps to see errors" do
|
155
|
-
allow(Humid.config).to receive("server_rendering_pack") { "reporting.js" }
|
156
|
-
allow(Humid.config).to receive("use_source_map") { true }
|
157
|
-
|
158
|
-
Humid.create_context
|
159
|
-
|
160
|
-
expect {
|
161
|
-
Humid.render
|
162
|
-
}.to raise_error { |error|
|
163
|
-
expect(error).to be_a(Humid::RenderError)
|
164
|
-
message = <<~MSG
|
165
|
-
Error: ^^ Look! These stack traces map to the actual source code :)
|
166
|
-
JavaScript at throwSomeError (/webpack:/app/javascript/packs/components/error-causing-component.js:2:1)
|
167
|
-
JavaScript at ErrorCausingComponent (/webpack:/app/javascript/packs/components/error-causing-component.js:8:1)
|
168
|
-
JavaScript at /webpack:/app/javascript/packs/reporting.js:18:1
|
169
|
-
MSG
|
170
|
-
|
171
|
-
expect(error.message).to eql message.strip
|
172
|
-
}
|
173
|
-
end
|
174
|
-
|
175
|
-
it "siliences render errors to the log" do
|
176
|
-
allow(Humid.config).to receive("server_rendering_pack") { "reporting.js" }
|
177
|
-
allow(Humid.config).to receive("raise_render_errors") { false }
|
178
|
-
allow(Humid.config).to receive("use_source_map") { true }
|
179
|
-
|
180
|
-
Humid.create_context
|
181
|
-
|
182
|
-
expect(Humid.logger).to receive(:error)
|
183
|
-
output = Humid.render
|
184
|
-
|
185
|
-
expect(output).to eql("")
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|