Skip to content
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
20 changes: 17 additions & 3 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ use turbopack_core::{
};
use turbopack_css::chunk::CssChunkType;
use turbopack_ecmascript::{
AnalyzeMode, TypeofWindow, chunk::EcmascriptChunkType, references::esm::UrlRewriteBehavior,
transform::PresetEnvConfig,
AnalyzeMode, TypeofWindow,
chunk::EcmascriptChunkType,
references::esm::UrlRewriteBehavior,
transform::{PresetEnvConfig, ReactCompilerTarget},
};
use turbopack_node::{
execution_context::ExecutionContext,
Expand All @@ -53,7 +55,10 @@ use crate::{
},
next_shared::{
resolve::NextSharedRuntimeResolvePlugin,
webpack_rules::{WebpackLoaderBuiltinCondition, webpack_loader_options},
webpack_rules::{
WebpackLoaderBuiltinCondition, babel::detect_react_compiler_target,
webpack_loader_options,
},
},
transform_options::{
get_decorators_transform_options, get_jsx_transform_options,
Expand Down Expand Up @@ -344,6 +349,14 @@ pub async fn get_client_module_options_context(
});

let enable_rust_react_compiler = *next_config.rust_react_compiler().await?;
let rust_react_compiler_target = if enable_rust_react_compiler.is_some() {
match detect_react_compiler_target(&project_path).await? {
Some(ReactCompilerTarget::React18) => ReactCompilerTarget::React18,
_ => ReactCompilerTarget::React19,
}
} else {
ReactCompilerTarget::React19
};

let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
Expand Down Expand Up @@ -426,6 +439,7 @@ pub async fn get_client_module_options_context(
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options.to_resolved().await?),
enable_rust_react_compiler,
rust_react_compiler_target,
..module_options_context.ecmascript.clone()
},
enable_webpack_loaders,
Expand Down
11 changes: 3 additions & 8 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ use turbopack_core::{
};
use turbopack_ecmascript::{
OptionTreeShaking, TreeShakingMode,
transform::{OptionReactCompilerCompilationMode, ReactCompilerCompilationMode},
transform::{
OptionReactCompilerCompilationMode, ReactCompilerCompilationMode, ReactCompilerTarget,
},
};
use turbopack_ecmascript_plugins::transform::{
emotion::EmotionTransformConfig, relay::RelayConfig,
Expand Down Expand Up @@ -940,13 +942,6 @@ pub enum ReactCompilerPanicThreshold {
AllErrors,
}

#[turbo_tasks::value(shared, operation)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ReactCompilerTarget {
#[serde(rename = "18")]
React18,
}

/// Subset of react compiler options, we pass these options through to the webpack loader, so it
/// must be serializable
#[turbo_tasks::value(shared, operation)]
Expand Down
16 changes: 14 additions & 2 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use turbopack_core::{
use turbopack_css::chunk::CssChunkType;
use turbopack_ecmascript::{
AnalyzeMode, CustomTransformer, TransformPlugin, TypeofWindow, chunk::EcmascriptChunkType,
references::esm::UrlRewriteBehavior,
references::esm::UrlRewriteBehavior, transform::ReactCompilerTarget,
};
use turbopack_ecmascript_plugins::transform::directives::{
client::ClientDirectiveTransformer, client_disallowed::ClientDisallowedDirectiveTransformer,
Expand Down Expand Up @@ -64,7 +64,10 @@ use crate::{
styled_jsx::get_styled_jsx_transform_rule,
swc_ecma_transform_plugins::get_swc_ecma_transform_plugin_rule,
},
webpack_rules::{WebpackLoaderBuiltinCondition, webpack_loader_options},
webpack_rules::{
WebpackLoaderBuiltinCondition, babel::detect_react_compiler_target,
webpack_loader_options,
},
},
transform_options::{
get_decorators_transform_options, get_jsx_transform_options,
Expand Down Expand Up @@ -556,6 +559,14 @@ pub async fn get_server_module_options_context(
.collect();

let enable_rust_react_compiler = *next_config.rust_react_compiler().await?;
let rust_react_compiler_target = if enable_rust_react_compiler.is_some() {
match detect_react_compiler_target(&project_path).await? {
Some(ReactCompilerTarget::React18) => ReactCompilerTarget::React18,
_ => ReactCompilerTarget::React19,
}
} else {
ReactCompilerTarget::React19
};

let source_maps = *next_config.server_source_maps().await?;
let module_options_context = ModuleOptionsContext {
Expand Down Expand Up @@ -725,6 +736,7 @@ pub async fn get_server_module_options_context(
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options.to_resolved().await?),
enable_rust_react_compiler,
rust_react_compiler_target,
..module_options_context.ecmascript
},
enable_webpack_loaders,
Expand Down
6 changes: 3 additions & 3 deletions crates/next-core/src/next_shared/webpack_rules/babel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use turbopack_core::{
resolve::{node::node_cjs_resolve_options, parse::Request, pattern::Pattern, resolve},
source::Source,
};
use turbopack_ecmascript::transform::ReactCompilerCompilationMode;
use turbopack_ecmascript::transform::{ReactCompilerCompilationMode, ReactCompilerTarget};
use turbopack_node::transforms::webpack::WebpackLoaderItem;

use crate::{
next_config::{NextConfig, ReactCompilerOptions, ReactCompilerTarget},
next_config::{NextConfig, ReactCompilerOptions},
next_import_map::try_get_next_package,
next_shared::webpack_rules::{
ManuallyConfiguredBuiltinLoaderIssue, WebpackLoaderBuiltinCondition,
Expand Down Expand Up @@ -248,7 +248,7 @@ pub async fn get_babel_loader_rules(
)])
}

async fn detect_react_compiler_target(
pub async fn detect_react_compiler_target(
project_path: &FileSystemPath,
) -> Result<Option<ReactCompilerTarget>> {
#[derive(Deserialize)]
Expand Down
23 changes: 10 additions & 13 deletions turbopack/crates/turbo-tasks-backend/src/backend/cell_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ impl MergeRestore for CellData {
// value is more authoritative than the decoded one.
debug_assert!(
!matches!(
registry::get_value_type(k.type_id).evictability,
registry::get_value_type(k.type_id()).evictability,
Evictability::Always,
),
"Found an evictable cell in a task we are restoring into: {}",
registry::get_value_type(k.type_id).ty.name,
registry::get_value_type(k.type_id()).ty.name,
);
}
}
Expand All @@ -84,12 +84,12 @@ impl DropPartial for CellData {
/// - `Evictability::Never` — value type holds session-scoped state that must not leave memory
/// (`State<>` cells, file watchers, worker pools).
fn drop_partial(&mut self) -> DropPartialOutcome {
self.0.retain(
|cell_id, _| match registry::get_value_type(cell_id.type_id).evictability {
self.0.retain(|cell_id, _| {
match registry::get_value_type(cell_id.type_id()).evictability {
Evictability::Always => false,
Evictability::Expensive | Evictability::Never => true,
},
);
}
});
if self.0.is_empty() {
return DropPartialOutcome::Empty;
}
Expand Down Expand Up @@ -141,15 +141,15 @@ impl TurboBincodeEncode for CellData {
.iter()
.filter(|(cell, _)| {
matches!(
registry::get_value_type(cell.type_id).persistence,
registry::get_value_type(cell.type_id()).persistence,
ValueTypePersistence::Persistable(_, _),
)
})
.count();
count.encode(encoder)?;
// TODO: consider sorting by type_id and delta encoding indices to reduce serialized size
for (cell_id, reference) in self.0.iter() {
let value_type = registry::get_value_type(cell_id.type_id);
let value_type = registry::get_value_type(cell_id.type_id());
let ValueTypePersistence::Persistable(encode_fn, _) = value_type.persistence else {
continue;
};
Expand All @@ -173,7 +173,7 @@ impl<Context> TurboBincodeDecode<Context> for CellData {
let mut map = InnerMap::with_capacity_and_hasher(count, BuildHasherDefault::default());
for _ in 0..count {
let cell = CellId::decode(decoder)?;
let value_type = registry::get_value_type(cell.type_id);
let value_type = registry::get_value_type(cell.type_id());
let ValueTypePersistence::Persistable(_, decode_fn) = value_type.persistence else {
return Err(DecodeError::OtherString(format!(
"cell of type {} has no bincode decoder",
Expand Down Expand Up @@ -227,10 +227,7 @@ mod tests {
struct HashOnlyV(#[allow(dead_code)] u32);

fn cell_of<V: VcValueType>(index: u32) -> CellId {
CellId {
type_id: V::get_value_type_id(),
index,
}
CellId::new(V::get_value_type_id(), index)
}

fn dummy_ref() -> SharedReference {
Expand Down
54 changes: 27 additions & 27 deletions turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ use tokio::time::{Duration, Instant};
use tracing::{Span, trace_span};
use turbo_bincode::{TurboBincodeBuffer, new_turbo_bincode_decoder, new_turbo_bincode_encoder};
use turbo_tasks::{
CellId, DynTaskInputsStorage, RawVc, ReadCellOptions, ReadCellTracking, ReadConsistency,
ReadOutputOptions, ReadTracking, SharedReference, TRANSIENT_TASK_BIT, TaskExecutionReason,
TaskId, TaskPersistence, TaskPriority, TraitTypeId, TurboTasks, TurboTasksCallApi,
TurboTasksPanic, ValueTypeId,
CellId, DynTaskInputsStorage, RawVc, RawVcUnpacked, ReadCellOptions, ReadCellTracking,
ReadConsistency, ReadOutputOptions, ReadTracking, SharedReference, TRANSIENT_TASK_BIT,
TaskExecutionReason, TaskId, TaskPersistence, TaskPriority, TraitTypeId, TurboTasks,
TurboTasksCallApi, TurboTasksPanic, ValueTypeId,
backend::{
Backend, CachedTaskType, CachedTaskTypeArc, CellContent, CellHash, TaskExecutionSpec,
TransientTaskType, TurboTaskContextError, TurboTaskLocalContextError, TurboTasksError,
Expand Down Expand Up @@ -689,8 +689,8 @@ impl TurboTasksBackend {

if let Some(output) = task.get_output() {
let result = match output {
OutputValue::Cell(cell) => Ok(Ok(RawVc::TaskCell(cell.task, cell.cell))),
OutputValue::Output(task) => Ok(Ok(RawVc::TaskOutput(*task))),
OutputValue::Cell(cell) => Ok(Ok(RawVc::task_cell(cell.task, cell.cell))),
OutputValue::Output(task) => Ok(Ok(RawVc::task_output(*task))),
OutputValue::Error(error) => Err(error.clone()),
};
if let Some(mut reader_task) = reader_task.take()
Expand Down Expand Up @@ -851,7 +851,7 @@ impl TurboTasksBackend {
add_cell_dependency(task_id, task, reader, reader_task, cell, tracking.key());
}
return Ok(Ok(TypedCellContent(
cell.type_id,
cell.type_id(),
CellContent(Some(content)),
)));
}
Expand All @@ -868,7 +868,7 @@ impl TurboTasksBackend {
let is_cancelled = matches!(in_progress, Some(InProgressState::Canceled));

// Check cell index range (cell might not exist at all)
let max_id = task.get_cell_type_max_index(&cell.type_id).copied();
let max_id = task.get_cell_type_max_index(&cell.type_id()).copied();
let Some(max_id) = max_id else {
let task_desc = task.get_task_description();
if tracking.should_track(true) {
Expand All @@ -878,7 +878,7 @@ impl TurboTasksBackend {
"Cell {cell:?} no longer exists in task {task_desc} (no cell of this type exists)",
);
};
if cell.index >= max_id {
if cell.index() >= max_id {
let task_desc = task.get_task_description();
if tracking.should_track(true) {
add_cell_dependency(task_id, task, reader, reader_task, cell, tracking.key());
Expand All @@ -904,8 +904,8 @@ impl TurboTasksBackend {

let _span = tracing::trace_span!(
"recomputation",
cell_type = get_value_type(cell.type_id).ty.global_name,
cell_index = cell.index
cell_type = get_value_type(cell.type_id()).ty.global_name,
cell_index = cell.index()
)
.entered();

Expand Down Expand Up @@ -1712,7 +1712,7 @@ impl TurboTasksBackend {
inner_cached(
self,
task_type,
cell_id.map(|c| c.type_id),
cell_id.map(|c| c.type_id()),
&mut FxHashSet::default(),
)
}
Expand Down Expand Up @@ -2328,8 +2328,8 @@ impl TurboTasksBackend {
let current_output = task.get_output();
#[cfg(feature = "verify_determinism")]
let no_output_set = current_output.is_none();
let new_output = match result {
Ok(RawVc::TaskOutput(output_task_id)) => {
let new_output = match result.map(RawVc::unpack) {
Ok(RawVcUnpacked::TaskOutput(output_task_id)) => {
if let Some(OutputValue::Output(current_task_id)) = current_output
&& *current_task_id == output_task_id
{
Expand All @@ -2338,7 +2338,7 @@ impl TurboTasksBackend {
Some(OutputValue::Output(output_task_id))
}
}
Ok(RawVc::TaskCell(output_task_id, cell)) => {
Ok(RawVcUnpacked::TaskCell(output_task_id, cell)) => {
if let Some(OutputValue::Cell(CellRef {
task: current_task_id,
cell: current_cell,
Expand All @@ -2354,7 +2354,7 @@ impl TurboTasksBackend {
}))
}
}
Ok(RawVc::LocalOutput(..)) => {
Ok(RawVcUnpacked::LocalOutput(..)) => {
panic!("Non-local tasks must not return a local Vc");
}
Err(err) => {
Expand Down Expand Up @@ -2771,8 +2771,8 @@ impl TurboTasksBackend {
.iter_cell_data()
.filter_map(|(cell, _)| {
cell_counters
.get(&cell.type_id)
.is_none_or(|start_index| cell.index >= *start_index)
.get(&cell.type_id())
.is_none_or(|start_index| cell.index() >= *start_index)
.then_some(*cell)
})
.collect();
Expand All @@ -2787,8 +2787,8 @@ impl TurboTasksBackend {
.iter_cell_data_hash()
.filter_map(|(cell, _)| {
cell_counters
.get(&cell.type_id)
.is_none_or(|start_index| cell.index >= *start_index)
.get(&cell.type_id())
.is_none_or(|start_index| cell.index() >= *start_index)
.then_some(*cell)
})
.collect();
Expand Down Expand Up @@ -3008,9 +3008,9 @@ impl TurboTasksBackend {
let mut ctx = self.execute_context(turbo_tasks);
let task = ctx.task(task_id, TaskDataCategory::Data);
if let Some(content) = task.get_cell_data(&cell).cloned() {
Ok(CellContent(Some(content)).into_typed(cell.type_id))
Ok(CellContent(Some(content)).into_typed(cell.type_id()))
} else {
Ok(CellContent(None).into_typed(cell.type_id))
Ok(CellContent(None).into_typed(cell.type_id()))
}
}

Expand Down Expand Up @@ -3043,7 +3043,7 @@ impl TurboTasksBackend {
for (collectible, count) in task.iter_aggregated_collectibles() {
if *count > 0 && collectible.collectible_type == collectible_type {
*collectibles
.entry(RawVc::TaskCell(
.entry(RawVc::task_cell(
collectible.cell.task,
collectible.cell.cell,
))
Expand All @@ -3053,7 +3053,7 @@ impl TurboTasksBackend {
for (&collectible, &count) in task.iter_collectibles() {
if collectible.collectible_type == collectible_type {
*collectibles
.entry(RawVc::TaskCell(
.entry(RawVc::task_cell(
collectible.cell.task,
collectible.cell.cell,
))
Expand Down Expand Up @@ -3086,7 +3086,7 @@ impl TurboTasksBackend {
) {
self.assert_valid_collectible(task_id, collectible);

let RawVc::TaskCell(collectible_task, cell) = collectible else {
let Some((collectible_task, cell)) = collectible.as_task_cell() else {
panic!("Collectibles need to be resolved");
};
let cell = CellRef {
Expand Down Expand Up @@ -3114,7 +3114,7 @@ impl TurboTasksBackend {
) {
self.assert_valid_collectible(task_id, collectible);

let RawVc::TaskCell(collectible_task, cell) = collectible else {
let Some((collectible_task, cell)) = collectible.as_task_cell() else {
panic!("Collectibles need to be resolved");
};
let cell = CellRef {
Expand Down Expand Up @@ -3438,7 +3438,7 @@ impl TurboTasksBackend {

fn assert_valid_collectible(&self, task_id: TaskId, collectible: RawVc) {
// these checks occur in a potentially hot codepath, but they're cheap
let RawVc::TaskCell(col_task_id, col_cell_id) = collectible else {
let Some((col_task_id, col_cell_id)) = collectible.as_task_cell() else {
// This should never happen: The collectible APIs use ResolvedVc
let task_info = if let Some(col_task_ty) = collectible
.try_get_task_id()
Expand Down
Loading
Loading