lib/sewing_kit/webpack/helper.rb in sewing_kit-0.8.0 vs lib/sewing_kit/webpack/helper.rb in sewing_kit-0.27.0

- old
+ new

@@ -9,84 +9,74 @@ include ActionView::Helpers class UnknownJavaScriptAssetError < StandardError end - def sewing_kit_asset_paths(entrypoint_name, extension: 'js') + Asset = Struct.new(:path, :integrity) + + def sewing_kit_assets(entrypoint_name, extension: 'js') return '' unless entrypoint_name.present? - paths = SewingKit::Webpack::Manifest.asset_paths(entrypoint_name) - return '' unless paths && paths[extension] + assets = SewingKit::Webpack::Manifest.asset_dependencies(entrypoint_name) + return [] unless assets && assets[extension] - entry_paths = paths[extension] - entry_paths.unshift('/webpack/assets/dll/vendor.js') if serve_development_assets? && extension == 'js' - entry_paths + dependencies = assets[extension] + dependencies.unshift(dll_asset) if serve_development_assets? && extension == 'js' + dependencies.map do |raw_asset| + Asset.new(raw_asset['path'], raw_asset['integrity']) + end end - def sewing_kit_link_tag(*paths) - options = paths.extract_options! + def sewing_kit_link_tag(*assets) + options = assets.extract_options! - tags = paths.uniq.map do |path| - next '' if path == '' - create_asset_tag(:link, path, options) + tags = assets.uniq.map do |asset| + next '' if asset.path == '' + create_asset_tag(:link, asset, options) end safe_join(tags, "\n") end - def sewing_kit_script_tag(*paths) - options = paths.extract_options! + def sewing_kit_script_tag(*assets) + options = assets.extract_options! + tags = assets.map do |asset| + next '' if asset.path == '' - tags = paths.uniq.map do |path| - next '' if path == '' - - create_asset_tag(:script, path, options) + create_asset_tag(:script, asset, options) end safe_join(tags, "\n") end private - def create_asset_tag(tag_type, path, tag_options) + def create_asset_tag(tag_type, asset, tag_options) raise ArgumentError, "Invalid tag type: #{tag_type}" unless [:script, :link].include? tag_type options = tag_options.clone - if tag_options[:integrity] - file_hash = extract_hash(path) - options[:integrity] = maybe_integrity(file_hash) + if tag_options[:integrity] && asset.integrity + options[:integrity] = asset.integrity + else + options.delete(:integrity) end case tag_type when :script - content_tag(:script, '', options.reverse_merge(src: path)) + content_tag(:script, '', options.reverse_merge(src: asset.path)) when :link - tag(:link, options.reverse_merge(href: path, rel: 'stylesheet')) + tag(:link, options.reverse_merge(href: asset.path, rel: 'stylesheet')) end end - def maybe_integrity(file_hash) - return nil unless file_hash - encode_hash(file_hash) - end - - def encode_hash(hash) - # sewing-kit's default config places a sha256 hash in the filename. Browsers expect integrity - # to be the Base64 encoded version of the binary hash prefixed with the hashing algorithm - binary_hash = Array(hash).pack('H*') - "sha256-#{Base64.strict_encode64(binary_hash)}" - end - - def extract_hash(path) - # Consumes sewing-kit's [name]-[chunkhash].[ext] format - return unless path =~ /.*-[A-Za-z0-9]{64}\.(js|css)/ - path.rpartition('-').last.split('.').first - end - def serve_development_assets? return false if ENV['SK_SIMULATE_PRODUCTION'] == '1' Rails.env.development? + end + + def dll_asset + Asset.new('/webpack/assets/dll/vendor.js') end end end end