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',
}
VERSION =
'3.0.3'
DEFAULT_VITE_VERSION =

Internal: Versions used by default when running ‘vite install`.

'^2.6.13'
DEFAULT_PLUGIN_VERSION =
'^3.0.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**config_options) ⇒ ViteRuby



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

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

Instance Attribute Details

#loggerObject



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

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

Class Method Details

.bootstrapObject

Internal: Refreshes the manifest.



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

def bootstrap
  instance.manifest.refresh
end

.framework_librariesObject

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



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

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

.install_tasksObject

Internal: Loads all available rake tasks.



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

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

.instanceObject



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

def instance
  @instance ||= new
end

.reload_with(**config_options) ⇒ Object

Internal: Creates a new instance with the specified options.



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

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.



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

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

#commandsObject

Internal: Helper to run commands related with Vite.



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

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

#configObject

Public: Current instance configuration for Vite.



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

def config
  @config ||= ViteRuby::Config.resolve_config(**@config_options)
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.



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

def dev_server_running?
  return false unless run_proxy?
  return true if defined?(@running_at) && @running_at && Time.now - @running_at < 1

  Socket.tcp(config.host, config.port, connect_timeout: config.dev_server_connect_timeout).close
  @running_at = Time.now
  true
rescue StandardError
  @running_at = false
end

#envObject

Public: Additional environment variables to pass to Vite.

Example:

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


95
96
97
# File 'lib/vite_ruby.rb', line 95

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.



128
129
130
# File 'lib/vite_ruby.rb', line 128

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

#run(argv, **options) ⇒ Object

Internal: Executes the vite binary.



108
109
110
# File 'lib/vite_ruby.rb', line 108

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

#run_proxy?Boolean

Public: The proxy for assets should only run in development mode.



100
101
102
103
104
105
# File 'lib/vite_ruby.rb', line 100

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