Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 1 #5

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
65 changes: 60 additions & 5 deletions test/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,85 @@ def test_build_returns_failure_status_when_stale

def test_clean
with_rails_env('test') { |config|
manifest = config.build_output_dir.join('.vite/manifest.json')
ensure_output_dirs(config)

js_file = config.build_output_dir.join('assets/application.js')
js_file.write('export {}')
css_module = config.build_output_dir.join('assets/styles.css')
css_module.write('.foo {}')
image = config.build_output_dir.join('assets/image.svg')
image.write('<svg/>')

# Simulate using vite-plugin-rails & rollup-plugin-gzip to produce
# source maps, gzip & brotli compressed versions of the file.
source_map_file = config.build_output_dir.join('assets/application.js.map')
source_map_file.write('export {}')
gzip_file = config.build_output_dir.join('assets/application.js.gz')
gzip_file.write('export {}')
brotli_file = config.build_output_dir.join('assets/application.js.br')
brotli_file.write('export {}')
css_gzip_file = config.build_output_dir.join('assets/styles.css.gz')
css_gzip_file.write('.foo {}')
css_brotli_file = config.build_output_dir.join('assets/styles.css.br')
css_brotli_file.write('.foo {}')

# Should not clean, the manifest does not exist.
ensure_output_dirs(config)
refute clean

# Should not clean, the file is recent.
manifest = config.build_output_dir.join('.vite/manifest.json')
manifest.write('{}')
js_file.write('export {}')

# Should not clean, the file is recent.
assert clean_from_task(OpenStruct.new)
assert_path_exists manifest
assert_path_exists js_file
assert_path_exists source_map_file
assert_path_exists gzip_file
assert_path_exists brotli_file
assert_path_exists css_module
assert_path_exists css_gzip_file
assert_path_exists css_brotli_file
assert_path_exists image

# Should not clean if directly referenced.
manifest.write('{ "application.js": { "file": "assets/application.js" } }')
manifest.write('{
"application.js": {
"assets": [
"assets/image.svg"
],
"css": [
"assets/styles.css"
],
"file": "assets/application.js"
},
"noassetsorcss.js": {
"file": "assets/noassetsorcss.js"
}
}')
assert clean(keep_up_to: 0, age_in_seconds: 0)
assert_path_exists manifest
assert_path_exists js_file
assert_path_exists source_map_file
assert_path_exists gzip_file
assert_path_exists brotli_file
assert_path_exists css_module
assert_path_exists css_gzip_file
assert_path_exists css_brotli_file
assert_path_exists image

# Should clean if we remove age restrictions.
manifest.write('{}')
assert clean(keep_up_to: 0, age_in_seconds: 0)
assert_path_exists config.build_output_dir
assert_path_exists manifest
refute_path_exists js_file
refute_path_exists source_map_file
refute_path_exists gzip_file
refute_path_exists brotli_file
refute_path_exists css_module
refute_path_exists css_gzip_file
refute_path_exists css_brotli_file
refute_path_exists image
}
end

Expand Down
12 changes: 9 additions & 3 deletions vite_ruby/lib/vite_ruby/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,25 @@ def clean_files(files)
end

def versions
all_files = Dir.glob("#{ config.build_output_dir }/**/*")
entries = all_files - config.manifest_paths - files_referenced_in_manifests
all_files = Dir.glob("#{ config.build_output_dir }/**/*", File::FNM_DOTMATCH)
entries = all_files - config.manifest_paths.map(&:to_s) - files_to_retain
entries.reject { |file| File.directory?(file) }
.group_by { |file| File.mtime(file).utc.to_i }
.sort.reverse
end

def files_referenced_in_manifests
config.manifest_paths.flat_map { |path|
JSON.parse(path.read).map { |_, entry| entry['file'] }
JSON.parse(path.read).flat_map do |_, entry|
[entry['file']] + entry.fetch('css', []) + entry.fetch('assets', [])
end
}.compact.uniq.map { |path| config.build_output_dir.join(path).to_s }
end

def files_to_retain
Dir.glob(files_referenced_in_manifests.map { |path| "#{ path }*" }).flatten
end

def with_node_env(env)
original = ENV['NODE_ENV']
ENV['NODE_ENV'] = env
Expand Down
23 changes: 16 additions & 7 deletions vite_ruby/lib/vite_ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,26 @@ def host_with_port

# Internal: Path to the manifest files generated by Vite and vite-plugin-ruby.
def known_manifest_paths
[
manifest_files = [
# NOTE: Generated by Vite when `manifest: true`, which vite-plugin-ruby enables.
'manifest.json',
# NOTE: Path where vite-plugin-ruby outputs the assets manifest file.
'manifest-assets.json',
].flat_map { |path|
[
build_output_dir.join(".vite/#{ path }"), # Vite 5 onwards
build_output_dir.join(path), # Vite 4 and below
]
}
]

vite5_manifest_paths = manifest_files.map do |path|
build_output_dir.join(".vite/#{ path }")
end

vite4_manifest_paths = manifest_files.map do |path|
build_output_dir.join(path)
end

if vite5_manifest_paths.any? { |path| File.exist?(path) }
vite5_manifest_paths
else
vite4_manifest_paths
end
end

# Internal: Path to the manifest files generated by Vite and vite-plugin-ruby.
Expand Down