Skip to content

Commit

Permalink
refactor(turbopack): Use ResolvedVc for turbopack crate (vercel#7…
Browse files Browse the repository at this point in the history
…2791)

### What?

Follow-up for vercel#72564

### Why?

We need to refactor this for future works.

### How?

---------

Co-authored-by: Benjamin Woodruff <[email protected]>
  • Loading branch information
kdy1 and bgw authored Nov 14, 2024
1 parent cc1b808 commit 7b89b02
Show file tree
Hide file tree
Showing 52 changed files with 472 additions and 380 deletions.
102 changes: 66 additions & 36 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,24 +309,32 @@ impl AppProject {
}

#[turbo_tasks::function]
fn rsc_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
async fn rsc_module_context(self: Vc<Self>) -> Result<Vc<ModuleAssetContext>> {
let transitions = [
(
ECMASCRIPT_CLIENT_TRANSITION_NAME.into(),
self.client_reference_transition(),
self.client_reference_transition().to_resolved().await?,
),
(
"next-dynamic".into(),
Vc::upcast(NextDynamicTransition::new(Vc::upcast(
self.client_transition(),
))),
ResolvedVc::upcast(
NextDynamicTransition::new(Vc::upcast(self.client_transition()))
.to_resolved()
.await?,
),
),
(
"next-ssr".into(),
ResolvedVc::upcast(self.ssr_transition().to_resolved().await?),
),
(
"next-shared".into(),
ResolvedVc::upcast(self.shared_transition().to_resolved().await?),
),
("next-ssr".into(), Vc::upcast(self.ssr_transition())),
("next-shared".into(), Vc::upcast(self.shared_transition())),
]
.into_iter()
.collect();
ModuleAssetContext::new(
Ok(ModuleAssetContext::new(
TransitionOptions {
named_transitions: transitions,
transition_rules: vec![TransitionRule::new(
Expand All @@ -340,31 +348,38 @@ impl AppProject {
self.rsc_module_options_context(),
self.rsc_resolve_options_context(),
Vc::cell("app-rsc".into()),
)
))
}

#[turbo_tasks::function]
fn edge_rsc_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
async fn edge_rsc_module_context(self: Vc<Self>) -> Result<Vc<ModuleAssetContext>> {
let transitions = [
(
ECMASCRIPT_CLIENT_TRANSITION_NAME.into(),
self.edge_client_reference_transition(),
self.edge_client_reference_transition()
.to_resolved()
.await?,
),
(
"next-dynamic".into(),
Vc::upcast(NextDynamicTransition::new(Vc::upcast(
self.client_transition(),
))),
ResolvedVc::upcast(
NextDynamicTransition::new(Vc::upcast(self.client_transition()))
.to_resolved()
.await?,
),
),
(
"next-ssr".into(),
ResolvedVc::upcast(self.edge_ssr_transition().to_resolved().await?),
),
("next-ssr".into(), Vc::upcast(self.edge_ssr_transition())),
(
"next-shared".into(),
Vc::upcast(self.edge_shared_transition()),
ResolvedVc::upcast(self.edge_shared_transition().to_resolved().await?),
),
]
.into_iter()
.collect();
ModuleAssetContext::new(
Ok(ModuleAssetContext::new(
TransitionOptions {
named_transitions: transitions,
transition_rules: vec![TransitionRule::new(
Expand All @@ -378,29 +393,37 @@ impl AppProject {
self.edge_rsc_module_options_context(),
self.edge_rsc_resolve_options_context(),
Vc::cell("app-edge-rsc".into()),
)
))
}

#[turbo_tasks::function]
fn route_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
async fn route_module_context(self: Vc<Self>) -> Result<Vc<ModuleAssetContext>> {
let transitions = [
(
ECMASCRIPT_CLIENT_TRANSITION_NAME.into(),
self.client_reference_transition(),
self.client_reference_transition().to_resolved().await?,
),
(
"next-dynamic".into(),
Vc::upcast(NextDynamicTransition::new(Vc::upcast(
self.client_transition(),
))),
ResolvedVc::upcast(
NextDynamicTransition::new(Vc::upcast(self.client_transition()))
.to_resolved()
.await?,
),
),
(
"next-ssr".into(),
ResolvedVc::upcast(self.ssr_transition().to_resolved().await?),
),
(
"next-shared".into(),
ResolvedVc::upcast(self.shared_transition().to_resolved().await?),
),
("next-ssr".into(), Vc::upcast(self.ssr_transition())),
("next-shared".into(), Vc::upcast(self.shared_transition())),
]
.into_iter()
.collect();

ModuleAssetContext::new(
Ok(ModuleAssetContext::new(
TransitionOptions {
named_transitions: transitions,
..Default::default()
Expand All @@ -410,31 +433,38 @@ impl AppProject {
self.route_module_options_context(),
self.route_resolve_options_context(),
Vc::cell("app-route".into()),
)
))
}

#[turbo_tasks::function]
fn edge_route_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
async fn edge_route_module_context(self: Vc<Self>) -> Result<Vc<ModuleAssetContext>> {
let transitions = [
(
ECMASCRIPT_CLIENT_TRANSITION_NAME.into(),
self.edge_client_reference_transition(),
self.edge_client_reference_transition()
.to_resolved()
.await?,
),
(
"next-dynamic".into(),
Vc::upcast(NextDynamicTransition::new(Vc::upcast(
self.client_transition(),
))),
ResolvedVc::upcast(
NextDynamicTransition::new(Vc::upcast(self.client_transition()))
.to_resolved()
.await?,
),
),
(
"next-ssr".into(),
ResolvedVc::upcast(self.edge_ssr_transition().to_resolved().await?),
),
("next-ssr".into(), Vc::upcast(self.ssr_transition())),
(
"next-shared".into(),
Vc::upcast(self.edge_shared_transition()),
ResolvedVc::upcast(self.edge_shared_transition().to_resolved().await?),
),
]
.into_iter()
.collect();
ModuleAssetContext::new(
Ok(ModuleAssetContext::new(
TransitionOptions {
named_transitions: transitions,
..Default::default()
Expand All @@ -444,7 +474,7 @@ impl AppProject {
self.edge_route_module_options_context(),
self.edge_route_resolve_options_context(),
Vc::cell("app-edge-route".into()),
)
))
}

#[turbo_tasks::function]
Expand Down
14 changes: 8 additions & 6 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,21 @@ impl PagesProject {
}

#[turbo_tasks::function]
fn transitions(self: Vc<Self>) -> Vc<TransitionOptions> {
TransitionOptions {
async fn transitions(self: Vc<Self>) -> Result<Vc<TransitionOptions>> {
Ok(TransitionOptions {
named_transitions: [(
"next-dynamic".into(),
Vc::upcast(NextDynamicTransition::new(Vc::upcast(
self.client_transition(),
))),
ResolvedVc::upcast(
NextDynamicTransition::new(Vc::upcast(self.client_transition()))
.to_resolved()
.await?,
),
)]
.into_iter()
.collect(),
..Default::default()
}
.cell()
.cell())
}

#[turbo_tasks::function]
Expand Down
15 changes: 12 additions & 3 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,10 @@ impl Project {
if let Some(app_project) = app_project {
transitions.push((
ECMASCRIPT_CLIENT_TRANSITION_NAME.into(),
app_project.edge_client_reference_transition(),
app_project
.edge_client_reference_transition()
.to_resolved()
.await?,
));
}

Expand Down Expand Up @@ -1025,7 +1028,10 @@ impl Project {
if let Some(app_project) = app_project {
transitions.push((
ECMASCRIPT_CLIENT_TRANSITION_NAME.into(),
app_project.client_reference_transition(),
app_project
.client_reference_transition()
.to_resolved()
.await?,
));
}

Expand Down Expand Up @@ -1075,7 +1081,10 @@ impl Project {
if let Some(app_project) = app_project {
transitions.push((
ECMASCRIPT_CLIENT_TRANSITION_NAME.into(),
app_project.edge_client_reference_transition(),
app_project
.edge_client_reference_transition()
.to_resolved()
.await?,
));
}

Expand Down
54 changes: 34 additions & 20 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,21 @@ pub enum ClientContextType {

#[turbo_tasks::function]
pub async fn get_client_resolve_options_context(
project_path: Vc<FileSystemPath>,
project_path: ResolvedVc<FileSystemPath>,
ty: Value<ClientContextType>,
mode: Vc<NextMode>,
next_config: Vc<NextConfig>,
execution_context: Vc<ExecutionContext>,
) -> Result<Vc<ResolveOptionsContext>> {
let next_client_import_map =
get_next_client_import_map(project_path, ty, next_config, execution_context)
get_next_client_import_map(*project_path, ty, next_config, execution_context)
.to_resolved()
.await?;
let next_client_fallback_import_map = get_next_client_fallback_import_map(ty)
.to_resolved()
.await?;
let next_client_resolved_map =
get_next_client_resolved_map(project_path, project_path, *mode.await?)
get_next_client_resolved_map(*project_path, project_path, *mode.await?)
.to_resolved()
.await?;
let custom_conditions = vec![mode.await?.condition().into()];
Expand All @@ -175,18 +175,18 @@ pub async fn get_client_resolve_options_context(
.await?,
),
ResolvedVc::upcast(
ModuleFeatureReportResolvePlugin::new(project_path)
ModuleFeatureReportResolvePlugin::new(*project_path)
.to_resolved()
.await?,
),
ResolvedVc::upcast(
NextFontLocalResolvePlugin::new(project_path)
NextFontLocalResolvePlugin::new(*project_path)
.to_resolved()
.await?,
),
],
after_resolve_plugins: vec![ResolvedVc::upcast(
NextSharedRuntimeResolvePlugin::new(project_path)
NextSharedRuntimeResolvePlugin::new(*project_path)
.to_resolved()
.await?,
)],
Expand Down Expand Up @@ -216,27 +216,36 @@ fn internal_assets_conditions() -> ContextCondition {

#[turbo_tasks::function]
pub async fn get_client_module_options_context(
project_path: Vc<FileSystemPath>,
project_path: ResolvedVc<FileSystemPath>,
execution_context: ResolvedVc<ExecutionContext>,
env: ResolvedVc<Environment>,
ty: Value<ClientContextType>,
mode: Vc<NextMode>,
next_config: Vc<NextConfig>,
) -> Result<Vc<ModuleOptionsContext>> {
let next_mode = mode.await?;
let resolve_options_context =
get_client_resolve_options_context(project_path, ty, mode, next_config, *execution_context);
let resolve_options_context = get_client_resolve_options_context(
*project_path,
ty,
mode,
next_config,
*execution_context,
);

let tsconfig = get_typescript_transform_options(project_path);
let decorators_options = get_decorators_transform_options(project_path);
let tsconfig = get_typescript_transform_options(*project_path)
.to_resolved()
.await?;
let decorators_options = get_decorators_transform_options(*project_path);
let enable_mdx_rs = *next_config.mdx_rs().await?;
let jsx_runtime_options = get_jsx_transform_options(
project_path,
*project_path,
mode,
Some(resolve_options_context),
false,
next_config,
);
)
.to_resolved()
.await?;

// A separate webpack rules will be applied to codes matching
// foreign_code_context_condition. This allows to import codes from
Expand Down Expand Up @@ -288,7 +297,7 @@ pub async fn get_client_module_options_context(

let postcss_transform_options = PostCssTransformOptions {
postcss_package: Some(
get_postcss_package_mapping(project_path)
get_postcss_package_mapping(*project_path)
.to_resolved()
.await?,
),
Expand All @@ -301,8 +310,8 @@ pub async fn get_client_module_options_context(
config_location: PostCssConfigLocation::ProjectPath,
..postcss_transform_options.clone()
};
let enable_postcss_transform = Some(postcss_transform_options.cell());
let enable_foreign_postcss_transform = Some(postcss_foreign_transform_options.cell());
let enable_postcss_transform = Some(postcss_transform_options.resolved_cell());
let enable_foreign_postcss_transform = Some(postcss_foreign_transform_options.resolved_cell());

let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
Expand Down Expand Up @@ -333,8 +342,10 @@ pub async fn get_client_module_options_context(

let internal_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
enable_jsx: Some(JsxTransformOptions::default().cell()),
enable_typescript_transform: Some(
TypescriptTransformOptions::default().resolved_cell(),
),
enable_jsx: Some(JsxTransformOptions::default().resolved_cell()),
..module_options_context.ecmascript.clone()
},
enable_postcss_transform: None,
Expand Down Expand Up @@ -364,9 +375,12 @@ pub async fn get_client_module_options_context(
rules: vec![
(
foreign_code_context_condition(next_config, project_path).await?,
foreign_codes_options_context.cell(),
foreign_codes_options_context.resolved_cell(),
),
(
internal_assets_conditions(),
internal_context.resolved_cell(),
),
(internal_assets_conditions(), internal_context.cell()),
],
module_rules: next_client_rules,
..module_options_context
Expand Down
Loading

0 comments on commit 7b89b02

Please sign in to comment.