vite_rails 1.0.5 → 1.0.10
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 +22 -0
- data/CONTRIBUTING.md +0 -1
- data/README.md +31 -69
- data/lib/install/javascript/entrypoints/application.js +8 -4
- data/lib/install/template.rb +1 -1
- data/lib/tasks/vite/build.rake +2 -6
- data/lib/tasks/vite/clean.rake +1 -3
- data/lib/tasks/vite/info.rake +0 -1
- data/lib/tasks/vite/verify_install.rake +3 -3
- data/lib/vite_rails.rb +14 -25
- data/lib/vite_rails/builder.rb +11 -13
- data/lib/vite_rails/commands.rb +51 -10
- data/lib/vite_rails/config.rb +65 -30
- data/lib/vite_rails/dev_server_proxy.rb +26 -18
- data/lib/vite_rails/helper.rb +15 -8
- data/lib/vite_rails/manifest.rb +6 -4
- data/lib/vite_rails/runner.rb +3 -6
- data/lib/vite_rails/version.rb +1 -1
- data/package.json +5 -1
- data/package/default.vite.json +2 -1
- data/test/builder_test.rb +27 -22
- data/test/commands_test.rb +67 -0
- data/test/config_test.rb +133 -0
- data/test/dev_server_proxy_test.rb +101 -0
- data/test/dev_server_test.rb +0 -30
- data/test/engine_rake_tasks_test.rb +55 -17
- data/test/helper_test.rb +37 -105
- data/test/manifest_test.rb +33 -29
- data/test/mode_test.rb +6 -11
- data/test/mounted_app/test/dummy/config/vite.json +5 -11
- data/test/mounted_app/test/dummy/package.json +2 -1
- data/test/mounted_app/test/dummy/yarn.lock +208 -0
- data/test/rake_tasks_test.rb +5 -19
- data/test/runner_test.rb +31 -0
- data/test/test_app/app/frontend/entrypoints/application.js +2 -0
- data/test/test_app/config/vite.json +0 -2
- data/test/test_app/config/vite_additional_paths.json +5 -0
- data/test/test_app/config/vite_public_dir.json +5 -0
- data/test/test_app/public/vite-production/manifest.json +22 -0
- data/test/test_helper.rb +48 -14
- metadata +23 -25
- data/test/command_test.rb +0 -35
- data/test/configuration_test.rb +0 -80
- data/test/dev_server_runner_test.rb +0 -83
- data/test/test_app/app/javascript/entrypoints/application.js +0 -10
- data/test/test_app/app/javascript/entrypoints/multi_entry.css +0 -4
- data/test/test_app/app/javascript/entrypoints/multi_entry.js +0 -4
- data/test/test_app/config/vite_public_root.yml +0 -20
- data/test/test_app/public/vite/manifest.json +0 -36
- data/test/vite_runner_test.rb +0 -59
- data/test/webpacker_test.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de1ef14e664eb5f06527b1126e5508598d50289c855edbf27d6e373f6ba805e1
|
4
|
+
data.tar.gz: 6754500ef38d0dcaba0e7d89de42da5b6f51fff79e215d9a4b02f116a332e13d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3207af12e9a3304b12da3f549c4ae1fe2610bb9a5511f164040b8a7e82e2324b472ebbc90163142db6fa06a5752acac2f38fb0c6ecefed0690ae6f8c2981702f
|
7
|
+
data.tar.gz: c84109e20d0ba02b594de5e99922abeee23cc69146b955816c29aed23a1b65cc1db7e6e39eae67458fb8ceb74b38784e22fad5ac98f6c209f5697e46e0bca9ac
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
## Vite Rails 1.0.10 (2020-01-23)
|
2
|
+
|
3
|
+
- Use `path_to_asset` in `vite_asset_path` so that it's prefixed automatically
|
4
|
+
when using a CDN (`config.action_controller.asset_host`).
|
5
|
+
|
6
|
+
## Vite Rails 1.0.9 (2020-01-22)
|
7
|
+
|
8
|
+
- Ensure `configPath` and `publicDir` are scoped from `root`, both in Ruby and JS.
|
9
|
+
|
10
|
+
## Vite Rails 1.0.8 (2020-01-21)
|
11
|
+
|
12
|
+
- Change the default of `sourceCodeDir` to `app/frontend`, add instructions for folks migrating
|
13
|
+
from a `app/javascript` structure.
|
14
|
+
|
15
|
+
## Vite Rails 1.0.7 (2020-01-20)
|
16
|
+
|
17
|
+
- Add `vite_client_tag` to ensure the Vite client can be loaded in apps that don't use any imports.
|
18
|
+
|
19
|
+
## Vite Rails 1.0.6 (2020-01-20)
|
20
|
+
|
21
|
+
- Ensure running `bin/rake assets:precompile` automatically invokes `vite:build`.
|
22
|
+
|
1
23
|
## Vite Rails 1.0.5 (2020-01-20)
|
2
24
|
|
3
25
|
- Automatically add `<link rel="modulepreload">` and `<link rel="stylesheet">` when using `vite_javascript_tag`, which simplifies usage.
|
data/CONTRIBUTING.md
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
<h1 align="center">
|
2
2
|
Vite ⚡️ Rails
|
3
3
|
<p align="center">
|
4
|
-
|
4
|
+
<a href="https://github.com/ElMassimo/vite_rails/actions">
|
5
5
|
<img alt="Build Status" src="https://github.com/ElMassimo/vite_rails/workflows/build/badge.svg"/>
|
6
|
-
</a>
|
7
|
-
|
6
|
+
</a>
|
7
|
+
<a href="https://codeclimate.com/github/ElMassimo/vite_rails">
|
8
8
|
<img alt="Maintainability" src="https://codeclimate.com/github/ElMassimo/vite_rails/badges/gpa.svg"/>
|
9
9
|
</a>
|
10
10
|
<a href="https://codeclimate.com/github/ElMassimo/vite_rails">
|
11
11
|
<img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/vite_rails/badges/coverage.svg"/>
|
12
|
-
</a>
|
12
|
+
</a>
|
13
13
|
<a href="https://rubygems.org/gems/vite_rails">
|
14
14
|
<img alt="Gem Version" src="https://img.shields.io/gem/v/vite_rails.svg?colorB=e9573f"/>
|
15
15
|
</a>
|
@@ -19,26 +19,35 @@
|
|
19
19
|
</p>
|
20
20
|
</h1>
|
21
21
|
|
22
|
+
[website]: https://vite-rails.netlify.app/
|
23
|
+
[configuration reference]: https://vite-rails.netlify.app/config/
|
24
|
+
[features]: https://vite-rails.netlify.app/guide/introduction.html
|
25
|
+
[guides]: https://vite-rails.netlify.app/guide/
|
26
|
+
[config]: https://vite-rails.netlify.app/config/
|
22
27
|
[vite_rails]: https://github.com/ElMassimo/vite_rails
|
23
28
|
[webpacker]: https://github.com/rails/webpacker
|
24
29
|
[vite]: http://vitejs.dev/
|
25
30
|
[config file]: https://github.com/ElMassimo/vite_rails/blob/main/package/default.vite.json
|
31
|
+
[example app]: https://github.com/ElMassimo/vite_rails/tree/main/examples/blog
|
32
|
+
[heroku]: https://vite-rails-demo.herokuapp.com/
|
26
33
|
|
27
|
-
[__Vite Rails__][vite_rails] allows you to use [Vite] to power the frontend.
|
34
|
+
[__Vite Rails__][vite_rails] allows you to use [Vite] to power the frontend of your Rails app.
|
28
35
|
|
29
36
|
[Vite] is to frontend tooling as Ruby to programming, pure joy! 😍
|
30
37
|
|
38
|
+
Check an [example app] running on [Heroku].
|
39
|
+
|
31
40
|
## Features ⚡️
|
32
41
|
|
33
|
-
- 🤖 Automatic
|
34
|
-
- ⚡️
|
35
|
-
-
|
36
|
-
- 🤝 Integrated with <kbd>assets:precompile</kbd>
|
37
|
-
- And more!
|
42
|
+
- 🤖 Automatic entrypoint detection
|
43
|
+
- ⚡️ Blazing fast hot reload
|
44
|
+
- 🚀 Zero-config deployments
|
45
|
+
- 🤝 Integrated with <kbd>assets:precompile</kbd>
|
46
|
+
- [And more!][features]
|
38
47
|
|
39
48
|
## Documentation 📖
|
40
49
|
|
41
|
-
|
50
|
+
Visit the [documentation website][website] to check out the [guides] and searchable [configuration reference].
|
42
51
|
|
43
52
|
## Installation 💿
|
44
53
|
|
@@ -57,74 +66,27 @@ bin/rake vite:install
|
|
57
66
|
|
58
67
|
This will generate configuration files and a sample setup.
|
59
68
|
|
60
|
-
|
69
|
+
Additional installation instructions are available in the [documentation website][website].
|
61
70
|
|
62
|
-
|
63
|
-
will be considered entries to your application (SPAs or pages).
|
71
|
+
## Getting Started 💻
|
64
72
|
|
65
|
-
|
66
|
-
for you.
|
73
|
+
Restart your Rails server, and then run <kbd>bin/vite</kbd> to start the Vite development server.
|
67
74
|
|
68
|
-
|
75
|
+
Add the following your `views/layouts/application.html.erb`:
|
69
76
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
```js
|
74
|
-
import { createApp } from 'vue'
|
75
|
-
import App from '~/App.vue'
|
76
|
-
import '~/channels'
|
77
|
-
|
78
|
-
createApp(App).mount('#app')
|
77
|
+
```erb
|
78
|
+
<%= vite_client_tag %>
|
79
|
+
<%= vite_javascript_tag 'application' %>
|
79
80
|
```
|
80
81
|
|
81
|
-
|
82
|
-
|
83
|
-
`vite_typescript_tag`, `vite_javascript_tag`, and `vite_stylesheet_tag` can be
|
84
|
-
used to output `<script>` and `<link>` tags in your Rails layouts or templates.
|
85
|
-
|
86
|
-
```html
|
87
|
-
<head>
|
88
|
-
<title>Joie</title>
|
89
|
-
<%= csrf_meta_tags %>
|
90
|
-
<%= csp_meta_tag %>
|
91
|
-
|
92
|
-
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
93
|
-
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
|
94
|
-
|
95
|
-
<%= vite_stylesheet_tag 'strange' %>
|
96
|
-
<%= vite_typescript_tag 'application' %>
|
97
|
-
</head>
|
98
|
-
```
|
99
|
-
|
100
|
-
For other types of assets, you can use `vite_asset_path` and pass that to the appropriate tag helper.
|
101
|
-
|
102
|
-
## Configuration ⚙️
|
103
|
-
|
104
|
-
This is what your `config/vite.json` might look like:
|
105
|
-
|
106
|
-
```json
|
107
|
-
{
|
108
|
-
"all": {
|
109
|
-
"watchAdditionalPaths": []
|
110
|
-
},
|
111
|
-
"development": {
|
112
|
-
"autoBuild": true,
|
113
|
-
"publicOutputDir": "vite-dev",
|
114
|
-
"port": 3036
|
115
|
-
},
|
116
|
-
"test": {
|
117
|
-
"autoBuild": true,
|
118
|
-
"publicOutputDir": "vite-test"
|
119
|
-
}
|
120
|
-
}
|
121
|
-
```
|
82
|
+
Visit any page and you should see a printed console output: `Vite ⚡️ Rails`.
|
122
83
|
|
123
|
-
|
84
|
+
For more [guides] and a full [configuration reference], check the [documentation website][website].
|
124
85
|
|
125
|
-
##
|
86
|
+
## Special Thanks 🙏
|
126
87
|
|
127
88
|
- [webpacker]
|
89
|
+
- [vite]
|
128
90
|
|
129
91
|
## License
|
130
92
|
|
@@ -1,3 +1,10 @@
|
|
1
|
+
// To see this message, add the following to the `<head>` section in your
|
2
|
+
// views/layouts/application.html.erb
|
3
|
+
//
|
4
|
+
// <%= vite_client_tag %>
|
5
|
+
// <%= vite_javascript_tag 'application' %>
|
6
|
+
console.log('Vite ⚡️ Rails')
|
7
|
+
|
1
8
|
// Example: Load Rails libraries in Vite.
|
2
9
|
//
|
3
10
|
// import '@rails/ujs'
|
@@ -11,8 +18,5 @@
|
|
11
18
|
// Turbolinks.start()
|
12
19
|
// ActiveStorage.start()
|
13
20
|
|
14
|
-
// Example: Import a stylesheet in app/
|
21
|
+
// Example: Import a stylesheet in app/frontend/index.css
|
15
22
|
// import '~/index.css'
|
16
|
-
|
17
|
-
console.log('Vite ⚡️ Rails')
|
18
|
-
|
data/lib/install/template.rb
CHANGED
@@ -6,7 +6,7 @@ copy_file "#{ __dir__ }/config/vite.json", ViteRails.config.config_path
|
|
6
6
|
copy_file "#{ __dir__ }/config/vite.config.ts", Rails.root.join('vite.config.ts')
|
7
7
|
|
8
8
|
say 'Creating entrypoints directory'
|
9
|
-
directory "#{ __dir__ }/javascript/entrypoints", ViteRails.config.
|
9
|
+
directory "#{ __dir__ }/javascript/entrypoints", ViteRails.config.resolved_entrypoints_dir
|
10
10
|
|
11
11
|
apply "#{ __dir__ }/binstubs.rb"
|
12
12
|
|
data/lib/tasks/vite/build.rake
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
$stdout.sync = true
|
4
4
|
|
5
5
|
def enhance_assets_precompile
|
6
|
-
Rake::Task['assets:precompile'] do |task|
|
6
|
+
Rake::Task['assets:precompile'].enhance do |task|
|
7
7
|
prefix = task.name.split(/#|assets:precompile/).first
|
8
8
|
|
9
9
|
Rake::Task["#{ prefix }vite:build"].invoke
|
@@ -13,11 +13,7 @@ end
|
|
13
13
|
namespace :vite do
|
14
14
|
desc 'Compile JavaScript packs using vite for production with digests'
|
15
15
|
task build: [:'vite:verify_install', :environment] do
|
16
|
-
ViteRails.
|
17
|
-
ViteRails.ensure_log_goes_to_stdout do
|
18
|
-
ViteRails.build || exit!
|
19
|
-
end
|
20
|
-
end
|
16
|
+
ViteRails.build_from_rake
|
21
17
|
end
|
22
18
|
end
|
23
19
|
|
data/lib/tasks/vite/clean.rake
CHANGED
@@ -5,9 +5,7 @@ $stdout.sync = true
|
|
5
5
|
namespace :vite do
|
6
6
|
desc 'Remove old compiled vites'
|
7
7
|
task :clean, [:keep, :age] => [:'vite:verify_install', :environment] do |_, args|
|
8
|
-
ViteRails.
|
9
|
-
ViteRails.clean(keep_up_to: Integer(args.keep || 2), age_in_seconds: Integer(args.age || 3600))
|
10
|
-
end
|
8
|
+
ViteRails.clean_from_rake(args)
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
data/lib/tasks/vite/info.rake
CHANGED
@@ -14,7 +14,6 @@ namespace :vite do
|
|
14
14
|
$stdout.puts "vite-plugin-ruby: \n#{ `npm list vite-plugin-ruby version` }"
|
15
15
|
|
16
16
|
$stdout.puts "Is bin/vite present?: #{ File.exist? 'bin/vite' }"
|
17
|
-
$stdout.puts "Is bin/vite-dev-server present?: #{ File.exist? 'bin/vite-dev-server' }"
|
18
17
|
$stdout.puts "Is bin/yarn present?: #{ File.exist? 'bin/yarn' }"
|
19
18
|
end
|
20
19
|
end
|
@@ -11,10 +11,10 @@ namespace :vite do
|
|
11
11
|
WARN
|
12
12
|
exit!
|
13
13
|
end
|
14
|
-
|
15
|
-
|
14
|
+
config_path = Rails.root.join(ViteRails.config.config_path)
|
15
|
+
unless config_path.exist?
|
16
16
|
warn <<~WARN
|
17
|
-
Configuration #{
|
17
|
+
Configuration #{ config_path } file for vite-plugin-ruby not found.
|
18
18
|
Make sure vite:install has run successfully before running dependent tasks.
|
19
19
|
WARN
|
20
20
|
exit!
|
data/lib/vite_rails.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
require 'active_support/
|
3
|
+
require 'rails'
|
4
|
+
require 'active_support/all'
|
5
5
|
|
6
6
|
require 'zeitwerk'
|
7
7
|
loader = Zeitwerk::Loader.for_gem
|
@@ -13,18 +13,10 @@ class ViteRails
|
|
13
13
|
# Internal: Prefix used for environment variables that modify the configuration.
|
14
14
|
ENV_PREFIX = 'VITE_RUBY'
|
15
15
|
|
16
|
-
# Public: Additional environment variables to pass to Vite.
|
17
|
-
#
|
18
|
-
# Example:
|
19
|
-
# ViteRails.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json'
|
20
|
-
cattr_accessor(:env) { ENV.select { |key, _| key.start_with?(ENV_PREFIX) } }
|
21
|
-
|
22
|
-
cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
|
23
|
-
|
24
16
|
class << self
|
25
17
|
delegate :config, :builder, :manifest, :commands, :dev_server, :dev_server_running?, to: :instance
|
26
18
|
delegate :mode, to: :config
|
27
|
-
delegate :bootstrap, :clean, :clobber, :build, to: :commands
|
19
|
+
delegate :bootstrap, :clean, :clean_from_rake, :clobber, :build, :build_from_rake, to: :commands
|
28
20
|
|
29
21
|
attr_writer :instance
|
30
22
|
|
@@ -45,23 +37,20 @@ class ViteRails
|
|
45
37
|
false
|
46
38
|
end
|
47
39
|
|
48
|
-
|
49
|
-
|
50
|
-
ENV
|
51
|
-
yield
|
52
|
-
ensure
|
53
|
-
ENV['NODE_ENV'] = original
|
54
|
-
end
|
55
|
-
|
56
|
-
def ensure_log_goes_to_stdout
|
57
|
-
old_logger = ViteRails.logger
|
58
|
-
ViteRails.logger = ActiveSupport::Logger.new(STDOUT)
|
59
|
-
yield
|
60
|
-
ensure
|
61
|
-
ViteRails.logger = old_logger
|
40
|
+
# Internal: Allows to obtain any env variables for configuration options.
|
41
|
+
def load_env_variables
|
42
|
+
ENV.select { |key, _| key.start_with?(ENV_PREFIX) }
|
62
43
|
end
|
63
44
|
end
|
64
45
|
|
46
|
+
# Public: Additional environment variables to pass to Vite.
|
47
|
+
#
|
48
|
+
# Example:
|
49
|
+
# ViteRails.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json'
|
50
|
+
cattr_accessor(:env) { load_env_variables }
|
51
|
+
|
52
|
+
cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
|
53
|
+
|
65
54
|
# Public: Returns true if the Vite development server is running.
|
66
55
|
def dev_server_running?
|
67
56
|
ViteRails.run_proxy? && dev_server.running?
|
data/lib/vite_rails/builder.rb
CHANGED
@@ -67,9 +67,18 @@ private
|
|
67
67
|
def build_with_vite
|
68
68
|
logger.info 'Building with Vite ⚡️'
|
69
69
|
|
70
|
-
|
71
|
-
|
70
|
+
command = "#{ which_ruby } ./bin/vite build --mode #{ config.mode }"
|
71
|
+
stdout, stderr, status = Open3.capture3(ViteRails.config.to_env, command, chdir: File.expand_path(config.root))
|
72
72
|
|
73
|
+
log_build_result(stdout, stderr, status)
|
74
|
+
|
75
|
+
status.success?
|
76
|
+
end
|
77
|
+
|
78
|
+
# Internal: Outputs the build results.
|
79
|
+
#
|
80
|
+
# NOTE: By default it also outputs the manifest entries.
|
81
|
+
def log_build_result(stdout, stderr, status)
|
73
82
|
if status.success?
|
74
83
|
logger.info "Build with Vite complete: #{ config.build_output_dir }"
|
75
84
|
logger.error(stderr.to_s) unless stderr.empty?
|
@@ -78,8 +87,6 @@ private
|
|
78
87
|
non_empty_streams = [stdout, stderr].delete_if(&:empty?)
|
79
88
|
logger.error "Build with Vite failed:\n#{ non_empty_streams.join("\n\n") }"
|
80
89
|
end
|
81
|
-
|
82
|
-
status.success?
|
83
90
|
end
|
84
91
|
|
85
92
|
# Internal: Used to prefix the bin/vite executable file.
|
@@ -101,13 +108,4 @@ private
|
|
101
108
|
config.config_path,
|
102
109
|
].freeze
|
103
110
|
end
|
104
|
-
|
105
|
-
# Internal: Sets additional environment variables for vite-plugin-ruby.
|
106
|
-
def vite_env
|
107
|
-
ViteRails.env.merge(
|
108
|
-
"#{ ViteRails::ENV_PREFIX }_CONFIG_PATH" => config.config_path,
|
109
|
-
"#{ ViteRails::ENV_PREFIX }_MODE" => config.mode,
|
110
|
-
"#{ ViteRails::ENV_PREFIX }_ROOT" => config.root,
|
111
|
-
).transform_values(&:to_s)
|
112
|
-
end
|
113
111
|
end
|
data/lib/vite_rails/commands.rb
CHANGED
@@ -11,6 +11,15 @@ class ViteRails::Commands
|
|
11
11
|
manifest.refresh
|
12
12
|
end
|
13
13
|
|
14
|
+
# Public: Defaults to production, and exits if the build fails.
|
15
|
+
def build_from_rake
|
16
|
+
with_node_env(ENV.fetch('NODE_ENV', 'production')) {
|
17
|
+
ensure_log_goes_to_stdout {
|
18
|
+
build || exit!
|
19
|
+
}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
14
23
|
# Public: Builds all assets that are managed by Vite, from the entrypoints.
|
15
24
|
def build
|
16
25
|
builder.build.tap { manifest.refresh }
|
@@ -20,6 +29,14 @@ class ViteRails::Commands
|
|
20
29
|
def clobber
|
21
30
|
config.build_output_dir.rmtree if config.build_output_dir.exist?
|
22
31
|
config.build_cache_dir.rmtree if config.build_cache_dir.exist?
|
32
|
+
config.vite_cache_dir.rmtree if config.vite_cache_dir.exist?
|
33
|
+
end
|
34
|
+
|
35
|
+
# Public: Receives arguments from a rake task.
|
36
|
+
def clean_from_rake(args)
|
37
|
+
ensure_log_goes_to_stdout {
|
38
|
+
clean(keep_up_to: Integer(args.keep || 2), age_in_seconds: Integer(args.age || 3600))
|
39
|
+
}
|
23
40
|
end
|
24
41
|
|
25
42
|
# Public: Cleanup old assets in the output directory.
|
@@ -33,21 +50,16 @@ class ViteRails::Commands
|
|
33
50
|
# To force only 1 backup to be kept: clean(1, 0)
|
34
51
|
# To only keep files created within the last 10 minutes: clean(0, 600)
|
35
52
|
def clean(keep_up_to: 2, age_in_seconds: 3600)
|
36
|
-
return false unless
|
53
|
+
return false unless may_clean?
|
37
54
|
|
38
|
-
versions
|
55
|
+
versions
|
39
56
|
.each_with_index
|
40
57
|
.drop_while { |(mtime, _), index|
|
41
58
|
max_age = [0, Time.now - Time.at(mtime)].max
|
42
59
|
max_age < age_in_seconds || index < keep_up_to
|
43
60
|
}
|
44
|
-
.each do |(_, files),
|
45
|
-
files
|
46
|
-
next unless File.file?(file)
|
47
|
-
|
48
|
-
File.delete(file)
|
49
|
-
logger.info("Removed #{ file }")
|
50
|
-
end
|
61
|
+
.each do |(_, files), _|
|
62
|
+
clean_files(files)
|
51
63
|
end
|
52
64
|
true
|
53
65
|
end
|
@@ -56,13 +68,42 @@ private
|
|
56
68
|
|
57
69
|
delegate :config, :builder, :manifest, :logger, to: :@vite_rails
|
58
70
|
|
71
|
+
def may_clean?
|
72
|
+
config.build_output_dir.exist? && config.manifest_path.exist?
|
73
|
+
end
|
74
|
+
|
75
|
+
def clean_files(files)
|
76
|
+
files.select { |file| File.file?(file) }.each do |file|
|
77
|
+
File.delete(file)
|
78
|
+
logger.info("Removed #{ file }")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
59
82
|
def versions
|
60
83
|
all_files = Dir.glob("#{ config.build_output_dir }/**/*")
|
61
84
|
entries = all_files - [config.manifest_path] - current_version_files
|
62
|
-
entries.reject { |file| File.directory?(file) }
|
85
|
+
entries.reject { |file| File.directory?(file) }
|
86
|
+
.group_by { |file| File.mtime(file).utc.to_i }
|
87
|
+
.sort.reverse
|
63
88
|
end
|
64
89
|
|
65
90
|
def current_version_files
|
66
91
|
Dir.glob(manifest.refresh.values.map { |value| config.build_output_dir.join("#{ value['file'] }*") })
|
67
92
|
end
|
93
|
+
|
94
|
+
def with_node_env(env)
|
95
|
+
original = ENV['NODE_ENV']
|
96
|
+
ENV['NODE_ENV'] = env
|
97
|
+
yield
|
98
|
+
ensure
|
99
|
+
ENV['NODE_ENV'] = original
|
100
|
+
end
|
101
|
+
|
102
|
+
def ensure_log_goes_to_stdout
|
103
|
+
old_logger = ViteRails.logger
|
104
|
+
ViteRails.logger = ActiveSupport::Logger.new(STDOUT)
|
105
|
+
yield
|
106
|
+
ensure
|
107
|
+
ViteRails.logger = old_logger
|
108
|
+
end
|
68
109
|
end
|