diff --git a/crates/next-core/src/next_server/resolve.rs b/crates/next-core/src/next_server/resolve.rs index f9a9e5eb6504c..408a40aa74cb4 100644 --- a/crates/next-core/src/next_server/resolve.rs +++ b/crates/next-core/src/next_server/resolve.rs @@ -24,8 +24,10 @@ use turbopack_binding::{ #[turbo_tasks::value(into = "shared")] pub enum ExternalPredicate { /// Mark all modules as external if they're not listed in the list. + /// Applies only to imports outside of node_modules. AllExcept(Vc>), - /// Only mark modules listed as external. + /// Only mark modules listed as external, whether inside node_modules or + /// not. Only(Vc>), } @@ -76,13 +78,10 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { async fn after_resolve( &self, fs_path: Vc, - context: Vc, + lookup_path: Vc, reference_type: Value, request: Vc, ) -> Result> { - if *condition(self.root).matches(context).await? { - return Ok(ResolveResultOption::none()); - } let request_value = &*request.await?; if !matches!(request_value, Request::Module { .. }) { return Ok(ResolveResultOption::none()); @@ -104,6 +103,10 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { let predicate = self.predicate.await?; let must_be_external = match &*predicate { ExternalPredicate::AllExcept(exceptions) => { + if *condition(self.root).matches(lookup_path).await? { + return Ok(ResolveResultOption::none()); + } + let exception_glob = packages_glob(*exceptions).await?; if let Some(PackagesGlobs { @@ -205,13 +208,13 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { let mut request = request; let node_resolve_options = if is_esm { - node_esm_resolve_options(context.root()) + node_esm_resolve_options(lookup_path.root()) } else { - node_cjs_resolve_options(context.root()) + node_cjs_resolve_options(lookup_path.root()) }; let result_from_original_location = loop { let node_resolved_from_original_location = resolve( - context, + lookup_path, reference_type.clone(), request, node_resolve_options, @@ -367,7 +370,7 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { // It would be more efficient to use an CJS external instead of an ESM external, // but we need to verify if that would be correct (as in resolves to the same // file). - let node_resolve_options = node_cjs_resolve_options(context.root()); + let node_resolve_options = node_cjs_resolve_options(lookup_path.root()); let node_resolved = resolve( self.project_path, reference_type.clone(), diff --git a/crates/next-core/src/next_shared/resolve.rs b/crates/next-core/src/next_shared/resolve.rs index 9214f07636fde..26a7956dfd322 100644 --- a/crates/next-core/src/next_shared/resolve.rs +++ b/crates/next-core/src/next_shared/resolve.rs @@ -232,7 +232,7 @@ impl AfterResolvePlugin for NextExternalResolvePlugin { async fn after_resolve( &self, fs_path: Vc, - _context: Vc, + _lookup_path: Vc, _reference_type: Value, _request: Vc, ) -> Result> { @@ -282,7 +282,7 @@ impl AfterResolvePlugin for NextNodeSharedRuntimeResolvePlugin { async fn after_resolve( &self, fs_path: Vc, - _context: Vc, + _lookup_path: Vc, _reference_type: Value, _request: Vc, ) -> Result> { @@ -406,7 +406,7 @@ impl AfterResolvePlugin for NextSharedRuntimeResolvePlugin { async fn after_resolve( &self, fs_path: Vc, - _context: Vc, + _lookup_path: Vc, _reference_type: Value, _request: Vc, ) -> Result> { diff --git a/docs/02-app/02-api-reference/05-next-config-js/serverExternalPackages.mdx b/docs/02-app/02-api-reference/05-next-config-js/serverExternalPackages.mdx index 45e2afe74524b..2583ab63d98ce 100644 --- a/docs/02-app/02-api-reference/05-next-config-js/serverExternalPackages.mdx +++ b/docs/02-app/02-api-reference/05-next-config-js/serverExternalPackages.mdx @@ -47,6 +47,7 @@ Next.js includes a [short list of popular packages](https://github.com/vercel/ne - `isolated-vm` - `jest` - `jsdom` +- `keyv` - `libsql` - `mdx-bundler` - `mongodb` diff --git a/docs/03-pages/02-api-reference/03-next-config-js/serverExternalPackages.mdx b/docs/03-pages/02-api-reference/03-next-config-js/serverExternalPackages.mdx index f869796edf7a8..dcb66eb0abdeb 100644 --- a/docs/03-pages/02-api-reference/03-next-config-js/serverExternalPackages.mdx +++ b/docs/03-pages/02-api-reference/03-next-config-js/serverExternalPackages.mdx @@ -47,6 +47,7 @@ Next.js includes a [short list of popular packages](https://github.com/vercel/ne - `isolated-vm` - `jest` - `jsdom` +- `keyv` - `libsql` - `mdx-bundler` - `mongodb` diff --git a/packages/next/src/lib/server-external-packages.json b/packages/next/src/lib/server-external-packages.json index f49323e2f25de..6dde1971f0667 100644 --- a/packages/next/src/lib/server-external-packages.json +++ b/packages/next/src/lib/server-external-packages.json @@ -28,6 +28,7 @@ "isolated-vm", "jest", "jsdom", + "keyv", "libsql", "mdx-bundler", "mongodb",