diff --git a/test/builder_test.rb b/test/builder_test.rb index ec2ad1d1..86120747 100644 --- a/test/builder_test.rb +++ b/test/builder_test.rb @@ -84,7 +84,7 @@ def test_external_env_variables end def test_missing_executable - refresh_config('VITE_RUBY_VITE_BIN_PATH' => 'none/vite') + refresh_config(vite_bin_path: 'none/vite') # It fails because we stub the File.exist? check, so the binary is missing. error = assert_raises(ViteRuby::MissingExecutableError) { diff --git a/test/dev_server_test.rb b/test/dev_server_test.rb index 41448957..7a6b19bd 100644 --- a/test/dev_server_test.rb +++ b/test/dev_server_test.rb @@ -6,12 +6,12 @@ class DevServerTest < ViteRuby::Test def test_not_running refute ViteRuby.instance.dev_server_running? - refresh_config('VITE_RUBY_MODE' => 'development') + refresh_config(mode: 'development') refute ViteRuby.instance.dev_server_running? end def test_running - refresh_config('VITE_RUBY_MODE' => 'development') + refresh_config(mode: 'development') ViteRuby.instance.instance_variable_set('@running_at', Time.now) assert ViteRuby.instance.dev_server_running? ensure diff --git a/test/engine_rake_tasks_test.rb b/test/engine_rake_tasks_test.rb index 3c37df7c..69bef1bc 100644 --- a/test/engine_rake_tasks_test.rb +++ b/test/engine_rake_tasks_test.rb @@ -58,7 +58,10 @@ def test_cli def test_cli_commands within_mounted_app_root { ViteRuby.commands.verify_install - refresh_config('VITE_RUBY_ROOT' => Dir.pwd) + + ENV['VITE_RUBY_ROOT'] = Dir.pwd + refresh_config + ViteRuby::CLI::Install.new.call ViteRuby.commands.verify_install ViteRuby::CLI::Version.new.call @@ -70,6 +73,9 @@ def test_cli_commands } ViteRuby::CLI::Clobber.new.call(mode: ViteRuby.mode) } + ensure + ENV.delete('VITE_RUBY_ROOT') + refresh_config end private @@ -88,7 +94,7 @@ def stub_runner(*args, **opts, &block) mock.expect(:call, [:stdout, :stderr, status]) do |*argv, **options| assert_equal [args, opts].flatten.reject(&:blank?), (argv + [options]).flatten.reject(&:blank?) end - ViteRuby.stub(:run, mock, &block) + ViteRuby.stub_any_instance(:run, ->(*stub_args, **stub_opts) { mock.call(*stub_args, **stub_opts) }, &block) mock.verify end diff --git a/test/test_helper.rb b/test/test_helper.rb index a47bc4bd..abe91c47 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -29,9 +29,8 @@ def teardown refresh_config end - def refresh_config(env_variables = ViteRuby.load_env_variables) - ViteRuby.env.tap(&:clear) - ViteRuby.reload_with(env_variables) + def refresh_config(**options) + ViteRuby.reload_with(**options).config end def with_rails_env(env) diff --git a/vite_ruby/lib/vite_ruby.rb b/vite_ruby/lib/vite_ruby.rb index c9fe2f16..d2158914 100644 --- a/vite_ruby/lib/vite_ruby.rb +++ b/vite_ruby/lib/vite_ruby.rb @@ -35,19 +35,11 @@ class ViteRuby class << self extend Forwardable - def_delegators :instance, :config, :commands, :run_proxy? + def_delegators :instance, :config, :commands, :env, :run, :run_proxy? def_delegators :config, :mode def instance - @instance ||= ViteRuby.new - end - - # Public: Additional environment variables to pass to Vite. - # - # Example: - # ViteRuby.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json' - def env - @env ||= load_env_variables + @instance ||= new end # Internal: Refreshes the manifest. @@ -60,21 +52,9 @@ def install_tasks load File.expand_path('tasks/vite.rake', __dir__) end - # Internal: Executes the vite binary. - def run(argv, **options) - ViteRuby::Runner.new(instance).run(argv, **options) - end - - # Internal: Refreshes the config after setting the env vars. - def reload_with(env_vars) - env.update(env_vars) - @instance = nil - config - end - - # Internal: Allows to obtain any env variables for configuration options. - def load_env_variables - ENV.select { |key, _| key.start_with?(ENV_PREFIX) } + # Internal: Creates a new instance with the specified options. + def reload_with(**config_options) + @instance = new(**config_options) end # Internal: Detects if the application has installed a framework-specific @@ -90,6 +70,10 @@ def framework_libraries attr_writer :logger + def initialize(**config_options) + @config_options = config_options + end + def logger @logger ||= Logger.new($stdout) end @@ -107,6 +91,14 @@ def dev_server_running? @running_at = false end + # Public: Additional environment variables to pass to Vite. + # + # Example: + # ViteRuby.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json' + def env + @env ||= ENV.select { |key, _| key.start_with?(ENV_PREFIX) } + end + # Public: The proxy for assets should only run in development mode. def run_proxy? config.mode == 'development' @@ -115,6 +107,11 @@ def run_proxy? false end + # Internal: Executes the vite binary. + def run(argv, **options) + (@runner ||= ViteRuby::Runner.new(self)).run(argv, **options) + end + # Public: Keeps track of watched files and triggers builds as needed. def builder @builder ||= ViteRuby::Builder.new(self) @@ -127,7 +124,7 @@ def commands # Public: Current instance configuration for Vite. def config - @config ||= ViteRuby::Config.resolve_config + @config ||= ViteRuby::Config.resolve_config(**@config_options) end # Public: Enables looking up assets managed by Vite using name and type. diff --git a/vite_ruby/lib/vite_ruby/builder.rb b/vite_ruby/lib/vite_ruby/builder.rb index 0a7cc375..f4824b78 100644 --- a/vite_ruby/lib/vite_ruby/builder.rb +++ b/vite_ruby/lib/vite_ruby/builder.rb @@ -33,7 +33,7 @@ def last_build_metadata extend Forwardable - def_delegators :@vite_ruby, :config, :logger + def_delegators :@vite_ruby, :config, :logger, :run # Internal: Reads metadata recorded on the last build, if it exists. def last_build_attrs @@ -69,7 +69,7 @@ def watched_files_digest def build_with_vite(*args) logger.info 'Building with Vite ⚡️' - stdout, stderr, status = ViteRuby.run(['build', *args]) + stdout, stderr, status = run(['build', *args]) log_build_result(stdout, stderr.to_s, status) status.success? diff --git a/vite_ruby/lib/vite_ruby/cli/install.rb b/vite_ruby/lib/vite_ruby/cli/install.rb index 13303807..549f98d2 100644 --- a/vite_ruby/lib/vite_ruby/cli/install.rb +++ b/vite_ruby/lib/vite_ruby/cli/install.rb @@ -72,7 +72,7 @@ def copy_template(path, to:) def create_configuration_files copy_template 'config/vite.config.ts', to: root.join('vite.config.ts') setup_app_files - ViteRuby.reload_with('VITE_RUBY_CONFIG_PATH' => config.config_path) + ViteRuby.reload_with(config_path: config.config_path) end # Internal: Installs vite and vite-plugin-ruby at the project level.