Module: Bridgetown::Rack::LoaderHooks
- Defined in:
- bridgetown-core/lib/bridgetown-core/rack/loader_hooks.rb
Class Method Summary collapse
-
.autoload_server_folder(server_folder) ⇒ Object
Sets up a Zeitwerk loader for the Roda routes in the server folder.
-
.register_hooks(server_folder, reload_file_path) ⇒ Object
Registers a
post_setup
andpost_reload
hook for the Zeitwerk loader in order to handle eager loading and, in development, the live reload watcher. -
.setup_autoload_listener(loader, server_folder, subclass_paths) ⇒ Object
Creates a listener to detect file changes within the server folder and notify Zeitwerk.
Class Method Details
.autoload_server_folder(server_folder) ⇒ Object
Sets up a Zeitwerk loader for the Roda routes in the server folder. Called by the server boot process when Rack starts up
10 11 12 13 14 15 16 |
# File 'bridgetown-core/lib/bridgetown-core/rack/loader_hooks.rb', line 10 def self.autoload_server_folder(server_folder) reload_file_path = Bridgetown.live_reload_path register_hooks server_folder, reload_file_path Bridgetown::Rack.loaders_manager.setup_loaders([server_folder]) end |
.register_hooks(server_folder, reload_file_path) ⇒ Object
Registers a post_setup
and post_reload
hook for the Zeitwerk loader in order to handle
eager loading and, in development, the live reload watcher
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'bridgetown-core/lib/bridgetown-core/rack/loader_hooks.rb', line 23 def self.register_hooks(server_folder, reload_file_path) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength Bridgetown::Hooks.register_one( :loader, :post_setup, reloadable: false ) do |loader, load_path| next unless load_path == server_folder loader.eager_load subclass_names = Roda.subclasses.map(&:name) subclass_paths = Set.new loader.all_expected_cpaths.each do |cpath, cname| if subclass_names.include?(cname) && cpath.start_with?(server_folder) subclass_paths << cpath loader.do_not_eager_load cpath end end unless ENV["BRIDGETOWN_ENV"] == "production" setup_autoload_listener loader, server_folder, subclass_paths end end Bridgetown::Hooks.register_one( :loader, :post_reload, reloadable: false ) do |loader, load_path| next unless load_path == server_folder loader.eager_load Bridgetown.touch_live_reload_file(reload_file_path) end end |
.setup_autoload_listener(loader, server_folder, subclass_paths) ⇒ Object
Creates a listener to detect file changes within the server folder and notify Zeitwerk
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'bridgetown-core/lib/bridgetown-core/rack/loader_hooks.rb', line 60 def self.setup_autoload_listener(loader, server_folder, subclass_paths) Listen.to(server_folder) do |modified, added, removed| c = modified + added + removed n = c.length unless n == 1 && subclass_paths.include?(c.first) Bridgetown.logger.info( "Reloading…", "#{n} file#{"s" if n > 1} changed at #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}" ) c.each do |path| Bridgetown.logger.info "", "- #{path["#{File.dirname(server_folder)}/".length..]}" end end loader.reload Bridgetown::Hooks.trigger :loader, :post_reload, loader, server_folder rescue SyntaxError => e Bridgetown::Errors.print_build_error(e) end.start end |