-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathengine.rb
executable file
·79 lines (65 loc) · 2.75 KB
/
engine.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require "importmap/map"
# Use Rails.application.importmap to access the map
Rails::Application.send(:attr_accessor, :importmap)
Rails::Application.send(:attr_accessor, :importmaps)
module Importmap
class Engine < ::Rails::Engine
config.importmap = ActiveSupport::OrderedOptions.new
config.importmap.paths = []
config.importmap.sweep_cache = Rails.env.development? || Rails.env.test?
config.importmap.cache_sweepers = []
config.importmap.rescuable_asset_errors = []
config.autoload_once_paths = %W( #{root}/app/helpers )
initializer "importmap" do |app|
app.importmap = Importmap::Map.new
app.importmaps = ActiveSupport::OrderedOptions.new
app.config.importmap.paths << app.root.join("config/importmap.rb")
app.config.importmap.paths.each { |path| app.importmap.draw(path) }
Dir.glob(app.root.join("config", "importmaps", "*.rb")).each do |path|
app.importmaps[File.basename(path, ".rb")] = Importmap::Map.new.draw(path)
end
end
initializer "importmap.reloader" do |app|
unless app.config.cache_classes
Importmap::Reloader.new.tap do |reloader|
reloader.execute
app.reloaders << reloader
app.reloader.to_run { reloader.execute }
end
end
end
initializer "importmap.cache_sweeper" do |app|
if app.config.importmap.sweep_cache && !app.config.cache_classes
app.config.importmap.cache_sweepers << app.root.join("app/javascript")
app.config.importmap.cache_sweepers << app.root.join("vendor/javascript")
app.importmap.cache_sweeper(watches: app.config.importmap.cache_sweepers)
app.importmaps.each_value { |map| map.cache_sweeper(watches: app.config.importmap.cache_sweepers) }
ActiveSupport.on_load(:action_controller_base) do
before_action do
Rails.application.importmap.cache_sweeper.execute_if_updated
Rails.application.importmaps.each_value { |map| map.cache_sweeper.execute_if_updated }
end
end
end
end
initializer "importmap.assets" do |app|
if app.config.respond_to?(:assets)
app.config.assets.paths << Rails.root.join("app/javascript")
app.config.assets.paths << Rails.root.join("vendor/javascript")
end
end
initializer "importmap.helpers" do
ActiveSupport.on_load(:action_controller_base) do
helper Importmap::ImportmapTagsHelper
end
end
initializer "importmap.rescuable_asset_errors" do |app|
if defined?(Propshaft)
app.config.importmap.rescuable_asset_errors << Propshaft::MissingAssetError
end
if defined?(Sprockets::Rails)
app.config.importmap.rescuable_asset_errors << Sprockets::Rails::Helper::AssetNotFound
end
end
end
end