vite_rails 1.0.7 → 1.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/CONTRIBUTING.md +0 -1
  4. data/README.md +34 -8
  5. data/lib/install/config/vite.config.ts +0 -3
  6. data/lib/install/javascript/entrypoints/application.js +8 -4
  7. data/lib/install/template.rb +3 -3
  8. data/lib/tasks/vite/build.rake +12 -6
  9. data/lib/tasks/vite/clean.rake +1 -3
  10. data/lib/tasks/vite/install_dependencies.rake +3 -9
  11. data/lib/tasks/vite/verify_install.rake +3 -3
  12. data/lib/vite_rails.rb +23 -34
  13. data/lib/vite_rails/builder.rb +11 -13
  14. data/lib/vite_rails/commands.rb +51 -10
  15. data/lib/vite_rails/config.rb +70 -35
  16. data/lib/vite_rails/dev_server_proxy.rb +26 -18
  17. data/lib/vite_rails/helper.rb +14 -10
  18. data/lib/vite_rails/manifest.rb +27 -22
  19. data/lib/vite_rails/runner.rb +3 -6
  20. data/lib/vite_rails/version.rb +1 -1
  21. data/package.json +9 -2
  22. data/package/default.vite.json +2 -1
  23. data/test/builder_test.rb +27 -22
  24. data/test/commands_test.rb +67 -0
  25. data/test/config_test.rb +133 -0
  26. data/test/dev_server_proxy_test.rb +102 -0
  27. data/test/dev_server_test.rb +0 -30
  28. data/test/engine_rake_tasks_test.rb +56 -17
  29. data/test/helper_test.rb +40 -103
  30. data/test/manifest_test.rb +39 -29
  31. data/test/mode_test.rb +6 -11
  32. data/test/mounted_app/test/dummy/config/vite.json +5 -11
  33. data/test/mounted_app/test/dummy/package.json +5 -4
  34. data/test/mounted_app/test/dummy/yarn.lock +208 -0
  35. data/test/rake_tasks_test.rb +7 -21
  36. data/test/runner_test.rb +31 -0
  37. data/test/test_app/app/frontend/entrypoints/application.js +2 -0
  38. data/test/test_app/config/vite.json +0 -2
  39. data/test/test_app/config/vite_additional_paths.json +5 -0
  40. data/test/test_app/config/vite_public_dir.json +5 -0
  41. data/test/test_app/public/vite-production/manifest-assets.json +10 -0
  42. data/test/test_app/public/vite-production/manifest.json +39 -0
  43. data/test/test_helper.rb +48 -14
  44. metadata +25 -25
  45. data/test/command_test.rb +0 -35
  46. data/test/configuration_test.rb +0 -80
  47. data/test/dev_server_runner_test.rb +0 -83
  48. data/test/test_app/app/javascript/entrypoints/application.js +0 -10
  49. data/test/test_app/app/javascript/entrypoints/multi_entry.css +0 -4
  50. data/test/test_app/app/javascript/entrypoints/multi_entry.js +0 -4
  51. data/test/test_app/config/vite_public_root.yml +0 -20
  52. data/test/test_app/public/vite/manifest.json +0 -36
  53. data/test/vite_runner_test.rb +0 -59
  54. data/test/webpacker_test.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f4c178774d28f47732953d6d672db2b2551030534b7a607f157e4692e992160
4
- data.tar.gz: 9cb4eeab752ca23b2857487b1217d7e3f5b73afa72663ea8e01372328129368f
3
+ metadata.gz: 57f866deafddde5401e02ec27cf751eb26a40a0be1fd832d9d3813fd7a69e721
4
+ data.tar.gz: 7cf22c583a3ac70f4353e227b0da4463b42067dd38f0c36774e665e619b31f2d
5
5
  SHA512:
6
- metadata.gz: 99ce8a2425f5eeaac8715f6204ec19b80d7b215dfe427cd765f25023c0fb82e72b086cf883e0e36931b4ae8f70457910fd57d21a59258befe139456e89bb130e
7
- data.tar.gz: e693145af6596c602eac3136ada51393bf2dd9e06d5d8372739dbef84d811cc8bc3e689411ba308f8535bbeafa0356e8f26452342d2bba9a05132fd839588623
6
+ metadata.gz: d101294de0e6f585b7df56c2881c465698f4c6ccbe8c4220117d7119c84f21e8b80e6aa2865dd353fa27b3b2c3d7a78095e80466c2d9b5f6eda60b6073a55ed0
7
+ data.tar.gz: fdbe92f7afafd6f82bafc6525a4099151efcdc385ce49e5cc5f696ea79efe5d411b6eaf093b9b1ec1402140a7d4ac34f629401fb70d9005535eaf38e989cf4cd
@@ -1,3 +1,28 @@
1
+ ## Vite Rails 1.0.12 (2020-01-29)
2
+
3
+ - Add support for Vite 2.0.0-beta.56, which modified the manifest to output a `css` field in the manifest.
4
+ - Start generating an assets manifest, since 2.0.0-beta.51 stopped including non-JS entrypoints in the manifest.
5
+
6
+ ## Vite Rails 1.0.11 (2020-01-24)
7
+
8
+ - Fix bug in `assetHost` that caused `base` to be configured incorrectly.
9
+ - Allow installing `vite` and `vite-plugin-ruby` as devDependencies, and install them when precompiling assets.
10
+ - Move `base` to the configuration root after Vite's update in beta.38
11
+
12
+ ## Vite Rails 1.0.10 (2020-01-23)
13
+
14
+ - Use `path_to_asset` in `vite_asset_path` so that it's prefixed automatically
15
+ when using a CDN (`config.action_controller.asset_host`).
16
+
17
+ ## Vite Rails 1.0.9 (2020-01-22)
18
+
19
+ - Ensure `configPath` and `publicDir` are scoped from `root`, both in Ruby and JS.
20
+
21
+ ## Vite Rails 1.0.8 (2020-01-21)
22
+
23
+ - Change the default of `sourceCodeDir` to `app/frontend`, add instructions for folks migrating
24
+ from a `app/javascript` structure.
25
+
1
26
  ## Vite Rails 1.0.7 (2020-01-20)
2
27
 
3
28
  - Add `vite_client_tag` to ensure the Vite client can be loaded in apps that don't use any imports.
@@ -6,7 +6,6 @@
6
6
 
7
7
  ```
8
8
  bundle install
9
- yarn
10
9
  ```
11
10
 
12
11
  ## Making sure your changes pass all tests
data/README.md CHANGED
@@ -1,15 +1,26 @@
1
1
  <h1 align="center">
2
- Vite ⚡️ Rails
2
+ <a href="https://vite-rails.netlify.app/">
3
+ <img src="https://raw.githubusercontent.com/ElMassimo/vite_rails/main/docs/public/logo.svg" width="120px"/>
4
+ </a>
5
+
6
+ <br>
7
+
8
+ <a href="https://vite-rails.netlify.app/">
9
+ Vite Rails
10
+ </a>
11
+
12
+ <br>
13
+
3
14
  <p align="center">
4
- <!-- <a href="https://github.com/ElMassimo/vite_rails/actions">
15
+ <a href="https://github.com/ElMassimo/vite_rails/actions">
5
16
  <img alt="Build Status" src="https://github.com/ElMassimo/vite_rails/workflows/build/badge.svg"/>
6
- </a> -->
7
- <!-- <a href="https://codeclimate.com/github/ElMassimo/vite_rails">
17
+ </a>
18
+ <a href="https://codeclimate.com/github/ElMassimo/vite_rails">
8
19
  <img alt="Maintainability" src="https://codeclimate.com/github/ElMassimo/vite_rails/badges/gpa.svg"/>
9
20
  </a>
10
21
  <a href="https://codeclimate.com/github/ElMassimo/vite_rails">
11
22
  <img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/vite_rails/badges/coverage.svg"/>
12
- </a> -->
23
+ </a>
13
24
  <a href="https://rubygems.org/gems/vite_rails">
14
25
  <img alt="Gem Version" src="https://img.shields.io/gem/v/vite_rails.svg?colorB=e9573f"/>
15
26
  </a>
@@ -20,6 +31,7 @@
20
31
  </h1>
21
32
 
22
33
  [website]: https://vite-rails.netlify.app/
34
+ [configuration reference]: https://vite-rails.netlify.app/config/
23
35
  [features]: https://vite-rails.netlify.app/guide/introduction.html
24
36
  [guides]: https://vite-rails.netlify.app/guide/
25
37
  [config]: https://vite-rails.netlify.app/config/
@@ -27,14 +39,20 @@
27
39
  [webpacker]: https://github.com/rails/webpacker
28
40
  [vite]: http://vitejs.dev/
29
41
  [config file]: https://github.com/ElMassimo/vite_rails/blob/main/package/default.vite.json
42
+ [example app]: https://github.com/ElMassimo/vite_rails/tree/main/examples/blog
43
+ [heroku]: https://vite-rails-demo.herokuapp.com/
44
+ [Issues]: https://github.com/ElMassimo/vite_rails/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc
45
+ [Discussions]: https://github.com/ElMassimo/vite_rails/discussions
30
46
 
31
47
  [__Vite Rails__][vite_rails] allows you to use [Vite] to power the frontend of your Rails app.
32
48
 
33
49
  [Vite] is to frontend tooling as Ruby to programming, pure joy! 😍
34
50
 
51
+ Check an [example app] running on [Heroku].
52
+
35
53
  ## Features ⚡️
36
54
 
37
- - 🤖 Automatic entrypoint detection
55
+ - 💡 Instant server start
38
56
  - ⚡️ Blazing fast hot reload
39
57
  - 🚀 Zero-config deployments
40
58
  - 🤝 Integrated with <kbd>assets:precompile</kbd>
@@ -65,7 +83,7 @@ Additional installation instructions are available in the [documentation website
65
83
 
66
84
  ## Getting Started 💻
67
85
 
68
- Restart your Rails server, and then run `bin/vite` to start the Vite development server.
86
+ Restart your Rails server, and then run <kbd>bin/vite</kbd> to start the Vite development server.
69
87
 
70
88
  Add the following your `views/layouts/application.html.erb`:
71
89
 
@@ -78,9 +96,17 @@ Visit any page and you should see a printed console output: `Vite ⚡️ Rails`.
78
96
 
79
97
  For more [guides] and a full [configuration reference], check the [documentation website][website].
80
98
 
81
- ## Inspiration 💡
99
+ ## Contact ✉️
100
+
101
+ Please use [Issues] to report bugs you find, and [Discussions] to make feature requests or get help.
102
+
103
+ Don't hesitate to _⭐️ star the project_ if you find it useful!
104
+
105
+
106
+ ## Special Thanks 🙏
82
107
 
83
108
  - [webpacker]
109
+ - [vite]
84
110
 
85
111
  ## License
86
112
 
@@ -5,7 +5,4 @@ export default defineConfig({
5
5
  plugins: [
6
6
  RubyPlugin(),
7
7
  ],
8
- optimizeDeps: {
9
- exclude: [/webpack/, /vite-plugin-ruby/],
10
- },
11
8
  })
@@ -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/javascript/index.css
21
+ // Example: Import a stylesheet in app/frontend/index.css
15
22
  // import '~/index.css'
16
-
17
- console.log('Vite ⚡️ Rails')
18
-
@@ -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.source_code_dir.join(ViteRails.config.entrypoints_dir)
9
+ directory "#{ __dir__ }/javascript/entrypoints", ViteRails.config.resolved_entrypoints_dir
10
10
 
11
11
  apply "#{ __dir__ }/binstubs.rb"
12
12
 
@@ -33,8 +33,8 @@ Dir.chdir(Rails.root) do
33
33
  vite_version = package_json.match(/"vite": "(.*)"/)[1]
34
34
  plugin_version = package_json.match(/"vite-plugin-ruby": "(.*)"/)[1]
35
35
 
36
- say 'Installing vite as direct dependencies'
37
- run "yarn add vite@#{ vite_version } vite-plugin-ruby@#{ plugin_version }"
36
+ say 'Installing vite as build dependencies'
37
+ run "yarn add -D vite@#{ vite_version } vite-plugin-ruby@#{ plugin_version }"
38
38
  end
39
39
 
40
40
  say 'Vite ⚡️ Rails successfully installed! 🎉', :green
@@ -3,21 +3,27 @@
3
3
  $stdout.sync = true
4
4
 
5
5
  def enhance_assets_precompile
6
+ # Before installing
7
+ ['yarn:install', 'webpacker:yarn_install'].each do |name|
8
+ Rake::Task[name].enhance([:'vite:set_node_env']) if Rake::Task.task_defined?(name)
9
+ end
10
+
11
+ # After precompiling
6
12
  Rake::Task['assets:precompile'].enhance do |task|
7
13
  prefix = task.name.split(/#|assets:precompile/).first
8
-
9
14
  Rake::Task["#{ prefix }vite:build"].invoke
10
15
  end
11
16
  end
12
17
 
13
18
  namespace :vite do
19
+ desc 'Fixes Rails management of node dev dependencies (build dependencies)'
20
+ task :set_node_env do
21
+ ENV['NODE_ENV'] = 'development'
22
+ end
23
+
14
24
  desc 'Compile JavaScript packs using vite for production with digests'
15
25
  task build: [:'vite:verify_install', :environment] do
16
- ViteRails.with_node_env(ENV.fetch('NODE_ENV', 'production')) do
17
- ViteRails.ensure_log_goes_to_stdout do
18
- ViteRails.build || exit!
19
- end
20
- end
26
+ ViteRails.build_from_rake
21
27
  end
22
28
  end
23
29
 
@@ -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.ensure_log_goes_to_stdout do
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
 
@@ -1,19 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  namespace :vite do
4
- desc 'Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn'
4
+ desc 'Install all JavaScript dependencies as specified via Yarn'
5
5
  task :install_dependencies do
6
6
  valid_node_envs = %w[test development production]
7
7
  node_env = ENV.fetch('NODE_ENV') { valid_node_envs.include?(Rails.env) ? Rails.env : 'production' }
8
8
  Dir.chdir(Rails.root) do
9
- install_command = if Rails.root.join('yarn.lock').exist?
10
- v1 = `yarn --version`.start_with?('1')
11
- "yarn install #{ v1 ? '--no-progress --frozen-lockfile' : '--immutable' }"
12
- elsif Rails.root.join('pnpm-lock.yaml').exist?
13
- 'pnpm install'
14
- else
15
- 'npm ci'
16
- end
9
+ v1 = `yarn --version`.start_with?('1')
10
+ install_command = "yarn install #{ v1 ? '--no-progress --frozen-lockfile' : '--immutable' } --production=false"
17
11
  system({ 'NODE_ENV' => node_env }, install_command)
18
12
  end
19
13
  end
@@ -11,10 +11,10 @@ namespace :vite do
11
11
  WARN
12
12
  exit!
13
13
  end
14
- unless ViteRails.config.config_path.exist?
15
- path = ViteRails.config.config_path.relative_path_from(Pathname.new(pwd)).to_s
14
+ config_path = Rails.root.join(ViteRails.config.config_path)
15
+ unless config_path.exist?
16
16
  warn <<~WARN
17
- Configuration #{ path } file for vite-plugin-ruby not found.
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!
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support'
4
- require 'active_support/core_ext/class/attribute_accessors'
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
- delegate :config, :builder, :manifest, :commands, :dev_server, :dev_server_running?, to: :instance
17
+ delegate :config, :builder, :manifest, :commands, :dev_server, :dev_server_running?, :run_proxy?, 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
 
@@ -37,34 +29,31 @@ class ViteRails
37
29
  ViteRails::Runner.new(args).run
38
30
  end
39
31
 
40
- # Public: The proxy for assets should only run in development mode.
41
- def run_proxy?
42
- config.mode == 'development'
43
- rescue StandardError => error
44
- logger.error("Failed to check mode for Vite: #{ error.message }")
45
- false
32
+ # Internal: Allows to obtain any env variables for configuration options.
33
+ def load_env_variables
34
+ ENV.select { |key, _| key.start_with?(ENV_PREFIX) }
46
35
  end
36
+ end
47
37
 
48
- def with_node_env(env)
49
- original = ENV['NODE_ENV']
50
- ENV['NODE_ENV'] = env
51
- yield
52
- ensure
53
- ENV['NODE_ENV'] = original
54
- end
38
+ # Public: Additional environment variables to pass to Vite.
39
+ #
40
+ # Example:
41
+ # ViteRails.env['VITE_RUBY_CONFIG_PATH'] = 'config/alternate_vite.json'
42
+ cattr_accessor(:env) { load_env_variables }
55
43
 
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
62
- end
63
- end
44
+ cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
64
45
 
65
46
  # Public: Returns true if the Vite development server is running.
66
47
  def dev_server_running?
67
- ViteRails.run_proxy? && dev_server.running?
48
+ run_proxy? && dev_server.running?
49
+ end
50
+
51
+ # Public: The proxy for assets should only run in development mode.
52
+ def run_proxy?
53
+ config.mode == 'development'
54
+ rescue StandardError => error
55
+ ViteRails.logger.error("Failed to check mode for Vite: #{ error.message }")
56
+ false
68
57
  end
69
58
 
70
59
  # Public: Current instance configuration for Vite.
@@ -67,9 +67,18 @@ private
67
67
  def build_with_vite
68
68
  logger.info 'Building with Vite ⚡️'
69
69
 
70
- stdout, stderr, status = Open3.capture3(vite_env,
71
- "#{ which_ruby } ./bin/vite build --mode #{ config.mode }", chdir: File.expand_path(config.root))
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
@@ -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 config.build_output_dir.exist? && config.manifest_path.exist?
53
+ return false unless may_clean?
37
54
 
38
- versions.sort.reverse
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), _index|
45
- files.each do |file|
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) }.group_by { |file| File.mtime(file).utc.to_i }
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