vite_ruby 3.11.0 → 4.0.0.alpha1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -511
- data/README.md +2 -2
- data/default.vite.json +2 -8
- data/exe/vite +3 -5
- data/lib/tasks/vite.rake +19 -56
- data/lib/vite_ruby/build.rb +17 -47
- data/lib/vite_ruby/builder.rb +35 -66
- data/lib/vite_ruby/cli/build.rb +4 -6
- data/lib/vite_ruby/cli/clobber.rb +4 -4
- data/lib/vite_ruby/cli/dev.rb +3 -3
- data/lib/vite_ruby/cli/file_utils.rb +9 -16
- data/lib/vite_ruby/cli/install.rb +42 -47
- data/lib/vite_ruby/cli/upgrade.rb +4 -4
- data/lib/vite_ruby/cli/upgrade_packages.rb +4 -3
- data/lib/vite_ruby/cli/version.rb +1 -1
- data/lib/vite_ruby/cli/vite.rb +9 -24
- data/lib/vite_ruby/cli.rb +8 -21
- data/lib/vite_ruby/commands.rb +75 -31
- data/lib/vite_ruby/compatibility_check.rb +8 -8
- data/lib/vite_ruby/config.rb +37 -76
- data/lib/vite_ruby/dev_server_proxy.rb +24 -31
- data/lib/vite_ruby/error.rb +1 -1
- data/lib/vite_ruby/io.rb +4 -4
- data/lib/vite_ruby/manifest.rb +32 -63
- data/lib/vite_ruby/missing_entrypoint_error.rb +14 -21
- data/lib/vite_ruby/missing_executable_error.rb +1 -1
- data/lib/vite_ruby/runner.rb +12 -20
- data/lib/vite_ruby/version.rb +3 -3
- data/lib/vite_ruby.rb +38 -76
- metadata +21 -48
- data/lib/vite_ruby/cli/ssr.rb +0 -27
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class ViteRuby::CLI::Upgrade < ViteRuby::CLI::Install
|
|
4
|
-
desc
|
|
4
|
+
desc 'Updates Vite Ruby related gems and npm packages.'
|
|
5
5
|
|
|
6
6
|
def call(**)
|
|
7
7
|
upgrade_ruby_gems
|
|
@@ -11,15 +11,15 @@ class ViteRuby::CLI::Upgrade < ViteRuby::CLI::Install
|
|
|
11
11
|
protected
|
|
12
12
|
|
|
13
13
|
def upgrade_ruby_gems
|
|
14
|
-
say
|
|
14
|
+
say 'Updating gems'
|
|
15
15
|
|
|
16
16
|
libraries = ViteRuby.framework_libraries.map { |_f, library| library.name }
|
|
17
17
|
|
|
18
|
-
run_with_capture("bundle update
|
|
18
|
+
run_with_capture("bundle update #{ libraries.join(' ') }")
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
# NOTE: Spawn a new process so that it uses the updated vite_ruby.
|
|
22
22
|
def upgrade_npm_packages
|
|
23
|
-
Kernel.exec(
|
|
23
|
+
Kernel.exec('bundle exec vite upgrade_packages')
|
|
24
24
|
end
|
|
25
25
|
end
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class ViteRuby::CLI::UpgradePackages < ViteRuby::CLI::Install
|
|
4
|
-
desc
|
|
4
|
+
desc 'Upgrades the npm packages to the recommended versions.'
|
|
5
5
|
|
|
6
6
|
def call(**)
|
|
7
|
-
say
|
|
8
|
-
|
|
7
|
+
say 'Upgrading npm packages'
|
|
8
|
+
deps = js_dependencies.join(' ')
|
|
9
|
+
run_with_capture("#{ npm_install } -D #{ deps }")
|
|
9
10
|
end
|
|
10
11
|
end
|
data/lib/vite_ruby/cli/vite.rb
CHANGED
|
@@ -1,35 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class ViteRuby::CLI::Vite < Dry::CLI::Command
|
|
4
|
-
CURRENT_ENV = ENV[
|
|
5
|
-
|
|
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)
|
|
11
|
-
end
|
|
4
|
+
CURRENT_ENV = ENV['RACK_ENV'] || ENV['RAILS_ENV']
|
|
12
5
|
|
|
13
6
|
def self.shared_options
|
|
14
|
-
|
|
15
|
-
option(:
|
|
16
|
-
option(:
|
|
7
|
+
option(:mode, default: self::DEFAULT_ENV, values: %w[development production test], aliases: ['m'], desc: 'The build mode for Vite')
|
|
8
|
+
option(:clobber, desc: 'Clear cache and previous builds', type: :boolean, aliases: %w[clean clear])
|
|
9
|
+
option(:debug, desc: 'Run Vite in verbose mode, printing all debugging output', aliases: ['verbose'], type: :boolean)
|
|
10
|
+
option(:inspect, desc: 'Run Vite in a debugging session with node --inspect-brk', aliases: ['inspect-brk'], type: :boolean)
|
|
11
|
+
option(:trace_deprecation, desc: 'Run Vite in debugging mode with node --trace-deprecation', aliases: ['trace-deprecation'], type: :boolean)
|
|
17
12
|
end
|
|
18
13
|
|
|
19
|
-
def call(mode:, args: [], clobber: false,
|
|
20
|
-
ViteRuby.env[
|
|
14
|
+
def call(mode:, args: [], clobber: false, **boolean_opts)
|
|
15
|
+
ViteRuby.env['VITE_RUBY_MODE'] = mode
|
|
21
16
|
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
|
-
|
|
31
|
-
boolean_opts.map { |name, value| args << "--#{name}" if value }
|
|
32
|
-
|
|
17
|
+
boolean_opts.map { |name, value| args << "--#{ name }" if value }
|
|
33
18
|
yield(args)
|
|
34
19
|
end
|
|
35
20
|
end
|
data/lib/vite_ruby/cli.rb
CHANGED
|
@@ -1,30 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
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
|
|
11
|
-
register
|
|
12
|
-
register
|
|
13
|
-
register
|
|
14
|
-
register
|
|
15
|
-
register
|
|
16
|
-
register
|
|
17
|
-
register "upgrade_packages", UpgradePackages, aliases: ["update_packages"]
|
|
18
|
-
|
|
19
|
-
# Internal: Allows framework-specific variants to extend the CLI.
|
|
20
|
-
def self.require_framework_libraries(path = "cli")
|
|
21
|
-
ViteRuby.framework_libraries.each do |_framework, library|
|
|
22
|
-
require [library.name.tr("-", "/").to_s, path].compact.join("/")
|
|
23
|
-
end
|
|
24
|
-
rescue LoadError
|
|
25
|
-
require_framework_libraries "installation" unless path == "installation"
|
|
26
|
-
end
|
|
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 'version', Version, aliases: ['v', '-v', '--version', 'info']
|
|
15
|
+
register 'upgrade', Upgrade, aliases: ['update']
|
|
16
|
+
register 'upgrade_packages', UpgradePackages, aliases: ['update_packages']
|
|
27
17
|
end
|
|
28
|
-
|
|
29
|
-
# NOTE: This allows framework-specific variants to extend the CLI.
|
|
30
|
-
ViteRuby::CLI.require_framework_libraries("cli")
|
data/lib/vite_ruby/commands.rb
CHANGED
|
@@ -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(
|
|
12
|
+
with_node_env(ENV.fetch('NODE_ENV', 'production')) {
|
|
13
13
|
ensure_log_goes_to_stdout {
|
|
14
14
|
build(*args) || exit!
|
|
15
15
|
}
|
|
@@ -23,31 +23,52 @@ class ViteRuby::Commands
|
|
|
23
23
|
|
|
24
24
|
# Public: Removes all build cache and previously compiled assets.
|
|
25
25
|
def clobber
|
|
26
|
-
dirs = [config.build_output_dir, config.
|
|
26
|
+
dirs = [config.build_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
|
-
#
|
|
32
|
-
def
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
# Public: Receives arguments from a rake task.
|
|
32
|
+
def clean_from_task(args)
|
|
33
|
+
ensure_log_goes_to_stdout {
|
|
34
|
+
clean(keep_up_to: Integer(args.keep || 2), age_in_seconds: Integer(args.age || 3600))
|
|
35
|
+
}
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
# Public: Cleanup old assets in the output directory.
|
|
39
|
+
#
|
|
40
|
+
# keep_up_to - Max amount of backups to preserve.
|
|
41
|
+
# age_in_seconds - Amount of time to look back in order to preserve them.
|
|
42
|
+
#
|
|
43
|
+
# NOTE: By default keeps the last version, or 2 if created in the past hour.
|
|
44
|
+
#
|
|
45
|
+
# Examples:
|
|
46
|
+
# To force only 1 backup to be kept: clean(1, 0)
|
|
47
|
+
# To only keep files created within the last 10 minutes: clean(0, 600)
|
|
48
|
+
def clean(keep_up_to: 2, age_in_seconds: 3600)
|
|
49
|
+
return false unless may_clean?
|
|
50
|
+
|
|
51
|
+
versions
|
|
52
|
+
.each_with_index
|
|
53
|
+
.drop_while { |(mtime, _), index|
|
|
54
|
+
max_age = [0, Time.now - Time.at(mtime)].max
|
|
55
|
+
max_age < age_in_seconds || index < keep_up_to
|
|
56
|
+
}
|
|
57
|
+
.each do |(_, files), _|
|
|
58
|
+
clean_files(files)
|
|
59
|
+
end
|
|
60
|
+
true
|
|
41
61
|
end
|
|
42
62
|
|
|
43
|
-
# Internal:
|
|
44
|
-
def
|
|
45
|
-
`
|
|
63
|
+
# Internal: Installs the binstub for the CLI in the appropriate path.
|
|
64
|
+
def install_binstubs
|
|
65
|
+
`bundle binstub vite_ruby --path #{ config.root.join('bin') }`
|
|
66
|
+
`bundle config --delete bin`
|
|
46
67
|
end
|
|
47
68
|
|
|
48
69
|
# Internal: Verifies if ViteRuby is properly installed.
|
|
49
70
|
def verify_install
|
|
50
|
-
unless File.exist?(config.root.join(
|
|
71
|
+
unless File.exist?(config.root.join('bin/vite'))
|
|
51
72
|
warn <<~WARN
|
|
52
73
|
|
|
53
74
|
vite binstub not found.
|
|
@@ -60,7 +81,7 @@ class ViteRuby::Commands
|
|
|
60
81
|
unless config_path.exist?
|
|
61
82
|
warn <<~WARN
|
|
62
83
|
|
|
63
|
-
Configuration #{config_path} file for vite-plugin-ruby not found.
|
|
84
|
+
Configuration #{ config_path } file for vite-plugin-ruby not found.
|
|
64
85
|
Make sure `bundle exec vite install` has run successfully before running dependent tasks.
|
|
65
86
|
WARN
|
|
66
87
|
exit!
|
|
@@ -69,24 +90,24 @@ class ViteRuby::Commands
|
|
|
69
90
|
|
|
70
91
|
# Internal: Prints information about ViteRuby's environment.
|
|
71
92
|
def print_info
|
|
72
|
-
config.
|
|
73
|
-
$stdout.puts "bin/vite present?: #{File.exist?
|
|
93
|
+
Dir.chdir(config.root) do
|
|
94
|
+
$stdout.puts "bin/vite present?: #{ File.exist? 'bin/vite' }"
|
|
74
95
|
|
|
75
|
-
$stdout.puts "vite_ruby: #{ViteRuby::VERSION}"
|
|
96
|
+
$stdout.puts "vite_ruby: #{ ViteRuby::VERSION }"
|
|
76
97
|
ViteRuby.framework_libraries.each do |framework, library|
|
|
77
|
-
$stdout.puts "#{library.name}: #{library.version}"
|
|
78
|
-
$stdout.puts "#{framework}: #{Gem.loaded_specs[framework]&.version}"
|
|
98
|
+
$stdout.puts "#{ library.name }: #{ library.version }"
|
|
99
|
+
$stdout.puts "#{ framework }: #{ Gem.loaded_specs[framework]&.version }"
|
|
79
100
|
end
|
|
80
101
|
|
|
81
|
-
$stdout.puts "
|
|
82
|
-
$stdout.puts "
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
$stdout.puts "
|
|
102
|
+
$stdout.puts "node: #{ `node --version` }"
|
|
103
|
+
$stdout.puts "npm: #{ `npm --version` }"
|
|
104
|
+
$stdout.puts "yarn: #{ `yarn --version` rescue nil }"
|
|
105
|
+
$stdout.puts "pnpm: #{ `pnpm --version` rescue nil }"
|
|
106
|
+
$stdout.puts "ruby: #{ `ruby --version` }"
|
|
86
107
|
|
|
87
108
|
$stdout.puts "\n"
|
|
88
109
|
packages = `npm ls vite vite-plugin-ruby`
|
|
89
|
-
packages_msg = packages.include?(
|
|
110
|
+
packages_msg = packages.include?('vite@') ? "installed packages:\n#{ packages }" : '❌ Check that vite and vite-plugin-ruby have been added as development dependencies and installed.'
|
|
90
111
|
$stdout.puts packages_msg
|
|
91
112
|
|
|
92
113
|
ViteRuby::CompatibilityCheck.verify_plugin_version(config.root)
|
|
@@ -99,19 +120,42 @@ private
|
|
|
99
120
|
|
|
100
121
|
def_delegators :@vite_ruby, :config, :builder, :manifest, :logger, :logger=
|
|
101
122
|
|
|
123
|
+
def may_clean?
|
|
124
|
+
config.build_output_dir.exist? && config.manifest_path.exist?
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def clean_files(files)
|
|
128
|
+
files.select { |file| File.file?(file) }.each do |file|
|
|
129
|
+
File.delete(file)
|
|
130
|
+
logger.info("Removed #{ file }")
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def versions
|
|
135
|
+
all_files = Dir.glob("#{ config.build_output_dir }/**/*")
|
|
136
|
+
entries = all_files - [config.manifest_path] - current_version_files
|
|
137
|
+
entries.reject { |file| File.directory?(file) }
|
|
138
|
+
.group_by { |file| File.mtime(file).utc.to_i }
|
|
139
|
+
.sort.reverse
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def current_version_files
|
|
143
|
+
Dir.glob(manifest.refresh.values.map { |value| config.build_output_dir.join("#{ value['file'] }*") })
|
|
144
|
+
end
|
|
145
|
+
|
|
102
146
|
def with_node_env(env)
|
|
103
|
-
original = ENV[
|
|
104
|
-
ENV[
|
|
147
|
+
original = ENV['NODE_ENV']
|
|
148
|
+
ENV['NODE_ENV'] = env
|
|
105
149
|
yield
|
|
106
150
|
ensure
|
|
107
|
-
ENV[
|
|
151
|
+
ENV['NODE_ENV'] = original
|
|
108
152
|
end
|
|
109
153
|
|
|
110
154
|
def ensure_log_goes_to_stdout
|
|
111
155
|
old_logger, original_sync = logger, $stdout.sync
|
|
112
156
|
|
|
113
157
|
$stdout.sync = true
|
|
114
|
-
self.logger = Logger.new($stdout, formatter: proc { |_, _, progname, msg|
|
|
158
|
+
self.logger = Logger.new($stdout, formatter: proc { |_, _, progname, msg| progname == 'vite' ? msg : "#{ msg }\n" })
|
|
115
159
|
yield
|
|
116
160
|
ensure
|
|
117
161
|
self.logger, $stdout.sync = old_logger, original_sync
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
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(
|
|
15
|
-
requirement = package.dig(
|
|
16
|
-
|
|
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,9 +22,9 @@ 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
|
-
You may disable this check if needed: https://vite-ruby.netlify.app/config/#
|
|
27
|
+
You may disable this check if needed: https://vite-ruby.netlify.app/config/#skipCompatibilityCheck
|
|
28
28
|
|
|
29
29
|
You may upgrade both by running:
|
|
30
30
|
|
|
@@ -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
|
|
45
|
+
rescue StandardError
|
|
46
46
|
true
|
|
47
47
|
end
|
|
48
48
|
end
|
data/lib/vite_ruby/config.rb
CHANGED
|
@@ -1,39 +1,26 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
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
|
-
# Internal: Name of the metadata file written by the Vite dev server.
|
|
9
|
-
DEV_SERVER_META_FILENAME = "vite-ruby.json"
|
|
10
|
-
|
|
11
|
-
def origin
|
|
12
|
-
"#{protocol}://#{host_with_port}"
|
|
13
|
-
end
|
|
14
|
-
|
|
15
8
|
def protocol
|
|
16
|
-
https ?
|
|
9
|
+
https ? 'https' : 'http'
|
|
17
10
|
end
|
|
18
11
|
|
|
19
12
|
def host_with_port
|
|
20
|
-
"#{host}:#{port}"
|
|
13
|
+
"#{ host }:#{ port }"
|
|
21
14
|
end
|
|
22
15
|
|
|
23
|
-
# Internal: Path
|
|
24
|
-
def
|
|
25
|
-
|
|
26
|
-
# NOTE: Generated by Vite when `manifest: true`, which vite-plugin-ruby enables.
|
|
27
|
-
build_output_dir.join(".vite/manifest.json"),
|
|
28
|
-
|
|
29
|
-
# NOTE: Path where vite-plugin-ruby outputs the assets manifest file.
|
|
30
|
-
build_output_dir.join(".vite/manifest-assets.json"),
|
|
31
|
-
]
|
|
16
|
+
# Internal: Path where Vite outputs the manifest file.
|
|
17
|
+
def manifest_path
|
|
18
|
+
build_output_dir.join('manifest.json')
|
|
32
19
|
end
|
|
33
20
|
|
|
34
|
-
# Internal: Path
|
|
35
|
-
def
|
|
36
|
-
|
|
21
|
+
# Internal: Path where vite-plugin-ruby outputs the assets manifest file.
|
|
22
|
+
def assets_manifest_path
|
|
23
|
+
build_output_dir.join('manifest-assets.json')
|
|
37
24
|
end
|
|
38
25
|
|
|
39
26
|
# Public: The directory where Vite will store the built assets.
|
|
@@ -41,11 +28,6 @@ class ViteRuby::Config
|
|
|
41
28
|
root.join(public_dir, public_output_dir)
|
|
42
29
|
end
|
|
43
30
|
|
|
44
|
-
# Internal: Path to the metadata file written by the Vite dev server.
|
|
45
|
-
def dev_server_meta_path
|
|
46
|
-
root.join("tmp", DEV_SERVER_META_FILENAME)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
31
|
# Public: The directory where the entries are located.
|
|
50
32
|
def resolved_entrypoints_dir
|
|
51
33
|
vite_root_dir.join(entrypoints_dir)
|
|
@@ -53,7 +35,7 @@ class ViteRuby::Config
|
|
|
53
35
|
|
|
54
36
|
# Internal: The directory where Vite stores its processing cache.
|
|
55
37
|
def vite_cache_dir
|
|
56
|
-
root.join(
|
|
38
|
+
root.join('node_modules/.vite')
|
|
57
39
|
end
|
|
58
40
|
|
|
59
41
|
# Public: The directory that Vite uses as root.
|
|
@@ -63,64 +45,46 @@ class ViteRuby::Config
|
|
|
63
45
|
|
|
64
46
|
# Public: Loads an optional config/vite.rb file that can modify ViteRuby.env
|
|
65
47
|
def load_ruby_config
|
|
66
|
-
rb_config_path = File.expand_path(config_path.sub(/.json$/,
|
|
48
|
+
rb_config_path = File.expand_path(config_path.sub(/.json$/, '.rb'), root)
|
|
67
49
|
load rb_config_path if File.exist?(rb_config_path)
|
|
68
50
|
end
|
|
69
51
|
|
|
70
52
|
# Public: Sets additional environment variables for vite-plugin-ruby.
|
|
71
|
-
def to_env
|
|
53
|
+
def to_env
|
|
72
54
|
CONFIGURABLE_WITH_ENV.each_with_object({}) do |option, env|
|
|
73
55
|
unless (value = @config[option]).nil?
|
|
74
|
-
env["#{ViteRuby::ENV_PREFIX}_#{option.upcase}"] = value.to_s
|
|
56
|
+
env["#{ ViteRuby::ENV_PREFIX }_#{ option.upcase }"] = value.to_s
|
|
75
57
|
end
|
|
76
|
-
end.merge(
|
|
58
|
+
end.merge(ViteRuby.env)
|
|
77
59
|
end
|
|
78
60
|
|
|
79
61
|
# Internal: Files and directories that should be watched for changes.
|
|
80
62
|
def watched_paths
|
|
81
63
|
[
|
|
82
64
|
*(watch_additional_paths + additional_entrypoints).reject { |dir|
|
|
83
|
-
dir.start_with?(
|
|
65
|
+
dir.start_with?('~/') || dir.start_with?(source_code_dir)
|
|
84
66
|
},
|
|
85
|
-
"#{source_code_dir}/**/*",
|
|
86
|
-
config_path.sub(/.json$/,
|
|
67
|
+
"#{ source_code_dir }/**/*",
|
|
68
|
+
config_path.sub(/.json$/, '.{rb,json}'),
|
|
87
69
|
*DEFAULT_WATCHED_PATHS,
|
|
88
70
|
].freeze
|
|
89
71
|
end
|
|
90
72
|
|
|
91
|
-
# Internal: Changes the current directory to the root dir.
|
|
92
|
-
def within_root(&block)
|
|
93
|
-
Dir.chdir(File.expand_path(root), &block)
|
|
94
|
-
end
|
|
95
|
-
|
|
96
73
|
private
|
|
97
74
|
|
|
98
75
|
# Internal: Coerces all the configuration values, in case they were passed
|
|
99
76
|
# as environment variables which are always strings.
|
|
100
77
|
def coerce_values(config)
|
|
101
|
-
config[
|
|
102
|
-
config[
|
|
103
|
-
config[
|
|
104
|
-
config[
|
|
105
|
-
config
|
|
106
|
-
config["dev_server_connect_timeout"] = config["dev_server_connect_timeout"].to_f
|
|
107
|
-
coerce_booleans(config, "auto_build", "dev_server_connection_check", "hide_build_console_output", "https", "skip_compatibility_check", "skip_proxy")
|
|
108
|
-
config["package_manager"] ||= detect_package_manager(root)
|
|
78
|
+
config['mode'] = config['mode'].to_s
|
|
79
|
+
config['port'] = config['port'].to_i
|
|
80
|
+
config['root'] = Pathname.new(config['root'])
|
|
81
|
+
config['build_cache_dir'] = config['root'].join(config['build_cache_dir'])
|
|
82
|
+
coerce_booleans(config, 'auto_build', 'hide_build_console_output', 'https', 'skip_compatibility_check')
|
|
109
83
|
end
|
|
110
84
|
|
|
111
85
|
# Internal: Coerces configuration options to boolean.
|
|
112
86
|
def coerce_booleans(config, *names)
|
|
113
|
-
|
|
114
|
-
names.each { |name| config[name] = truthy.include?(config[name]) }
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
def detect_package_manager(root)
|
|
118
|
-
return "npm" if root.join("package-lock.json").exist?
|
|
119
|
-
return "pnpm" if root.join("pnpm-lock.yaml").exist?
|
|
120
|
-
return "bun" if root.join("bun.lockb").exist? || root.join("bun.lock").exist?
|
|
121
|
-
return "yarn" if root.join("yarn.lock").exist?
|
|
122
|
-
|
|
123
|
-
"npm"
|
|
87
|
+
names.each { |name| config[name] = [true, 'true'].include?(config[name]) }
|
|
124
88
|
end
|
|
125
89
|
|
|
126
90
|
def initialize(attrs)
|
|
@@ -134,8 +98,8 @@ private
|
|
|
134
98
|
# Public: Returns the project configuration for Vite.
|
|
135
99
|
def resolve_config(**attrs)
|
|
136
100
|
config = config_defaults.merge(attrs.transform_keys(&:to_s))
|
|
137
|
-
file_path = File.join(config[
|
|
138
|
-
file_config = config_from_file(file_path, mode: config[
|
|
101
|
+
file_path = File.join(config['root'], config['config_path'])
|
|
102
|
+
file_config = config_from_file(file_path, mode: config['mode'])
|
|
139
103
|
new DEFAULT_CONFIG.merge(file_config).merge(config_from_env).merge(config)
|
|
140
104
|
end
|
|
141
105
|
|
|
@@ -143,21 +107,21 @@ private
|
|
|
143
107
|
|
|
144
108
|
# Internal: Converts camelCase to snake_case.
|
|
145
109
|
SNAKE_CASE = ->(camel_cased_word) {
|
|
146
|
-
camel_cased_word.to_s.gsub(
|
|
110
|
+
camel_cased_word.to_s.gsub(/::/, '/')
|
|
147
111
|
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
|
148
112
|
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
|
149
|
-
.tr(
|
|
113
|
+
.tr('-', '_')
|
|
150
114
|
.downcase
|
|
151
115
|
}
|
|
152
116
|
|
|
153
117
|
# Internal: Default values for a Ruby application.
|
|
154
|
-
def config_defaults(asset_host: nil, mode: ENV.fetch(
|
|
118
|
+
def config_defaults(asset_host: nil, mode: ENV.fetch('RACK_ENV', 'development'), root: Dir.pwd)
|
|
155
119
|
{
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
120
|
+
'asset_host' => option_from_env('asset_host') || asset_host,
|
|
121
|
+
'config_path' => option_from_env('config_path') || DEFAULT_CONFIG.fetch('config_path'),
|
|
122
|
+
'mode' => option_from_env('mode') || mode,
|
|
123
|
+
'root' => option_from_env('root') || root,
|
|
124
|
+
}
|
|
161
125
|
end
|
|
162
126
|
|
|
163
127
|
# Internal: Used to load a JSON file from the specified path.
|
|
@@ -171,7 +135,7 @@ private
|
|
|
171
135
|
|
|
172
136
|
# Internal: Retrieves a configuration option from environment variables.
|
|
173
137
|
def option_from_env(name)
|
|
174
|
-
ViteRuby.env["#{ViteRuby::ENV_PREFIX}_#{name.upcase}"]
|
|
138
|
+
ViteRuby.env["#{ ViteRuby::ENV_PREFIX }_#{ name.upcase }"]
|
|
175
139
|
end
|
|
176
140
|
|
|
177
141
|
# Internal: Extracts the configuration options provided as env vars.
|
|
@@ -186,16 +150,16 @@ private
|
|
|
186
150
|
# Internal: Loads the configuration options provided in a JSON file.
|
|
187
151
|
def config_from_file(path, mode:)
|
|
188
152
|
multi_env_config = load_json(path)
|
|
189
|
-
multi_env_config.fetch(
|
|
153
|
+
multi_env_config.fetch('all', {})
|
|
190
154
|
.merge(multi_env_config.fetch(mode, {}))
|
|
191
155
|
rescue Errno::ENOENT => error
|
|
192
|
-
$stderr << "Check that your vite.json configuration file is available in the load path:\n\n\t#{error.message}\n\n"
|
|
156
|
+
$stderr << "Check that your vite.json configuration file is available in the load path:\n\n\t#{ error.message }\n\n"
|
|
193
157
|
{}
|
|
194
158
|
end
|
|
195
159
|
end
|
|
196
160
|
|
|
197
161
|
# Internal: Shared configuration with the Vite plugin for Ruby.
|
|
198
|
-
DEFAULT_CONFIG = load_json("#{__dir__}/../../default.vite.json").freeze
|
|
162
|
+
DEFAULT_CONFIG = load_json("#{ __dir__ }/../../default.vite.json").freeze
|
|
199
163
|
|
|
200
164
|
# Internal: Configuration options that can not be provided as env vars.
|
|
201
165
|
NOT_CONFIGURABLE_WITH_ENV = %w[additional_entrypoints watch_additional_paths].freeze
|
|
@@ -205,15 +169,12 @@ private
|
|
|
205
169
|
|
|
206
170
|
# Internal: If any of these files is modified the build won't be skipped.
|
|
207
171
|
DEFAULT_WATCHED_PATHS = %w[
|
|
208
|
-
bun.lockb
|
|
209
172
|
package-lock.json
|
|
210
173
|
package.json
|
|
211
174
|
pnpm-lock.yaml
|
|
212
175
|
postcss.config.js
|
|
213
176
|
tailwind.config.js
|
|
214
177
|
vite.config.js
|
|
215
|
-
vite.config.mjs
|
|
216
|
-
vite.config.mts
|
|
217
178
|
vite.config.ts
|
|
218
179
|
windi.config.ts
|
|
219
180
|
yarn.lock
|