Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/resolver/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,7 @@ impl<'a> Resolver<'a> {
}
};

let mut remapped_to_relative_path = false;
if self.care_about_browser_field {
// Support remapping one package path to another via the "browser" field
if let Some(browser_scope) = source_dir_info.get_enclosing_browser_scope() {
Expand Down Expand Up @@ -2333,6 +2334,7 @@ impl<'a> Resolver<'a> {

import_path = remapped;
source_dir_info = browser_scope;
remapped_to_relative_path = !is_package_path(remapped);
}
}
}
Expand Down Expand Up @@ -2375,7 +2377,12 @@ impl<'a> Resolver<'a> {
return ResultUnion::Success(result);
}

if res.package_json.is_some() && self.care_about_browser_field {
// A path that a "browser" map lookup picked is final.
if res.package_json.is_some()
&& self.care_about_browser_field
&& !res.browser_remapped
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
&& !remapped_to_relative_path
Comment thread
robobun marked this conversation as resolved.
Resolved
{
Comment thread
robobun marked this conversation as resolved.
Resolved
let base_dir_info = match res.dir_info {
Some(d) => d,
None => match self.read_dir_info(result.path_pair.primary.name().dir) {
Expand Down Expand Up @@ -5265,6 +5272,7 @@ impl<'a> Resolver<'a> {
dec_ret!(MatchStatus::NotFound);
};

let mut browser_remapped = false;
if self.care_about_browser_field {
// Potentially remap using the "browser" field
if let Some(browser_scope) = dir_info.get_enclosing_browser_scope() {
Expand Down Expand Up @@ -5300,32 +5308,22 @@ impl<'a> Resolver<'a> {
Some(remapped) => remapped,
None => dec_ret!(MatchStatus::NotFound),
};
browser_remapped = true;
}
}
}
}

// Is this a file?
if let Some(result) = self.load_as_file(field_abs_path, extension_order) {
if let Some(package_json) = dir_info.package_json() {
*out = MatchResult {
path_pair: PathPair {
primary: Fs::Path::init(result.path),
secondary: None,
},
package_json: Some(std::ptr::from_ref(package_json)),
dirname_fd: result.dirname_fd,
..Default::default()
};
dec_ret!(MatchStatus::Success);
}

*out = MatchResult {
path_pair: PathPair {
primary: Fs::Path::init(result.path),
secondary: None,
},
package_json: dir_info.package_json().map(std::ptr::from_ref),
dirname_fd: result.dirname_fd,
browser_remapped,
..Default::default()
};
dec_ret!(MatchStatus::Success);
Expand All @@ -5342,6 +5340,9 @@ impl<'a> Resolver<'a> {
extension_order,
out,
);
if browser_remapped && r.is_success() {
out.browser_remapped = true;
}
if let Some(d) = self.debug_logs.as_mut() {
d.decrease_indent();
}
Expand Down Expand Up @@ -5577,6 +5578,7 @@ impl<'a> Resolver<'a> {
primary: Path::init(file_result.path),
secondary: None,
},
browser_remapped: true,
Comment on lines 5582 to +5585

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) Browser builds that import a package subdirectory with a nested "sideEffects": false and a chained browser map now emit an empty bundle instead of the mapped file. load_as_index_with_browser_remapping at src/resolver/resolver.rs:5575 writes the remapped file with no package_json, src/resolver/resolver.rs:5856 attributes it to the subdirectory's package.json, and the new browser_remapped: true at src/resolver/resolver.rs:5581 skips the post lookup that on base re-resolved the chain target with the outer package.json. Fix: attribute a map-picked file to the package.json that owns the file (its own directory's enclosing package), not the imported directory's, so sideEffects is read from the right package on every remap path.

Why this was flagged

Trigger: pkg/package.json browser map {"./dir/index.js":"./a.js","./a.js":"./b.js"}, pkg/dir/package.json with "sideEffects": false, entry does import "pkg/dir" with --target=browser. load_as_file_or_directory on pkg/dir calls load_as_index_with_browser_remapping (src/resolver/resolver.rs:5853); the file arm at :5575 returns pkg/a.js with package_json None and browser_remapped true; :5856 fills package_json with pkg/dir/package.json (sideEffects false). On base the post lookup at src/resolver/resolver.rs:2394 then mapped a.js to b.js and :2419 replaced package_json with pkg/package.json, so b.js was bundled with side effects kept. After merge :2383 skips that step, the file a.js carries sideEffects:false from a package.json that does not own it, and the tree shaker drops the whole import: the user gets an empty bundle with no error. Remedy: set package_json for a remapped index/main file from the file's own enclosing package before the sideEffects decision.

Verification: The result is written with package_json None and the new browser_remapped: true (src/resolver/resolver.rs:5575-5583). :5856 attributes a.js to pkg/dir/package.json. The post-resolution block is now gated by && !res.browser_remapped (:2381-2385), so it is skipped. On base :2419 overwrote the dir attribution, so base emitted b.js.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the second Downsides bullet of the description. The file that the map picks is right (a.js). What is wrong is where sideEffects comes from: the picked file takes the package.json of the imported directory. That does not need a chain key. {"./dir/index.js":"./a.js"} with "sideEffects": false in dir/package.json is already an empty bundle on 1.4.0 to 1.4.4 and on main.

An earlier version of this branch attached the package.json of the map's owner to a picked file outside the imported directory. It dropped a file that main keeps when the map sends the entry into another package and the owner says "sideEffects": false. The answer also depended on which import resolved the file first. #46911 reads sideEffects from the package.json above the file in finalize_result, for every route, so I left this shape to it.

..Default::default()
};
return MatchStatus::Success;
Expand All @@ -5588,6 +5590,7 @@ impl<'a> Resolver<'a> {
.load_as_index(new_dir, extension_order, out)
.is_success()
{
out.browser_remapped = true;
return MatchStatus::Success;
}
}
Expand Down Expand Up @@ -5820,6 +5823,7 @@ impl<'a> Resolver<'a> {
dirname_fd: out.dirname_fd,
package_json,
file_fd: auto_main_result.file_fd,
browser_remapped: out.browser_remapped,
..Default::default()
};
dec_ret!(MatchStatus::Success);
Expand Down
3 changes: 3 additions & 0 deletions src/resolver/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ pub struct MatchResult {
pub(crate) dir_info: Option<DirInfoRef>,
pub(crate) module_type: options::ModuleType,
pub(crate) is_external: bool,
/// A "browser" map lookup picked `path_pair.primary`.
pub(crate) browser_remapped: bool,
}

impl Default for MatchResult {
Expand All @@ -384,6 +386,7 @@ impl Default for MatchResult {
dir_info: None,
module_type: options::ModuleType::Unknown,
is_external: false,
browser_remapped: false,
}
}
}
Expand Down
Loading
Loading