Skip to content

Commit 555b131

Browse files
authored
fix(next-core): externalcjs resolve options (#57645)
### What Probably not a big deal since we run this against cjs / esm both, but looks like it's a minor typo. Closes WEB-1894
1 parent 62267ec commit 555b131

File tree

1 file changed

+37
-28
lines changed
  • packages/next-swc/crates/next-core/src/next_server

1 file changed

+37
-28
lines changed

packages/next-swc/crates/next-core/src/next_server/resolve.rs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ async fn is_node_resolveable(
6565
context,
6666
request,
6767
if is_esm {
68-
node_cjs_resolve_options(context.root())
69-
} else {
7068
node_esm_resolve_options(context.root())
69+
} else {
70+
node_cjs_resolve_options(context.root())
7171
},
7272
);
7373
let primary_node_assets = node_resolve_result.primary_sources().await?;
@@ -161,34 +161,43 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin {
161161
return Ok(ResolveResultOption::none());
162162
}
163163

164+
// We don't know if this is a ESM reference, so also check if it is resolveable
165+
// as ESM.
166+
let is_esm_resolveable =
167+
*is_node_resolveable(self.project_path, request, fs_path, true).await?;
168+
let is_cjs_resolveable =
169+
*is_node_resolveable(self.project_path, request, fs_path, false).await?;
170+
171+
if !is_cjs_resolveable && !is_esm_resolveable {
172+
// can't resolve request with node.js options
173+
return Ok(ResolveResultOption::none());
174+
}
175+
164176
// check if we can resolve the package from the project dir with node.js resolve
165177
// options (might be hidden by pnpm)
166-
if *is_node_resolveable(self.project_path, request, fs_path, false).await? {
167-
// We don't know if this is a ESM reference, so also check if it is resolveable
168-
// as ESM.
169-
if *is_node_resolveable(self.project_path, request, fs_path, true).await? {
170-
// mark as external
171-
return Ok(ResolveResultOption::some(
172-
ResolveResult::primary(ResolveResultItem::OriginalReferenceExternal).cell(),
173-
));
174-
} else if let Some(mut request_str) = request.await?.request() {
175-
// When it's not resolveable as ESM, there is maybe an extension missing, try
176-
// .js
177-
if !request_str.ends_with(".js") {
178-
request_str += ".js";
179-
let request =
180-
Request::parse(Value::new(Pattern::Constant(request_str.clone())));
181-
if *is_node_resolveable(self.project_path, request, fs_path, false).await?
182-
&& *is_node_resolveable(self.project_path, request, fs_path, true).await?
183-
{
184-
// mark as external, but with .js extension
185-
return Ok(ResolveResultOption::some(
186-
ResolveResult::primary(
187-
ResolveResultItem::OriginalReferenceTypeExternal(request_str),
188-
)
189-
.cell(),
190-
));
191-
}
178+
if is_cjs_resolveable {
179+
// mark as external
180+
return Ok(ResolveResultOption::some(
181+
ResolveResult::primary(ResolveResultItem::OriginalReferenceExternal).cell(),
182+
));
183+
}
184+
185+
// When it's not resolveable as ESM, there is maybe an extension missing, try
186+
// .js
187+
if let Some(mut request_str) = request.await?.request() {
188+
if !request_str.ends_with(".js") {
189+
request_str += ".js";
190+
let request = Request::parse(Value::new(Pattern::Constant(request_str.clone())));
191+
if *is_node_resolveable(self.project_path, request, fs_path, false).await?
192+
&& *is_node_resolveable(self.project_path, request, fs_path, true).await?
193+
{
194+
// mark as external, but with .js extension
195+
return Ok(ResolveResultOption::some(
196+
ResolveResult::primary(ResolveResultItem::OriginalReferenceTypeExternal(
197+
request_str,
198+
))
199+
.cell(),
200+
));
192201
}
193202
}
194203
}

0 commit comments

Comments
 (0)