Skip to content

Commit 5506753

Browse files
committed
fix(preserveModules): correctly generate absolute chunk file names
1 parent 63a053d commit 5506753

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

crates/rolldown/src/stages/generate_stage/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'a> GenerateStage<'a> {
174174
representative_file_name_for_preserve_modules(module_id.as_path());
175175

176176
let sanitized_absolute_filename =
177-
sanitize_filename.call(absolute_chunk_file_name.as_ref()).await?;
177+
sanitize_filename.call(absolute_chunk_file_name.as_str()).await?;
178178

179179
let sanitized_chunk_name = sanitize_filename.call(&chunk_name).await?;
180180
(sanitized_chunk_name, sanitized_absolute_filename)

crates/rolldown_std_utils/src/path_ext.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,13 @@ impl PathExt for Path {
5050
}
5151

5252
/// The first one is for chunk name, the second element is used for generate absolute file name
53-
pub fn representative_file_name_for_preserve_modules(path: &Path) -> (Cow<str>, Cow<str>) {
53+
pub fn representative_file_name_for_preserve_modules(path: &Path) -> (Cow<str>, String) {
5454
let file_name =
5555
path.file_stem().map_or_else(|| path.to_string_lossy(), |stem| stem.to_string_lossy());
56-
let idx = path.to_string_lossy().rfind(file_name.as_ref()).expect("should contains file_name");
57-
let ab_path = slice_cow_str(path.to_string_lossy(), 0, idx + file_name.len());
56+
let ab_path = path.with_extension("").to_string_lossy().into_owned();
5857
(file_name, ab_path)
5958
}
6059

61-
#[inline]
62-
fn slice_cow_str(cow: Cow<str>, start: usize, end: usize) -> Cow<'_, str> {
63-
match cow {
64-
Cow::Borrowed(s) => Cow::Borrowed(&s[start..end]),
65-
Cow::Owned(s) => Cow::Owned(s[start..end].to_string()),
66-
}
67-
}
68-
6960
#[test]
7061
fn test_representative_file_name() {
7162
let cwd = Path::new(".").join("project");
@@ -78,6 +69,10 @@ fn test_representative_file_name() {
7869
let path = cwd.join("vue").join("mod.ts");
7970
assert_eq!(path.representative_file_name(), "vue");
8071

72+
let path = cwd.join("x.jsx");
73+
let (_, ab_path) = representative_file_name_for_preserve_modules(&path);
74+
assert_eq!(Path::new(&ab_path).file_name().unwrap().to_string_lossy(), "x");
75+
8176
#[cfg(not(target_os = "windows"))]
8277
{
8378
let path = cwd.join("src").join("vue.js");

0 commit comments

Comments
 (0)