debian-mirror-gitlab/app/helpers/webpack_helper.rb

96 lines
2.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module WebpackHelper
2021-06-08 01:23:25 +05:30
def prefetch_link_tag(source)
href = asset_path(source)
link_tag = tag.link(rel: 'prefetch', href: href)
early_hints_link = "<#{href}>; rel=prefetch"
request.send_early_hints("Link" => early_hints_link)
link_tag
end
2018-10-15 14:42:47 +05:30
def webpack_bundle_tag(bundle)
javascript_include_tag(*webpack_entrypoint_paths(bundle))
2018-03-17 18:26:18 +05:30
end
2021-06-08 01:23:25 +05:30
def webpack_preload_asset_tag(asset, options = {})
path = Gitlab::Webpack::Manifest.asset_paths(asset).first
if options.delete(:prefetch)
prefetch_link_tag(path)
else
preload_link_tag(path, options)
end
2022-03-02 08:16:31 +05:30
rescue Gitlab::Webpack::Manifest::AssetMissingError
# In development/test, incremental compilation may be enabled, meaning not
# all chunks may be available/split out
raise unless Gitlab.dev_or_test_env?
2021-06-08 01:23:25 +05:30
end
2018-03-17 18:26:18 +05:30
def webpack_controller_bundle_tags
2018-10-15 14:42:47 +05:30
chunks = []
2018-03-17 18:26:18 +05:30
2018-03-27 19:54:05 +05:30
action = case controller.action_name
when 'create' then 'new'
when 'update' then 'edit'
else controller.action_name
end
route = [*controller.controller_path.split('/'), action].compact
2018-10-15 14:42:47 +05:30
until chunks.any? || route.empty?
entrypoint = "pages.#{route.join('.')}"
2018-03-17 18:26:18 +05:30
begin
2018-10-15 14:42:47 +05:30
chunks = webpack_entrypoint_paths(entrypoint, extension: 'js')
rescue Gitlab::Webpack::Manifest::AssetMissingError
2018-03-17 18:26:18 +05:30
# no bundle exists for this path
end
2018-03-27 19:54:05 +05:30
route.pop
2018-03-17 18:26:18 +05:30
end
2018-10-15 14:42:47 +05:30
if chunks.empty?
chunks = webpack_entrypoint_paths("default", extension: 'js')
end
javascript_include_tag(*chunks)
2017-08-17 22:00:37 +05:30
end
2018-10-15 14:42:47 +05:30
def webpack_entrypoint_paths(source, extension: nil, exclude_duplicates: true)
2017-08-17 22:00:37 +05:30
return "" unless source.present?
2018-10-15 14:42:47 +05:30
paths = Gitlab::Webpack::Manifest.entrypoint_paths(source)
2017-08-17 22:00:37 +05:30
if extension
2017-09-10 17:25:29 +05:30
paths.select! { |p| p.ends_with? ".#{extension}" }
2017-08-17 22:00:37 +05:30
end
2018-10-15 14:42:47 +05:30
force_host = webpack_public_host
if force_host
paths.map! { |p| "#{force_host}#{p}" }
2017-09-10 17:25:29 +05:30
end
2018-10-15 14:42:47 +05:30
if exclude_duplicates
@used_paths ||= []
new_paths = paths - @used_paths
@used_paths += new_paths
new_paths
else
paths
end
2017-09-10 17:25:29 +05:30
end
def webpack_public_host
2022-08-27 11:52:29 +05:30
# We proxy webpack output in 'test' and 'dev' environment, so we can just use asset_host
ActionController::Base.asset_host.try(:chomp, '/')
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def webpack_public_path
2021-01-03 14:25:43 +05:30
relative_path = Gitlab.config.gitlab.relative_url_root
webpack_path = Gitlab.config.webpack.public_path
2017-09-10 17:25:29 +05:30
File.join(webpack_public_host.to_s, relative_path.to_s, webpack_path.to_s, '')
2017-08-17 22:00:37 +05:30
end
end