Skip to content

Commit

Permalink
refactor(turbopack-resolve): Port all struct fields in the `turbopack…
Browse files Browse the repository at this point in the history
…-resolved` crate to `turbo_tasks::ResolvedVc`
  • Loading branch information
bgw committed Oct 28, 2024
1 parent 4fee48e commit ca833e5
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 96 deletions.
42 changes: 31 additions & 11 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,47 @@ pub async fn get_client_resolve_options_context(
execution_context: Vc<ExecutionContext>,
) -> Result<Vc<ResolveOptionsContext>> {
let next_client_import_map =
get_next_client_import_map(project_path, ty, next_config, execution_context);
let next_client_fallback_import_map = get_next_client_fallback_import_map(ty);
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()];
let module_options_context = ResolveOptionsContext {
enable_node_modules: Some(project_path.root().resolve().await?),
enable_node_modules: Some(project_path.root().to_resolved().await?),
custom_conditions,
import_map: Some(next_client_import_map),
fallback_import_map: Some(next_client_fallback_import_map),
resolved_map: Some(next_client_resolved_map),
browser: true,
module: true,
before_resolve_plugins: vec![
Vc::upcast(get_invalid_server_only_resolve_plugin(project_path)),
Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)),
Vc::upcast(NextFontLocalResolvePlugin::new(project_path)),
ResolvedVc::upcast(
get_invalid_server_only_resolve_plugin(project_path)
.to_resolved()
.await?,
),
ResolvedVc::upcast(
ModuleFeatureReportResolvePlugin::new(project_path)
.to_resolved()
.await?,
),
ResolvedVc::upcast(
NextFontLocalResolvePlugin::new(project_path)
.to_resolved()
.await?,
),
],
after_resolve_plugins: vec![Vc::upcast(NextSharedRuntimeResolvePlugin::new(
project_path,
))],
after_resolve_plugins: vec![ResolvedVc::upcast(
NextSharedRuntimeResolvePlugin::new(project_path)
.to_resolved()
.await?,
)],
..Default::default()
};
Ok(ResolveOptionsContext {
Expand All @@ -175,7 +195,7 @@ pub async fn get_client_resolve_options_context(
custom_extensions: next_config.resolve_extension().await?.clone_value(),
rules: vec![(
foreign_code_context_condition(next_config, project_path).await?,
module_options_context.clone().cell(),
module_options_context.clone().resolved_cell(),
)],
..module_options_context
}
Expand Down
46 changes: 30 additions & 16 deletions crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,28 @@ pub async fn get_edge_resolve_options_context(
execution_context: Vc<ExecutionContext>,
) -> Result<Vc<ResolveOptionsContext>> {
let next_edge_import_map =
get_next_edge_import_map(project_path, ty, next_config, execution_context);
get_next_edge_import_map(project_path, ty, next_config, execution_context)
.to_resolved()
.await?;

let ty: ServerContextType = ty.into_value();

let mut before_resolve_plugins = vec![Vc::upcast(ModuleFeatureReportResolvePlugin::new(
project_path,
))];
let mut before_resolve_plugins = vec![ResolvedVc::upcast(
ModuleFeatureReportResolvePlugin::new(project_path)
.to_resolved()
.await?,
)];
if matches!(
ty,
ServerContextType::Pages { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. }
) {
before_resolve_plugins.push(Vc::upcast(NextFontLocalResolvePlugin::new(project_path)));
before_resolve_plugins.push(ResolvedVc::upcast(
NextFontLocalResolvePlugin::new(project_path)
.to_resolved()
.await?,
));
};

if matches!(
Expand All @@ -120,17 +128,23 @@ pub async fn get_edge_resolve_options_context(
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation { .. }
) {
before_resolve_plugins.push(Vc::upcast(get_invalid_client_only_resolve_plugin(
project_path,
)));
before_resolve_plugins.push(Vc::upcast(get_invalid_styled_jsx_resolve_plugin(
project_path,
)));
before_resolve_plugins.push(ResolvedVc::upcast(
get_invalid_client_only_resolve_plugin(project_path)
.to_resolved()
.await?,
));
before_resolve_plugins.push(ResolvedVc::upcast(
get_invalid_styled_jsx_resolve_plugin(project_path)
.to_resolved()
.await?,
));
}

let after_resolve_plugins = vec![Vc::upcast(NextSharedRuntimeResolvePlugin::new(
project_path,
))];
let after_resolve_plugins = vec![ResolvedVc::upcast(
NextSharedRuntimeResolvePlugin::new(project_path)
.to_resolved()
.await?,
)];

// https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102
let mut custom_conditions = vec![mode.await?.condition().into()];
Expand All @@ -147,7 +161,7 @@ pub async fn get_edge_resolve_options_context(
};

let resolve_options_context = ResolveOptionsContext {
enable_node_modules: Some(project_path.root().resolve().await?),
enable_node_modules: Some(project_path.root().to_resolved().await?),
enable_edge_node_externals: true,
custom_conditions,
import_map: Some(next_edge_import_map),
Expand All @@ -166,7 +180,7 @@ pub async fn get_edge_resolve_options_context(
custom_extensions: next_config.resolve_extension().await?.clone_value(),
rules: vec![(
foreign_code_context_condition(next_config, project_path).await?,
resolve_options_context.clone().cell(),
resolve_options_context.clone().resolved_cell(),
)],
..resolve_options_context
}
Expand Down
70 changes: 46 additions & 24 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,22 @@ pub async fn get_server_resolve_options_context(
execution_context: Vc<ExecutionContext>,
) -> Result<Vc<ResolveOptionsContext>> {
let next_server_import_map =
get_next_server_import_map(project_path, ty, next_config, execution_context);
get_next_server_import_map(project_path, ty, next_config, execution_context)
.to_resolved()
.await?;
let foreign_code_context_condition =
foreign_code_context_condition(next_config, project_path).await?;
let root_dir = project_path.root().resolve().await?;
let module_feature_report_resolve_plugin = ModuleFeatureReportResolvePlugin::new(project_path);
let invalid_client_only_resolve_plugin = get_invalid_client_only_resolve_plugin(project_path);
let root_dir = project_path.root().to_resolved().await?;
let module_feature_report_resolve_plugin = ModuleFeatureReportResolvePlugin::new(project_path)
.to_resolved()
.await?;
let invalid_client_only_resolve_plugin = get_invalid_client_only_resolve_plugin(project_path)
.to_resolved()
.await?;
let invalid_styled_jsx_client_only_resolve_plugin =
get_invalid_styled_jsx_resolve_plugin(project_path);
get_invalid_styled_jsx_resolve_plugin(project_path)
.to_resolved()
.await?;

// Always load these predefined packages as external.
let mut external_packages: Vec<RcStr> = load_next_js_templateon(
Expand Down Expand Up @@ -181,7 +189,9 @@ pub async fn get_server_resolve_options_context(
project_path.root(),
ExternalPredicate::Only(ResolvedVc::cell(external_packages)).cell(),
*next_config.import_externals().await?,
);
)
.to_resolved()
.await?;

let mut custom_conditions = vec![mode.await?.condition().to_string().into()];
custom_conditions.extend(
Expand All @@ -205,27 +215,37 @@ pub async fn get_server_resolve_options_context(
ExternalPredicate::AllExcept(ResolvedVc::cell(transpiled_packages)).cell(),
*next_config.import_externals().await?,
)
.to_resolved()
.await?
};

let next_external_plugin = NextExternalResolvePlugin::new(project_path);
let next_external_plugin = NextExternalResolvePlugin::new(project_path)
.to_resolved()
.await?;
let next_node_shared_runtime_plugin =
NextNodeSharedRuntimeResolvePlugin::new(project_path, Value::new(ty));
NextNodeSharedRuntimeResolvePlugin::new(project_path, Value::new(ty))
.to_resolved()
.await?;

let mut before_resolve_plugins = match ty {
ServerContextType::Pages { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. } => {
vec![
Vc::upcast(NextFontLocalResolvePlugin::new(project_path)),
Vc::upcast(module_feature_report_resolve_plugin),
ResolvedVc::upcast(
NextFontLocalResolvePlugin::new(project_path)
.to_resolved()
.await?,
),
ResolvedVc::upcast(module_feature_report_resolve_plugin),
]
}
ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppRoute { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation { .. } => {
vec![Vc::upcast(module_feature_report_resolve_plugin)]
vec![ResolvedVc::upcast(module_feature_report_resolve_plugin)]
}
};

Expand All @@ -234,28 +254,28 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::PagesApi { .. }
| ServerContextType::PagesData { .. } => {
vec![
Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(external_cjs_modules_plugin),
Vc::upcast(next_external_plugin),
ResolvedVc::upcast(next_node_shared_runtime_plugin),
ResolvedVc::upcast(external_cjs_modules_plugin),
ResolvedVc::upcast(next_external_plugin),
]
}
ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. } => {
vec![
Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(server_external_packages_plugin),
Vc::upcast(next_external_plugin),
ResolvedVc::upcast(next_node_shared_runtime_plugin),
ResolvedVc::upcast(server_external_packages_plugin),
ResolvedVc::upcast(next_external_plugin),
]
}
ServerContextType::Middleware { .. } => {
vec![Vc::upcast(next_node_shared_runtime_plugin)]
vec![ResolvedVc::upcast(next_node_shared_runtime_plugin)]
}
ServerContextType::Instrumentation { .. } => {
vec![
Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(server_external_packages_plugin),
Vc::upcast(next_external_plugin),
ResolvedVc::upcast(next_node_shared_runtime_plugin),
ResolvedVc::upcast(server_external_packages_plugin),
ResolvedVc::upcast(next_external_plugin),
]
}
};
Expand All @@ -276,8 +296,10 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::AppRoute { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation { .. } => {
before_resolve_plugins.push(Vc::upcast(invalid_client_only_resolve_plugin));
before_resolve_plugins.push(Vc::upcast(invalid_styled_jsx_client_only_resolve_plugin));
before_resolve_plugins.push(ResolvedVc::upcast(invalid_client_only_resolve_plugin));
before_resolve_plugins.push(ResolvedVc::upcast(
invalid_styled_jsx_client_only_resolve_plugin,
));
}
ServerContextType::AppSSR { .. } => {
//[TODO] Build error in this context makes rsc-build-error.ts fail which expects runtime error code
Expand All @@ -304,7 +326,7 @@ pub async fn get_server_resolve_options_context(
custom_extensions: next_config.resolve_extension().await?.clone_value(),
rules: vec![(
foreign_code_context_condition,
resolve_options_context.clone().cell(),
resolve_options_context.clone().resolved_cell(),
)],
..resolve_options_context
}
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ async fn create_module_asset(
ResolvedMap {
by_glob: glob_mappings,
}
.cell(),
.resolved_cell(),
);
}

Expand Down
6 changes: 3 additions & 3 deletions turbopack/crates/turbopack-cli/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ pub fn get_client_import_map(project_path: Vc<FileSystemPath>) -> Vc<ImportMap>
pub async fn get_client_resolve_options_context(
project_path: Vc<FileSystemPath>,
) -> Result<Vc<ResolveOptionsContext>> {
let next_client_import_map = get_client_import_map(project_path);
let next_client_import_map = get_client_import_map(project_path).to_resolved().await?;
let module_options_context = ResolveOptionsContext {
enable_node_modules: Some(project_path.root().resolve().await?),
enable_node_modules: Some(project_path.root().to_resolved().await?),
custom_conditions: vec!["development".into()],
import_map: Some(next_client_import_map),
browser: true,
Expand All @@ -88,7 +88,7 @@ pub async fn get_client_resolve_options_context(
enable_react: true,
rules: vec![(
foreign_code_context_condition().await?,
module_options_context.clone().cell(),
module_options_context.clone().resolved_cell(),
)],
..module_options_context
}
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-core/src/issue/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Issue for ResolvingIssue {

if let Some(import_map) = &self.resolve_options.await?.import_map {
for request in request_parts {
match lookup_import_map(*import_map, self.file_path, *request).await {
match lookup_import_map(**import_map, self.file_path, *request).await {
Ok(None) => {}
Ok(Some(str)) => writeln!(description, "Import map: {}", str)?,
Err(err) => {
Expand Down
12 changes: 7 additions & 5 deletions turbopack/crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,12 @@ pub struct ResolveOptions {
/// The default files to resolve in a folder.
pub default_files: Vec<RcStr>,
/// An import map to use before resolving a request.
pub import_map: Option<Vc<ImportMap>>,
pub import_map: Option<ResolvedVc<ImportMap>>,
/// An import map to use when a request is otherwise unresolvable.
pub fallback_import_map: Option<Vc<ImportMap>>,
pub resolved_map: Option<Vc<ResolvedMap>>,
pub before_resolve_plugins: Vec<Vc<Box<dyn BeforeResolvePlugin>>>,
pub plugins: Vec<Vc<Box<dyn AfterResolvePlugin>>>,
pub resolved_map: Option<ResolvedVc<ResolvedMap>>,
pub before_resolve_plugins: Vec<ResolvedVc<Box<dyn BeforeResolvePlugin>>>,
pub plugins: Vec<ResolvedVc<Box<dyn AfterResolvePlugin>>>,
/// Support resolving *.js requests to *.ts files
pub enable_typescript_with_output_extension: bool,
/// Warn instead of error for resolve errors
Expand All @@ -521,7 +521,9 @@ impl ResolveOptions {
resolve_options
.import_map
.map(|current_import_map| current_import_map.extend(import_map))
.unwrap_or(import_map),
.unwrap_or(import_map)
.to_resolved()
.await?,
);
Ok(resolve_options.into())
}
Expand Down
8 changes: 4 additions & 4 deletions turbopack/crates/turbopack-resolve/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async fn base_resolve_options(
let additional_import_map = additional_import_map.await?;
import_map.extend_ref(&additional_import_map);
}
let import_map = import_map.cell();
let import_map = import_map.resolved_cell();

let plugins = opt.after_resolve_plugins.clone();

Expand Down Expand Up @@ -281,7 +281,7 @@ pub async fn resolve_options(
let context_value = &*resolve_path.await?;
for (condition, new_options_context) in options_context_value.rules.iter() {
if condition.matches(context_value).await? {
return Ok(resolve_options(resolve_path, *new_options_context));
return Ok(resolve_options(resolve_path, **new_options_context));
}
}
}
Expand All @@ -304,13 +304,13 @@ pub async fn resolve_options(
// overwrites any other mappings.
let resolve_options = options_context_value
.import_map
.map(|import_map| resolve_options.with_extended_import_map(import_map))
.map(|import_map| resolve_options.with_extended_import_map(*import_map))
.unwrap_or(resolve_options);
// And the same for the fallback_import_map
let resolve_options = options_context_value
.fallback_import_map
.map(|fallback_import_map| {
resolve_options.with_extended_fallback_import_map(fallback_import_map)
resolve_options.with_extended_fallback_import_map(*fallback_import_map)
})
.unwrap_or(resolve_options);

Expand Down
Loading

0 comments on commit ca833e5

Please sign in to comment.