Skip to content

Commit

Permalink
fix: prevent clean from deleting assets referenced in the manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Nov 16, 2023
1 parent f063f28 commit 8a581c1
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 364 deletions.
28 changes: 23 additions & 5 deletions test/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,49 @@ def test_build_returns_failure_status_when_stale

def test_clean
with_rails_env('test') { |config|
manifest = config.build_output_dir.join('manifest.json')
manifest = config.build_output_dir.join('.vite/manifest.json')
js_file = config.build_output_dir.join('assets/application.js')

# Should not clean, the manifest does not exist.
config.build_output_dir.mkdir unless config.build_output_dir.exist?
ensure_output_dirs(config)
refute clean

# Should not clean, the file is recent.
manifest.write('{}')
js_file.write('export {}')
assert clean_from_task(OpenStruct.new)
assert_path_exists manifest
assert_path_exists js_file

# Should not clean if directly referenced.
manifest.write('{ "application.js": { "file": "assets/application.js" } }')
assert clean(keep_up_to: 0, age_in_seconds: 0)
assert_path_exists js_file

# 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
refute_path_exists manifest
refute_path_exists js_file
}
end

def test_clobber
with_rails_env('test') { |config|
config.build_output_dir.mkdir unless config.build_output_dir.exist?
config.build_output_dir.join('manifest.json').write('{}')
ensure_output_dirs(config)
config.build_output_dir.join('.vite/manifest.json').write('{}')
assert_path_exists config.build_output_dir
clobber
refute_path_exists config.build_output_dir
}
end

private

def ensure_output_dirs(config)
config.build_output_dir.rmtree rescue nil
config.build_output_dir.mkdir unless config.build_output_dir.exist?
config.build_output_dir.join('.vite').mkdir unless config.build_output_dir.join('.vite').exist?
config.build_output_dir.join('assets').mkdir unless config.build_output_dir.join('assets').exist?
end
end
12 changes: 6 additions & 6 deletions test/engine_rake_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_rake_tasks

within_mounted_app { `bundle exec rake app:vite:build` }
assert_path_exists app_public_dir
assert_path_exists app_public_dir.join('manifest.json')
assert_path_exists app_public_dir.join('.vite/manifest.json')
assert_path_exists app_public_dir.join('assets')
refute_path_exists app_ssr_dir

Expand All @@ -39,14 +39,14 @@ def test_rake_tasks

within_mounted_app { `bundle exec rake app:vite:build_all` }
assert_path_exists app_ssr_dir.join('ssr.mjs')
refute_path_exists app_ssr_dir.join('manifest.json')
refute_path_exists app_ssr_dir.join('manifest-assets.json')
refute_path_exists app_ssr_dir.join('.vite/manifest.json')
refute_path_exists app_ssr_dir.join('.vite/manifest-assets.json')

within_mounted_app { `bundle exec rake app:vite:clean` }
assert_path_exists app_public_dir.join('manifest.json') # Still fresh
refute Dir.empty?(app_public_dir.join('assets')) # Still fresh

within_mounted_app { `bundle exec rake app:vite:clean[0,0]` }
refute_path_exists app_public_dir.join('manifest.json')
refute Dir.empty?(app_public_dir.join('assets')) # Still referenced in manifest

within_mounted_app { `bundle exec rake app:vite:clobber` }
refute_path_exists app_public_dir
Expand All @@ -61,7 +61,7 @@ def test_cli

within_mounted_app_root { `bin/vite build --mode development` }
assert_path_exists app_public_dir
assert_path_exists app_public_dir.join('manifest.json')
assert_path_exists app_public_dir.join('.vite/manifest.json')
assert_path_exists app_public_dir.join('assets')

within_mounted_app_root { assert_includes `bin/vite version`, ViteRails::VERSION }
Expand Down
4 changes: 2 additions & 2 deletions test/mounted_app/test/dummy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"devDependencies": {
"ci": "^2.0.0",
"ni": "^0.0.2",
"vite": "^4.3.0",
"vite-plugin-ruby": "^3.2.0"
"vite": "^5.0.0",
"vite-plugin-ruby": "^5.0.0"
}
}
Loading

0 comments on commit 8a581c1

Please sign in to comment.