Skip to content

Commit

Permalink
feat: adds naive bun support
Browse files Browse the repository at this point in the history
  • Loading branch information
pftg committed Sep 14, 2023
1 parent 6249f3b commit 2613df8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
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
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 @@ -184,6 +184,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
17 changes: 13 additions & 4 deletions vite_ruby/lib/vite_ruby/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def run(argv, exec: false)
def command_for(args)
[config.to_env(env)].tap do |cmd|
args = args.clone
cmd.push('node', '--inspect-brk') if args.delete('--inspect')
cmd.push('node', '--trace-deprecation') if args.delete('--trace_deprecation')
unless config.root.join('bun.lockb').exist?
cmd.push('node', '--inspect-brk') if args.delete('--inspect')
cmd.push('node', '--trace-deprecation') if args.delete('--trace_deprecation')
end
cmd.push(*vite_executable)
cmd.push(*args)
cmd.push('--mode', config.mode) unless args.include?('--mode') || args.include?('-m')
Expand All @@ -40,9 +42,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

0 comments on commit 2613df8

Please sign in to comment.