-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathscript_helper.rb
83 lines (71 loc) · 2.09 KB
/
script_helper.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
80
81
82
83
# frozen_string_literal: true
# rubocop:disable Rails/HelperInstanceVariable
module ScriptHelper
def javascript_packs_tag_once(*names, url_params: nil, **attributes)
@scripts = @scripts.to_h.merge(names.index_with([url_params, attributes]))
nil
end
alias_method :enqueue_component_scripts, :javascript_packs_tag_once
def render_javascript_pack_once_tags(...)
capture do
javascript_packs_tag_once(...)
return if @scripts.blank?
concat javascript_assets_tag
crossorigin = local_crossorigin_sources?.presence
@scripts.each do |name, (url_params, attributes)|
asset_sources.get_sources(name).each do |source|
integrity = asset_sources.get_integrity(source)
if attributes.delete(:preload_links_header) != false
AssetPreloadLinker.append(
headers: response.headers,
as: :script,
url: source,
crossorigin:,
integrity:,
)
end
concat tag.script(
src: UriService.add_params(source, url_params),
**attributes,
crossorigin:,
integrity:,
)
end
end
end
end
private
SAME_ORIGIN_ASSETS = %w[
sprite.svg
].to_set.freeze
def asset_sources
Rails.application.config.asset_sources
end
def local_crossorigin_sources?
Rails.env.development? && ENV['WEBPACK_PORT'].present?
end
def javascript_assets_tag
assets = asset_sources.get_assets(*@scripts.keys)
if assets.present?
asset_map = assets.index_with { |path| asset_path(path, host: asset_host(path)) }
content_tag(
:script,
asset_map.to_json,
{ type: 'application/json', data: { asset_map: '' } },
false,
)
end
end
def asset_host(path)
if IdentityConfig.store.asset_host.present?
if SAME_ORIGIN_ASSETS.include?(path)
IdentityConfig.store.domain_name
else
IdentityConfig.store.asset_host
end
elsif request
request.base_url
end
end
end
# rubocop:enable Rails/HelperInstanceVariable