hotwire-livereload 0.1.2 → 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: 5bcabf2d518f67b06afa47b3b462f6d190b35ee3cfe2690148b599a0c28ba94e
4
- data.tar.gz: 87d3401bddb1a879fb40b73aaef596ff0f17ba019e421411e9a0a3ce4b6562a2
3
+ metadata.gz: a4397b862ba6f621e8442f674fbf5ffa478422e8d01b293f299afbd8213154ac
4
+ data.tar.gz: e9e4a35a16d3f09c9f88467e2e0267ee98220d6be0ddda7bd28fc9b3cb8bff95
5
5
  SHA512:
6
- metadata.gz: a517e60ba65450c6346e6c574ff047f12a8dd09c1b8ebaed16cb393ddaba254490869c829f022c7cc929b6d00c4c32bf0f86c3d2b0e986ac608fcad471107995
7
- data.tar.gz: 544f51ca80f96bd73d30ee582acaecf1e734e44d4132d5a1a415bb11b1f87dbc969e0885663c961554e3635a2c72080e03a6952b6abdf4f6f4e435e075f472e5
6
+ metadata.gz: 71e11b2197803862c7afb86717f6ccc753ddd60923e7f330b715af28f2263fe49e8baa839bf6d7110ff5bf6dfddeba49c84c7182ea5edc54400c9597e7cbd0ec
7
+ data.tar.gz: a847e23ec3f9eb51ba0f7a7cc3d9ee112980b5eae0ae250e5d82505c19cebd97f6849c01a058d227946d9c075c043cca5909120aa09cc3f95fafa4e121233fe9
data/README.md CHANGED
@@ -8,9 +8,61 @@ 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 hotwire_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
+ ```
20
+
21
+ ## Configuration
22
+
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
+ ```
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:
44
+ ```ruby
45
+ # config/environments/development.rb
46
+
47
+ Rails.application.configure do
48
+ # ...
49
+ config.hotwire_livereload.force_reload_paths << Rails.root.join("app/javascript")
50
+ end
51
+ ```
52
+
53
+ ## Disable livereload
54
+
55
+ To temporarily disable livereload use:
56
+ ```bash
57
+ bin/rails livereload:disable
58
+ ```
59
+
60
+ To re-enable:
61
+ ```bash
62
+ bin/rails livereload:enable
63
+ ```
64
+
65
+ No server restart is required. Disabling is managed by `tmp/livereload-disabled.txt` file.
14
66
 
15
67
  ## Development
16
68
 
@@ -600,20 +600,24 @@
600
600
  // app/javascript/hotwire-livereload.js
601
601
  var import_actioncable = __toModule(require_action_cable());
602
602
  var import_debounce = __toModule(require_debounce());
603
- var endpoint = "/hotwire-livereload/cable";
604
- var uid = (Date.now() + (Math.random() * 100 | 0)).toString();
605
- var consumer = (0, import_actioncable.createConsumer)(`${endpoint}?uid=${uid}`);
606
- var received = (0, import_debounce.default)(() => {
607
- console.log("Hotwire::Livereload files changed");
608
- Turbo.visit(window.location.href);
603
+ var consumer = (0, import_actioncable.createConsumer)();
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
+ }
609
613
  }, 300);
610
614
  consumer.subscriptions.create("Hotwire::Livereload::ReloadChannel", {
611
615
  received,
612
616
  connected() {
613
- console.log("Hotwire::Livereload websocket connected");
617
+ console.log("[Hotwire::Livereload] Websocket connected");
614
618
  },
615
619
  disconnected() {
616
- console.log("Hotwire::Livereload websocket disconnected");
620
+ console.log("[Hotwire::Livereload] Websocket disconnected");
617
621
  }
618
622
  });
619
623
  })();
@@ -1,5 +1,5 @@
1
1
  class Hotwire::Livereload::ReloadChannel < ActionCable::Channel::Base
2
2
  def subscribed
3
- stream_from "reload"
3
+ stream_from "hotwire-reload"
4
4
  end
5
5
  end
@@ -1,20 +1,27 @@
1
1
  import { createConsumer } from "@rails/actioncable"
2
2
  import debounce from "debounce"
3
3
 
4
- const endpoint = "/hotwire-livereload/cable"
5
- const uid = (Date.now() + ((Math.random() * 100) | 0)).toString()
6
- const consumer = createConsumer(`${endpoint}?uid=${uid}`)
7
- const received = debounce(() => {
8
- console.log("Hotwire::Livereload files changed")
9
- Turbo.visit(window.location.href)
4
+ const consumer = createConsumer()
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
+ }
10
15
  }, 300)
11
16
 
12
17
  consumer.subscriptions.create("Hotwire::Livereload::ReloadChannel", {
13
18
  received,
19
+
14
20
  connected() {
15
- console.log("Hotwire::Livereload websocket connected")
21
+ console.log("[Hotwire::Livereload] Websocket connected")
16
22
  },
23
+
17
24
  disconnected() {
18
- console.log("Hotwire::Livereload websocket disconnected")
25
+ console.log("[Hotwire::Livereload] Websocket disconnected")
19
26
  },
20
27
  })
@@ -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,31 +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")
42
+ options.listen_paths += default_listen_paths.select { |p| Dir.exist?(p) }
32
43
  end
33
44
 
34
- initializer "hotwire_livereload.cable.config" do |app|
35
- config_path = Hotwire::Livereload::Engine.root.join("config", "livereload_cable.yml")
36
- cable = Hotwire::Livereload::Engine.cable
45
+ config.after_initialize do |app|
46
+ if Rails.env.development? && defined?(Rails::Server)
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("|")
37
50
 
38
- if Rails.env.development?
39
- cable.cable = app.config_for(config_path).with_indifferent_access
40
- cable.mount_path = "/cable"
41
- cable.connection_class = -> { Hotwire::Livereload::Connection }
42
- cable.logger ||= Rails.logger
43
- end
44
- end
51
+ @listener = Listen.to(*listen_paths) do |modified, added, removed|
52
+ unless File.exists?(DISABLE_FILE)
53
+ changed = [modified, removed, added].flatten.uniq
54
+ return unless changed.any?
45
55
 
46
- config.after_initialize do |app|
47
- if Rails.env.development?
48
- @listener = Listen.to(*app.config.hotwire_livereload.listen_paths) do |modified, added, removed|
49
- if modified.any? || removed.any? || added.any?
50
- Hotwire::Livereload::Engine.websocket.broadcast(
51
- "reload",
52
- { modified: modified, removed: removed, added: added }
53
- )
56
+ force_reload = force_reload_paths.present? && changed.any? do |path|
57
+ path.match(%r{#{force_reload_paths}})
58
+ end
59
+
60
+ ActionCable.server.broadcast("hotwire-reload", {
61
+ changed: changed,
62
+ force_reload: force_reload
63
+ })
54
64
  end
55
65
  end
56
66
  @listener.start
@@ -62,16 +72,6 @@ module Hotwire
62
72
  @listener&.stop
63
73
  end
64
74
  end
65
-
66
- class << self
67
- def websocket
68
- @websocket ||= ActionCable::Server::Base.new(config: Hotwire::Livereload::Engine.cable)
69
- end
70
-
71
- def cable
72
- @cable ||= ActionCable::Server::Configuration.new
73
- end
74
- end
75
75
  end
76
76
  end
77
77
  end
@@ -1,5 +1,5 @@
1
1
  module Hotwire
2
2
  module Livereload
3
- VERSION = "0.1.2"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -2,5 +2,6 @@ require "hotwire/livereload/engine"
2
2
 
3
3
  module Hotwire
4
4
  module Livereload
5
+ DISABLE_FILE = "tmp/livereload-disabled.txt"
5
6
  end
6
7
  end
@@ -1,12 +1,31 @@
1
- route("mount Hotwire::Livereload::Engine, at: '/hotwire-livereload'")
1
+ APP_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
2
+ CABLE_CONFIG_PATH = Rails.root.join("config/cable.yml")
2
3
 
3
- if (app_layout_path = Rails.root.join("app/views/layouts/application.html.erb")).exist?
4
- say "Add Hotwire::Livereload tag in application layout"
5
- insert_into_file app_layout_path.to_s, before: /\s*<\/head>/ do <<-HTML
6
- \n <%= hotwire_livereload_tags %>
4
+ if APP_LAYOUT_PATH.exist?
5
+ say "Add Hotwire Livereload tag in application layout"
6
+ content = <<-HTML
7
+ \n <% if Rails.env.development? %>
8
+ <%= javascript_include_tag "hotwire-livereload", defer: true %>
9
+ <% end %>
7
10
  HTML
8
- end
11
+ insert_into_file APP_LAYOUT_PATH, content.chop, before: /\s*<\/head>/
9
12
  else
10
13
  say "Default application.html.erb is missing!", :red
11
- say %( Add <%= hotwire_livereload_tags %> within the <head> tag in your custom layout.)
14
+ say %( Add <%= hotwire_livereload_tags %> within the <head> tag in your custom layout.)
15
+ end
16
+
17
+ if CABLE_CONFIG_PATH.exist?
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
26
+
27
+ say "Switch development cable to use redis"
28
+ gsub_file CABLE_CONFIG_PATH.to_s, /development:\n\s+adapter: async/, "development:\n adapter: redis\n url: redis://localhost:6379/1"
29
+ else
30
+ say 'ActionCable config file (config/cable.yml) is missing. Uncomment "gem \'redis\'" in your Gemfile and create config/cable.yml to use Hotwire Livereload.'
12
31
  end
@@ -0,0 +1,21 @@
1
+ namespace :livereload do
2
+ desc "Install Hotwire::Livereload into the app"
3
+ task :install do
4
+ system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/install.rb", __dir__)}"
5
+ end
6
+
7
+ desc "Disable Hotwire::Livereload"
8
+ task :disable do
9
+ FileUtils.mkdir_p("tmp")
10
+ FileUtils.touch Hotwire::Livereload::DISABLE_FILE
11
+ puts "Livereload disabled."
12
+ end
13
+
14
+ desc "Enable Hotwire::Livereload"
15
+ task :enable do
16
+ if File.exist?(Hotwire::Livereload::DISABLE_FILE)
17
+ File.delete Hotwire::Livereload::DISABLE_FILE
18
+ end
19
+ puts "Livereload enabled."
20
+ end
21
+ end
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.1.2
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-09-03 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
@@ -48,17 +48,14 @@ files:
48
48
  - MIT-LICENSE
49
49
  - README.md
50
50
  - app/assets/javascripts/hotwire-livereload.js
51
- - app/channels/hotwire/livereload/connection.rb
52
51
  - app/channels/hotwire/livereload/reload_channel.rb
53
52
  - app/helpers/hotwire/livereload/livereload_tags_helper.rb
54
53
  - app/javascript/hotwire-livereload.js
55
- - config/livereload_cable.yml
56
- - config/routes.rb
57
54
  - lib/hotwire/livereload.rb
58
55
  - lib/hotwire/livereload/engine.rb
59
56
  - lib/hotwire/livereload/version.rb
60
57
  - lib/install/install.rb
61
- - lib/tasks/hotwire/livereload/install.rake
58
+ - lib/tasks/livereload_tasks.rake
62
59
  homepage: https://github.com/kirillplatonov/hotwire-livereload
63
60
  licenses:
64
61
  - MIT
@@ -78,8 +75,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
75
  - !ruby/object:Gem::Version
79
76
  version: '0'
80
77
  requirements: []
81
- rubygems_version: 3.2.22
78
+ rubygems_version: 3.2.32
82
79
  signing_key:
83
80
  specification_version: 4
84
- summary: Automatically reload Hotwire Turbo when 'view' files are modified.
81
+ summary: Automatically reload Hotwire Turbo when app files are modified.
85
82
  test_files: []
@@ -1,9 +0,0 @@
1
- class Hotwire::Livereload::Connection < ActionCable::Connection::Base
2
- identified_by :uid
3
-
4
- def connect
5
- self.uid = request.params[:uid]
6
- logger.add_tags(uid)
7
- logger.info "connected to Hotwire::Livereload"
8
- end
9
- end
@@ -1,2 +0,0 @@
1
- development:
2
- adapter: async
data/config/routes.rb DELETED
@@ -1,5 +0,0 @@
1
- Hotwire::Livereload::Engine.routes.draw do
2
- if Rails.env.development?
3
- mount Hotwire::Livereload::Engine.websocket => Hotwire::Livereload::Engine.cable.mount_path
4
- end
5
- end
@@ -1,6 +0,0 @@
1
- namespace :hotwire_livereload do
2
- desc "Setup Hotwire Livereload"
3
- task :install do
4
- system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../../install/install.rb", __dir__)}"
5
- end
6
- end