Skip to content

Commit 330f61f

Browse files
authored
feat: change default execution to use npx vite instead (#480)
closes #462
1 parent 9147224 commit 330f61f

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

vite-plugin-ruby/default.vite.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"https": null,
1717
"port": 3036,
1818
"hideBuildConsoleOutput": false,
19-
"viteBinPath": "node_modules/.bin/vite",
19+
"viteBinPath": null,
2020
"watchAdditionalPaths": [],
2121
"base": "",
2222
"ssrBuildEnabled": false,

vite_ruby/default.vite.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"https": null,
1717
"port": 3036,
1818
"hideBuildConsoleOutput": false,
19-
"viteBinPath": "node_modules/.bin/vite",
19+
"viteBinPath": null,
2020
"watchAdditionalPaths": [],
2121
"base": "",
2222
"ssrBuildEnabled": false,

vite_ruby/lib/vite_ruby/cli/vite.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class ViteRuby::CLI::Vite < Dry::CLI::Command
55

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

18-
def call(mode:, args: [], clobber: false, **boolean_opts)
19+
def call(mode:, args: [], clobber: false, node_options: nil, inspect: nil, trace_deprecation: nil, **boolean_opts)
1920
ViteRuby.env['VITE_RUBY_MODE'] = mode
2021
ViteRuby.commands.clobber if clobber
22+
23+
node_options = [
24+
node_options,
25+
('--inspect-brk' if inspect),
26+
('--trace-deprecation' if trace_deprecation),
27+
].compact.join(' ')
28+
29+
args << %(--node-options="#{ node_options }") unless node_options.empty?
30+
2131
boolean_opts.map { |name, value| args << "--#{ name }" if value }
32+
2233
yield(args)
2334
end
2435
end

vite_ruby/lib/vite_ruby/runner.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,27 @@ def run(argv, exec: false)
2828
# Internal: Returns an Array with the command to run.
2929
def command_for(args)
3030
[config.to_env(env)].tap do |cmd|
31-
args = args.clone
32-
cmd.push('node', '--inspect-brk') if args.delete('--inspect')
33-
cmd.push('node', '--trace-deprecation') if args.delete('--trace_deprecation')
31+
npx_options, vite_args = args.partition { |arg| arg.start_with?('--node-options') }
3432
cmd.push(*vite_executable)
35-
cmd.push(*args)
33+
34+
# NOTE: Vite will parse args, so we need to disambiguate and pass them to
35+
# `npx` before specifying the `vite` executable.
36+
cmd.insert(-2, *npx_options) unless npx_options.empty?
37+
38+
cmd.push(*vite_args)
3639
cmd.push('--mode', config.mode) unless args.include?('--mode') || args.include?('-m')
3740
end
3841
end
3942

4043
# Internal: Resolves to an executable for Vite.
4144
def vite_executable
4245
bin_path = config.vite_bin_path
43-
return [bin_path] if File.exist?(bin_path)
46+
return [bin_path] if bin_path && File.exist?(bin_path)
4447

4548
if config.root.join('yarn.lock').exist?
4649
%w[yarn vite]
4750
else
48-
["#{ `npm bin`.chomp }/vite"]
51+
%w[npx vite]
4952
end
5053
end
5154
end

0 commit comments

Comments
 (0)