vite_ruby 3.9.0 → 3.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ViteRuby::CLI::Vite < Dry::CLI::Command
4
- CURRENT_ENV = ENV['RACK_ENV'] || ENV['RAILS_ENV']
4
+ CURRENT_ENV = ENV["RACK_ENV"] || ENV["RAILS_ENV"]
5
5
 
6
6
  def self.executable_options
7
- 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'])
9
- option(:inspect, desc: 'Run Vite in a debugging session with node --inspect-brk', aliases: ['inspect-brk'], type: :boolean)
10
- option(:trace_deprecation, desc: 'Run Vite in debugging mode with node --trace-deprecation', aliases: ['trace-deprecation'], type: :boolean)
7
+ 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"])
9
+ option(:inspect, desc: "Run Vite in a debugging session with node --inspect-brk", aliases: ["inspect-brk"], type: :boolean)
10
+ option(:trace_deprecation, desc: "Run Vite in debugging mode with node --trace-deprecation", aliases: ["trace-deprecation"], type: :boolean)
11
11
  end
12
12
 
13
13
  def self.shared_options
14
14
  executable_options
15
- option(:debug, desc: 'Run Vite in verbose mode, printing all debugging output', aliases: ['verbose'], type: :boolean)
16
- option(:clobber, desc: 'Clear cache and previous builds', type: :boolean, aliases: %w[clean clear])
15
+ option(:debug, desc: "Run Vite in verbose mode, printing all debugging output", aliases: ["verbose"], type: :boolean)
16
+ option(:clobber, desc: "Clear cache and previous builds", type: :boolean, aliases: %w[clean clear])
17
17
  end
18
18
 
19
19
  def call(mode:, args: [], clobber: false, node_options: nil, inspect: nil, trace_deprecation: nil, **boolean_opts)
20
- ViteRuby.env['VITE_RUBY_MODE'] = mode
20
+ ViteRuby.env["VITE_RUBY_MODE"] = mode
21
21
  ViteRuby.commands.clobber if clobber
22
22
 
23
23
  node_options = [
24
24
  node_options,
25
- ('--inspect-brk' if inspect),
26
- ('--trace-deprecation' if trace_deprecation),
27
- ].compact.join(' ')
25
+ ("--inspect-brk" if inspect),
26
+ ("--trace-deprecation" if trace_deprecation),
27
+ ].compact.join(" ")
28
28
 
29
- args << %(--node-options="#{ node_options }") unless node_options.empty?
29
+ args << %(--node-options="#{node_options}") unless node_options.empty?
30
30
 
31
- boolean_opts.map { |name, value| args << "--#{ name }" if value }
31
+ boolean_opts.map { |name, value| args << "--#{name}" if value }
32
32
 
33
33
  yield(args)
34
34
  end
data/lib/vite_ruby/cli.rb CHANGED
@@ -1,30 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/cli'
3
+ require "dry/cli"
4
4
 
5
5
  # Public: Command line interface that allows to install the library, and run
6
6
  # simple commands.
7
7
  class ViteRuby::CLI
8
8
  extend Dry::CLI::Registry
9
9
 
10
- register 'build', Build, aliases: ['b']
11
- register 'clobber', Clobber, aliases: %w[clean clear]
12
- register 'dev', Dev, aliases: %w[d serve]
13
- register 'install', Install
14
- register 'ssr', SSR
15
- register 'version', Version, aliases: ['v', '-v', '--version', 'info']
16
- register 'upgrade', Upgrade, aliases: ['update']
17
- register 'upgrade_packages', UpgradePackages, aliases: ['update_packages']
10
+ register "build", Build, aliases: ["b"]
11
+ register "clobber", Clobber, aliases: %w[clean clear]
12
+ register "dev", Dev, aliases: %w[d serve]
13
+ register "install", Install
14
+ register "ssr", SSR
15
+ register "version", Version, aliases: ["v", "-v", "--version", "info"]
16
+ register "upgrade", Upgrade, aliases: ["update"]
17
+ register "upgrade_packages", UpgradePackages, aliases: ["update_packages"]
18
18
 
19
19
  # Internal: Allows framework-specific variants to extend the CLI.
20
- def self.require_framework_libraries(path = 'cli')
20
+ def self.require_framework_libraries(path = "cli")
21
21
  ViteRuby.framework_libraries.each do |_framework, library|
22
- require [library.name.tr('-', '/').to_s, path].compact.join('/')
22
+ require [library.name.tr("-", "/").to_s, path].compact.join("/")
23
23
  end
24
24
  rescue LoadError
25
- require_framework_libraries 'installation' unless path == 'installation'
25
+ require_framework_libraries "installation" unless path == "installation"
26
26
  end
27
27
  end
28
28
 
29
29
  # NOTE: This allows framework-specific variants to extend the CLI.
30
- ViteRuby::CLI.require_framework_libraries('cli')
30
+ ViteRuby::CLI.require_framework_libraries("cli")
@@ -9,7 +9,7 @@ class ViteRuby::Commands
9
9
 
10
10
  # Public: Defaults to production, and exits if the build fails.
11
11
  def build_from_task(*args)
12
- with_node_env(ENV.fetch('NODE_ENV', 'production')) {
12
+ with_node_env(ENV.fetch("NODE_ENV", "production")) {
13
13
  ensure_log_goes_to_stdout {
14
14
  build(*args) || exit!
15
15
  }
@@ -25,12 +25,12 @@ class ViteRuby::Commands
25
25
  def clobber
26
26
  dirs = [config.build_output_dir, config.ssr_output_dir, config.build_cache_dir, config.vite_cache_dir]
27
27
  dirs.each { |dir| dir.rmtree if dir.exist? }
28
- $stdout.puts "Removed vite cache and output dirs:\n\t#{ dirs.join("\n\t") }"
28
+ $stdout.puts "Removed vite cache and output dirs:\n\t#{dirs.join("\n\t")}"
29
29
  end
30
30
 
31
31
  # Internal: Installs the binstub for the CLI in the appropriate path.
32
32
  def install_binstubs
33
- `bundle binstub vite_ruby --path #{ config.root.join('bin') }`
33
+ `bundle binstub vite_ruby --path #{config.root.join("bin")}`
34
34
  `bundle config --delete bin`
35
35
  end
36
36
 
@@ -46,7 +46,7 @@ class ViteRuby::Commands
46
46
 
47
47
  # Internal: Verifies if ViteRuby is properly installed.
48
48
  def verify_install
49
- unless File.exist?(config.root.join('bin/vite'))
49
+ unless File.exist?(config.root.join("bin/vite"))
50
50
  warn <<~WARN
51
51
 
52
52
  vite binstub not found.
@@ -59,7 +59,7 @@ class ViteRuby::Commands
59
59
  unless config_path.exist?
60
60
  warn <<~WARN
61
61
 
62
- Configuration #{ config_path } file for vite-plugin-ruby not found.
62
+ Configuration #{config_path} file for vite-plugin-ruby not found.
63
63
  Make sure `bundle exec vite install` has run successfully before running dependent tasks.
64
64
  WARN
65
65
  exit!
@@ -69,23 +69,23 @@ class ViteRuby::Commands
69
69
  # Internal: Prints information about ViteRuby's environment.
70
70
  def print_info
71
71
  config.within_root do
72
- $stdout.puts "bin/vite present?: #{ File.exist? 'bin/vite' }"
72
+ $stdout.puts "bin/vite present?: #{File.exist? "bin/vite"}"
73
73
 
74
- $stdout.puts "vite_ruby: #{ ViteRuby::VERSION }"
74
+ $stdout.puts "vite_ruby: #{ViteRuby::VERSION}"
75
75
  ViteRuby.framework_libraries.each do |framework, library|
76
- $stdout.puts "#{ library.name }: #{ library.version }"
77
- $stdout.puts "#{ framework }: #{ Gem.loaded_specs[framework]&.version }"
76
+ $stdout.puts "#{library.name}: #{library.version}"
77
+ $stdout.puts "#{framework}: #{Gem.loaded_specs[framework]&.version}"
78
78
  end
79
79
 
80
- $stdout.puts "ruby: #{ `ruby --version` }"
81
- $stdout.puts "node: #{ `node --version` }"
80
+ $stdout.puts "ruby: #{`ruby --version`}"
81
+ $stdout.puts "node: #{`node --version`}"
82
82
 
83
83
  pkg = config.package_manager
84
- $stdout.puts "#{ pkg }: #{ `#{ pkg } --version` rescue nil }"
84
+ $stdout.puts "#{pkg}: #{`#{pkg} --version` rescue nil}"
85
85
 
86
86
  $stdout.puts "\n"
87
87
  packages = `npm ls vite vite-plugin-ruby`
88
- packages_msg = packages.include?('vite@') ? "installed packages:\n#{ packages }" : '❌ Check that vite and vite-plugin-ruby have been added as development dependencies and installed.'
88
+ packages_msg = packages.include?("vite@") ? "installed packages:\n#{packages}" : "❌ Check that vite and vite-plugin-ruby have been added as development dependencies and installed."
89
89
  $stdout.puts packages_msg
90
90
 
91
91
  ViteRuby::CompatibilityCheck.verify_plugin_version(config.root)
@@ -99,18 +99,18 @@ private
99
99
  def_delegators :@vite_ruby, :config, :builder, :manifest, :logger, :logger=
100
100
 
101
101
  def with_node_env(env)
102
- original = ENV['NODE_ENV']
103
- ENV['NODE_ENV'] = env
102
+ original = ENV["NODE_ENV"]
103
+ ENV["NODE_ENV"] = env
104
104
  yield
105
105
  ensure
106
- ENV['NODE_ENV'] = original
106
+ ENV["NODE_ENV"] = original
107
107
  end
108
108
 
109
109
  def ensure_log_goes_to_stdout
110
110
  old_logger, original_sync = logger, $stdout.sync
111
111
 
112
112
  $stdout.sync = true
113
- self.logger = Logger.new($stdout, formatter: proc { |_, _, progname, msg| progname == 'vite' ? msg : "#{ msg }\n" })
113
+ self.logger = Logger.new($stdout, formatter: proc { |_, _, progname, msg| (progname == "vite") ? msg : "#{msg}\n" })
114
114
  yield
115
115
  ensure
116
116
  self.logger, $stdout.sync = old_logger, original_sync
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
3
+ require "json"
4
4
 
5
5
  # Internal: Verifies that the installed vite-plugin-ruby version is compatible
6
6
  # with the current version of vite_ruby.
@@ -11,9 +11,9 @@ module ViteRuby::CompatibilityCheck
11
11
  class << self
12
12
  # Public: Attempt to verify that the vite-plugin-ruby version is compatible.
13
13
  def verify_plugin_version(root)
14
- package = JSON.parse(root.join('package.json').read) rescue {}
15
- requirement = package.dig('devDependencies', 'vite-plugin-ruby') ||
16
- package.dig('dependencies', 'vite-plugin-ruby')
14
+ package = JSON.parse(root.join("package.json").read) rescue {}
15
+ requirement = package.dig("devDependencies", "vite-plugin-ruby") ||
16
+ package.dig("dependencies", "vite-plugin-ruby")
17
17
 
18
18
  raise_unless_satisfied(requirement, ViteRuby::DEFAULT_PLUGIN_VERSION)
19
19
  end
@@ -22,7 +22,7 @@ module ViteRuby::CompatibilityCheck
22
22
  def raise_unless_satisfied(npm_req, ruby_req)
23
23
  unless compatible_plugin?(npm_req, ruby_req)
24
24
  raise ArgumentError, <<~ERROR
25
- vite-plugin-ruby@#{ npm_req } might not be compatible with vite_ruby-#{ ViteRuby::VERSION }
25
+ vite-plugin-ruby@#{npm_req} might not be compatible with vite_ruby-#{ViteRuby::VERSION}
26
26
 
27
27
  You may disable this check if needed: https://vite-ruby.netlify.app/config/#skipcompatibilitycheck
28
28
 
@@ -37,12 +37,12 @@ module ViteRuby::CompatibilityCheck
37
37
  # requirement.
38
38
  def compatible_plugin?(npm_req, ruby_req)
39
39
  npm_req, ruby_req = [npm_req, ruby_req]
40
- .map { |req| Gem::Requirement.new(req.sub('^', '~>')) }
40
+ .map { |req| Gem::Requirement.new(req.sub("^", "~>")) }
41
41
 
42
42
  current_version = npm_req.requirements.first.second
43
43
 
44
44
  ruby_req.satisfied_by?(current_version)
45
- rescue StandardError
45
+ rescue
46
46
  true
47
47
  end
48
48
  end
@@ -1,30 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
3
+ require "json"
4
4
 
5
5
  # Public: Allows to resolve configuration sourced from `config/vite.json` and
6
6
  # environment variables, combining them with the default options.
7
7
  class ViteRuby::Config
8
8
  def origin
9
- "#{ protocol }://#{ host_with_port }"
9
+ "#{protocol}://#{host_with_port}"
10
10
  end
11
11
 
12
12
  def protocol
13
- https ? 'https' : 'http'
13
+ https ? "https" : "http"
14
14
  end
15
15
 
16
16
  def host_with_port
17
- "#{ host }:#{ port }"
17
+ "#{host}:#{port}"
18
18
  end
19
19
 
20
20
  # Internal: Path to the manifest files generated by Vite and vite-plugin-ruby.
21
21
  def known_manifest_paths
22
22
  [
23
23
  # NOTE: Generated by Vite when `manifest: true`, which vite-plugin-ruby enables.
24
- build_output_dir.join('.vite/manifest.json'),
24
+ build_output_dir.join(".vite/manifest.json"),
25
25
 
26
26
  # NOTE: Path where vite-plugin-ruby outputs the assets manifest file.
27
- build_output_dir.join('.vite/manifest-assets.json'),
27
+ build_output_dir.join(".vite/manifest-assets.json"),
28
28
  ]
29
29
  end
30
30
 
@@ -45,7 +45,7 @@ class ViteRuby::Config
45
45
 
46
46
  # Internal: The directory where Vite stores its processing cache.
47
47
  def vite_cache_dir
48
- root.join('node_modules/.vite')
48
+ root.join("node_modules/.vite")
49
49
  end
50
50
 
51
51
  # Public: The directory that Vite uses as root.
@@ -55,7 +55,7 @@ class ViteRuby::Config
55
55
 
56
56
  # Public: Loads an optional config/vite.rb file that can modify ViteRuby.env
57
57
  def load_ruby_config
58
- rb_config_path = File.expand_path(config_path.sub(/.json$/, '.rb'), root)
58
+ rb_config_path = File.expand_path(config_path.sub(/.json$/, ".rb"), root)
59
59
  load rb_config_path if File.exist?(rb_config_path)
60
60
  end
61
61
 
@@ -63,7 +63,7 @@ class ViteRuby::Config
63
63
  def to_env(env_vars = ViteRuby.env)
64
64
  CONFIGURABLE_WITH_ENV.each_with_object({}) do |option, env|
65
65
  unless (value = @config[option]).nil?
66
- env["#{ ViteRuby::ENV_PREFIX }_#{ option.upcase }"] = value.to_s
66
+ env["#{ViteRuby::ENV_PREFIX}_#{option.upcase}"] = value.to_s
67
67
  end
68
68
  end.merge(env_vars)
69
69
  end
@@ -72,10 +72,10 @@ class ViteRuby::Config
72
72
  def watched_paths
73
73
  [
74
74
  *(watch_additional_paths + additional_entrypoints).reject { |dir|
75
- dir.start_with?('~/') || dir.start_with?(source_code_dir)
75
+ dir.start_with?("~/") || dir.start_with?(source_code_dir)
76
76
  },
77
- "#{ source_code_dir }/**/*",
78
- config_path.sub(/.json$/, '.{rb,json}'),
77
+ "#{source_code_dir}/**/*",
78
+ config_path.sub(/.json$/, ".{rb,json}"),
79
79
  *DEFAULT_WATCHED_PATHS,
80
80
  ].freeze
81
81
  end
@@ -90,27 +90,28 @@ private
90
90
  # Internal: Coerces all the configuration values, in case they were passed
91
91
  # as environment variables which are always strings.
92
92
  def coerce_values(config)
93
- config['mode'] = config['mode'].to_s
94
- config['port'] = config['port'].to_i
95
- config['root'] = root = Pathname.new(config['root'])
96
- config['build_cache_dir'] = root.join(config['build_cache_dir'])
97
- config['ssr_output_dir'] = root.join(config['ssr_output_dir'])
98
- coerce_booleans(config, 'auto_build', 'hide_build_console_output', 'https', 'skip_compatibility_check', 'skip_proxy')
99
- config['package_manager'] ||= detect_package_manager(root)
93
+ config["mode"] = config["mode"].to_s
94
+ config["port"] = config["port"].to_i
95
+ config["root"] = root = Pathname.new(config["root"])
96
+ config["build_cache_dir"] = root.join(config["build_cache_dir"])
97
+ config["ssr_output_dir"] = root.join(config["ssr_output_dir"])
98
+ coerce_booleans(config, "auto_build", "hide_build_console_output", "https", "skip_compatibility_check", "skip_proxy")
99
+ config["package_manager"] ||= detect_package_manager(root)
100
100
  end
101
101
 
102
102
  # Internal: Coerces configuration options to boolean.
103
103
  def coerce_booleans(config, *names)
104
- names.each { |name| config[name] = [true, 'true'].include?(config[name]) }
104
+ truthy = [true, "true"]
105
+ names.each { |name| config[name] = truthy.include?(config[name]) }
105
106
  end
106
107
 
107
108
  def detect_package_manager(root)
108
- return 'npm' if root.join('package-lock.json').exist?
109
- return 'pnpm' if root.join('pnpm-lock.yaml').exist?
110
- return 'bun' if root.join('bun.lockb').exist?
111
- return 'yarn' if root.join('yarn.lock').exist?
109
+ return "npm" if root.join("package-lock.json").exist?
110
+ return "pnpm" if root.join("pnpm-lock.yaml").exist?
111
+ return "bun" if root.join("bun.lockb").exist? || root.join("bun.lock").exist?
112
+ return "yarn" if root.join("yarn.lock").exist?
112
113
 
113
- 'npm'
114
+ "npm"
114
115
  end
115
116
 
116
117
  def initialize(attrs)
@@ -124,8 +125,8 @@ private
124
125
  # Public: Returns the project configuration for Vite.
125
126
  def resolve_config(**attrs)
126
127
  config = config_defaults.merge(attrs.transform_keys(&:to_s))
127
- file_path = File.join(config['root'], config['config_path'])
128
- file_config = config_from_file(file_path, mode: config['mode'])
128
+ file_path = File.join(config["root"], config["config_path"])
129
+ file_config = config_from_file(file_path, mode: config["mode"])
129
130
  new DEFAULT_CONFIG.merge(file_config).merge(config_from_env).merge(config)
130
131
  end
131
132
 
@@ -133,20 +134,20 @@ private
133
134
 
134
135
  # Internal: Converts camelCase to snake_case.
135
136
  SNAKE_CASE = ->(camel_cased_word) {
136
- camel_cased_word.to_s.gsub(/::/, '/')
137
+ camel_cased_word.to_s.gsub("::", "/")
137
138
  .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
138
139
  .gsub(/([a-z\d])([A-Z])/, '\1_\2')
139
- .tr('-', '_')
140
+ .tr("-", "_")
140
141
  .downcase
141
142
  }
142
143
 
143
144
  # Internal: Default values for a Ruby application.
144
- def config_defaults(asset_host: nil, mode: ENV.fetch('RACK_ENV', 'development'), root: Dir.pwd)
145
+ def config_defaults(asset_host: nil, mode: ENV.fetch("RACK_ENV", "development"), root: Dir.pwd)
145
146
  {
146
- 'asset_host' => option_from_env('asset_host') || asset_host,
147
- 'config_path' => option_from_env('config_path') || DEFAULT_CONFIG.fetch('config_path'),
148
- 'mode' => option_from_env('mode') || mode,
149
- 'root' => option_from_env('root') || root,
147
+ "asset_host" => option_from_env("asset_host") || asset_host,
148
+ "config_path" => option_from_env("config_path") || DEFAULT_CONFIG.fetch("config_path"),
149
+ "mode" => option_from_env("mode") || mode,
150
+ "root" => option_from_env("root") || root,
150
151
  }.select { |_, value| value }
151
152
  end
152
153
 
@@ -161,7 +162,7 @@ private
161
162
 
162
163
  # Internal: Retrieves a configuration option from environment variables.
163
164
  def option_from_env(name)
164
- ViteRuby.env["#{ ViteRuby::ENV_PREFIX }_#{ name.upcase }"]
165
+ ViteRuby.env["#{ViteRuby::ENV_PREFIX}_#{name.upcase}"]
165
166
  end
166
167
 
167
168
  # Internal: Extracts the configuration options provided as env vars.
@@ -176,16 +177,16 @@ private
176
177
  # Internal: Loads the configuration options provided in a JSON file.
177
178
  def config_from_file(path, mode:)
178
179
  multi_env_config = load_json(path)
179
- multi_env_config.fetch('all', {})
180
+ multi_env_config.fetch("all", {})
180
181
  .merge(multi_env_config.fetch(mode, {}))
181
182
  rescue Errno::ENOENT => error
182
- $stderr << "Check that your vite.json configuration file is available in the load path:\n\n\t#{ error.message }\n\n"
183
+ $stderr << "Check that your vite.json configuration file is available in the load path:\n\n\t#{error.message}\n\n"
183
184
  {}
184
185
  end
185
186
  end
186
187
 
187
188
  # Internal: Shared configuration with the Vite plugin for Ruby.
188
- DEFAULT_CONFIG = load_json("#{ __dir__ }/../../default.vite.json").freeze
189
+ DEFAULT_CONFIG = load_json("#{__dir__}/../../default.vite.json").freeze
189
190
 
190
191
  # Internal: Configuration options that can not be provided as env vars.
191
192
  NOT_CONFIGURABLE_WITH_ENV = %w[additional_entrypoints watch_additional_paths].freeze
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rack/proxy'
3
+ require "rack/proxy"
4
4
 
5
5
  # Public: Allows to relay asset requests to the Vite development server.
6
6
  class ViteRuby::DevServerProxy < Rack::Proxy
7
7
  HOST_WITH_PORT_REGEX = %r{^(.+?)(:\d+)/}
8
- VITE_DEPENDENCY_PREFIX = '/@'
8
+ VITE_DEPENDENCY_PREFIX = "/@"
9
9
 
10
10
  def initialize(app = nil, options = {})
11
11
  @vite_ruby = options.delete(:vite_ruby) || ViteRuby.instance
12
- options[:streaming] = false if config.mode == 'test' && !options.key?(:streaming)
12
+ options[:streaming] = false if config.mode == "test" && !options.key?(:streaming)
13
13
  super
14
14
  end
15
15
 
@@ -17,7 +17,7 @@ class ViteRuby::DevServerProxy < Rack::Proxy
17
17
  def perform_request(env)
18
18
  if vite_should_handle?(env) && dev_server_running?
19
19
  forward_to_vite_dev_server(env)
20
- super(env)
20
+ super
21
21
  else
22
22
  @app.call(env)
23
23
  end
@@ -30,39 +30,40 @@ private
30
30
  def_delegators :@vite_ruby, :config, :dev_server_running?
31
31
 
32
32
  def rewrite_uri_for_vite(env)
33
- uri = env.fetch('REQUEST_URI') { [env['PATH_INFO'], env['QUERY_STRING']].reject { |str| str.to_s.strip.empty? }.join('?') }
34
- env['PATH_INFO'], env['QUERY_STRING'] = (env['REQUEST_URI'] = normalize_uri(uri)).split('?')
33
+ uri = env.fetch("REQUEST_URI") { [env["PATH_INFO"], env["QUERY_STRING"]].reject { |str| str.to_s.strip.empty? }.join("?") }
34
+ env["PATH_INFO"], env["QUERY_STRING"] = (env["REQUEST_URI"] = normalize_uri(uri)).split("?")
35
35
  end
36
36
 
37
37
  def normalize_uri(uri)
38
38
  uri
39
- .sub(HOST_WITH_PORT_REGEX, '/') # Hanami adds the host and port.
40
- .sub('.ts.js', '.ts') # Hanami's javascript helper always adds the extension.
39
+ .sub(HOST_WITH_PORT_REGEX, "/") # Hanami adds the host and port.
40
+ .sub(".ts.js", ".ts") # Hanami's javascript helper always adds the extension.
41
41
  .sub(/\.(sass|scss|styl|stylus|less|pcss|postcss)\.css$/, '.\1') # Rails' stylesheet_link_tag always adds the extension.
42
42
  end
43
43
 
44
44
  def forward_to_vite_dev_server(env)
45
45
  rewrite_uri_for_vite(env)
46
- env['QUERY_STRING'] ||= ''
47
- env['HTTP_HOST'] = env['HTTP_X_FORWARDED_HOST'] = config.host
48
- env['HTTP_X_FORWARDED_SERVER'] = config.host_with_port
49
- env['HTTP_PORT'] = env['HTTP_X_FORWARDED_PORT'] = config.port.to_s
50
- env['HTTP_X_FORWARDED_PROTO'] = env['HTTP_X_FORWARDED_SCHEME'] = config.protocol
51
- env['HTTPS'] = env['HTTP_X_FORWARDED_SSL'] = 'off' unless config.https
52
- env['SCRIPT_NAME'] = ''
46
+ env["QUERY_STRING"] ||= ""
47
+ env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = config.host
48
+ env["HTTP_X_FORWARDED_SERVER"] = config.host_with_port
49
+ env["HTTP_PORT"] = env["HTTP_X_FORWARDED_PORT"] = config.port.to_s
50
+ env["HTTP_X_FORWARDED_PROTO"] = env["HTTP_X_FORWARDED_SCHEME"] = config.protocol
51
+ env["HTTPS"] = env["HTTP_X_FORWARDED_SSL"] = "off" unless config.https
52
+ env["SCRIPT_NAME"] = ""
53
53
  end
54
54
 
55
55
  def vite_should_handle?(env)
56
- path = normalize_uri(env['PATH_INFO'])
56
+ path = normalize_uri(env["PATH_INFO"])
57
57
  return true if path.start_with?(vite_url_prefix) # Vite asset
58
- return true if file_in_vite_root?(path) # Fallback if Vite can serve the file
58
+
59
+ true if file_in_vite_root?(path) # Fallback if Vite can serve the file
59
60
  end
60
61
 
61
62
  # NOTE: When using an empty 'public_output_dir', we need to rely on a
62
63
  # filesystem check to check whether Vite should serve the request.
63
64
  def file_in_vite_root?(path)
64
- path.include?('.') && # Check for extension, avoid filesystem if possible.
65
- config.vite_root_dir.join(path.start_with?('/') ? path[1..-1] : path).file?
65
+ path.include?(".") && # Check for extension, avoid filesystem if possible.
66
+ config.vite_root_dir.join(path.start_with?("/") ? path[1..] : path).file?
66
67
  end
67
68
 
68
69
  # NOTE: Vite is configured to use 'public_output_dir' as the base, which can
@@ -70,6 +71,6 @@ private
70
71
  #
71
72
  # If the path starts with that prefix, it will be redirected to Vite.
72
73
  def vite_url_prefix
73
- @vite_url_prefix ||= config.public_output_dir.empty? ? VITE_DEPENDENCY_PREFIX : "/#{ config.public_output_dir }/"
74
+ @vite_url_prefix ||= config.public_output_dir.empty? ? VITE_DEPENDENCY_PREFIX : "/#{config.public_output_dir}/"
74
75
  end
75
76
  end
@@ -3,7 +3,7 @@
3
3
  # Internal: Provides common functionality for errors.
4
4
  class ViteRuby::Error < StandardError
5
5
  def message
6
- super.sub(':troubleshooting:', <<~MSG)
6
+ super.sub(":troubleshooting:", <<~MSG)
7
7
  Visit the Troubleshooting guide for more information:
8
8
  https://vite-ruby.netlify.app/guide/troubleshooting.html#troubleshooting
9
9
  MSG
data/lib/vite_ruby/io.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'open3'
3
+ require "open3"
4
4
 
5
5
  # Public: Builds on top of Ruby I/O open3 providing a friendlier experience.
6
6
  module ViteRuby::IO
7
7
  class << self
8
8
  # Internal: A modified version of capture3 that can continuosly print stdout.
9
9
  # NOTE: Streaming output provides a better UX when running bin/vite build.
10
- def capture(*cmd, with_output: $stdout.method(:puts), stdin_data: '', **opts)
10
+ def capture(*cmd, with_output: $stdout.method(:puts), stdin_data: "", **opts)
11
11
  return Open3.capture3(*cmd, **opts) unless with_output
12
12
 
13
13
  Open3.popen3(*cmd, **opts) { |stdin, stdout, stderr, wait_threads|
@@ -21,7 +21,7 @@ module ViteRuby::IO
21
21
 
22
22
  # Internal: Reads and yield every line in the stream. Returns the full content.
23
23
  def read_lines(io)
24
- buffer = +''
24
+ buffer = +""
25
25
  while line = io.gets
26
26
  buffer << line
27
27
  yield line