2018-08-18 07:19:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 15:05:23 -04:00
|
|
|
module WebpackHelper
|
2021-05-14 08:10:58 -04:00
|
|
|
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-05-03 12:59:03 -04:00
|
|
|
def webpack_bundle_tag(bundle)
|
2018-05-07 12:19:54 -04:00
|
|
|
javascript_include_tag(*webpack_entrypoint_paths(bundle))
|
2017-04-12 15:05:23 -04:00
|
|
|
end
|
|
|
|
|
2021-05-11 08:10:20 -04:00
|
|
|
def webpack_preload_asset_tag(asset, options = {})
|
2021-05-14 08:10:58 -04:00
|
|
|
path = Gitlab::Webpack::Manifest.asset_paths(asset).first
|
|
|
|
|
|
|
|
if options.delete(:prefetch)
|
|
|
|
prefetch_link_tag(path)
|
|
|
|
else
|
|
|
|
preload_link_tag(path, options)
|
|
|
|
end
|
2022-01-15 04:13:58 -05:00
|
|
|
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-05-11 08:10:20 -04:00
|
|
|
end
|
|
|
|
|
2018-02-01 17:01:36 -05:00
|
|
|
def webpack_controller_bundle_tags
|
2018-04-27 14:00:00 -04:00
|
|
|
chunks = []
|
2018-02-01 17:01:36 -05:00
|
|
|
|
2018-02-02 19:14:24 -05:00
|
|
|
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-04-27 18:09:05 -04:00
|
|
|
until chunks.any? || route.empty?
|
|
|
|
entrypoint = "pages.#{route.join('.')}"
|
|
|
|
begin
|
2018-05-07 12:19:54 -04:00
|
|
|
chunks = webpack_entrypoint_paths(entrypoint, extension: 'js')
|
2018-04-27 18:09:05 -04:00
|
|
|
rescue Gitlab::Webpack::Manifest::AssetMissingError
|
|
|
|
# no bundle exists for this path
|
|
|
|
end
|
|
|
|
route.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
if chunks.empty?
|
2018-05-07 12:19:54 -04:00
|
|
|
chunks = webpack_entrypoint_paths("default", extension: 'js')
|
2018-02-01 17:01:36 -05:00
|
|
|
end
|
|
|
|
|
2018-04-27 14:00:00 -04:00
|
|
|
javascript_include_tag(*chunks)
|
2018-02-01 17:01:36 -05:00
|
|
|
end
|
|
|
|
|
2018-05-07 12:19:54 -04:00
|
|
|
def webpack_entrypoint_paths(source, extension: nil, exclude_duplicates: true)
|
2017-04-12 15:05:23 -04:00
|
|
|
return "" unless source.present?
|
|
|
|
|
2018-04-26 16:12:39 -04:00
|
|
|
paths = Gitlab::Webpack::Manifest.entrypoint_paths(source)
|
2017-04-12 15:05:23 -04:00
|
|
|
if extension
|
2017-06-28 22:57:35 -04:00
|
|
|
paths.select! { |p| p.ends_with? ".#{extension}" }
|
2017-04-12 15:05:23 -04:00
|
|
|
end
|
|
|
|
|
2018-05-03 12:59:03 -04:00
|
|
|
force_host = webpack_public_host
|
|
|
|
if force_host
|
|
|
|
paths.map! { |p| "#{force_host}#{p}" }
|
2017-06-28 22:57:35 -04:00
|
|
|
end
|
|
|
|
|
2018-05-03 16:13:35 -04:00
|
|
|
if exclude_duplicates
|
|
|
|
@used_paths ||= []
|
|
|
|
new_paths = paths - @used_paths
|
|
|
|
@used_paths += new_paths
|
|
|
|
new_paths
|
|
|
|
else
|
|
|
|
paths
|
|
|
|
end
|
2017-06-28 22:57:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def webpack_public_host
|
2022-07-21 23:08:32 -04:00
|
|
|
# We proxy webpack output in 'test' and 'dev' environment, so we can just use asset_host
|
|
|
|
ActionController::Base.asset_host.try(:chomp, '/')
|
2017-06-28 22:57:35 -04:00
|
|
|
end
|
2017-04-12 15:05:23 -04:00
|
|
|
|
2017-06-28 22:57:35 -04:00
|
|
|
def webpack_public_path
|
2020-10-07 02:09:03 -04:00
|
|
|
relative_path = Gitlab.config.gitlab.relative_url_root
|
|
|
|
webpack_path = Gitlab.config.webpack.public_path
|
2017-07-28 13:49:07 -04:00
|
|
|
File.join(webpack_public_host.to_s, relative_path.to_s, webpack_path.to_s, '')
|
2017-04-12 15:05:23 -04:00
|
|
|
end
|
|
|
|
end
|