humid 0.0.3 → 0.0.4

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
  SHA256:
3
- metadata.gz: 6d6b6ec66d3f61678381e5f6733db579618f56460a4aefc297219428ec6a112b
4
- data.tar.gz: 7581d1a9107fa247494f185116e0ee5773ed0dc3e79cefaca7ff771a6ed9be12
3
+ metadata.gz: 64ab68e5648b4211e7d39b272da5f94e1aaf3bfcdf625db30ee5f6c72a7dc529
4
+ data.tar.gz: 251e41301a91ea534547caa84b76ffef68b463299fd012c39b2b26e4626b6d13
5
5
  SHA512:
6
- metadata.gz: ec2d956eb06a761e5bb23563165500f014e86cb64a6ef0be9d816c4a5f3f4faeea7f9c9860ac10acc571ca1c355270f62ff578165b5a546e9413dfe41dcc92e3
7
- data.tar.gz: 7593b70d6e439688002385bba8c555f7f12876f107cd982df26617f753afb4fb55d22e12aa0ec1d169876245bd44fe101a54767ec999e98413bc6f3c9c59377c
6
+ metadata.gz: f97030ff349d70d7b4605969953194c34b8012dc12f477aa8c5f6f1ccdfee88be2ccb5c495fc880378be03c381ed88e11c8453abd1d8a92ad3621aad34a0b553
7
+ data.tar.gz: 23f59abc3c56fe9d11b6084e89b0c02a3b11c4e12175bcab6ea4a641630616950b6943a9948c64be270173461f383a540fd570fdb69fd439cfcd7007d6e542ea
data/README.md CHANGED
@@ -46,13 +46,19 @@ Humid.configure do |config|
46
46
  config.logger = Rails.logger
47
47
 
48
48
  # context_options. Options passed to mini_racer. Defaults to
49
- # config.context_options = {
50
- # timeout: 1000,
51
- # ensure_gc_after_idle: 2000
52
- # }
49
+ # empty.
50
+ # config.context_options = {}
51
+ config.context_options = {
52
+ timeout: 1000,
53
+ ensure_gc_after_idle: 2000
54
+ }
53
55
  end
54
56
 
55
- # If using puma in single mode
57
+ # Common development options
58
+ # You may need to use single_threaded mode with Spring
59
+ # MiniRacer::Platform.set_flags! :single_threaded
60
+ #
61
+ # If you're using Puma in single mode:
56
62
  # Humid.create_context
57
63
  ```
58
64
 
data/lib/humid.rb CHANGED
@@ -11,6 +11,9 @@ module Humid
11
11
  extend self
12
12
  include ActiveSupport::Configurable
13
13
 
14
+ class RenderError < StandardError
15
+ end
16
+
14
17
  config_accessor :server_rendering_file do
15
18
  "server_rendering.js"
16
19
  end
@@ -24,10 +27,7 @@ module Humid
24
27
  end
25
28
 
26
29
  config_accessor :context_options do
27
- {
28
- timeout: 1000,
29
- ensure_gc_after_idle: 2000
30
- }
30
+ {}
31
31
  end
32
32
 
33
33
  def remove_functions
@@ -54,16 +54,30 @@ module Humid
54
54
  JS
55
55
  end
56
56
 
57
- def context
58
- if @@context && Webpacker.env.development? && Webpacker.compiler.stale?
57
+ def handle_stale_files
58
+ if Webpacker.compiler.stale?
59
59
  Webpacker.compiler.compile
60
+ end
61
+
62
+ public_path = Webpacker.config.public_path
63
+ server_rendering_file = config.server_rendering_file
64
+ source_path = public_path.join(Webpacker.manifest.lookup(server_rendering_file)[1..-1])
65
+ filename = File.basename(source_path.to_s)
66
+
67
+ if @@current_filename != filename
60
68
  dispose
61
69
  create_context
62
- else
63
- @@context
64
70
  end
65
71
  end
66
72
 
73
+ def context
74
+ if @@context && Webpacker.env.development?
75
+ handle_stale_files
76
+ end
77
+
78
+ @@context
79
+ end
80
+
67
81
  def dispose
68
82
  if @@context
69
83
  @@context.dispose
@@ -89,20 +103,23 @@ module Humid
89
103
 
90
104
  source_path = public_path.join(Webpacker.manifest.lookup(server_rendering_file)[1..-1])
91
105
  map_path = public_path.join(Webpacker.manifest.lookup(server_rendering_map)[1..-1])
92
-
93
- filename = File.basename(source_path.to_s)
94
- ctx.eval(File.read(source_path), filename: filename)
95
-
96
106
  if config.use_source_map
97
107
  ctx.attach("readSourceMap", proc { File.read(map_path) })
98
108
  end
99
109
 
110
+ filename = File.basename(source_path.to_s)
111
+ @@current_filename = filename
112
+ ctx.eval(File.read(source_path), filename: filename)
113
+
100
114
  @@context = ctx
101
115
  end
102
116
 
103
117
  def render(*args)
104
118
  ActiveSupport::Notifications.instrument("render.humid") do
105
- @@context.call("__renderer", *args)
119
+ context.call("__renderer", *args)
120
+ rescue MiniRacer::RuntimeError => e
121
+ message = ([e.message] + e.backtrace.filter {|x| x.starts_with? "JavaScript"}).join("\n")
122
+ raise Humid::RenderError.new(message)
106
123
  end
107
124
  end
108
125
  end
data/lib/humid/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Humid
2
- VERSION = "0.0.3".freeze
2
+ VERSION = "0.0.4".freeze
3
3
  end
data/spec/render_spec.rb CHANGED
@@ -79,8 +79,8 @@ RSpec.describe "Humid" do
79
79
  end
80
80
  end
81
81
 
82
- context "when the js is stale and env is development" do
83
- it "compiles the JS" do
82
+ context "when the env is development" do
83
+ it "compiles the JS when stale" do
84
84
  allow(Webpacker).to receive_message_chain("env.development?") { true }
85
85
  allow(Webpacker).to receive_message_chain("compiler.stale?") { true }
86
86
  allow(Webpacker).to receive_message_chain("compiler.compile")
@@ -91,12 +91,36 @@ RSpec.describe "Humid" do
91
91
  expect(prev_context).to be_kind_of(MiniRacer::Context)
92
92
 
93
93
  allow(Webpacker).to receive_message_chain("compiler.stale?") { true }
94
+ # This simulates a changing file
95
+ allow(Humid.config).to receive("server_rendering_file") { "simple_changed.js" }
94
96
 
95
97
  next_context = Humid.context
96
98
 
97
99
  expect(prev_context).to_not eql(next_context)
98
100
  expect(next_context).to be_kind_of(MiniRacer::Context)
99
101
  end
102
+
103
+ it "creates a new context when webpack-devserver already handled JS staleness" do
104
+ allow(Webpacker).to receive_message_chain("env.development?") { true }
105
+ allow(Webpacker).to receive_message_chain("compiler.stale?") { true }
106
+ allow(Webpacker).to receive_message_chain("compiler.compile")
107
+ allow(Humid.config).to receive("server_rendering_file") { "simple.js" }
108
+
109
+ Humid.create_context
110
+ prev_context = Humid.context
111
+ expect(Humid.render).to eql("hello")
112
+ expect(prev_context).to be_kind_of(MiniRacer::Context)
113
+
114
+ allow(Webpacker).to receive_message_chain("compiler.stale?") { false }
115
+ # This simulates a changing file
116
+ allow(Humid.config).to receive("server_rendering_file") { "simple_changed.js" }
117
+
118
+ next_context = Humid.context
119
+
120
+ expect(prev_context).to_not eql(next_context)
121
+ expect(next_context).to be_kind_of(MiniRacer::Context)
122
+ expect(Humid.render).to eql("hello changed")
123
+ end
100
124
  end
101
125
  end
102
126
 
@@ -126,11 +150,12 @@ RSpec.describe "Humid" do
126
150
  expect {
127
151
  Humid.render
128
152
  }.to raise_error { |error|
129
- expect(error).to be_a(MiniRacer::RuntimeError)
130
- expect(error.message).to eql("Error: ^^ Look! These stack traces map to the actual source code :)")
131
- expect(error.backtrace[0]).to eql("JavaScript at throwSomeError (/webpack:/app/javascript/packs/components/error-causing-component.js:2:1)")
132
- expect(error.backtrace[1]).to eql("JavaScript at ErrorCausingComponent (/webpack:/app/javascript/packs/components/error-causing-component.js:8:1)")
133
- expect(error.backtrace[2]).to eql("JavaScript at /webpack:/app/javascript/packs/reporting.js:18:1")
153
+ expect(error).to be_a(Humid::RenderError)
154
+ message = error.message.split("\n")
155
+ expect(message[0]).to eql("Error: ^^ Look! These stack traces map to the actual source code :)")
156
+ expect(message[1]).to eql("JavaScript at throwSomeError (/webpack:/app/javascript/packs/components/error-causing-component.js:2:1)")
157
+ expect(message[2]).to eql("JavaScript at ErrorCausingComponent (/webpack:/app/javascript/packs/components/error-causing-component.js:8:1)")
158
+ expect(message[3]).to eql("JavaScript at /webpack:/app/javascript/packs/reporting.js:18:1")
134
159
  }
135
160
  end
136
161
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-17 00:00:00.000000000 Z
11
+ date: 2021-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webpacker