Skip to content

Commit 41a37b7

Browse files
ElMassimoSDRACK
authored andcommitted
fix: prevent clean from deleting assets referenced in the manifests
1 parent bea9fdd commit 41a37b7

File tree

7 files changed

+511
-364
lines changed

7 files changed

+511
-364
lines changed

test/commands_test.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,49 @@ def test_build_returns_failure_status_when_stale
3737

3838
def test_clean
3939
with_rails_env('test') { |config|
40-
manifest = config.build_output_dir.join('manifest.json')
40+
manifest = config.build_output_dir.join('.vite/manifest.json')
41+
js_file = config.build_output_dir.join('assets/application.js')
4142

4243
# Should not clean, the manifest does not exist.
43-
config.build_output_dir.mkdir unless config.build_output_dir.exist?
44+
ensure_output_dirs(config)
4445
refute clean
4546

4647
# Should not clean, the file is recent.
4748
manifest.write('{}')
49+
js_file.write('export {}')
4850
assert clean_from_task(OpenStruct.new)
4951
assert_path_exists manifest
52+
assert_path_exists js_file
53+
54+
# Should not clean if directly referenced.
55+
manifest.write('{ "application.js": { "file": "assets/application.js" } }')
56+
assert clean(keep_up_to: 0, age_in_seconds: 0)
57+
assert_path_exists js_file
5058

5159
# Should clean if we remove age restrictions.
60+
manifest.write('{}')
5261
assert clean(keep_up_to: 0, age_in_seconds: 0)
5362
assert_path_exists config.build_output_dir
54-
refute_path_exists manifest
63+
refute_path_exists js_file
5564
}
5665
end
5766

5867
def test_clobber
5968
with_rails_env('test') { |config|
60-
config.build_output_dir.mkdir unless config.build_output_dir.exist?
61-
config.build_output_dir.join('manifest.json').write('{}')
69+
ensure_output_dirs(config)
70+
config.build_output_dir.join('.vite/manifest.json').write('{}')
6271
assert_path_exists config.build_output_dir
6372
clobber
6473
refute_path_exists config.build_output_dir
6574
}
6675
end
76+
77+
private
78+
79+
def ensure_output_dirs(config)
80+
config.build_output_dir.rmtree rescue nil
81+
config.build_output_dir.mkdir unless config.build_output_dir.exist?
82+
config.build_output_dir.join('.vite').mkdir unless config.build_output_dir.join('.vite').exist?
83+
config.build_output_dir.join('assets').mkdir unless config.build_output_dir.join('assets').exist?
84+
end
6785
end

test/engine_rake_tasks_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_rake_tasks
3030

3131
within_mounted_app { `bundle exec rake app:vite:build` }
3232
assert_path_exists app_public_dir
33-
assert_path_exists app_public_dir.join('manifest.json')
33+
assert_path_exists app_public_dir.join('.vite/manifest.json')
3434
assert_path_exists app_public_dir.join('assets')
3535
refute_path_exists app_ssr_dir
3636

@@ -39,14 +39,14 @@ def test_rake_tasks
3939

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

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

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

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

6262
within_mounted_app_root { `bin/vite build --mode development` }
6363
assert_path_exists app_public_dir
64-
assert_path_exists app_public_dir.join('manifest.json')
64+
assert_path_exists app_public_dir.join('.vite/manifest.json')
6565
assert_path_exists app_public_dir.join('assets')
6666

6767
within_mounted_app_root { assert_includes `bin/vite version`, ViteRails::VERSION }

test/mounted_app/test/dummy/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"devDependencies": {
55
"ci": "^2.0.0",
66
"ni": "^0.0.2",
7-
"vite": "^4.3.0",
8-
"vite-plugin-ruby": "^3.2.0"
7+
"vite": "^5.0.0",
8+
"vite-plugin-ruby": "^5.0.0"
99
}
1010
}

0 commit comments

Comments
 (0)