Class: ViteRuby::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vite_ruby/config.rb

Overview

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

Constant Summary collapse

DEV_SERVER_META_FILENAME =

Internal: Name of the metadata file written by the Vite dev server.

"vite-ruby.json"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resolve_config(**attrs) ⇒ Object

Public: Returns the project configuration for Vite.



135
136
137
138
139
140
# File 'lib/vite_ruby/config.rb', line 135

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

Instance Method Details

#build_output_dirObject

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



40
41
42
# File 'lib/vite_ruby/config.rb', line 40

def build_output_dir
  root.join(public_dir, public_output_dir)
end

#dev_server_meta_pathObject

Internal: Path to the metadata file written by the Vite dev server.



45
46
47
# File 'lib/vite_ruby/config.rb', line 45

def dev_server_meta_path
  root.join("tmp", DEV_SERVER_META_FILENAME)
end

#host_with_portObject



19
20
21
# File 'lib/vite_ruby/config.rb', line 19

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

#known_manifest_pathsObject

Internal: Path to the manifest files generated by Vite and vite-plugin-ruby.



24
25
26
27
28
29
30
31
32
# File 'lib/vite_ruby/config.rb', line 24

def known_manifest_paths
  [
    # NOTE: Generated by Vite when `manifest: true`, which vite-plugin-ruby enables.
    build_output_dir.join(".vite/manifest.json"),

    # NOTE: Path where vite-plugin-ruby outputs the assets manifest file.
    build_output_dir.join(".vite/manifest-assets.json"),
  ]
end

#load_ruby_configObject

Public: Loads an optional config/vite.rb file that can modify ViteRuby.env



65
66
67
68
# File 'lib/vite_ruby/config.rb', line 65

def load_ruby_config
  rb_config_path = File.expand_path(config_path.sub(/.json$/, ".rb"), root)
  load rb_config_path if File.exist?(rb_config_path)
end

#manifest_pathsObject

Internal: Path to the manifest files generated by Vite and vite-plugin-ruby.



35
36
37
# File 'lib/vite_ruby/config.rb', line 35

def manifest_paths
  known_manifest_paths.select(&:exist?)
end

#originObject



11
12
13
# File 'lib/vite_ruby/config.rb', line 11

def origin
  "#{protocol}://#{host_with_port}"
end

#protocolObject



15
16
17
# File 'lib/vite_ruby/config.rb', line 15

def protocol
  https ? "https" : "http"
end

#resolved_entrypoints_dirObject

Public: The directory where the entries are located.



50
51
52
# File 'lib/vite_ruby/config.rb', line 50

def resolved_entrypoints_dir
  vite_root_dir.join(entrypoints_dir)
end

#to_env(env_vars = ViteRuby.env) ⇒ Object

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



71
72
73
74
75
76
77
# File 'lib/vite_ruby/config.rb', line 71

def to_env(env_vars = ViteRuby.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(env_vars)
end

#vite_cache_dirObject

Internal: The directory where Vite stores its processing cache.



55
56
57
# File 'lib/vite_ruby/config.rb', line 55

def vite_cache_dir
  root.join("node_modules/.vite")
end

#vite_root_dirObject

Public: The directory that Vite uses as root.



60
61
62
# File 'lib/vite_ruby/config.rb', line 60

def vite_root_dir
  root.join(source_code_dir)
end

#watched_pathsObject

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



80
81
82
83
84
85
86
87
88
89
# File 'lib/vite_ruby/config.rb', line 80

def watched_paths
  [
    *(watch_additional_paths + additional_entrypoints).reject { |dir|
      dir.start_with?("~/") || dir.start_with?(source_code_dir)
    },
    "#{source_code_dir}/**/*",
    config_path.sub(/.json$/, ".{rb,json}"),
    *DEFAULT_WATCHED_PATHS,
  ].freeze
end

#within_root(&block) ⇒ Object

Internal: Changes the current directory to the root dir.



92
93
94
# File 'lib/vite_ruby/config.rb', line 92

def within_root(&block)
  Dir.chdir(File.expand_path(root), &block)
end