2018-11-19 21:01:13 -05:00
# frozen_string_literal: true
2018-04-26 16:12:39 -04:00
module Gitlab
module Webpack
2020-09-23 11:10:14 -04:00
class Manifest
# Raised if we can't read our webpack manifest for whatever reason
class ManifestLoadError < StandardError
def initialize ( message , orig )
2020-10-07 02:09:03 -04:00
super " #{ message } \n \n (original error #{ orig . class . name } : #{ orig } ) "
2020-09-23 11:10:14 -04:00
end
end
# Raised if webpack couldn't build one of your entry points
class WebpackError < StandardError
def initialize ( errors )
super " Error in webpack compile, details follow below: \n #{ errors . join ( " \n \n " ) } "
end
end
# Raised if a supplied entry point does not exist in the webpack manifest
2018-04-26 16:12:39 -04:00
AssetMissingError = Class . new ( StandardError )
class << self
2020-09-23 23:09:48 -04:00
include Gitlab :: Utils :: StrongMemoize
2018-04-26 16:12:39 -04:00
def entrypoint_paths ( source )
2020-09-23 11:10:14 -04:00
raise WebpackError , manifest [ " errors " ] unless manifest_bundled?
2018-04-26 16:12:39 -04:00
2019-12-17 04:07:48 -05:00
dll_assets = manifest . fetch ( " dllAssets " , [ ] )
2018-04-26 16:12:39 -04:00
entrypoint = manifest [ " entrypoints " ] [ source ]
if entrypoint && entrypoint [ " assets " ]
# Can be either a string or an array of strings.
# Do not include source maps as they are not javascript
2019-12-17 04:07:48 -05:00
[ dll_assets , entrypoint [ " assets " ] ] . flatten . reject { | p | p =~ / .* \ .map$ / } . map do | p |
2020-10-07 02:09:03 -04:00
" / #{ Gitlab . config . webpack . public_path } / #{ p } "
2018-04-26 16:12:39 -04:00
end
2020-09-23 11:10:14 -04:00
else
raise AssetMissingError , " Can't find asset ' #{ source } ' in webpack manifest "
end
end
def asset_paths ( source )
raise WebpackError , manifest [ " errors " ] unless manifest_bundled?
paths = manifest [ " assetsByChunkName " ] [ source ]
if paths
# Can be either a string or an array of strings.
# Do not include source maps as they are not javascript
[ paths ] . flatten . reject { | p | p =~ / .* \ .map$ / } . map do | p |
2020-10-07 02:09:03 -04:00
" / #{ Gitlab . config . webpack . public_path } / #{ p } "
2020-09-23 11:10:14 -04:00
end
2018-04-26 16:12:39 -04:00
else
raise AssetMissingError , " Can't find entry point ' #{ source } ' in webpack manifest "
end
end
2020-09-23 11:10:14 -04:00
2020-09-23 23:09:48 -04:00
def clear_manifest!
clear_memoization ( :manifest )
end
2020-09-23 11:10:14 -04:00
private
def manifest_bundled?
! manifest [ " errors " ] . any? { | error | error . include? " Module build failed " }
end
def manifest
2020-10-07 02:09:03 -04:00
if Gitlab . config . webpack . dev_server . enabled
2021-01-07 19:10:44 -05:00
# Only cache at request level if we're in dev server mode, manifest may change ...
Gitlab :: SafeRequestStore . fetch ( 'manifest.json' ) { load_manifest }
2020-09-23 11:10:14 -04:00
else
# ... otherwise cache at class level, as JSON loading/parsing can be expensive
2020-09-23 23:09:48 -04:00
strong_memoize ( :manifest ) { load_manifest }
2020-09-23 11:10:14 -04:00
end
end
def load_manifest
2021-11-08 10:13:35 -05:00
data = Gitlab :: Webpack :: FileLoader . load ( Gitlab . config . webpack . manifest_filename )
2020-09-23 11:10:14 -04:00
Gitlab :: Json . parse ( data )
2021-11-08 10:13:35 -05:00
rescue Gitlab :: Webpack :: FileLoader :: StaticLoadError = > e
raise ManifestLoadError . new ( " Could not load compiled manifest from #{ e . uri } . \n \n Have you run `rake gitlab:assets:compile`? " , e . original_error )
rescue Gitlab :: Webpack :: FileLoader :: DevServerSSLError = > e
2020-10-07 02:09:03 -04:00
ssl_status = Gitlab . config . webpack . dev_server . https ? ' over SSL' : ''
2021-11-08 10:13:35 -05:00
raise ManifestLoadError . new ( " Could not connect to webpack-dev-server at #{ e . uri } #{ ssl_status } . \n \n Is SSL enabled? Check that settings in `gitlab.yml` and webpack-dev-server match. " , e . original_error )
rescue Gitlab :: Webpack :: FileLoader :: DevServerLoadError = > e
raise ManifestLoadError . new ( " Could not load manifest from webpack-dev-server at #{ e . uri } . \n \n Is webpack-dev-server running? Try running `gdk status webpack` or `gdk tail webpack`. " , e . original_error )
2020-09-23 11:10:14 -04:00
end
2018-04-26 16:12:39 -04:00
end
end
end
end