class ViteRuby::Config

environment variables, combining them with the default options.
Public: Allows to resolve configuration sourced from ‘config/vite.json` and

def assets_manifest_path

Internal: Path where vite-plugin-ruby outputs the assets manifest file.
def assets_manifest_path
  build_output_dir.join('manifest-assets.json')
end

def build_output_dir

Public: The directory where Vite will store the built assets.
def build_output_dir
  root.join(public_dir, public_output_dir)
end

def coerce_booleans(config, *names)

Internal: Coerces configuration options to boolean.
def coerce_booleans(config, *names)
  names.each { |name| config[name] = [true, 'true'].include?(config[name]) }
end

def coerce_values(config)

as environment variables which are always strings.
Internal: Coerces all the configuration values, in case they were passed
def coerce_values(config)
  config['mode'] = config['mode'].to_s
  config['port'] = config['port'].to_i
  config['root'] = Pathname.new(config['root'])
  config['build_cache_dir'] = config['root'].join(config['build_cache_dir'])
  coerce_booleans(config, 'auto_build', 'hide_build_console_output', 'https')
end

def config_defaults(asset_host: nil, mode: ENV.fetch('RACK_ENV', 'development'), root: Dir.pwd)

Internal: Default values for a Ruby application.
def config_defaults(asset_host: nil, mode: ENV.fetch('RACK_ENV', 'development'), root: Dir.pwd)
  {
    'asset_host' => option_from_env('asset_host') || asset_host,
    'config_path' => option_from_env('config_path') || DEFAULT_CONFIG.fetch('config_path'),
    'mode' => option_from_env('mode') || mode,
    'root' => option_from_env('root') || root,
  }
end

def config_from_env

Internal: Extracts the configuration options provided as env vars.
def config_from_env
  CONFIGURABLE_WITH_ENV.each_with_object({}) do |option, env_vars|
    if value = option_from_env(option)
      env_vars[option] = value
    end
  end
end

def config_from_file(path, mode:)

Internal: Loads the configuration options provided in a JSON file.
def config_from_file(path, mode:)
  multi_env_config = load_json(path)
  multi_env_config.fetch('all', {})
    .merge(multi_env_config.fetch(mode, {}))
rescue Errno::ENOENT => error
  warn "Check that your vite.json configuration file is available in the load path:\n\n\t#{ error.message }\n\n"
  {}
end

def host_with_port

def host_with_port
  "#{ host }:#{ port }"
end

def initialize(attrs)

def initialize(attrs)
  @config = attrs.tap { |config| coerce_values(config) }.freeze
end

def load_json(path)

Internal: Used to load a JSON file from the specified path.
def load_json(path)
  JSON.parse(File.read(File.expand_path(path))).each do |_env, config|
    config.transform_keys!(&SNAKE_CASE) if config.is_a?(Hash)
  end.tap do |config|
    config.transform_keys!(&SNAKE_CASE)
  end
end

def manifest_path

Internal: Path where Vite outputs the manifest file.
def manifest_path
  build_output_dir.join('manifest.json')
end

def option_from_env(name)

Internal: Retrieves a configuration option from environment variables.
def option_from_env(name)
  ViteRuby.env["#{ ViteRuby::ENV_PREFIX }_#{ name.upcase }"]
end

def protocol

def protocol
  https ? 'https' : 'http'
end

def resolve_config(**attrs)

Public: Returns the project configuration for Vite.
def resolve_config(**attrs)
  config = config_defaults.merge(attrs.transform_keys(&:to_s))
  file_path = File.join(config['root'], config['config_path'])
  file_config = config_from_file(file_path, mode: config['mode'])
  new DEFAULT_CONFIG.merge(file_config).merge(config_from_env).merge(config)
end

def resolved_entrypoints_dir

Public: The directory where the entries are located.
def resolved_entrypoints_dir
  root.join(source_code_dir, entrypoints_dir)
end

def to_env

Public: Sets additional environment variables for vite-plugin-ruby.
def to_env
  CONFIGURABLE_WITH_ENV.each_with_object({}) do |option, env|
    unless (value = @config[option]).nil?
      env["#{ ViteRuby::ENV_PREFIX }_#{ option.upcase }"] = value.to_s
    end
  end.merge(ViteRuby.env)
end

def vite_cache_dir

Internal: The directory where Vite stores its processing cache.
def vite_cache_dir
  root.join('node_modules/.vite')
end