class ViteRuby::Config

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

Constants

CONFIGURABLE_WITH_ENV

Internal: Configuration options that can be provided as env vars.

DEFAULT_CONFIG

Internal: Shared configuration with the Vite plugin for Ruby.

DEFAULT_WATCHED_PATHS

Internal: If any of these files is modified the build won't be skipped.

NOT_CONFIGURABLE_WITH_ENV

Internal: Configuration options that can not be provided as env vars.

SNAKE_CASE

Internal: Converts camelCase to snake_case.

Private Class Methods

config_defaults(asset_host: nil, mode: ENV.fetch('RACK_ENV', 'development'), root: Dir.pwd) click to toggle source

Internal: Default values for a Ruby application.

# File lib/vite_ruby/config.rb, line 112
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
config_from_env() click to toggle source

Internal: Extracts the configuration options provided as env vars.

# File lib/vite_ruby/config.rb, line 136
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
config_from_file(path, mode:) click to toggle source

Internal: Loads the configuration options provided in a JSON file.

# File lib/vite_ruby/config.rb, line 145
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
  $stderr << "Check that your vite.json configuration file is available in the load path:\n\n\t#{ error.message }\n\n"
  {}
end
load_json(path) click to toggle source

Internal: Used to load a JSON file from the specified path.

# File lib/vite_ruby/config.rb, line 122
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
new(attrs) click to toggle source
# File lib/vite_ruby/config.rb, line 84
def initialize(attrs)
  @config = attrs.tap { |config| coerce_values(config) }.freeze
  ViteRuby::CompatibilityCheck.verify_plugin_version(root) unless skip_compatibility_check
end
option_from_env(name) click to toggle source

Internal: Retrieves a configuration option from environment variables.

# File lib/vite_ruby/config.rb, line 131
def option_from_env(name)
  ViteRuby.env["#{ ViteRuby::ENV_PREFIX }_#{ name.upcase }"]
end
resolve_config(**attrs) click to toggle source

Public: Returns the project configuration for Vite.

# File lib/vite_ruby/config.rb, line 93
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

Public Instance Methods

assets_manifest_path() click to toggle source

Internal: Path where vite-plugin-ruby outputs the assets manifest file.

# File lib/vite_ruby/config.rb, line 22
def assets_manifest_path
  build_output_dir.join('manifest-assets.json')
end
build_output_dir() click to toggle source

Public: The directory where Vite will store the built assets.

# File lib/vite_ruby/config.rb, line 27
def build_output_dir
  root.join(public_dir, public_output_dir)
end
host_with_port() click to toggle source
# File lib/vite_ruby/config.rb, line 12
def host_with_port
  "#{ host }:#{ port }"
end
manifest_path() click to toggle source

Internal: Path where Vite outputs the manifest file.

# File lib/vite_ruby/config.rb, line 17
def manifest_path
  build_output_dir.join('manifest.json')
end
protocol() click to toggle source
# File lib/vite_ruby/config.rb, line 8
def protocol
  https ? 'https' : 'http'
end
resolved_entrypoints_dir() click to toggle source

Public: The directory where the entries are located.

# File lib/vite_ruby/config.rb, line 32
def resolved_entrypoints_dir
  vite_root_dir.join(entrypoints_dir)
end
to_env() click to toggle source

Public: Sets additional environment variables for vite-plugin-ruby.

# File lib/vite_ruby/config.rb, line 47
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
vite_cache_dir() click to toggle source

Internal: The directory where Vite stores its processing cache.

# File lib/vite_ruby/config.rb, line 37
def vite_cache_dir
  root.join('node_modules/.vite')
end
vite_root_dir() click to toggle source

Public: The directory that Vite uses as root.

# File lib/vite_ruby/config.rb, line 42
def vite_root_dir
  root.join(source_code_dir)
end
watched_paths() click to toggle source

Internal: Files and directories that should be watched for changes.

# File lib/vite_ruby/config.rb, line 56
def watched_paths
  [
    *(watch_additional_paths + additional_entrypoints).reject { |dir|
      dir.start_with?('~/') || dir.start_with?(source_code_dir)
    },
    "#{ source_code_dir }/**/*",
    config_path,
    *DEFAULT_WATCHED_PATHS,
  ].freeze
end

Private Instance Methods

coerce_booleans(config, *names) click to toggle source

Internal: Coerces configuration options to boolean.

# File lib/vite_ruby/config.rb, line 80
def coerce_booleans(config, *names)
  names.each { |name| config[name] = [true, 'true'].include?(config[name]) }
end
coerce_values(config) click to toggle source

Internal: Coerces all the configuration values, in case they were passed as environment variables which are always strings.

# File lib/vite_ruby/config.rb, line 71
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', 'skip_compatibility_check')
end