Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack: unique node middleware layer name #76447

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
102 changes: 65 additions & 37 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@
self.project().hmr_identifiers()
}

/// Gets a source map for a particular `file_path`. If `dev` mode is

Check warning on line 498 in crates/next-api/src/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

unresolved link to `OptionSourceMap::none`
/// disabled, this will always return [`OptionSourceMap::none`].
#[turbo_tasks::function]
pub fn get_source_map(
Expand Down Expand Up @@ -1223,7 +1223,7 @@
}

#[turbo_tasks::function]
async fn middleware_context(self: Vc<Self>) -> Result<Vc<Box<dyn AssetContext>>> {
async fn edge_middleware_context(self: Vc<Self>) -> Result<Vc<Box<dyn AssetContext>>> {
let mut transitions = vec![];

let app_dir = *find_app_dir(self.project_path()).await?;
Expand All @@ -1244,7 +1244,7 @@
));
}

let edge_module_context = ModuleAssetContext::new(
Ok(Vc::upcast(ModuleAssetContext::new(
TransitionOptions {
named_transitions: transitions.clone().into_iter().collect(),
..Default::default()
Expand Down Expand Up @@ -1273,8 +1273,68 @@
self.next_config(),
self.execution_context(),
),
Vc::cell("middleware-edge".into()),
)))
}

#[turbo_tasks::function]
async fn node_middleware_context(self: Vc<Self>) -> Result<Vc<Box<dyn AssetContext>>> {
let mut transitions = vec![];

let app_dir = *find_app_dir(self.project_path()).await?;
let app_project = *self.app_project().await?;

let ecmascript_client_reference_transition_name = match app_project {
Some(app_project) => Some(app_project.client_transition_name().to_resolved().await?),
None => None,
};

if let Some(app_project) = app_project {
transitions.push((
ECMASCRIPT_CLIENT_TRANSITION_NAME.into(),
app_project
.edge_ecmascript_client_reference_transition()
.to_resolved()
.await?,
));
}

Ok(Vc::upcast(ModuleAssetContext::new(
TransitionOptions {
named_transitions: transitions.clone().into_iter().collect(),
..Default::default()
}
.cell(),
self.server_compile_time_info(),
get_server_module_options_context(
self.project_path(),
self.execution_context(),
Value::new(ServerContextType::Middleware {
app_dir,
ecmascript_client_reference_transition_name,
}),
self.next_mode(),
self.next_config(),
NextRuntime::NodeJs,
self.encryption_key(),
),
get_server_resolve_options_context(
self.project_path(),
Value::new(ServerContextType::Middleware {
app_dir,
ecmascript_client_reference_transition_name,
}),
self.next_mode(),
self.next_config(),
self.execution_context(),
),
Vc::cell("middleware".into()),
);
)))
}

#[turbo_tasks::function]
async fn middleware_context(self: Vc<Self>) -> Result<Vc<Box<dyn AssetContext>>> {
let edge_module_context = self.edge_middleware_context();

let middleware = self.find_middleware();
let FindContextFileResult::Found(fs_path, _) = *middleware.await? else {
Expand All @@ -1292,41 +1352,9 @@
let config = parse_config_from_source(module, NextRuntime::Edge).await?;

if matches!(config.runtime, NextRuntime::NodeJs) {
let server_module_context = ModuleAssetContext::new(
TransitionOptions {
named_transitions: transitions.clone().into_iter().collect(),
..Default::default()
}
.cell(),
self.server_compile_time_info(),
get_server_module_options_context(
self.project_path(),
self.execution_context(),
Value::new(ServerContextType::Middleware {
app_dir,
ecmascript_client_reference_transition_name,
}),
self.next_mode(),
self.next_config(),
NextRuntime::NodeJs,
self.encryption_key(),
),
get_server_resolve_options_context(
self.project_path(),
Value::new(ServerContextType::Middleware {
app_dir,
ecmascript_client_reference_transition_name,
}),
self.next_mode(),
self.next_config(),
self.execution_context(),
),
Vc::cell("middleware".into()),
);

Ok(Vc::upcast(server_module_context))
Ok(self.node_middleware_context())
} else {
Ok(Vc::upcast(edge_module_context))
Ok(edge_module_context)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/development/middleware-errors/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe('middleware - development errors', () => {
isTurbopack
? '\n ⨯ Error: booooom!' +
// TODO(veil): Should be sourcemapped
'\n at [project]/middleware.js [middleware] (ecmascript)'
'\n at [project]/middleware.js [middleware-edge] (ecmascript)'
: '\n ⨯ Error: booooom!' +
// TODO: Should be anonymous method without a method name
'\n at <unknown> (middleware.js:3)' +
Expand Down
Loading