vite_ruby 3.9.1 → 4.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -409
- data/README.md +1 -1
- data/default.vite.json +2 -7
- 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 +34 -29
- 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 +8 -8
- data/lib/vite_ruby/cli/install.rb +38 -43
- 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 -30
- data/lib/vite_ruby/compatibility_check.rb +8 -8
- data/lib/vite_ruby/config.rb +37 -67
- data/lib/vite_ruby/dev_server_proxy.rb +24 -30
- data/lib/vite_ruby/error.rb +1 -1
- data/lib/vite_ruby/io.rb +4 -4
- data/lib/vite_ruby/manifest.rb +32 -46
- data/lib/vite_ruby/missing_entrypoint_error.rb +14 -22
- 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 +34 -49
- metadata +10 -46
- data/lib/vite_ruby/cli/ssr.rb +0 -27
data/lib/vite_ruby/build.rb
CHANGED
@@ -1,35 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require "time"
|
3
|
+
require 'time'
|
5
4
|
|
6
5
|
# Internal: Value object with information about the last build.
|
7
|
-
ViteRuby::Build = Struct.new(:success, :timestamp, :vite_ruby, :digest, :current_digest
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
# Internal: Reads metadata recorded on the last build, if it exists.
|
21
|
-
def parse_metadata(pathname)
|
22
|
-
return default_metadata unless pathname.exist?
|
23
|
-
|
24
|
-
JSON.parse(pathname.read.to_s).transform_keys(&:to_sym).slice(*members)
|
25
|
-
rescue JSON::JSONError, Errno::ENOENT, Errno::ENOTDIR
|
26
|
-
default_metadata
|
27
|
-
end
|
28
|
-
|
29
|
-
# Internal: To make it evident that there's no last build in error messages.
|
30
|
-
def default_metadata
|
31
|
-
{timestamp: "never", digest: "none"}
|
32
|
-
end
|
6
|
+
ViteRuby::Build = Struct.new(:success, :timestamp, :vite_ruby, :digest, :current_digest) do
|
7
|
+
# Internal: Combines information from a previous build with the current digest.
|
8
|
+
def self.from_previous(attrs, current_digest)
|
9
|
+
new(
|
10
|
+
attrs['success'],
|
11
|
+
attrs['timestamp'] || 'never',
|
12
|
+
attrs['vite_ruby'] || 'unknown',
|
13
|
+
attrs['digest'] || 'none',
|
14
|
+
current_digest,
|
15
|
+
)
|
33
16
|
end
|
34
17
|
|
35
18
|
# Internal: A build is considered stale when watched files have changed since
|
@@ -53,30 +36,17 @@ ViteRuby::Build = Struct.new(:success, :timestamp, :vite_ruby, :digest, :current
|
|
53
36
|
end
|
54
37
|
|
55
38
|
# Internal: Returns a new build with the specified result.
|
56
|
-
def with_result(
|
39
|
+
def with_result(success)
|
57
40
|
self.class.new(
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
current_digest: current_digest,
|
63
|
-
last_build_path: last_build_path,
|
41
|
+
success,
|
42
|
+
Time.now.strftime('%F %T'),
|
43
|
+
ViteRuby::VERSION,
|
44
|
+
current_digest,
|
64
45
|
)
|
65
46
|
end
|
66
47
|
|
67
|
-
# Internal: Writes the result of the new build to a local file.
|
68
|
-
def write_to_cache
|
69
|
-
last_build_path.write to_json
|
70
|
-
end
|
71
|
-
|
72
48
|
# Internal: Returns a JSON string with the metadata of the build.
|
73
49
|
def to_json(*_args)
|
74
|
-
JSON.pretty_generate(
|
75
|
-
success: success,
|
76
|
-
errors: errors,
|
77
|
-
timestamp: timestamp,
|
78
|
-
vite_ruby: vite_ruby,
|
79
|
-
digest: digest,
|
80
|
-
)
|
50
|
+
JSON.pretty_generate(to_h)
|
81
51
|
end
|
82
52
|
end
|
data/lib/vite_ruby/builder.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'json'
|
4
|
+
require 'digest/sha1'
|
4
5
|
|
5
6
|
# Public: Keeps track of watched files and triggers builds as needed.
|
6
7
|
class ViteRuby::Builder
|
@@ -11,25 +12,21 @@ class ViteRuby::Builder
|
|
11
12
|
# Public: Checks if the watched files have changed since the last compilation,
|
12
13
|
# and triggers a Vite build if any files have changed.
|
13
14
|
def build(*args)
|
14
|
-
last_build = last_build_metadata
|
15
|
-
|
16
|
-
|
17
|
-
stdout, stderr, status = build_with_vite(*args)
|
18
|
-
log_build_result(stdout, stderr, status)
|
19
|
-
record_build_metadata(last_build, errors: stderr, success: status.success?)
|
20
|
-
status.success?
|
15
|
+
last_build = last_build_metadata
|
16
|
+
if args.delete('--force') || last_build.stale?
|
17
|
+
build_with_vite(*args).tap { |success| record_build_metadata(success, last_build) }
|
21
18
|
elsif last_build.success
|
22
|
-
logger.debug "Skipping vite build. Watched files have not changed since the last build at #{last_build.timestamp}"
|
19
|
+
logger.debug "Skipping vite build. Watched files have not changed since the last build at #{ last_build.timestamp }"
|
23
20
|
true
|
24
21
|
else
|
25
|
-
logger.error "Skipping vite build. Watched files have not changed since the build failed at #{last_build.timestamp} ❌"
|
22
|
+
logger.error "Skipping vite build. Watched files have not changed since the build failed at #{ last_build.timestamp } ❌"
|
26
23
|
false
|
27
24
|
end
|
28
25
|
end
|
29
26
|
|
30
27
|
# Internal: Reads the result of the last compilation from disk.
|
31
|
-
def last_build_metadata
|
32
|
-
ViteRuby::Build.from_previous(
|
28
|
+
def last_build_metadata
|
29
|
+
ViteRuby::Build.from_previous(last_build_attrs, watched_files_digest)
|
33
30
|
end
|
34
31
|
|
35
32
|
private
|
@@ -38,35 +35,44 @@ private
|
|
38
35
|
|
39
36
|
def_delegators :@vite_ruby, :config, :logger, :run
|
40
37
|
|
38
|
+
# Internal: Reads metadata recorded on the last build, if it exists.
|
39
|
+
def last_build_attrs
|
40
|
+
last_build_path.exist? ? JSON.parse(last_build_path.read.to_s) : {}
|
41
|
+
rescue JSON::JSONError, Errno::ENOENT, Errno::ENOTDIR
|
42
|
+
{}
|
43
|
+
end
|
44
|
+
|
41
45
|
# Internal: Writes a digest of the watched files to disk for future checks.
|
42
|
-
def record_build_metadata(
|
46
|
+
def record_build_metadata(success, build)
|
43
47
|
config.build_cache_dir.mkpath
|
44
|
-
build.with_result(
|
48
|
+
last_build_path.write build.with_result(success).to_json
|
45
49
|
end
|
46
50
|
|
47
51
|
# Internal: The file path where metadata of the last build is stored.
|
48
|
-
def last_build_path
|
49
|
-
config.build_cache_dir.join("last
|
52
|
+
def last_build_path
|
53
|
+
config.build_cache_dir.join("last-build-#{ config.mode }.json")
|
50
54
|
end
|
51
55
|
|
52
56
|
# Internal: Returns a digest of all the watched files, allowing to detect
|
53
57
|
# changes, and skip Vite builds if no files have changed.
|
54
58
|
def watched_files_digest
|
55
|
-
|
56
|
-
|
57
|
-
config.within_root do
|
59
|
+
Dir.chdir File.expand_path(config.root) do
|
58
60
|
files = Dir[*config.watched_paths].reject { |f| File.directory?(f) }
|
59
|
-
file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
|
60
|
-
|
61
|
-
@last_digest = Digest::SHA1.hexdigest(file_ids.join("/"))
|
61
|
+
file_ids = files.sort.map { |f| "#{ File.basename(f) }/#{ Digest::SHA1.file(f).hexdigest }" }
|
62
|
+
Digest::SHA1.hexdigest(file_ids.join('/'))
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
65
66
|
# Public: Initiates a Vite build command to generate assets.
|
67
|
+
#
|
68
|
+
# Returns true if the build is successful, or false if it failed.
|
66
69
|
def build_with_vite(*args)
|
67
|
-
logger.info
|
70
|
+
logger.info 'Building with Vite ⚡️'
|
71
|
+
|
72
|
+
stdout, stderr, status = run(['build', *args])
|
73
|
+
log_build_result(stdout, stderr.to_s, status)
|
68
74
|
|
69
|
-
|
75
|
+
status.success?
|
70
76
|
end
|
71
77
|
|
72
78
|
# Internal: Outputs the build results.
|
@@ -74,13 +80,12 @@ private
|
|
74
80
|
# NOTE: By default it also outputs the manifest entries.
|
75
81
|
def log_build_result(_stdout, stderr, status)
|
76
82
|
if status.success?
|
77
|
-
logger.info "Build with Vite complete: #{config.build_output_dir}"
|
78
|
-
logger.error stderr unless stderr.empty?
|
83
|
+
logger.info "Build with Vite complete: #{ config.build_output_dir }"
|
84
|
+
logger.error stderr.to_s unless stderr.empty?
|
79
85
|
else
|
80
86
|
logger.error stderr
|
81
|
-
logger.error
|
82
|
-
logger.error
|
83
|
-
logger.error "❌ Check that vite and vite-plugin-ruby are in devDependencies and have been installed. " if stderr.include?("ERR! canceled")
|
87
|
+
logger.error 'Build with Vite failed! ❌'
|
88
|
+
logger.error '❌ Check that vite and vite-plugin-ruby are in devDependencies and have been installed. ' if stderr.include?('ERR! canceled')
|
84
89
|
end
|
85
90
|
end
|
86
91
|
end
|
data/lib/vite_ruby/cli/build.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class ViteRuby::CLI::Build < ViteRuby::CLI::Vite
|
4
|
-
DEFAULT_ENV = CURRENT_ENV ||
|
4
|
+
DEFAULT_ENV = CURRENT_ENV || 'production'
|
5
5
|
|
6
|
-
desc
|
6
|
+
desc 'Bundle all entrypoints using Vite.'
|
7
7
|
shared_options
|
8
|
-
option(:
|
9
|
-
option(:
|
10
|
-
option(:watch, desc: "Start the Rollup watcher and rebuild on files changes", type: :boolean)
|
11
|
-
option(:profile, desc: "Gather performance metrics from the build ", type: :boolean)
|
8
|
+
option(:force, desc: 'Force the build even if assets have not changed', type: :boolean)
|
9
|
+
option(:watch, desc: 'Start the Rollup watcher and rebuild on files changes', type: :boolean)
|
12
10
|
|
13
11
|
def call(**options)
|
14
12
|
super { |args| ViteRuby.commands.build_from_task(*args) }
|
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class ViteRuby::CLI::Clobber < Dry::CLI::Command
|
4
|
-
desc
|
4
|
+
desc 'Clear the Vite cache, temp files, and builds'
|
5
5
|
|
6
|
-
current_env = ENV[
|
6
|
+
current_env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'
|
7
7
|
|
8
|
-
option(:mode, default: current_env, values: %w[development production test], aliases: [
|
8
|
+
option(:mode, default: current_env, values: %w[development production test], aliases: ['m'], desc: 'The mode to use')
|
9
9
|
|
10
10
|
def call(mode:, **)
|
11
|
-
ViteRuby.env[
|
11
|
+
ViteRuby.env['VITE_RUBY_MODE'] = mode
|
12
12
|
ViteRuby.commands.clobber
|
13
13
|
end
|
14
14
|
end
|
data/lib/vite_ruby/cli/dev.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class ViteRuby::CLI::Dev < ViteRuby::CLI::Vite
|
4
|
-
DEFAULT_ENV = CURRENT_ENV ||
|
4
|
+
DEFAULT_ENV = CURRENT_ENV || 'development'
|
5
5
|
|
6
|
-
desc
|
6
|
+
desc 'Start the Vite development server.'
|
7
7
|
shared_options
|
8
|
-
option(:force, desc:
|
8
|
+
option(:force, desc: 'Force Vite to re-bundle dependencies', type: :boolean)
|
9
9
|
|
10
10
|
def call(**options)
|
11
11
|
super { |args| ViteRuby.run(args, exec: true) }
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'pathname'
|
4
|
+
require 'fileutils'
|
5
5
|
|
6
6
|
# NOTE: Extracted from dry-cli version 0.6.0, which later removed this file as
|
7
7
|
# it was refactored and extracted into the more complete (and complex) dry-files.
|
@@ -36,7 +36,7 @@ module ViteRuby::CLI::FileUtils
|
|
36
36
|
return if content.join.include?(contents)
|
37
37
|
|
38
38
|
content << "\n" unless content.last&.end_with?("\n")
|
39
|
-
content << "#{contents}\n"
|
39
|
+
content << "#{ contents }\n"
|
40
40
|
|
41
41
|
write(path, content)
|
42
42
|
end
|
@@ -47,7 +47,7 @@ module ViteRuby::CLI::FileUtils
|
|
47
47
|
# @api private
|
48
48
|
def replace_first_line(path, target, replacement)
|
49
49
|
content = read_lines(path)
|
50
|
-
content[index(content, path, target)] = "#{replacement}\n"
|
50
|
+
content[index(content, path, target)] = "#{ replacement }\n"
|
51
51
|
|
52
52
|
write(path, content)
|
53
53
|
end
|
@@ -95,14 +95,14 @@ module ViteRuby::CLI::FileUtils
|
|
95
95
|
# @api private
|
96
96
|
def index(content, path, target)
|
97
97
|
content.index { |line| line.include?(target) } ||
|
98
|
-
raise(ArgumentError, "Cannot find `#{target}' inside `#{path}'.")
|
98
|
+
raise(ArgumentError, "Cannot find `#{ target }' inside `#{ path }'.")
|
99
99
|
end
|
100
100
|
|
101
101
|
# @since 1.2.11
|
102
102
|
# @api private
|
103
103
|
def rindex(content, path, target)
|
104
104
|
content.rindex { |line| line.include?(target) } ||
|
105
|
-
raise(ArgumentError, "Cannot find `#{target}' inside `#{path}'.")
|
105
|
+
raise(ArgumentError, "Cannot find `#{ target }' inside `#{ path }'.")
|
106
106
|
end
|
107
107
|
|
108
108
|
# @since 1.2.11
|
@@ -113,7 +113,7 @@ module ViteRuby::CLI::FileUtils
|
|
113
113
|
|
114
114
|
i = finder.call(content, path, target)
|
115
115
|
|
116
|
-
content.insert(i, "#{contents}\n")
|
116
|
+
content.insert(i, "#{ contents }\n")
|
117
117
|
write(path, content)
|
118
118
|
end
|
119
119
|
|
@@ -125,7 +125,7 @@ module ViteRuby::CLI::FileUtils
|
|
125
125
|
|
126
126
|
i = finder.call(content, path, target)
|
127
127
|
|
128
|
-
content.insert(i + 1, "#{contents}\n")
|
128
|
+
content.insert(i + 1, "#{ contents }\n")
|
129
129
|
write(path, content)
|
130
130
|
end
|
131
131
|
end
|
@@ -1,31 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require "json"
|
3
|
+
require 'stringio'
|
5
4
|
|
6
5
|
class ViteRuby::CLI::Install < Dry::CLI::Command
|
7
|
-
desc
|
8
|
-
|
9
|
-
option(:package_manager, values: %w[npm pnpm yarn bun], aliases: %w[package-manager with], desc: "The package manager to use when installing JS dependencies.")
|
10
|
-
|
11
|
-
def call(package_manager: nil, **)
|
12
|
-
ENV["VITE_RUBY_PACKAGE_MANAGER"] ||= package_manager if package_manager
|
6
|
+
desc 'Performs the initial configuration setup to get started with Vite Ruby.'
|
13
7
|
|
8
|
+
def call(**)
|
14
9
|
$stdout.sync = true
|
15
10
|
|
16
|
-
say
|
11
|
+
say 'Creating binstub'
|
17
12
|
ViteRuby.commands.install_binstubs
|
18
13
|
|
19
|
-
say
|
14
|
+
say 'Creating configuration files'
|
20
15
|
create_configuration_files
|
21
16
|
|
22
|
-
say
|
17
|
+
say 'Installing sample files'
|
23
18
|
install_sample_files
|
24
19
|
|
25
|
-
say
|
20
|
+
say 'Installing js dependencies'
|
26
21
|
install_js_dependencies
|
27
22
|
|
28
|
-
say
|
23
|
+
say 'Adding files to .gitignore'
|
29
24
|
install_gitignore
|
30
25
|
|
31
26
|
say "\nVite ⚡️ Ruby successfully installed! 🎉"
|
@@ -36,30 +31,30 @@ protected
|
|
36
31
|
# Internal: The JS packages that should be added to the app.
|
37
32
|
def js_dependencies
|
38
33
|
[
|
39
|
-
"vite@#{ViteRuby::DEFAULT_VITE_VERSION}",
|
40
|
-
"vite-plugin-ruby@#{ViteRuby::DEFAULT_PLUGIN_VERSION}",
|
34
|
+
"vite@#{ ViteRuby::DEFAULT_VITE_VERSION }",
|
35
|
+
"vite-plugin-ruby@#{ ViteRuby::DEFAULT_PLUGIN_VERSION }",
|
41
36
|
]
|
42
37
|
end
|
43
38
|
|
44
39
|
# Internal: Setup for a plain Rack application.
|
45
40
|
def setup_app_files
|
46
|
-
copy_template
|
41
|
+
copy_template 'config/vite.json', to: config.config_path
|
47
42
|
|
48
|
-
if (rackup_file = root.join(
|
49
|
-
inject_line_after_last rackup_file,
|
43
|
+
if (rackup_file = root.join('config.ru')).exist?
|
44
|
+
inject_line_after_last rackup_file, 'require', 'use(ViteRuby::DevServerProxy, ssl_verify_none: true) if ViteRuby.run_proxy?'
|
50
45
|
end
|
51
46
|
end
|
52
47
|
|
53
48
|
# Internal: Create a sample JS file and attempt to inject it in an HTML template.
|
54
49
|
def install_sample_files
|
55
|
-
copy_template
|
50
|
+
copy_template 'entrypoints/application.js', to: config.resolved_entrypoints_dir.join('application.js')
|
56
51
|
end
|
57
52
|
|
58
53
|
private
|
59
54
|
|
60
55
|
extend Forwardable
|
61
56
|
|
62
|
-
def_delegators
|
57
|
+
def_delegators 'ViteRuby', :config
|
63
58
|
|
64
59
|
%i[append cp inject_line_after inject_line_after_last inject_line_before replace_first_line write].each do |util|
|
65
60
|
define_method(util) { |*args|
|
@@ -67,7 +62,7 @@ private
|
|
67
62
|
}
|
68
63
|
end
|
69
64
|
|
70
|
-
TEMPLATES_PATH = Pathname.new(File.expand_path(
|
65
|
+
TEMPLATES_PATH = Pathname.new(File.expand_path('../../../templates', __dir__))
|
71
66
|
|
72
67
|
def copy_template(path, to:)
|
73
68
|
cp TEMPLATES_PATH.join(path), to
|
@@ -75,39 +70,30 @@ private
|
|
75
70
|
|
76
71
|
# Internal: Creates the Vite and vite-plugin-ruby configuration files.
|
77
72
|
def create_configuration_files
|
78
|
-
copy_template
|
79
|
-
append root.join(
|
73
|
+
copy_template 'config/vite.config.ts', to: root.join('vite.config.ts')
|
74
|
+
append root.join('Procfile.dev'), 'vite: bin/vite dev'
|
80
75
|
setup_app_files
|
81
76
|
ViteRuby.reload_with(config_path: config.config_path)
|
82
77
|
end
|
83
78
|
|
84
79
|
# Internal: Installs vite and vite-plugin-ruby at the project level.
|
85
80
|
def install_js_dependencies
|
86
|
-
package_json = root.join(
|
87
|
-
unless package_json.exist?
|
88
|
-
|
89
|
-
|
90
|
-
"private": true,
|
91
|
-
"type": "module"
|
92
|
-
}
|
93
|
-
JSON
|
94
|
-
end
|
95
|
-
|
96
|
-
if (JSON.parse(package_json.read)["type"] != "module" rescue nil)
|
97
|
-
FileUtils.mv root.join("vite.config.ts"), root.join("vite.config.mts"), force: true, verbose: true
|
98
|
-
end
|
99
|
-
|
100
|
-
install_js_packages js_dependencies.join(" ")
|
81
|
+
package_json = root.join('package.json')
|
82
|
+
write(package_json, '{}') unless package_json.exist?
|
83
|
+
deps = js_dependencies.join(' ')
|
84
|
+
run_with_capture("#{ npm_install } -D #{ deps }", stdin_data: "\n")
|
101
85
|
end
|
102
86
|
|
103
87
|
# Internal: Adds compilation output dirs to git ignore.
|
104
88
|
def install_gitignore
|
105
|
-
return unless (gitignore_file = root.join(
|
89
|
+
return unless (gitignore_file = root.join('.gitignore')).exist?
|
106
90
|
|
107
91
|
append(gitignore_file, <<~GITIGNORE)
|
108
92
|
|
109
93
|
# Vite Ruby
|
110
|
-
/public/vite
|
94
|
+
/public/vite
|
95
|
+
/public/vite-dev
|
96
|
+
/public/vite-test
|
111
97
|
node_modules
|
112
98
|
# Vite uses dotenv and suggests to ignore local-only env files. See
|
113
99
|
# https://vitejs.dev/guide/env-and-mode.html#env-files
|
@@ -127,12 +113,16 @@ private
|
|
127
113
|
def run_with_capture(*args, **options)
|
128
114
|
Dir.chdir(root) do
|
129
115
|
_, stderr, status = ViteRuby::IO.capture(*args, **options)
|
130
|
-
say(stderr) unless status.success? || stderr.empty?
|
116
|
+
say(stderr) unless status.success? || stderr.to_s.empty?
|
131
117
|
end
|
132
118
|
end
|
133
119
|
|
134
|
-
|
135
|
-
|
120
|
+
# Internal: Support all popular package managers.
|
121
|
+
def npm_install
|
122
|
+
return 'yarn add' if root.join('yarn.lock').exist?
|
123
|
+
return 'pnpm install' if root.join('pnpm-lock.yaml').exist?
|
124
|
+
|
125
|
+
'npm install'
|
136
126
|
end
|
137
127
|
|
138
128
|
# Internal: Avoid printing warning about missing vite.json, we will create one.
|
@@ -144,3 +134,8 @@ private
|
|
144
134
|
$stderr = old_stderr
|
145
135
|
end
|
146
136
|
end
|
137
|
+
|
138
|
+
# NOTE: This allows framework-specific variants to extend the installation.
|
139
|
+
ViteRuby.framework_libraries.each do |_framework, library|
|
140
|
+
require "#{ library.name.tr('-', '/') }/installation"
|
141
|
+
end
|
@@ -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 #{libraries.join(
|
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")
|