Unverified Commit 84eb323f authored by Benjamin Woodruff ‮'s avatar Benjamin Woodruff Committed by GitHub

Turbopack: Allow ResolveMap entries to match against all possible roots (#99555)

`get_next_client_resolved_map` shouldn't require the glob match within a
specific root, because the package could be in the package manager's
GVS, which can be configured as an additional root.
parent aa78e963
...@@ -167,7 +167,6 @@ pub async fn get_client_resolve_options_context( ...@@ -167,7 +167,6 @@ pub async fn get_client_resolve_options_context(
.await?; .await?;
let concurrent_router_queue = *next_config.enable_concurrent_router_queue().await?; let concurrent_router_queue = *next_config.enable_concurrent_router_queue().await?;
let next_client_resolved_map = get_next_client_resolved_map( let next_client_resolved_map = get_next_client_resolved_map(
project_path.clone(),
project_path.clone(), project_path.clone(),
*mode.await?, *mode.await?,
expose_testing_api, expose_testing_api,
......
...@@ -580,7 +580,6 @@ async fn insert_unsupported_node_internal_aliases(import_map: &mut ImportMap) -> ...@@ -580,7 +580,6 @@ async fn insert_unsupported_node_internal_aliases(import_map: &mut ImportMap) ->
pub async fn get_next_client_resolved_map( pub async fn get_next_client_resolved_map(
context_path: FileSystemPath, context_path: FileSystemPath,
root: FileSystemPath,
_mode: NextMode, _mode: NextMode,
expose_testing_api: bool, expose_testing_api: bool,
concurrent_router_queue: bool, concurrent_router_queue: bool,
...@@ -591,14 +590,13 @@ pub async fn get_next_client_resolved_map( ...@@ -591,14 +590,13 @@ pub async fn get_next_client_resolved_map(
// into the client bundle. This is the Turbopack analog of the webpack alias in // into the client bundle. This is the Turbopack analog of the webpack alias in
// `create-compiler-aliases.ts` and is client-only because `get_next_client_resolved_map` // `create-compiler-aliases.ts` and is client-only because `get_next_client_resolved_map`
// is used only by the client context. Matching is on the resolved file path, so it // is used only by the client context. Matching is on the resolved file path, so it
// intercepts the relative import regardless of which module pulls it in. Anchored at the // intercepts the relative import regardless of which module pulls it in. Not anchored to
// filesystem root so it matches wherever `next` resolves from (node_modules, pnpm store, // any filesystem, so it matches wherever `next` resolves from (node_modules, pnpm store,
// or monorepo `packages/next`). // monorepo `packages/next`, or an additional root such as a global pnpm virtual store).
let fs_root = root.root().owned().await?;
let mut glob_mappings = Vec::with_capacity(BROWSER_VARIANT_MODULES.len() + 1); let mut glob_mappings = Vec::with_capacity(BROWSER_VARIANT_MODULES.len() + 1);
for module in BROWSER_VARIANT_MODULES { for module in BROWSER_VARIANT_MODULES {
glob_mappings.push(( glob_mappings.push((
fs_root.clone(), None,
Glob::new( Glob::new(
format!("**/next/dist/{module}.js").into(), format!("**/next/dist/{module}.js").into(),
GlobOptions::default(), GlobOptions::default(),
...@@ -619,7 +617,7 @@ pub async fn get_next_client_resolved_map( ...@@ -619,7 +617,7 @@ pub async fn get_next_client_resolved_map(
// alias in `create-compiler-aliases.ts`. // alias in `create-compiler-aliases.ts`.
if !expose_testing_api { if !expose_testing_api {
glob_mappings.push(( glob_mappings.push((
fs_root.clone(), None,
Glob::new( Glob::new(
rcstr!("**/next/dist/client/components/segment-cache/navigation-testing-lock.js"), rcstr!("**/next/dist/client/components/segment-cache/navigation-testing-lock.js"),
GlobOptions::default(), GlobOptions::default(),
...@@ -642,7 +640,7 @@ pub async fn get_next_client_resolved_map( ...@@ -642,7 +640,7 @@ pub async fn get_next_client_resolved_map(
// This mirrors the webpack alias in `create-compiler-aliases.ts`. // This mirrors the webpack alias in `create-compiler-aliases.ts`.
if concurrent_router_queue { if concurrent_router_queue {
glob_mappings.push(( glob_mappings.push((
fs_root.clone(), None,
Glob::new( Glob::new(
rcstr!("**/next/dist/client/components/navigator.js"), rcstr!("**/next/dist/client/components/navigator.js"),
GlobOptions::default(), GlobOptions::default(),
...@@ -655,7 +653,7 @@ pub async fn get_next_client_resolved_map( ...@@ -655,7 +653,7 @@ pub async fn get_next_client_resolved_map(
), ),
)); ));
glob_mappings.push(( glob_mappings.push((
fs_root, None,
Glob::new( Glob::new(
rcstr!("**/next/dist/client/app-call-server.js"), rcstr!("**/next/dist/client/app-call-server.js"),
GlobOptions::default(), GlobOptions::default(),
......
...@@ -397,7 +397,13 @@ impl ImportMap { ...@@ -397,7 +397,13 @@ impl ImportMap {
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct ResolvedMap { pub struct ResolvedMap {
pub by_glob: Vec<(FileSystemPath, ResolvedVc<Glob>, ResolvedVc<ImportMapping>)>, /// Each glob is matched against the resolved path relative to its root. A root of `None`
/// matches resolved paths on any filesystem, relative to that filesystem's root.
pub by_glob: Vec<(
/* root */ Option<FileSystemPath>,
ResolvedVc<Glob>,
ResolvedVc<ImportMapping>,
)>,
} }
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
...@@ -609,7 +615,11 @@ impl ResolvedMap { ...@@ -609,7 +615,11 @@ impl ResolvedMap {
request: Vc<Request>, request: Vc<Request>,
) -> Result<Vc<ImportMapResult>> { ) -> Result<Vc<ImportMapResult>> {
for (root, glob, mapping) in self.by_glob.iter() { for (root, glob, mapping) in self.by_glob.iter() {
if let Some(path) = root.get_path_to(&resolved) let path = match root {
Some(root) => root.get_path_to(&resolved),
None => Some(&*resolved.path),
};
if let Some(path) = path
&& glob.await?.matches(path) && glob.await?.matches(path)
{ {
return Ok(import_mapping_to_result( return Ok(import_mapping_to_result(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment