Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/rake_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def test_rake_vite_install_dependencies_in_production_environment
'Expected development dependencies to be installed as well'
end

def test_rake_vite_install_dependencies_supports_bun
ViteRuby.commands.send(:with_node_env, 'production') do
Dir.chdir(test_app_path) do
`touch bun.lockb`
`bundle exec rake vite:install_dependencies`
end
end

assert_includes installed_node_module_names, 'right-pad',
'Expected development dependencies to be installed as well'
ensure
FileUtils.rm_f(File.expand_path('bun.lockb', test_app_path))
end

private

def test_app_path
Expand Down
4 changes: 3 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
require 'minitest/reporters'
require 'minitest/stub_any_instance'

Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true, location: true, fast_fail: true)]
unless ENV['RM_INFO']
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true, location: true, fast_fail: true)]
end

require 'rails'
require 'rails/test_help'
Expand Down
6 changes: 6 additions & 0 deletions vite_ruby/lib/tasks/vite.rake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ namespace :vite do
desc 'Ensure build dependencies like Vite are installed before bundling'
task :install_dependencies do
install_env_args = ENV['VITE_RUBY_SKIP_INSTALL_DEV_DEPENDENCIES'] == 'true' ? {} : { 'NODE_ENV' => 'development' }

if File.exist?('bun.lockb')
result = system(install_env_args, 'bun install')
next unless result.nil?
end

cmd = ViteRuby.commands.legacy_npm_version? ? 'npx ci --yes' : 'npx --yes ci'
result = system(install_env_args, cmd)
# Fallback to `yarn` if `npx` is not available.
Expand Down
5 changes: 4 additions & 1 deletion vite_ruby/lib/vite_ruby/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def watched_files_digest
def build_with_vite(*args)
logger.info 'Building with Vite ⚡️'

run(['build', *args])
p(run(['build', *args]))

ensure
logger.debug '[Done]'
end

# Internal: Outputs the build results.
Expand Down
1 change: 1 addition & 0 deletions vite_ruby/lib/vite_ruby/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def run_with_capture(*args, **options)

# Internal: Support all popular package managers.
def npm_install
return 'bun install' if root.join('bun.lockb').exist?
return 'yarn add' if root.join('yarn.lock').exist?
return 'pnpm install' if root.join('pnpm-lock.yaml').exist?

Expand Down
1 change: 1 addition & 0 deletions vite_ruby/lib/vite_ruby/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def print_info
$stdout.puts "npm: #{ `npm --version` }"
$stdout.puts "yarn: #{ `yarn --version` rescue nil }"
$stdout.puts "pnpm: #{ `pnpm --version` rescue nil }"
$stdout.puts "bun: #{ `bun --version` rescue nil }"
$stdout.puts "ruby: #{ `ruby --version` }"

$stdout.puts "\n"
Expand Down
1 change: 1 addition & 0 deletions vite_ruby/lib/vite_ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def config_from_file(path, mode:)

# Internal: If any of these files is modified the build won't be skipped.
DEFAULT_WATCHED_PATHS = %w[
bun.lockb
package-lock.json
package.json
pnpm-lock.yaml
Expand Down
4 changes: 2 additions & 2 deletions vite_ruby/lib/vite_ruby/dev_server_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def forward_to_vite_dev_server(env)

def vite_should_handle?(env)
path = normalize_uri(env['PATH_INFO'])
return true if path.start_with?(vite_url_prefix) # Vite asset
return true if file_in_vite_root?(path) # Fallback if Vite can serve the file
path.start_with?(vite_url_prefix) || # Vite asset
file_in_vite_root?(path) # Fallback if Vite can serve the file
end

# NOTE: When using an empty 'public_output_dir', we need to rely on a
Expand Down
3 changes: 2 additions & 1 deletion vite_ruby/lib/vite_ruby/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ class << self
# Internal: A modified version of capture3 that can continuosly print stdout.
# NOTE: Streaming output provides a better UX when running bin/vite build.
def capture(*cmd, with_output: $stdout.method(:puts), stdin_data: '', **opts)
pp [with_output, *cmd, **opts]
return Open3.capture3(*cmd, **opts) unless with_output

Open3.popen3(*cmd, **opts) { |stdin, stdout, stderr, wait_threads|
stdin << stdin_data
stdin.close
out = Thread.new { read_lines(stdout, &with_output) }
err = Thread.new { stderr.read }
[out.value, err.value.to_s, wait_threads.value]
p([out.value, err.value.to_s, wait_threads.value])
}
end

Expand Down
28 changes: 23 additions & 5 deletions vite_ruby/lib/vite_ruby/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ def initialize(vite_ruby)

# Public: Executes Vite with the specified arguments.
def run(argv, exec: false)
pp "run: #{argv}"
config.within_root {
cmd = command_for(argv)
p cmd = command_for(argv)
puts "Executing (#{exec.inspect}) ..."
return Kernel.exec(*cmd) if exec

log_or_noop = ->(line) { logger.info('vite') { line } } unless config.hide_build_console_output
Expand All @@ -27,10 +29,19 @@ def run(argv, exec: false)

# Internal: Returns an Array with the command to run.
def command_for(args)
puts '-' * 100
puts "command_for: #{env.inspect}"
[config.to_env(env)].tap do |cmd|
puts "cmd: #{cmd}"
args = args.clone
cmd.push('node', '--inspect-brk') if args.delete('--inspect')
cmd.push('node', '--trace-deprecation') if args.delete('--trace_deprecation')
pp [config.root, config.root.join('bun.lockb')]
unless config.root.join('bun.lockb').exist?
puts '-' * 100
puts 'WARNING: NOT BUN.'
cmd.push('node', '--inspect-brk') if args.delete('--inspect')
cmd.push('node', '--trace-deprecation') if args.delete('--trace_deprecation')
end
pp [*vite_executable]
cmd.push(*vite_executable)
cmd.push(*args)
cmd.push('--mode', config.mode) unless args.include?('--mode') || args.include?('-m')
Expand All @@ -40,9 +51,16 @@ def command_for(args)
# Internal: Resolves to an executable for Vite.
def vite_executable
bin_path = config.vite_bin_path
return [bin_path] if File.exist?(bin_path)

if config.root.join('yarn.lock').exist?
if File.exist?(bin_path)
return ['bun', '--bun', bin_path] if config.root.join('bun.lockb').exist?

return [bin_path]
end

if config.root.join('bun.lockb').exist?
%w[bun --bun vite]
elsif config.root.join('yarn.lock').exist?
%w[yarn vite]
else
["#{ `npm bin`.chomp }/vite"]
Expand Down