# frozen_string_literal: true require 'sewing_kit/webpack/manifest/base' module SewingKit module Webpack class Manifest class Development < Base def initialize super @metadata_path = nil end def asset_dependencies(entrypoint_name, _target) current_metadata = metadata if current_metadata.key?('development') current_metadata['development']['hangTight'] else current_metadata['entrypoints'][entrypoint_name] end end def clear_cache! super @metadata_path = nil end def manifest raise OnlyUseInProductionError end def metadata load_metadata end def load_metadata begin if @metadata_path result = load_metadata_from_fs(@metadata_path) return normalize_metadata(result) end rescue => e Rails.logger.warn "[sewing_kit] could not read manifest from #{@metadata_path}; falling back to node. #{e}" @metadata_path = nil end result = SewingKit::Webpack::Manifest::Base.load_metadata_from_node metadata = normalize_metadata(result) @metadata_path = metadata && metadata['path'] metadata end def load_metadata_from_fs(path) begin json_str = File.read(path) rescue => e raise ManifestLoadError.new(path, e) end begin JSON.parse(json_str) rescue => e raise ManifestParseError, e end end protected def normalize_metadata(maybe_array_metadata) if maybe_array_metadata.is_a?(Array) maybe_array_metadata.last else maybe_array_metadata end end def mode 'development' end end class OnlyUseInProductionError < StandardError def initialize super "The SewingKit::Webpack::Manifest#manifest is intended "\ "for deploying assets to a CDN. Do not use it in development." end end end end end