Skip to content

Commit

Permalink
feat: change default execution to use npx vite instead (#480)
Browse files Browse the repository at this point in the history
closes #462
  • Loading branch information
ElMassimo authored Jul 16, 2024
1 parent 9147224 commit 330f61f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion vite-plugin-ruby/default.vite.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"https": null,
"port": 3036,
"hideBuildConsoleOutput": false,
"viteBinPath": "node_modules/.bin/vite",
"viteBinPath": null,
"watchAdditionalPaths": [],
"base": "",
"ssrBuildEnabled": false,
Expand Down
2 changes: 1 addition & 1 deletion vite_ruby/default.vite.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"https": null,
"port": 3036,
"hideBuildConsoleOutput": false,
"viteBinPath": "node_modules/.bin/vite",
"viteBinPath": null,
"watchAdditionalPaths": [],
"base": "",
"ssrBuildEnabled": false,
Expand Down
13 changes: 12 additions & 1 deletion vite_ruby/lib/vite_ruby/cli/vite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ViteRuby::CLI::Vite < Dry::CLI::Command

def self.executable_options
option(:mode, default: self::DEFAULT_ENV, values: %w[development production test], aliases: ['m'], desc: 'The build mode for Vite')
option(:node_options, desc: 'Node options for the Vite executable', aliases: ['node-options'])
option(:inspect, desc: 'Run Vite in a debugging session with node --inspect-brk', aliases: ['inspect-brk'], type: :boolean)
option(:trace_deprecation, desc: 'Run Vite in debugging mode with node --trace-deprecation', aliases: ['trace-deprecation'], type: :boolean)
end
Expand All @@ -15,10 +16,20 @@ def self.shared_options
option(:clobber, desc: 'Clear cache and previous builds', type: :boolean, aliases: %w[clean clear])
end

def call(mode:, args: [], clobber: false, **boolean_opts)
def call(mode:, args: [], clobber: false, node_options: nil, inspect: nil, trace_deprecation: nil, **boolean_opts)
ViteRuby.env['VITE_RUBY_MODE'] = mode
ViteRuby.commands.clobber if clobber

node_options = [
node_options,
('--inspect-brk' if inspect),
('--trace-deprecation' if trace_deprecation),
].compact.join(' ')

args << %(--node-options="#{ node_options }") unless node_options.empty?

boolean_opts.map { |name, value| args << "--#{ name }" if value }

yield(args)
end
end
15 changes: 9 additions & 6 deletions vite_ruby/lib/vite_ruby/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,27 @@ def run(argv, exec: false)
# Internal: Returns an Array with the command to run.
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')
npx_options, vite_args = args.partition { |arg| arg.start_with?('--node-options') }
cmd.push(*vite_executable)
cmd.push(*args)

# NOTE: Vite will parse args, so we need to disambiguate and pass them to
# `npx` before specifying the `vite` executable.
cmd.insert(-2, *npx_options) unless npx_options.empty?

cmd.push(*vite_args)
cmd.push('--mode', config.mode) unless args.include?('--mode') || args.include?('-m')
end
end

# Internal: Resolves to an executable for Vite.
def vite_executable
bin_path = config.vite_bin_path
return [bin_path] if File.exist?(bin_path)
return [bin_path] if bin_path && File.exist?(bin_path)

if config.root.join('yarn.lock').exist?
%w[yarn vite]
else
["#{ `npm bin`.chomp }/vite"]
%w[npx vite]
end
end
end

0 comments on commit 330f61f

Please sign in to comment.