Skip to content

Turbopack: support config.turbopack and deprecate config.experimental.turbopack. #77850

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

Merged
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
4 changes: 1 addition & 3 deletions bench/heavy-npm-deps/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const nextConfig = {
ignoreBuildErrors: true,
},
experimental: {
turbo: {
unstablePersistentCaching: process.env.TURBO_CACHE === '1',
},
turbopackPersistentCaching: process.env.TURBO_CACHE === '1',
},
}

Expand Down
45 changes: 22 additions & 23 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use next_core::{
middleware::middleware_files,
mode::NextMode,
next_client::{get_client_chunking_context, get_client_compile_time_info},
next_config::{JsConfig, ModuleIdStrategy as ModuleIdStrategyConfig, NextConfig},
next_config::{JsConfig, ModuleIds as ModuleIdStrategyConfig, NextConfig},
next_server::{
get_server_chunking_context, get_server_chunking_context_with_client_assets,
get_server_compile_time_info, get_server_module_options_context,
Expand Down Expand Up @@ -773,7 +773,7 @@ impl Project {
node_build_environment().to_resolved().await?,
next_mode.runtime_type(),
)
.source_maps(if *self.next_config().turbo_source_maps().await? {
.source_maps(if *self.next_config().server_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
Expand Down Expand Up @@ -987,9 +987,9 @@ impl Project {
self.next_config().chunk_suffix_path(),
self.client_compile_time_info().environment(),
self.next_mode(),
self.module_id_strategy(),
self.module_ids(),
self.next_config().turbo_minify(self.next_mode()),
self.next_config().turbo_source_maps(),
self.next_config().client_source_maps(self.next_mode()),
self.no_mangling(),
)
}
Expand All @@ -1008,9 +1008,9 @@ impl Project {
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
self.server_compile_time_info().environment(),
self.module_id_strategy(),
self.module_ids(),
self.next_config().turbo_minify(self.next_mode()),
self.next_config().turbo_source_maps(),
self.next_config().server_source_maps(),
self.no_mangling(),
)
} else {
Expand All @@ -1020,9 +1020,9 @@ impl Project {
self.node_root(),
self.node_root_to_root_path(),
self.server_compile_time_info().environment(),
self.module_id_strategy(),
self.module_ids(),
self.next_config().turbo_minify(self.next_mode()),
self.next_config().turbo_source_maps(),
self.next_config().server_source_maps(),
self.no_mangling(),
)
}
Expand All @@ -1042,9 +1042,9 @@ impl Project {
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
self.edge_compile_time_info().environment(),
self.module_id_strategy(),
self.module_ids(),
self.next_config().turbo_minify(self.next_mode()),
self.next_config().turbo_source_maps(),
self.next_config().server_source_maps(),
self.no_mangling(),
)
} else {
Expand All @@ -1054,9 +1054,9 @@ impl Project {
self.node_root(),
self.node_root_to_root_path(),
self.edge_compile_time_info().environment(),
self.module_id_strategy(),
self.module_ids(),
self.next_config().turbo_minify(self.next_mode()),
self.next_config().turbo_source_maps(),
self.next_config().server_source_maps(),
self.no_mangling(),
)
}
Expand Down Expand Up @@ -1709,17 +1709,16 @@ impl Project {

/// Gets the module id strategy for the project.
#[turbo_tasks::function]
pub async fn module_id_strategy(self: Vc<Self>) -> Result<Vc<Box<dyn ModuleIdStrategy>>> {
let module_id_strategy = if let Some(module_id_strategy) =
&*self.next_config().module_id_strategy_config().await?
{
*module_id_strategy
} else {
match *self.next_mode().await? {
NextMode::Development => ModuleIdStrategyConfig::Named,
NextMode::Build => ModuleIdStrategyConfig::Deterministic,
}
};
pub async fn module_ids(self: Vc<Self>) -> Result<Vc<Box<dyn ModuleIdStrategy>>> {
let module_id_strategy =
if let Some(module_id_strategy) = &*self.next_config().module_ids().await? {
*module_id_strategy
} else {
match *self.next_mode().await? {
NextMode::Development => ModuleIdStrategyConfig::Named,
NextMode::Build => ModuleIdStrategyConfig::Deterministic,
}
};

match module_id_strategy {
ModuleIdStrategyConfig::Named => Ok(Vc::upcast(DevModuleIdStrategy::new())),
Expand Down
17 changes: 7 additions & 10 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,22 +329,19 @@ pub async fn get_client_module_options_context(
let enable_postcss_transform = Some(postcss_transform_options.resolved_cell());
let enable_foreign_postcss_transform = Some(postcss_foreign_transform_options.resolved_cell());

let source_maps = if *next_config.client_source_maps(mode).await? {
SourceMapsType::Full
} else {
SourceMapsType::None
};
let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
enable_typeof_window_inlining: Some(TypeofWindow::Object),
source_maps: if *next_config.turbo_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
},
source_maps,
..Default::default()
},
css: CssOptionsContext {
source_maps: if *next_config.turbo_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
},
source_maps,
..Default::default()
},
preset_env_versions: Some(env),
Expand Down
80 changes: 31 additions & 49 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub struct NextConfig {
pub cross_origin: Option<CrossOriginConfig>,
pub dev_indicators: Option<DevIndicatorsConfig>,
pub output: Option<OutputType>,
pub turbopack: Option<TurbopackConfig>,
production_browser_source_maps: bool,

/// Enables the bundling of node_modules packages (externals) for pages
/// server-side bundles.
Expand Down Expand Up @@ -129,7 +131,6 @@ pub struct NextConfig {
http_agent_options: HttpAgentConfig,
on_demand_entries: OnDemandEntriesConfig,
powered_by_header: bool,
production_browser_source_maps: bool,
public_runtime_config: FxIndexMap<String, serde_json::Value>,
server_runtime_config: FxIndexMap<String, serde_json::Value>,
static_page_generation_timeout: f64,
Expand Down Expand Up @@ -536,17 +537,13 @@ pub enum RemotePatternProtocal {
OperationValue,
)]
#[serde(rename_all = "camelCase")]
pub struct ExperimentalTurboConfig {
pub struct TurbopackConfig {
/// This option has been replaced by `rules`.
pub loaders: Option<JsonValue>,
pub rules: Option<FxIndexMap<RcStr, RuleConfigItemOrShortcut>>,
pub resolve_alias: Option<FxIndexMap<RcStr, JsonValue>>,
pub resolve_extensions: Option<Vec<RcStr>>,
pub tree_shaking: Option<bool>,
pub module_id_strategy: Option<ModuleIdStrategy>,
pub minify: Option<bool>,
pub source_maps: Option<bool>,
pub unstable_persistent_caching: Option<bool>,
pub module_ids: Option<ModuleIds>,
}

#[derive(
Expand Down Expand Up @@ -590,13 +587,13 @@ pub enum LoaderItem {
#[turbo_tasks::value(operation)]
#[derive(Copy, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub enum ModuleIdStrategy {
pub enum ModuleIds {
Named,
Deterministic,
}

#[turbo_tasks::value(transparent)]
pub struct OptionModuleIdStrategy(pub Option<ModuleIdStrategy>);
pub struct OptionModuleIds(pub Option<ModuleIds>);

#[derive(
Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, OperationValue,
Expand Down Expand Up @@ -668,7 +665,6 @@ pub struct ExperimentalConfig {
mdx_rs: Option<MdxRsOptions>,
strict_next_head: Option<bool>,
swc_plugins: Option<Vec<(RcStr, serde_json::Value)>>,
turbo: Option<ExperimentalTurboConfig>,
external_middleware_rewrites_resolve: Option<bool>,
scroll_restoration: Option<bool>,
manual_client_base_path: Option<bool>,
Expand Down Expand Up @@ -748,6 +744,11 @@ pub struct ExperimentalConfig {
/// (doesn't apply to Turbopack).
webpack_build_worker: Option<bool>,
worker_threads: Option<bool>,

turbopack_minify: Option<bool>,
turbopack_persistent_caching: Option<bool>,
turbopack_source_maps: Option<bool>,
turbopack_tree_shaking: Option<bool>,
}

#[derive(
Expand Down Expand Up @@ -1144,12 +1145,7 @@ impl NextConfig {

#[turbo_tasks::function]
pub fn webpack_rules(&self, active_conditions: Vec<RcStr>) -> Vc<OptionWebpackRules> {
let Some(turbo_rules) = self
.experimental
.turbo
.as_ref()
.and_then(|t| t.rules.as_ref())
else {
let Some(turbo_rules) = self.turbopack.as_ref().and_then(|t| t.rules.as_ref()) else {
return Vc::cell(None);
};
if turbo_rules.is_empty() {
Expand Down Expand Up @@ -1234,18 +1230,15 @@ impl NextConfig {
pub fn persistent_caching_enabled(&self) -> Result<Vc<bool>> {
Ok(Vc::cell(
self.experimental
.turbo
.as_ref()
.and_then(|t| t.unstable_persistent_caching)
.turbopack_persistent_caching
.unwrap_or_default(),
))
}

#[turbo_tasks::function]
pub fn resolve_alias_options(&self) -> Result<Vc<ResolveAliasMap>> {
let Some(resolve_alias) = self
.experimental
.turbo
.turbopack
.as_ref()
.and_then(|t| t.resolve_alias.as_ref())
else {
Expand All @@ -1258,8 +1251,7 @@ impl NextConfig {
#[turbo_tasks::function]
pub fn resolve_extension(&self) -> Vc<ResolveExtensions> {
let Some(resolve_extensions) = self
.experimental
.turbo
.turbopack
.as_ref()
.and_then(|t| t.resolve_extensions.as_ref())
else {
Expand Down Expand Up @@ -1476,13 +1468,7 @@ impl NextConfig {
&self,
_is_development: bool,
) -> Vc<OptionTreeShaking> {
let tree_shaking = self
.experimental
.turbo
.as_ref()
.and_then(|v| v.tree_shaking);

OptionTreeShaking(match tree_shaking {
OptionTreeShaking(match self.experimental.turbopack_tree_shaking {
Some(false) => Some(TreeShakingMode::ReexportsOnly),
Some(true) => Some(TreeShakingMode::ModuleFragments),
None => Some(TreeShakingMode::ReexportsOnly),
Expand All @@ -1492,13 +1478,7 @@ impl NextConfig {

#[turbo_tasks::function]
pub fn tree_shaking_mode_for_user_code(&self, _is_development: bool) -> Vc<OptionTreeShaking> {
let tree_shaking = self
.experimental
.turbo
.as_ref()
.and_then(|v| v.tree_shaking);

OptionTreeShaking(match tree_shaking {
OptionTreeShaking(match self.experimental.turbopack_tree_shaking {
Some(false) => Some(TreeShakingMode::ReexportsOnly),
Some(true) => Some(TreeShakingMode::ModuleFragments),
None => Some(TreeShakingMode::ReexportsOnly),
Expand All @@ -1507,31 +1487,33 @@ impl NextConfig {
}

#[turbo_tasks::function]
pub fn module_id_strategy_config(&self) -> Vc<OptionModuleIdStrategy> {
let Some(module_id_strategy) = self
.experimental
.turbo
.as_ref()
.and_then(|t| t.module_id_strategy)
else {
pub fn module_ids(&self) -> Vc<OptionModuleIds> {
let Some(module_ids) = self.turbopack.as_ref().and_then(|t| t.module_ids) else {
return Vc::cell(None);
};
Vc::cell(Some(module_id_strategy))
Vc::cell(Some(module_ids))
}

#[turbo_tasks::function]
pub async fn turbo_minify(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> {
let minify = self.experimental.turbo.as_ref().and_then(|t| t.minify);

let minify = self.experimental.turbopack_minify;
Ok(Vc::cell(
minify.unwrap_or(matches!(*mode.await?, NextMode::Build)),
))
}

#[turbo_tasks::function]
pub async fn turbo_source_maps(&self) -> Result<Vc<bool>> {
let source_maps = self.experimental.turbo.as_ref().and_then(|t| t.source_maps);
pub async fn client_source_maps(&self, _mode: Vc<NextMode>) -> Result<Vc<bool>> {
// Temporarily always enable client source maps as tests regress.
// TODO: Respect both `self.experimental.turbopack_source_maps` and
// `self.production_browser_source_maps`
let source_maps = self.experimental.turbopack_source_maps;
Ok(Vc::cell(source_maps.unwrap_or(true)))
}

#[turbo_tasks::function]
pub async fn server_source_maps(&self) -> Result<Vc<bool>> {
let source_maps = self.experimental.turbopack_source_maps;
Ok(Vc::cell(source_maps.unwrap_or(true)))
}

Expand Down
17 changes: 7 additions & 10 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,25 +536,22 @@ pub async fn get_server_module_options_context(
get_styled_components_transform_rule(next_config).await?;
let styled_jsx_transform_rule = get_styled_jsx_transform_rule(next_config, versions).await?;

let source_maps = if *next_config.server_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
};
let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
enable_typeof_window_inlining: Some(TypeofWindow::Undefined),
import_externals: *next_config.import_externals().await?,
ignore_dynamic_requests: true,
source_maps: if *next_config.turbo_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
},
source_maps,
..Default::default()
},
execution_context: Some(execution_context),
css: CssOptionsContext {
source_maps: if *next_config.turbo_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
},
source_maps,
..Default::default()
},
tree_shaking_mode: tree_shaking_mode_for_user_code,
Expand Down
Loading
Loading