Skip to content

Commit e118e8c

Browse files
committed
now working with zeitwerk loader
1 parent 6f7ff47 commit e118e8c

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

ruby/rails-hyperstack/lib/hyperstack/server_side_auto_require.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
Rails.configuration.autoloader = :classic #:zeitwerk
1+
# require "hyperstack/server_side_auto_require.rb" in your hyperstack initializer
2+
# to autoload shadowed server side files that match files
3+
# in the hyperstack directory
4+
5+
if Rails.configuration.try(:autoloader) == :zeitwerk
6+
Rails.autoloaders.each do |loader|
7+
loader.on_load do |_cpath, _value, abspath|
8+
ActiveSupport::Dependencies.add_server_side_dependency(abspath) do |load_path|
9+
loader.send(:log, "Hyperstack loading server side shadowed file: #{load_path}") if loader&.logger
10+
require("#{load_path}.rb")
11+
end
12+
end
13+
end
14+
end
215

316
module ActiveSupport
417
module Dependencies
@@ -11,7 +24,7 @@ class << self
1124
# and add that as a dependency
1225

1326
def require_or_load(file_name, const_path = nil)
14-
add_server_side_dependency(file_name)
27+
add_server_side_dependency(file_name) { |load_path| require_dependency load_path }
1528
original_require_or_load(file_name, const_path)
1629
end
1730

@@ -20,7 +33,7 @@ def require_or_load(file_name, const_path = nil)
2033
# the filename, and if a ruby file exists at that location then
2134
# add it as a dependency
2235

23-
def add_server_side_dependency(file_name)
36+
def add_server_side_dependency(file_name, loader = nil)
2437
path = File.expand_path(file_name.chomp(".rb"))
2538
.split(File::SEPARATOR).reverse
2639
hs_index = path.find_index(HYPERSTACK_DIR)
@@ -32,7 +45,7 @@ def add_server_side_dependency(file_name)
3245

3346
return unless File.exist? "#{load_path}.rb"
3447

35-
require_dependency load_path
48+
yield load_path
3649
end
3750
end
3851
end

0 commit comments

Comments
 (0)