Class: Bridgetown::Utils::LoadersManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown-core/utils/loaders_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ LoadersManager

Returns a new instance of LoadersManager.

Parameters:



12
13
14
15
16
17
18
# File 'lib/bridgetown-core/utils/loaders_manager.rb', line 12

def initialize(config)
  @config = config
  @loaders = {}
  @root_dir = config.root_dir

  FileUtils.rm_f(Bridgetown.build_errors_path)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/bridgetown-core/utils/loaders_manager.rb', line 6

def config
  @config
end

#loadersObject (readonly)

Returns the value of attribute loaders.



8
9
10
# File 'lib/bridgetown-core/utils/loaders_manager.rb', line 8

def loaders
  @loaders
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



8
9
10
# File 'lib/bridgetown-core/utils/loaders_manager.rb', line 8

def root_dir
  @root_dir
end

Instance Method Details

#reload_loadersObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bridgetown-core/utils/loaders_manager.rb', line 64

def reload_loaders
  FileUtils.rm_f(Bridgetown.build_errors_path)

  @loaders.each do |load_path, loader|
    next unless reloading_enabled?(load_path)

    Bridgetown::Hooks.trigger :loader, :pre_reload, loader, load_path
    loader.reload
    loader.eager_load if config.eager_load_paths.include?(load_path)
    Bridgetown::Hooks.trigger :loader, :post_reload, loader, load_path
  end
end

#reloading_enabled?(load_path) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/bridgetown-core/utils/loaders_manager.rb', line 27

def reloading_enabled?(load_path)
  load_path.start_with?(root_dir) && ENV["BRIDGETOWN_ENV"] != "production"
end

#setup_loaders(autoload_paths = []) ⇒ Object

rubocop:todo Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bridgetown-core/utils/loaders_manager.rb', line 31

def setup_loaders(autoload_paths = []) # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  (autoload_paths.presence || config.autoload_paths).each do |load_path|
    if @loaders.key?(load_path)
      raise "Zeitwerk loader already added for `#{load_path}'. Please check your config"
    end

    next unless Dir.exist? load_path

    loader = Zeitwerk::Loader.new
    loader.inflector = config.inflector if config.inflector
    begin
      loader.push_dir(load_path)
    rescue Zeitwerk::Error
      next
    end
    loader.enable_reloading if reloading_enabled?(load_path)
    loader.ignore(File.join(load_path, "**", "*.js.rb"))
    loader.ignore(
      File.join(File.expand_path(config[:islands_dir], config[:source]), "**", "routes")
    )
    config.autoloader_collapsed_paths.each do |collapsed_path|
      next unless collapsed_path.starts_with?(load_path)

      loader.collapse(collapsed_path)
    end
    Bridgetown::Hooks.trigger :loader, :pre_setup, loader, load_path
    loader.setup
    loader.eager_load if config.eager_load_paths.include?(load_path)
    Bridgetown::Hooks.trigger :loader, :post_setup, loader, load_path
    @loaders[load_path] = loader
  end
end

#unload_loadersObject



20
21
22
23
24
25
# File 'lib/bridgetown-core/utils/loaders_manager.rb', line 20

def unload_loaders
  return if @loaders.keys.empty?

  @loaders.each_value(&:unload)
  @loaders = {}
end