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

refactor(turbopack-resolve): Port all struct fields in the turbopack-resolve crate to turbo_tasks::ResolvedVc #71948

Merged
merged 1 commit into from
Oct 31, 2024
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
let resolve_options = resolve_options(
project_path,
ResolveOptionsContext {
enable_node_modules: Some(project_path.root().resolve().await?),
enable_node_modules: Some(project_path.root().to_resolved().await?),
enable_node_native_modules: true,
..Default::default()
}
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
Loading
Loading