Class: ViteRuby
- Inherits:
-
Object
- Object
- ViteRuby
- 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", "roda-vite" => "roda", }
- VERSION =
"3.9.2.3"- DEFAULT_VITE_VERSION =
Internal: Versions used by default when running
vite install. "^6.2.6"- DEFAULT_PLUGIN_VERSION =
"^5.1.1"
Instance Attribute Summary collapse
Class Method Summary collapse
-
.bootstrap ⇒ Object
Internal: Refreshes the manifest.
-
.framework_libraries ⇒ Object
Internal: Detects if the application has installed a framework-specific variant of Vite Ruby.
-
.install_tasks ⇒ Object
Internal: Loads all available rake tasks.
- .instance ⇒ Object
-
.reload_with(**config_options) ⇒ Object
Internal: Creates a new instance with the specified options.
Instance Method Summary collapse
-
#builder ⇒ Object
Public: Keeps track of watched files and triggers builds as needed.
-
#commands ⇒ Object
Internal: Helper to run commands related with Vite.
-
#config ⇒ Object
Public: Current instance configuration for Vite.
-
#configure(**options) ⇒ Object
Public: Allows overriding the configuration for this instance.
-
#dev_server_running? ⇒ Boolean
Public: Returns true if the Vite development server is currently running.
-
#digest ⇒ Object
Public: Returns a digest of all the watched files, allowing to detect changes.
-
#env ⇒ Object
Public: Additional environment variables to pass to Vite.
-
#initialize(**config_options) ⇒ ViteRuby
constructor
A new instance of ViteRuby.
-
#manifest ⇒ Object
Public: Enables looking up assets managed by Vite using name and type.
-
#run(argv, **options) ⇒ Object
Internal: Executes the vite binary.
-
#run_proxy? ⇒ Boolean
Public: The proxy for assets should only run in development mode.
Constructor Details
#initialize(**config_options) ⇒ ViteRuby
Returns a new instance of ViteRuby.
72 73 74 |
# File 'lib/vite_ruby.rb', line 72 def initialize(**) = end |
Instance Attribute Details
#logger ⇒ Object
76 77 78 |
# File 'lib/vite_ruby.rb', line 76 def logger @logger ||= Logger.new($stdout) end |
Class Method Details
.bootstrap ⇒ Object
Internal: Refreshes the manifest.
45 46 47 |
# File 'lib/vite_ruby.rb', line 45 def bootstrap instance.manifest.refresh end |
.framework_libraries ⇒ Object
Internal: Detects if the application has installed a framework-specific variant of Vite Ruby.
61 62 63 64 65 66 67 |
# File 'lib/vite_ruby.rb', line 61 def framework_libraries COMPANION_LIBRARIES.filter_map { |name, framework| if library = Gem.loaded_specs[name] [framework, library] end } end |
.install_tasks ⇒ Object
Internal: Loads all available rake tasks.
50 51 52 |
# File 'lib/vite_ruby.rb', line 50 def install_tasks load File.("tasks/vite.rake", __dir__) end |
.instance ⇒ Object
40 41 42 |
# File 'lib/vite_ruby.rb', line 40 def instance @instance ||= new end |
.reload_with(**config_options) ⇒ Object
Internal: Creates a new instance with the specified options.
55 56 57 |
# File 'lib/vite_ruby.rb', line 55 def reload_with(**) @instance = new(**) end |
Instance Method Details
#builder ⇒ Object
Public: Keeps track of watched files and triggers builds as needed.
124 125 126 |
# File 'lib/vite_ruby.rb', line 124 def builder @builder ||= ViteRuby::Builder.new(self) end |
#commands ⇒ Object
Internal: Helper to run commands related with Vite.
129 130 131 |
# File 'lib/vite_ruby.rb', line 129 def commands @commands ||= ViteRuby::Commands.new(self) end |
#config ⇒ Object
Public: Current instance configuration for Vite.
134 135 136 137 138 139 140 141 |
# File 'lib/vite_ruby.rb', line 134 def config unless defined?(@config) configure @config.load_ruby_config end @config end |
#configure(**options) ⇒ Object
Public: Allows overriding the configuration for this instance.
144 145 146 |
# File 'lib/vite_ruby.rb', line 144 def configure(**) @config = ViteRuby::Config.resolve_config(**, **) end |
#dev_server_running? ⇒ Boolean
Public: Returns true if the Vite development server is currently running. NOTE: Checks only once every second since every lookup calls this method.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/vite_ruby.rb', line 88 def dev_server_running? return false unless run_proxy? return @running if defined?(@running) && Time.now - @running_checked_at < 1 begin Socket.tcp(config.host, config.port, connect_timeout: config.dev_server_connect_timeout).close @running = true rescue @running = false ensure @running_checked_at = Time.now end end |
#digest ⇒ Object
Public: Returns a digest of all the watched files, allowing to detect changes. Useful to perform version checks in single-page applications.
82 83 84 |
# File 'lib/vite_ruby.rb', line 82 def digest builder.send(:watched_files_digest) end |
#env ⇒ Object
Public: Additional environment variables to pass to Vite.
Example:
ViteRuby.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json'
106 107 108 |
# File 'lib/vite_ruby.rb', line 106 def env @env ||= ENV.select { |key, _| key.start_with?(ENV_PREFIX) } end |
#manifest ⇒ Object
Public: Enables looking up assets managed by Vite using name and type.
149 150 151 |
# File 'lib/vite_ruby.rb', line 149 def manifest @manifest ||= ViteRuby::Manifest.new(self) end |
#run(argv, **options) ⇒ Object
Internal: Executes the vite binary.
119 120 121 |
# File 'lib/vite_ruby.rb', line 119 def run(argv, **) (@runner ||= ViteRuby::Runner.new(self)).run(argv, **) end |
#run_proxy? ⇒ Boolean
Public: The proxy for assets should only run in development mode.
111 112 113 114 115 116 |
# File 'lib/vite_ruby.rb', line 111 def run_proxy? config.mode == "development" || (config.mode == "test" && !ENV["CI"]) rescue => error logger.error("Failed to check mode for Vite: #{error.message}") false end |