# 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_bundle_name(_user_agent) "dev" end def asset_dependencies(entrypoint_name, _user_agent) 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 = load_metadata_from_node metadata = normalize_metadata(result) @metadata_path = metadata && metadata["path"] metadata end private def normalize_metadata(maybe_array_metadata) if maybe_array_metadata.is_a?(Array) maybe_array_metadata.last else maybe_array_metadata end end def load_metadata_from_node begin stdout, stderr, status = Open3.capture3( "node_modules/.bin/sewing-kit", "manifest", "--mode=development" ) rescue => e raise NodeSewingKitNotRunnable.new("development", e) end raise NodeSewingKitManifestMissing, stderr unless status.success? parse_metadata(stdout) 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