fix(turbopack): avoid duplicated .js ext of entry asset#119
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the logic for replacing the Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses an issue with duplicated file extensions by introducing more sophisticated logic in replace_name_placeholder. The implementation is sound. I've added one suggestion to further optimize the new logic by using Cow<str> to avoid unnecessary string allocations, which is a common pattern for this kind of string manipulation in Rust.
| .replace_all(s, |caps: ®ex::Captures| { | ||
| let m = caps.get(0).unwrap(); | ||
| let after = &s[m.end()..]; | ||
| // If the name already ends with an extension (e.g. "foo.js") and the template | ||
| // text right after [name] starts with that same extension (e.g. ".js"), strip | ||
| // the extension from the name to avoid duplication like "foo.js.js". | ||
| if let Some(dot_pos) = name.rfind('.') { | ||
| let ext = &name[dot_pos..]; // e.g. ".js" | ||
| if after.starts_with(ext) { | ||
| return name[..dot_pos].to_string(); | ||
| } | ||
| } | ||
| name.to_string() | ||
| }) |
There was a problem hiding this comment.
For performance, you can avoid allocating a new String in the case where the name doesn't need to be modified. By using std::borrow::Cow<str> as the return type of the closure, you can return a borrowed slice (Cow::Borrowed) when no changes are made, and an owned String (Cow::Owned) only when the extension is stripped. This avoids unnecessary allocations.
| .replace_all(s, |caps: ®ex::Captures| { | |
| let m = caps.get(0).unwrap(); | |
| let after = &s[m.end()..]; | |
| // If the name already ends with an extension (e.g. "foo.js") and the template | |
| // text right after [name] starts with that same extension (e.g. ".js"), strip | |
| // the extension from the name to avoid duplication like "foo.js.js". | |
| if let Some(dot_pos) = name.rfind('.') { | |
| let ext = &name[dot_pos..]; // e.g. ".js" | |
| if after.starts_with(ext) { | |
| return name[..dot_pos].to_string(); | |
| } | |
| } | |
| name.to_string() | |
| }) | |
| .replace_all(s, |caps: ®ex::Captures| -> std::borrow::Cow<str> { | |
| let m = caps.get(0).unwrap(); | |
| let after = &s[m.end()..]; | |
| // If the name already ends with an extension (e.g. "foo.js") and the template | |
| // text right after [name] starts with that same extension (e.g. ".js"), strip | |
| // the extension from the name to avoid duplication like "foo.js.js". | |
| if let Some(dot_pos) = name.rfind('.') { | |
| let ext = &name[dot_pos..]; // e.g. ".js" | |
| if after.starts_with(ext) { | |
| return std::borrow::Cow::Owned(name[..dot_pos].to_string()); | |
| } | |
| } | |
| std::borrow::Cow::Borrowed(name) | |
| }) |
This pull request updates the logic in the
replace_name_placeholderfunction to improve how file extensions are handled when replacing the[name]placeholder. The new implementation prevents duplicated file extensions in output filenames, addressing cases where the name already contains an extension and the template also appends the same extension.Filename placeholder handling improvements:
replace_name_placeholderinchunking_context.rsto strip duplicate extensions from the name when the template text after[name]starts with the same extension, preventing outputs likefoo.js.js.