hotwire-livereload 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
  SHA256:
3
- metadata.gz: 05ad19b6e77492fec265a2da6af3ddd9aa8b7b149ae284397644db40b553974b
4
- data.tar.gz: 7f497acf2a34e1774d80c7a2fa1030b6a2eb9228cc2fe257b73a7cdad0e912fc
3
+ metadata.gz: a4397b862ba6f621e8442f674fbf5ffa478422e8d01b293f299afbd8213154ac
4
+ data.tar.gz: e9e4a35a16d3f09c9f88467e2e0267ee98220d6be0ddda7bd28fc9b3cb8bff95
5
5
  SHA512:
6
- metadata.gz: 554901b08d7d257da5660c5020cbfb8bab03e987eff1d329337fa9943119c6e3a8bead3bc62c97edec32250f7c0bb5503b7d306373e794bd7908a208f211284a
7
- data.tar.gz: 8e213677c64df89f7903d672c421b7faa9d8a5d12e136ef24e92e7dbc1a0deedc650d92cd882b06627f20e89ddcd4d46ea72943e22cf035152b9aff2e3284c02
6
+ metadata.gz: 71e11b2197803862c7afb86717f6ccc753ddd60923e7f330b715af28f2263fe49e8baa839bf6d7110ff5bf6dfddeba49c84c7182ea5edc54400c9597e7cbd0ec
7
+ data.tar.gz: a847e23ec3f9eb51ba0f7a7cc3d9ee112980b5eae0ae250e5d82505c19cebd97f6849c01a058d227946d9c075c043cca5909120aa09cc3f95fafa4e121233fe9
data/README.md CHANGED
@@ -8,20 +8,45 @@ https://user-images.githubusercontent.com/839922/131983979-afd0bcc7-86e8-4c53-97
8
8
 
9
9
  The JavaScript for Hotwire::Livereload is installed via asset pipeline, which is included with this gem.
10
10
 
11
- 1. Add `hotwire-livereload` gem to your Gemfile: `gem 'hotwire-livereload'`
12
- 2. Run `./bin/bundle install`
13
- 3. Run `./bin/rails livereload:install`
11
+ Add `hotwire-livereload` to your Gemfile:
12
+ ```
13
+ bundle add hotwire-livereload --group development
14
+ ```
15
+
16
+ Run installer:
17
+ ```
18
+ rails livereload:install
19
+ ```
14
20
 
15
21
  ## Configuration
16
22
 
17
23
  You can watch for changes in additional folders by adding them to `listen_paths`. For example, you can watch for CSS changes:
24
+ ```ruby
25
+ # config/environments/development.rb
26
+
27
+ Rails.application.configure do
28
+ # ...
29
+ config.hotwire_livereload.listen_paths << Rails.root.join("app/custom_folder")
30
+ end
31
+ ```
18
32
 
33
+ Folders listened by default:
34
+ - `app/views`
35
+ - `app/helpers`
36
+ - `app/javascript`
37
+ - `app/assets/stylesheets`
38
+ - `app/assets/javascripts`
39
+ - `app/assets/images`
40
+ - `app/components`
41
+ - `config/locales`
42
+
43
+ You can setup force reloading (full page reload) for changes in some folders using `force_reload_paths` option. For example, you can trigger force reload on JS changes:
19
44
  ```ruby
20
45
  # config/environments/development.rb
21
46
 
22
47
  Rails.application.configure do
23
48
  # ...
24
- config.hotwire_livereload.listen_paths << Rails.root.join("app/assets/stylesheets")
49
+ config.hotwire_livereload.force_reload_paths << Rails.root.join("app/javascript")
25
50
  end
26
51
  ```
27
52
 
@@ -601,9 +601,15 @@
601
601
  var import_actioncable = __toModule(require_action_cable());
602
602
  var import_debounce = __toModule(require_debounce());
603
603
  var consumer = (0, import_actioncable.createConsumer)();
604
- var received = (0, import_debounce.default)(() => {
605
- console.log("[Hotwire::Livereload] Files changed");
606
- Turbo.visit(window.location.href);
604
+ var received = (0, import_debounce.default)(({ force_reload }) => {
605
+ const onErrorPage = document.title === "Action Controller: Exception caught";
606
+ if (onErrorPage || force_reload) {
607
+ console.log("[Hotwire::Livereload] Files changed. Force reloading..");
608
+ document.location.reload();
609
+ } else {
610
+ console.log("[Hotwire::Livereload] Files changed. Reloading..");
611
+ Turbo.visit(window.location.href);
612
+ }
607
613
  }, 300);
608
614
  consumer.subscriptions.create("Hotwire::Livereload::ReloadChannel", {
609
615
  received,
@@ -2,9 +2,16 @@ import { createConsumer } from "@rails/actioncable"
2
2
  import debounce from "debounce"
3
3
 
4
4
  const consumer = createConsumer()
5
- const received = debounce(() => {
6
- console.log("[Hotwire::Livereload] Files changed")
7
- Turbo.visit(window.location.href)
5
+ const received = debounce(({force_reload}) => {
6
+ const onErrorPage = document.title === "Action Controller: Exception caught"
7
+
8
+ if (onErrorPage || force_reload) {
9
+ console.log("[Hotwire::Livereload] Files changed. Force reloading..")
10
+ document.location.reload()
11
+ } else {
12
+ console.log("[Hotwire::Livereload] Files changed. Reloading..")
13
+ Turbo.visit(window.location.href)
14
+ }
8
15
  }, 300)
9
16
 
10
17
  consumer.subscriptions.create("Hotwire::Livereload::ReloadChannel", {
@@ -8,6 +8,7 @@ module Hotwire
8
8
  isolate_namespace Hotwire::Livereload
9
9
  config.hotwire_livereload = ActiveSupport::OrderedOptions.new
10
10
  config.hotwire_livereload.listen_paths ||= []
11
+ config.hotwire_livereload.force_reload_paths ||= []
11
12
  config.autoload_once_paths = %W(
12
13
  #{root}/app/channels
13
14
  #{root}/app/helpers
@@ -26,23 +27,40 @@ module Hotwire
26
27
  end
27
28
 
28
29
  initializer "hotwire_livereload.set_configs" do |app|
30
+ default_listen_paths = %w[
31
+ app/views
32
+ app/helpers
33
+ app/javascript
34
+ app/assets/stylesheets
35
+ app/assets/javascripts
36
+ app/assets/images
37
+ app/components
38
+ config/locales
39
+ ].map { |p| Rails.root.join(p) }
40
+
29
41
  options = app.config.hotwire_livereload
30
- options.listen_paths = options.listen_paths.map(&:to_s)
31
- options.listen_paths << Rails.root.join("app/views")
32
- options.listen_paths << Rails.root.join("app/helpers")
33
- if Dir.exist?(Rails.root.join("app/javascript"))
34
- options.listen_paths << Rails.root.join("app/javascript")
35
- end
42
+ options.listen_paths += default_listen_paths.select { |p| Dir.exist?(p) }
36
43
  end
37
44
 
38
45
  config.after_initialize do |app|
39
46
  if Rails.env.development? && defined?(Rails::Server)
40
- @listener = Listen.to(*app.config.hotwire_livereload.listen_paths) do |modified, added, removed|
47
+ options = app.config.hotwire_livereload
48
+ listen_paths = options.listen_paths.map(&:to_s).uniq
49
+ force_reload_paths = options.force_reload_paths.map(&:to_s).uniq.join("|")
50
+
51
+ @listener = Listen.to(*listen_paths) do |modified, added, removed|
41
52
  unless File.exists?(DISABLE_FILE)
42
- if (modified.any? || removed.any? || added.any?)
43
- content = { modified: modified, removed: removed, added: added }
44
- ActionCable.server.broadcast("hotwire-reload", content)
53
+ changed = [modified, removed, added].flatten.uniq
54
+ return unless changed.any?
55
+
56
+ force_reload = force_reload_paths.present? && changed.any? do |path|
57
+ path.match(%r{#{force_reload_paths}})
45
58
  end
59
+
60
+ ActionCable.server.broadcast("hotwire-reload", {
61
+ changed: changed,
62
+ force_reload: force_reload
63
+ })
46
64
  end
47
65
  end
48
66
  @listener.start
@@ -1,5 +1,5 @@
1
1
  module Hotwire
2
2
  module Livereload
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -3,18 +3,26 @@ CABLE_CONFIG_PATH = Rails.root.join("config/cable.yml")
3
3
 
4
4
  if APP_LAYOUT_PATH.exist?
5
5
  say "Add Hotwire Livereload tag in application layout"
6
- insert_into_file APP_LAYOUT_PATH, before: /\s*<\/head>/ do <<-HTML
7
- \n <%= hotwire_livereload_tags %>
6
+ content = <<-HTML
7
+ \n <% if Rails.env.development? %>
8
+ <%= javascript_include_tag "hotwire-livereload", defer: true %>
9
+ <% end %>
8
10
  HTML
9
- end
11
+ insert_into_file APP_LAYOUT_PATH, content.chop, before: /\s*<\/head>/
10
12
  else
11
13
  say "Default application.html.erb is missing!", :red
12
14
  say %( Add <%= hotwire_livereload_tags %> within the <head> tag in your custom layout.)
13
15
  end
14
16
 
15
17
  if CABLE_CONFIG_PATH.exist?
16
- say "Enable redis in bundle"
17
- uncomment_lines "Gemfile", %(gem 'redis')
18
+ gemfile = File.read(Rails.root.join("Gemfile"))
19
+ if gemfile.include?("redis")
20
+ say "Uncomment redis in Gemfile"
21
+ uncomment_lines "Gemfile", %(gem "redis")
22
+ else
23
+ say "Add redis to Gemfile"
24
+ gem "redis"
25
+ end
18
26
 
19
27
  say "Switch development cable to use redis"
20
28
  gsub_file CABLE_CONFIG_PATH.to_s, /development:\n\s+adapter: async/, "development:\n adapter: redis\n url: redis://localhost:6379/1"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotwire-livereload
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
  - Kirill Platonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-04 00:00:00.000000000 Z
11
+ date: 2021-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -75,8 +75,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 3.2.22
78
+ rubygems_version: 3.2.32
79
79
  signing_key:
80
80
  specification_version: 4
81
- summary: Automatically reload Hotwire Turbo when 'view' files are modified.
81
+ summary: Automatically reload Hotwire Turbo when app files are modified.
82
82
  test_files: []