2017-04-12 15:05:23 -04:00
|
|
|
require 'webpack/rails/manifest'
|
|
|
|
|
|
|
|
module WebpackHelper
|
2018-01-19 04:38:34 -05:00
|
|
|
def webpack_bundle_tag(bundle, force_same_domain: false)
|
2018-01-25 15:40:48 -05:00
|
|
|
javascript_include_tag(*gitlab_webpack_asset_paths(bundle, force_same_domain: force_same_domain))
|
2017-04-12 15:05:23 -04:00
|
|
|
end
|
|
|
|
|
2018-02-01 17:01:36 -05:00
|
|
|
def webpack_controller_bundle_tags
|
|
|
|
bundles = []
|
|
|
|
|
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
|
|
|
|
|
|
|
|
until route.empty?
|
2018-02-01 17:01:36 -05:00
|
|
|
begin
|
2018-02-02 19:14:24 -05:00
|
|
|
asset_paths = gitlab_webpack_asset_paths("pages.#{route.join('.')}", extension: 'js')
|
2018-02-01 17:01:36 -05:00
|
|
|
bundles.unshift(*asset_paths)
|
|
|
|
rescue Webpack::Rails::Manifest::EntryPointMissingError
|
|
|
|
# no bundle exists for this path
|
|
|
|
end
|
|
|
|
|
2018-02-02 19:14:24 -05:00
|
|
|
route.pop
|
2018-02-01 17:01:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
javascript_include_tag(*bundles)
|
|
|
|
end
|
|
|
|
|
2017-04-12 15:05:23 -04:00
|
|
|
# override webpack-rails gem helper until changes can make it upstream
|
2018-01-19 04:38:34 -05:00
|
|
|
def gitlab_webpack_asset_paths(source, extension: nil, force_same_domain: false)
|
2017-04-12 15:05:23 -04:00
|
|
|
return "" unless source.present?
|
|
|
|
|
|
|
|
paths = Webpack::Rails::Manifest.asset_paths(source)
|
|
|
|
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-01-19 04:38:34 -05:00
|
|
|
unless force_same_domain
|
|
|
|
force_host = webpack_public_host
|
|
|
|
if force_host
|
|
|
|
paths.map! { |p| "#{force_host}#{p}" }
|
|
|
|
end
|
2017-06-28 22:57:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
paths
|
|
|
|
end
|
|
|
|
|
|
|
|
def webpack_public_host
|
2017-04-12 15:05:23 -04:00
|
|
|
if Rails.env.test? && Rails.configuration.webpack.dev_server.enabled
|
|
|
|
host = Rails.configuration.webpack.dev_server.host
|
|
|
|
port = Rails.configuration.webpack.dev_server.port
|
|
|
|
protocol = Rails.configuration.webpack.dev_server.https ? 'https' : 'http'
|
2017-06-28 22:57:35 -04:00
|
|
|
"#{protocol}://#{host}:#{port}"
|
|
|
|
else
|
|
|
|
ActionController::Base.asset_host.try(:chomp, '/')
|
2017-04-12 15:05:23 -04:00
|
|
|
end
|
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
|
2017-07-28 13:49:07 -04:00
|
|
|
relative_path = Rails.application.config.relative_url_root
|
|
|
|
webpack_path = Rails.application.config.webpack.public_path
|
|
|
|
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
|