Class: ViteRuby

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vite_ruby.rb,
lib/vite_ruby/version.rb

Defined Under Namespace

Modules: CompatibilityCheck, IO Classes: Build, Builder, CLI, Commands, Config, DevServerProxy, Error, Manifest, MissingEntrypointError, MissingExecutableError, Runner

Constant Summary collapse

ENV_PREFIX =

Internal: Prefix used for environment variables that modify the configuration.

"VITE_RUBY"
COMPANION_LIBRARIES =

Internal: Companion libraries for Vite Ruby, and their target framework.

{
  "vite_rails" => "rails",
  "vite_hanami" => "hanami",
  "vite_padrino" => "padrino",
  "jekyll-vite" => "jekyll",
  "vite_rails_legacy" => "rails",
  "vite_plugin_legacy" => "rack",
}
VERSION =
"3.11.0"
DEFAULT_VITE_VERSION =

Internal: Versions used by default when running vite install.

"^8.0.0"
DEFAULT_PLUGIN_VERSION =
"^5.2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**config_options) ⇒ ViteRuby

Returns a new instance of ViteRuby.



71
72
73
# File 'lib/vite_ruby.rb', line 71

def initialize(**config_options)
  @config_options = config_options
end

Instance Attribute Details

#loggerObject



75
76
77
# File 'lib/vite_ruby.rb', line 75

def logger
  @logger ||= Logger.new($stdout)
end

Class Method Details

.bootstrapObject

Internal: Refreshes the manifest.



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

def bootstrap
  instance.manifest.refresh
end

.framework_librariesObject

Internal: Detects if the application has installed a framework-specific variant of Vite Ruby.



60
61
62
63
64
65
66
# File 'lib/vite_ruby.rb', line 60

def framework_libraries
  COMPANION_LIBRARIES.filter_map { |name, framework|
    if library = Gem.loaded_specs[name]
      [framework, library]
    end
  }
end

.install_tasksObject

Internal: Loads all available rake tasks.



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

def install_tasks
  load File.expand_path("tasks/vite.rake", __dir__)
end

.instanceObject



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

def instance
  @instance ||= new
end

.reload_with(**config_options) ⇒ Object

Internal: Creates a new instance with the specified options.



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

def reload_with(**config_options)
  @instance = new(**config_options)
end

Instance Method Details

#builderObject

Public: Keeps track of watched files and triggers builds as needed.



117
118
119
# File 'lib/vite_ruby.rb', line 117

def builder
  @builder ||= ViteRuby::Builder.new(self)
end

#commandsObject

Internal: Helper to run commands related with Vite.



122
123
124
# File 'lib/vite_ruby.rb', line 122

def commands
  @commands ||= ViteRuby::Commands.new(self)
end

#configObject

Public: Current instance configuration for Vite.



127
128
129
130
131
132
133
134
# File 'lib/vite_ruby.rb', line 127

def config
  unless defined?(@config)
    configure
    @config.load_ruby_config
  end

  @config
end

#configure(**options) ⇒ Object

Public: Allows overriding the configuration for this instance.



137
138
139
# File 'lib/vite_ruby.rb', line 137

def configure(**options)
  @config = ViteRuby::Config.resolve_config(**@config_options, **options)
end

#dev_mode?Boolean Also known as: run_proxy?

Public: Whether we are in an environment that might run the Vite dev server.

Returns:

  • (Boolean)


102
103
104
105
106
107
# File 'lib/vite_ruby.rb', line 102

def dev_mode?
  config.mode == "development" || (config.mode == "test" && !ENV["CI"])
rescue => error
  logger.error("Failed to check mode for Vite: #{error.message}")
  false
end

#dev_server_running?Boolean

Public: Returns true if the Vite development server is currently running.

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/vite_ruby.rb', line 86

def dev_server_running?
  return false unless dev_mode?
  return dev_server_connected? if config.dev_server_connection_check

  !dev_server_meta.nil?
end

#digestObject

Public: Returns a digest of all the watched files, allowing to detect changes. Useful to perform version checks in single-page applications.



81
82
83
# File 'lib/vite_ruby.rb', line 81

def digest
  builder.send(:watched_files_digest)
end

#envObject

Public: Additional environment variables to pass to Vite.

Example:

ViteRuby.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json'


97
98
99
# File 'lib/vite_ruby.rb', line 97

def env
  @env ||= ENV.select { |key, _| key.start_with?(ENV_PREFIX) }
end

#manifestObject

Public: Enables looking up assets managed by Vite using name and type.



142
143
144
# File 'lib/vite_ruby.rb', line 142

def manifest
  @manifest ||= ViteRuby::Manifest.new(self)
end

#run(argv, **options) ⇒ Object

Internal: Executes the vite binary.



112
113
114
# File 'lib/vite_ruby.rb', line 112

def run(argv, **options)
  (@runner ||= ViteRuby::Runner.new(self)).run(argv, **options)
end