Unverified Commit 4f5531b1 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Turbopack: support `config.turbopack` and deprecate `config.experimental.turbopack`. (#77850)

Closes PACK-4263

This:
- Adds support for using `config.turbopack` for Turbopack. The accepted value for this option is identical to the previous experimental one without the deprecated field `loaders`.
- Deprecates `config.experimental.turbo`. Warns on its use, but will continue to accept it, merging it with `config.turbopack` while preferring fields on `config.turbopack`.
- also see https://github.com/vercel/next.js/pull/77850#pullrequestreview-2749738760

Test Plan:
- [x] Convert use of `config.experimental.turbo` in tests to `config.turbopack`
- [x] Automated test for accepting the experimental option for compatibility, as well as the config merging
parent 9d3b5633
...@@ -7,9 +7,7 @@ const nextConfig = { ...@@ -7,9 +7,7 @@ const nextConfig = {
ignoreBuildErrors: true, ignoreBuildErrors: true,
}, },
experimental: { experimental: {
turbo: { turbopackPersistentCaching: process.env.TURBO_CACHE === '1',
unstablePersistentCaching: process.env.TURBO_CACHE === '1',
},
}, },
} }
......
...@@ -11,7 +11,7 @@ use next_core::{ ...@@ -11,7 +11,7 @@ use next_core::{
middleware::middleware_files, middleware::middleware_files,
mode::NextMode, mode::NextMode,
next_client::{get_client_chunking_context, get_client_compile_time_info}, 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::{ next_server::{
get_server_chunking_context, get_server_chunking_context_with_client_assets, get_server_chunking_context, get_server_chunking_context_with_client_assets,
get_server_compile_time_info, get_server_module_options_context, get_server_compile_time_info, get_server_module_options_context,
...@@ -773,7 +773,7 @@ impl Project { ...@@ -773,7 +773,7 @@ impl Project {
node_build_environment().to_resolved().await?, node_build_environment().to_resolved().await?,
next_mode.runtime_type(), 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 SourceMapsType::Full
} else { } else {
SourceMapsType::None SourceMapsType::None
...@@ -987,9 +987,9 @@ impl Project { ...@@ -987,9 +987,9 @@ impl Project {
self.next_config().chunk_suffix_path(), self.next_config().chunk_suffix_path(),
self.client_compile_time_info().environment(), self.client_compile_time_info().environment(),
self.next_mode(), self.next_mode(),
self.module_id_strategy(), self.module_ids(),
self.next_config().turbo_minify(self.next_mode()), 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(), self.no_mangling(),
) )
} }
...@@ -1008,9 +1008,9 @@ impl Project { ...@@ -1008,9 +1008,9 @@ impl Project {
self.client_relative_path(), self.client_relative_path(),
self.next_config().computed_asset_prefix(), self.next_config().computed_asset_prefix(),
self.server_compile_time_info().environment(), 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_minify(self.next_mode()),
self.next_config().turbo_source_maps(), self.next_config().server_source_maps(),
self.no_mangling(), self.no_mangling(),
) )
} else { } else {
...@@ -1020,9 +1020,9 @@ impl Project { ...@@ -1020,9 +1020,9 @@ impl Project {
self.node_root(), self.node_root(),
self.node_root_to_root_path(), self.node_root_to_root_path(),
self.server_compile_time_info().environment(), 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_minify(self.next_mode()),
self.next_config().turbo_source_maps(), self.next_config().server_source_maps(),
self.no_mangling(), self.no_mangling(),
) )
} }
...@@ -1042,9 +1042,9 @@ impl Project { ...@@ -1042,9 +1042,9 @@ impl Project {
self.client_relative_path(), self.client_relative_path(),
self.next_config().computed_asset_prefix(), self.next_config().computed_asset_prefix(),
self.edge_compile_time_info().environment(), 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_minify(self.next_mode()),
self.next_config().turbo_source_maps(), self.next_config().server_source_maps(),
self.no_mangling(), self.no_mangling(),
) )
} else { } else {
...@@ -1054,9 +1054,9 @@ impl Project { ...@@ -1054,9 +1054,9 @@ impl Project {
self.node_root(), self.node_root(),
self.node_root_to_root_path(), self.node_root_to_root_path(),
self.edge_compile_time_info().environment(), 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_minify(self.next_mode()),
self.next_config().turbo_source_maps(), self.next_config().server_source_maps(),
self.no_mangling(), self.no_mangling(),
) )
} }
...@@ -1709,17 +1709,16 @@ impl Project { ...@@ -1709,17 +1709,16 @@ impl Project {
/// Gets the module id strategy for the project. /// Gets the module id strategy for the project.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn module_id_strategy(self: Vc<Self>) -> Result<Vc<Box<dyn ModuleIdStrategy>>> { pub async fn module_ids(self: Vc<Self>) -> Result<Vc<Box<dyn ModuleIdStrategy>>> {
let module_id_strategy = if let Some(module_id_strategy) = let module_id_strategy =
&*self.next_config().module_id_strategy_config().await? if let Some(module_id_strategy) = &*self.next_config().module_ids().await? {
{ *module_id_strategy
*module_id_strategy } else {
} else { match *self.next_mode().await? {
match *self.next_mode().await? { NextMode::Development => ModuleIdStrategyConfig::Named,
NextMode::Development => ModuleIdStrategyConfig::Named, NextMode::Build => ModuleIdStrategyConfig::Deterministic,
NextMode::Build => ModuleIdStrategyConfig::Deterministic, }
} };
};
match module_id_strategy { match module_id_strategy {
ModuleIdStrategyConfig::Named => Ok(Vc::upcast(DevModuleIdStrategy::new())), ModuleIdStrategyConfig::Named => Ok(Vc::upcast(DevModuleIdStrategy::new())),
......
...@@ -329,22 +329,19 @@ pub async fn get_client_module_options_context( ...@@ -329,22 +329,19 @@ pub async fn get_client_module_options_context(
let enable_postcss_transform = Some(postcss_transform_options.resolved_cell()); let enable_postcss_transform = Some(postcss_transform_options.resolved_cell());
let enable_foreign_postcss_transform = Some(postcss_foreign_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 { let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext { ecmascript: EcmascriptOptionsContext {
enable_typeof_window_inlining: Some(TypeofWindow::Object), enable_typeof_window_inlining: Some(TypeofWindow::Object),
source_maps: if *next_config.turbo_source_maps().await? { source_maps,
SourceMapsType::Full
} else {
SourceMapsType::None
},
..Default::default() ..Default::default()
}, },
css: CssOptionsContext { css: CssOptionsContext {
source_maps: if *next_config.turbo_source_maps().await? { source_maps,
SourceMapsType::Full
} else {
SourceMapsType::None
},
..Default::default() ..Default::default()
}, },
preset_env_versions: Some(env), preset_env_versions: Some(env),
......
...@@ -94,6 +94,8 @@ pub struct NextConfig { ...@@ -94,6 +94,8 @@ pub struct NextConfig {
pub cross_origin: Option<CrossOriginConfig>, pub cross_origin: Option<CrossOriginConfig>,
pub dev_indicators: Option<DevIndicatorsConfig>, pub dev_indicators: Option<DevIndicatorsConfig>,
pub output: Option<OutputType>, pub output: Option<OutputType>,
pub turbopack: Option<TurbopackConfig>,
production_browser_source_maps: bool,
/// Enables the bundling of node_modules packages (externals) for pages /// Enables the bundling of node_modules packages (externals) for pages
/// server-side bundles. /// server-side bundles.
...@@ -129,7 +131,6 @@ pub struct NextConfig { ...@@ -129,7 +131,6 @@ pub struct NextConfig {
http_agent_options: HttpAgentConfig, http_agent_options: HttpAgentConfig,
on_demand_entries: OnDemandEntriesConfig, on_demand_entries: OnDemandEntriesConfig,
powered_by_header: bool, powered_by_header: bool,
production_browser_source_maps: bool,
public_runtime_config: FxIndexMap<String, serde_json::Value>, public_runtime_config: FxIndexMap<String, serde_json::Value>,
server_runtime_config: FxIndexMap<String, serde_json::Value>, server_runtime_config: FxIndexMap<String, serde_json::Value>,
static_page_generation_timeout: f64, static_page_generation_timeout: f64,
...@@ -536,17 +537,13 @@ pub enum RemotePatternProtocal { ...@@ -536,17 +537,13 @@ pub enum RemotePatternProtocal {
OperationValue, OperationValue,
)] )]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ExperimentalTurboConfig { pub struct TurbopackConfig {
/// This option has been replaced by `rules`. /// This option has been replaced by `rules`.
pub loaders: Option<JsonValue>, pub loaders: Option<JsonValue>,
pub rules: Option<FxIndexMap<RcStr, RuleConfigItemOrShortcut>>, pub rules: Option<FxIndexMap<RcStr, RuleConfigItemOrShortcut>>,
pub resolve_alias: Option<FxIndexMap<RcStr, JsonValue>>, pub resolve_alias: Option<FxIndexMap<RcStr, JsonValue>>,
pub resolve_extensions: Option<Vec<RcStr>>, pub resolve_extensions: Option<Vec<RcStr>>,
pub tree_shaking: Option<bool>, pub module_ids: Option<ModuleIds>,
pub module_id_strategy: Option<ModuleIdStrategy>,
pub minify: Option<bool>,
pub source_maps: Option<bool>,
pub unstable_persistent_caching: Option<bool>,
} }
#[derive( #[derive(
...@@ -590,13 +587,13 @@ pub enum LoaderItem { ...@@ -590,13 +587,13 @@ pub enum LoaderItem {
#[turbo_tasks::value(operation)] #[turbo_tasks::value(operation)]
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum ModuleIdStrategy { pub enum ModuleIds {
Named, Named,
Deterministic, Deterministic,
} }
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct OptionModuleIdStrategy(pub Option<ModuleIdStrategy>); pub struct OptionModuleIds(pub Option<ModuleIds>);
#[derive( #[derive(
Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, OperationValue, Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, OperationValue,
...@@ -668,7 +665,6 @@ pub struct ExperimentalConfig { ...@@ -668,7 +665,6 @@ pub struct ExperimentalConfig {
mdx_rs: Option<MdxRsOptions>, mdx_rs: Option<MdxRsOptions>,
strict_next_head: Option<bool>, strict_next_head: Option<bool>,
swc_plugins: Option<Vec<(RcStr, serde_json::Value)>>, swc_plugins: Option<Vec<(RcStr, serde_json::Value)>>,
turbo: Option<ExperimentalTurboConfig>,
external_middleware_rewrites_resolve: Option<bool>, external_middleware_rewrites_resolve: Option<bool>,
scroll_restoration: Option<bool>, scroll_restoration: Option<bool>,
manual_client_base_path: Option<bool>, manual_client_base_path: Option<bool>,
...@@ -748,6 +744,11 @@ pub struct ExperimentalConfig { ...@@ -748,6 +744,11 @@ pub struct ExperimentalConfig {
/// (doesn't apply to Turbopack). /// (doesn't apply to Turbopack).
webpack_build_worker: Option<bool>, webpack_build_worker: Option<bool>,
worker_threads: 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( #[derive(
...@@ -1144,12 +1145,7 @@ impl NextConfig { ...@@ -1144,12 +1145,7 @@ impl NextConfig {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn webpack_rules(&self, active_conditions: Vec<RcStr>) -> Vc<OptionWebpackRules> { pub fn webpack_rules(&self, active_conditions: Vec<RcStr>) -> Vc<OptionWebpackRules> {
let Some(turbo_rules) = self let Some(turbo_rules) = self.turbopack.as_ref().and_then(|t| t.rules.as_ref()) else {
.experimental
.turbo
.as_ref()
.and_then(|t| t.rules.as_ref())
else {
return Vc::cell(None); return Vc::cell(None);
}; };
if turbo_rules.is_empty() { if turbo_rules.is_empty() {
...@@ -1234,9 +1230,7 @@ impl NextConfig { ...@@ -1234,9 +1230,7 @@ impl NextConfig {
pub fn persistent_caching_enabled(&self) -> Result<Vc<bool>> { pub fn persistent_caching_enabled(&self) -> Result<Vc<bool>> {
Ok(Vc::cell( Ok(Vc::cell(
self.experimental self.experimental
.turbo .turbopack_persistent_caching
.as_ref()
.and_then(|t| t.unstable_persistent_caching)
.unwrap_or_default(), .unwrap_or_default(),
)) ))
} }
...@@ -1244,8 +1238,7 @@ impl NextConfig { ...@@ -1244,8 +1238,7 @@ impl NextConfig {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn resolve_alias_options(&self) -> Result<Vc<ResolveAliasMap>> { pub fn resolve_alias_options(&self) -> Result<Vc<ResolveAliasMap>> {
let Some(resolve_alias) = self let Some(resolve_alias) = self
.experimental .turbopack
.turbo
.as_ref() .as_ref()
.and_then(|t| t.resolve_alias.as_ref()) .and_then(|t| t.resolve_alias.as_ref())
else { else {
...@@ -1258,8 +1251,7 @@ impl NextConfig { ...@@ -1258,8 +1251,7 @@ impl NextConfig {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn resolve_extension(&self) -> Vc<ResolveExtensions> { pub fn resolve_extension(&self) -> Vc<ResolveExtensions> {
let Some(resolve_extensions) = self let Some(resolve_extensions) = self
.experimental .turbopack
.turbo
.as_ref() .as_ref()
.and_then(|t| t.resolve_extensions.as_ref()) .and_then(|t| t.resolve_extensions.as_ref())
else { else {
...@@ -1476,13 +1468,7 @@ impl NextConfig { ...@@ -1476,13 +1468,7 @@ impl NextConfig {
&self, &self,
_is_development: bool, _is_development: bool,
) -> Vc<OptionTreeShaking> { ) -> Vc<OptionTreeShaking> {
let tree_shaking = self OptionTreeShaking(match self.experimental.turbopack_tree_shaking {
.experimental
.turbo
.as_ref()
.and_then(|v| v.tree_shaking);
OptionTreeShaking(match tree_shaking {
Some(false) => Some(TreeShakingMode::ReexportsOnly), Some(false) => Some(TreeShakingMode::ReexportsOnly),
Some(true) => Some(TreeShakingMode::ModuleFragments), Some(true) => Some(TreeShakingMode::ModuleFragments),
None => Some(TreeShakingMode::ReexportsOnly), None => Some(TreeShakingMode::ReexportsOnly),
...@@ -1492,13 +1478,7 @@ impl NextConfig { ...@@ -1492,13 +1478,7 @@ impl NextConfig {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn tree_shaking_mode_for_user_code(&self, _is_development: bool) -> Vc<OptionTreeShaking> { pub fn tree_shaking_mode_for_user_code(&self, _is_development: bool) -> Vc<OptionTreeShaking> {
let tree_shaking = self OptionTreeShaking(match self.experimental.turbopack_tree_shaking {
.experimental
.turbo
.as_ref()
.and_then(|v| v.tree_shaking);
OptionTreeShaking(match tree_shaking {
Some(false) => Some(TreeShakingMode::ReexportsOnly), Some(false) => Some(TreeShakingMode::ReexportsOnly),
Some(true) => Some(TreeShakingMode::ModuleFragments), Some(true) => Some(TreeShakingMode::ModuleFragments),
None => Some(TreeShakingMode::ReexportsOnly), None => Some(TreeShakingMode::ReexportsOnly),
...@@ -1507,31 +1487,33 @@ impl NextConfig { ...@@ -1507,31 +1487,33 @@ impl NextConfig {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn module_id_strategy_config(&self) -> Vc<OptionModuleIdStrategy> { pub fn module_ids(&self) -> Vc<OptionModuleIds> {
let Some(module_id_strategy) = self let Some(module_ids) = self.turbopack.as_ref().and_then(|t| t.module_ids) else {
.experimental
.turbo
.as_ref()
.and_then(|t| t.module_id_strategy)
else {
return Vc::cell(None); return Vc::cell(None);
}; };
Vc::cell(Some(module_id_strategy)) Vc::cell(Some(module_ids))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn turbo_minify(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> { 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( Ok(Vc::cell(
minify.unwrap_or(matches!(*mode.await?, NextMode::Build)), minify.unwrap_or(matches!(*mode.await?, NextMode::Build)),
)) ))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn turbo_source_maps(&self) -> Result<Vc<bool>> { pub async fn client_source_maps(&self, _mode: Vc<NextMode>) -> Result<Vc<bool>> {
let source_maps = self.experimental.turbo.as_ref().and_then(|t| t.source_maps); // 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))) Ok(Vc::cell(source_maps.unwrap_or(true)))
} }
......
...@@ -536,25 +536,22 @@ pub async fn get_server_module_options_context( ...@@ -536,25 +536,22 @@ pub async fn get_server_module_options_context(
get_styled_components_transform_rule(next_config).await?; get_styled_components_transform_rule(next_config).await?;
let styled_jsx_transform_rule = get_styled_jsx_transform_rule(next_config, versions).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 { let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext { ecmascript: EcmascriptOptionsContext {
enable_typeof_window_inlining: Some(TypeofWindow::Undefined), enable_typeof_window_inlining: Some(TypeofWindow::Undefined),
import_externals: *next_config.import_externals().await?, import_externals: *next_config.import_externals().await?,
ignore_dynamic_requests: true, ignore_dynamic_requests: true,
source_maps: if *next_config.turbo_source_maps().await? { source_maps,
SourceMapsType::Full
} else {
SourceMapsType::None
},
..Default::default() ..Default::default()
}, },
execution_context: Some(execution_context), execution_context: Some(execution_context),
css: CssOptionsContext { css: CssOptionsContext {
source_maps: if *next_config.turbo_source_maps().await? { source_maps,
SourceMapsType::Full
} else {
SourceMapsType::None
},
..Default::default() ..Default::default()
}, },
tree_shaking_mode: tree_shaking_mode_for_user_code, tree_shaking_mode: tree_shaking_mode_for_user_code,
......
...@@ -50,7 +50,7 @@ The following options are available for the `turbo` configuration: ...@@ -50,7 +50,7 @@ The following options are available for the `turbo` configuration:
| `rules` | List of supported webpack loaders to apply when running with Turbopack. | | `rules` | List of supported webpack loaders to apply when running with Turbopack. |
| `resolveAlias` | Map aliased imports to modules to load in their place. | | `resolveAlias` | Map aliased imports to modules to load in their place. |
| `resolveExtensions` | List of extensions to resolve when importing files. | | `resolveExtensions` | List of extensions to resolve when importing files. |
| `moduleIdStrategy` | Assign module IDs | | `moduleIds` | Assign module IDs |
| `treeShaking` | Enable tree shaking for the turbopack dev server and build. | | `treeShaking` | Enable tree shaking for the turbopack dev server and build. |
| `memoryLimit` | A target memory limit for turbo, in bytes. | | `memoryLimit` | A target memory limit for turbo, in bytes. |
...@@ -157,14 +157,12 @@ Turbopack currently supports two strategies for assigning module IDs: ...@@ -157,14 +157,12 @@ Turbopack currently supports two strategies for assigning module IDs:
If not set, Turbopack will use `'named'` for development builds and `'deterministic'` for production builds. If not set, Turbopack will use `'named'` for development builds and `'deterministic'` for production builds.
To configure the module IDs strategy, use the `moduleIdStrategy` field in `next.config.js`: To configure the module IDs strategy, use the `moduleIds` field in `next.config.js`:
```js filename="next.config.js" ```js filename="next.config.js"
module.exports = { module.exports = {
experimental: { turbo: {
turbo: { moduleIds: 'deterministic',
moduleIdStrategy: 'deterministic',
},
}, },
} }
``` ```
......
...@@ -130,7 +130,7 @@ Turbopack can be configured via `next.config.js` (or `next.config.ts`) under the ...@@ -130,7 +130,7 @@ Turbopack can be configured via `next.config.js` (or `next.config.ts`) under the
Create manual aliases (like `resolve.alias` in webpack). Create manual aliases (like `resolve.alias` in webpack).
- **`resolveExtensions`** - **`resolveExtensions`**
Change or extend file extensions for module resolution. Change or extend file extensions for module resolution.
- **`moduleIdStrategy`** - **`moduleIds`**
Set how module IDs are generated (`'named'` vs `'deterministic'`). Set how module IDs are generated (`'named'` vs `'deterministic'`).
- **`treeShaking`** - **`treeShaking`**
Enable or disable tree shaking in dev and future production builds. Enable or disable tree shaking in dev and future production builds.
......
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
module.exports = { module.exports = {
experimental: { turbopack: {
turbo: { resolveAlias: {
resolveAlias: { "react-native": "react-native-web",
"react-native": "react-native-web",
},
resolveExtensions: [
".web.js",
".web.jsx",
".web.ts",
".web.tsx",
".mdx",
".tsx",
".ts",
".jsx",
".js",
".mjs",
".json",
],
}, },
resolveExtensions: [
".web.js",
".web.jsx",
".web.ts",
".web.tsx",
".mdx",
".tsx",
".ts",
".jsx",
".js",
".mjs",
".json",
],
}, },
webpack: (config) => { webpack: (config) => {
config.resolve.alias = { config.resolve.alias = {
......
module.exports = { module.exports = {
experimental: { turbopack: {
turbo: { rules: {
rules: { "*.react.svg": {
"*.react.svg": { loaders: ["@svgr/webpack"],
loaders: ["@svgr/webpack"], as: "*.js",
as: "*.js", },
}, "*.styl": {
"*.styl": { loaders: ["stylus-loader"],
loaders: ["stylus-loader"], as: "*.css",
as: "*.css",
},
}, },
}, },
}, },
......
...@@ -3756,9 +3756,14 @@ function warnAboutTurbopackBuilds(config?: NextConfigComplete) { ...@@ -3756,9 +3756,14 @@ function warnAboutTurbopackBuilds(config?: NextConfigComplete) {
`We don't recommend deploying mission-critical applications to production.` `We don't recommend deploying mission-critical applications to production.`
) )
warningStr += warningStr +=
'\n\n- It is expected that your bundle size might be different from `next build` with webpack. This will be improved as we work towards stability.' '\n\n- ' +
bold(
'Turbopack currently always builds production sourcemaps for the browser. This will include project sourcecode if deployed to production.'
)
warningStr +=
'\n- It is expected that your bundle size might be different from `next build` with webpack. This will be improved as we work towards stability.'
if (!config?.experimental.turbo?.unstablePersistentCaching) { if (!config?.experimental.turbopackPersistentCaching) {
warningStr += warningStr +=
'\n- This build is without disk caching; subsequent builds will become faster when disk caching becomes available.' '\n- This build is without disk caching; subsequent builds will become faster when disk caching becomes available.'
} }
......
...@@ -10,10 +10,10 @@ import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile' ...@@ -10,10 +10,10 @@ import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile'
import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc' import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc'
import type { import type {
NextConfigComplete, NextConfigComplete,
TurboLoaderItem, TurbopackLoaderItem,
TurboRuleConfigItem, TurbopackRuleConfigItem,
TurboRuleConfigItemOptions, TurbopackRuleConfigItemOptions,
TurboRuleConfigItemOrShortcut, TurbopackRuleConfigItemOrShortcut,
} from '../../server/config-shared' } from '../../server/config-shared'
import { isDeepStrictEqual } from 'util' import { isDeepStrictEqual } from 'util'
import { import {
...@@ -786,24 +786,24 @@ function bindingToApi( ...@@ -786,24 +786,24 @@ function bindingToApi(
if (reactCompilerOptions) { if (reactCompilerOptions) {
const ruleKeys = ['*.ts', '*.js', '*.jsx', '*.tsx'] const ruleKeys = ['*.ts', '*.js', '*.jsx', '*.tsx']
if ( if (
Object.keys(nextConfig?.experimental?.turbo?.rules ?? []).some((key) => Object.keys(nextConfig?.turbopack?.rules ?? []).some((key) =>
ruleKeys.includes(key) ruleKeys.includes(key)
) )
) { ) {
Log.warn( Log.warn(
`The React Compiler cannot be enabled automatically because 'experimental.turbo' contains a rule for '*.ts', '*.js', '*.jsx', and '*.tsx'. Remove this rule, or add 'babel-loader' and 'babel-plugin-react-compiler' to the Turbopack configuration manually.` `The React Compiler cannot be enabled automatically because 'turbopack.rules' contains a rule for '*.ts', '*.js', '*.jsx', and '*.tsx'. Remove this rule, or add 'babel-loader' and 'babel-plugin-react-compiler' to the Turbopack configuration manually.`
) )
} else { } else {
if (!nextConfig.experimental.turbo) { if (!nextConfig.turbopack) {
nextConfig.experimental.turbo = {} nextConfig.turbopack = {}
} }
if (!nextConfig.experimental.turbo.rules) { if (!nextConfig.turbopack.rules) {
nextConfig.experimental.turbo.rules = {} nextConfig.turbopack.rules = {}
} }
for (const key of ['*.ts', '*.js', '*.jsx', '*.tsx']) { for (const key of ['*.ts', '*.js', '*.jsx', '*.tsx']) {
nextConfig.experimental.turbo.rules[key] = { nextConfig.turbopack.rules[key] = {
browser: { browser: {
foreign: false, foreign: false,
loaders: [ loaders: [
...@@ -840,7 +840,7 @@ function bindingToApi( ...@@ -840,7 +840,7 @@ function bindingToApi(
if (nextConfigSerializable.experimental?.turbo?.rules) { if (nextConfigSerializable.experimental?.turbo?.rules) {
ensureLoadersHaveSerializableOptions( ensureLoadersHaveSerializableOptions(
nextConfigSerializable.experimental.turbo?.rules nextConfigSerializable.turbopack?.rules
) )
} }
...@@ -878,7 +878,7 @@ function bindingToApi( ...@@ -878,7 +878,7 @@ function bindingToApi(
} }
function ensureLoadersHaveSerializableOptions( function ensureLoadersHaveSerializableOptions(
turbopackRules: Record<string, TurboRuleConfigItemOrShortcut> turbopackRules: Record<string, TurbopackRuleConfigItemOrShortcut>
) { ) {
for (const [glob, rule] of Object.entries(turbopackRules)) { for (const [glob, rule] of Object.entries(turbopackRules)) {
if (Array.isArray(rule)) { if (Array.isArray(rule)) {
...@@ -888,10 +888,10 @@ function bindingToApi( ...@@ -888,10 +888,10 @@ function bindingToApi(
} }
} }
function checkConfigItem(rule: TurboRuleConfigItem, glob: string) { function checkConfigItem(rule: TurbopackRuleConfigItem, glob: string) {
if (!rule) return if (!rule) return
if ('loaders' in rule) { if ('loaders' in rule) {
checkLoaderItems((rule as TurboRuleConfigItemOptions).loaders, glob) checkLoaderItems((rule as TurbopackRuleConfigItemOptions).loaders, glob)
} else { } else {
for (const key in rule) { for (const key in rule) {
const inner = rule[key] const inner = rule[key]
...@@ -902,7 +902,10 @@ function bindingToApi( ...@@ -902,7 +902,10 @@ function bindingToApi(
} }
} }
function checkLoaderItems(loaderItems: TurboLoaderItem[], glob: string) { function checkLoaderItems(
loaderItems: TurbopackLoaderItem[],
glob: string
) {
for (const loaderItem of loaderItems) { for (const loaderItem of loaderItems) {
if ( if (
typeof loaderItem !== 'string' && typeof loaderItem !== 'string' &&
......
...@@ -54,8 +54,7 @@ export async function turbopackBuild(): Promise<{ ...@@ -54,8 +54,7 @@ export async function turbopackBuild(): Promise<{
const project = await bindings.turbo.createProject( const project = await bindings.turbo.createProject(
{ {
projectPath: dir, projectPath: dir,
rootPath: rootPath: config.turbopack?.root || config.outputFileTracingRoot || dir,
config.experimental?.turbo?.root || config.outputFileTracingRoot || dir,
distDir, distDir,
nextConfig: config, nextConfig: config,
jsConfig: await getTurbopackJsConfig(dir, config), jsConfig: await getTurbopackJsConfig(dir, config),
...@@ -83,7 +82,7 @@ export async function turbopackBuild(): Promise<{ ...@@ -83,7 +82,7 @@ export async function turbopackBuild(): Promise<{
}, },
{ {
persistentCaching, persistentCaching,
memoryLimit: config.experimental.turbo?.memoryLimit, memoryLimit: config.experimental?.turbopackMemoryLimit,
dependencyTracking: persistentCaching, dependencyTracking: persistentCaching,
} }
) )
......
...@@ -144,7 +144,7 @@ export async function validateTurboNextConfig({ ...@@ -144,7 +144,7 @@ export async function validateTurboNextConfig({
if (key.startsWith('webpack') && rawNextConfig.webpack) { if (key.startsWith('webpack') && rawNextConfig.webpack) {
hasWebpackConfig = true hasWebpackConfig = true
} }
if (key.startsWith('experimental.turbo')) { if (key.startsWith('turbopack')) {
hasTurboConfig = true hasTurboConfig = true
} }
......
...@@ -7,10 +7,12 @@ import type zod from 'next/dist/compiled/zod' ...@@ -7,10 +7,12 @@ import type zod from 'next/dist/compiled/zod'
import type { SizeLimit } from '../types' import type { SizeLimit } from '../types'
import type { import type {
ExportPathMap, ExportPathMap,
TurboLoaderItem, TurbopackLoaderItem,
TurboRuleConfigItem, DeprecatedExperimentalTurboOptions,
TurboRuleConfigItemOptions, TurbopackOptions,
TurboRuleConfigItemOrShortcut, TurbopackRuleConfigItem,
TurbopackRuleConfigItemOptions,
TurbopackRuleConfigItemOrShortcut,
} from './config-shared' } from './config-shared'
import type { import type {
Header, Header,
...@@ -99,7 +101,7 @@ const zHeader: zod.ZodType<Header> = z.object({ ...@@ -99,7 +101,7 @@ const zHeader: zod.ZodType<Header> = z.object({
internal: z.boolean().optional(), internal: z.boolean().optional(),
}) })
const zTurboLoaderItem: zod.ZodType<TurboLoaderItem> = z.union([ const zTurboLoaderItem: zod.ZodType<TurbopackLoaderItem> = z.union([
z.string(), z.string(),
z.object({ z.object({
loader: z.string(), loader: z.string(),
...@@ -108,13 +110,13 @@ const zTurboLoaderItem: zod.ZodType<TurboLoaderItem> = z.union([ ...@@ -108,13 +110,13 @@ const zTurboLoaderItem: zod.ZodType<TurboLoaderItem> = z.union([
}), }),
]) ])
const zTurboRuleConfigItemOptions: zod.ZodType<TurboRuleConfigItemOptions> = const zTurboRuleConfigItemOptions: zod.ZodType<TurbopackRuleConfigItemOptions> =
z.object({ z.object({
loaders: z.array(zTurboLoaderItem), loaders: z.array(zTurboLoaderItem),
as: z.string().optional(), as: z.string().optional(),
}) })
const zTurboRuleConfigItem: zod.ZodType<TurboRuleConfigItem> = z.union([ const zTurboRuleConfigItem: zod.ZodType<TurbopackRuleConfigItem> = z.union([
z.literal(false), z.literal(false),
z.record( z.record(
z.string(), z.string(),
...@@ -122,10 +124,50 @@ const zTurboRuleConfigItem: zod.ZodType<TurboRuleConfigItem> = z.union([ ...@@ -122,10 +124,50 @@ const zTurboRuleConfigItem: zod.ZodType<TurboRuleConfigItem> = z.union([
), ),
zTurboRuleConfigItemOptions, zTurboRuleConfigItemOptions,
]) ])
const zTurboRuleConfigItemOrShortcut: zod.ZodType<TurbopackRuleConfigItemOrShortcut> =
const zTurboRuleConfigItemOrShortcut: zod.ZodType<TurboRuleConfigItemOrShortcut> =
z.union([z.array(zTurboLoaderItem), zTurboRuleConfigItem]) z.union([z.array(zTurboLoaderItem), zTurboRuleConfigItem])
const zTurbopackConfig: zod.ZodType<TurbopackOptions> = z.strictObject({
rules: z.record(z.string(), zTurboRuleConfigItemOrShortcut).optional(),
resolveAlias: z
.record(
z.string(),
z.union([
z.string(),
z.array(z.string()),
z.record(z.string(), z.union([z.string(), z.array(z.string())])),
])
)
.optional(),
resolveExtensions: z.array(z.string()).optional(),
moduleIds: z.enum(['named', 'deterministic']).optional(),
})
// Same as zTurbopackConfig but with deprecated properties. Unfortunately, base
// properties are duplicated here as `ZodType`s do not export `extend()`.
const zDeprecatedExperimentalTurboConfig: zod.ZodType<DeprecatedExperimentalTurboOptions> =
z.strictObject({
loaders: z.record(z.string(), z.array(zTurboLoaderItem)).optional(),
rules: z.record(z.string(), zTurboRuleConfigItemOrShortcut).optional(),
resolveAlias: z
.record(
z.string(),
z.union([
z.string(),
z.array(z.string()),
z.record(z.string(), z.union([z.string(), z.array(z.string())])),
])
)
.optional(),
resolveExtensions: z.array(z.string()).optional(),
treeShaking: z.boolean().optional(),
persistentCaching: z.union([z.number(), z.literal(false)]).optional(),
memoryLimit: z.number().optional(),
moduleIds: z.enum(['named', 'deterministic']).optional(),
minify: z.boolean().optional(),
sourceMaps: z.boolean().optional(),
})
export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
z.strictObject({ z.strictObject({
allowedDevOrigins: z.array(z.string()).optional(), allowedDevOrigins: z.array(z.string()).optional(),
...@@ -392,36 +434,15 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -392,36 +434,15 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
typedRoutes: z.boolean().optional(), typedRoutes: z.boolean().optional(),
webpackBuildWorker: z.boolean().optional(), webpackBuildWorker: z.boolean().optional(),
webpackMemoryOptimizations: z.boolean().optional(), webpackMemoryOptimizations: z.boolean().optional(),
turbo: z /**
.object({ * @deprecated Use `config.turbopack` instead.
loaders: z.record(z.string(), z.array(zTurboLoaderItem)).optional(), */
rules: z turbo: zDeprecatedExperimentalTurboConfig.optional(),
.record(z.string(), zTurboRuleConfigItemOrShortcut) turbopackMemoryLimit: z.number().optional(),
.optional(), turbopackMinify: z.boolean().optional(),
resolveAlias: z turbopackPersistentCaching: z.boolean().optional(),
.record( turbopackSourceMaps: z.boolean().optional(),
z.string(), turbopackTreeShaking: z.boolean().optional(),
z.union([
z.string(),
z.array(z.string()),
z.record(
z.string(),
z.union([z.string(), z.array(z.string())])
),
])
)
.optional(),
resolveExtensions: z.array(z.string()).optional(),
treeShaking: z.boolean().optional(),
persistentCaching: z
.union([z.number(), z.literal(false)])
.optional(),
memoryLimit: z.number().optional(),
moduleIdStrategy: z.enum(['named', 'deterministic']).optional(),
minify: z.boolean().optional(),
sourceMaps: z.boolean().optional(),
})
.optional(),
optimizePackageImports: z.array(z.string()).optional(), optimizePackageImports: z.array(z.string()).optional(),
optimizeServerReact: z.boolean().optional(), optimizeServerReact: z.boolean().optional(),
clientTraceMetadata: z.array(z.string()).optional(), clientTraceMetadata: z.array(z.string()).optional(),
...@@ -658,6 +679,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -658,6 +679,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
target: z.string().optional(), target: z.string().optional(),
trailingSlash: z.boolean().optional(), trailingSlash: z.boolean().optional(),
transpilePackages: z.array(z.string()).optional(), transpilePackages: z.array(z.string()).optional(),
turbopack: zTurbopackConfig.optional(),
typescript: z typescript: z
.strictObject({ .strictObject({
ignoreBuildErrors: z.boolean().optional(), ignoreBuildErrors: z.boolean().optional(),
......
...@@ -23,7 +23,7 @@ export type NextConfigComplete = Required<NextConfig> & { ...@@ -23,7 +23,7 @@ export type NextConfigComplete = Required<NextConfig> & {
// override NextConfigComplete.experimental.htmlLimitedBots to string // override NextConfigComplete.experimental.htmlLimitedBots to string
// because it's not defined in NextConfigComplete.experimental // because it's not defined in NextConfigComplete.experimental
htmlLimitedBots: string | undefined htmlLimitedBots: string | undefined
experimental: ExperimentalConfig experimental: Omit<ExperimentalConfig, 'turbo'>
} }
export type I18NDomains = readonly DomainLocale[] export type I18NDomains = readonly DomainLocale[]
...@@ -94,7 +94,10 @@ type JSONValue = ...@@ -94,7 +94,10 @@ type JSONValue =
| JSONValue[] | JSONValue[]
| { [k: string]: JSONValue } | { [k: string]: JSONValue }
export type TurboLoaderItem = /**
* @deprecated Use `TurbopackRuleConfigItem` instead.
*/
export type TurbopackLoaderItem =
| string | string
| { | {
loader: string loader: string
...@@ -102,21 +105,21 @@ export type TurboLoaderItem = ...@@ -102,21 +105,21 @@ export type TurboLoaderItem =
options: Record<string, JSONValue> options: Record<string, JSONValue>
} }
export type TurboRuleConfigItemOrShortcut = export type TurbopackRuleConfigItemOrShortcut =
| TurboLoaderItem[] | TurbopackLoaderItem[]
| TurboRuleConfigItem | TurbopackRuleConfigItem
export type TurboRuleConfigItemOptions = { export type TurbopackRuleConfigItemOptions = {
loaders: TurboLoaderItem[] loaders: TurbopackLoaderItem[]
as?: string as?: string
} }
export type TurboRuleConfigItem = export type TurbopackRuleConfigItem =
| TurboRuleConfigItemOptions | TurbopackRuleConfigItemOptions
| { [condition: string]: TurboRuleConfigItem } | { [condition: string]: TurbopackRuleConfigItem }
| false | false
export interface ExperimentalTurboOptions { export interface TurbopackOptions {
/** /**
* (`next --turbopack` only) A mapping of aliased imports to modules to load in their place. * (`next --turbopack` only) A mapping of aliased imports to modules to load in their place.
* *
...@@ -139,50 +142,52 @@ export interface ExperimentalTurboOptions { ...@@ -139,50 +142,52 @@ export interface ExperimentalTurboOptions {
* *
* @see [Turbopack Loaders](https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders) * @see [Turbopack Loaders](https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders)
*/ */
loaders?: Record<string, TurboLoaderItem[]> rules?: Record<string, TurbopackRuleConfigItemOrShortcut>
/**
* The module ID strategy to use for Turbopack.
* If not set, the default is `'named'` for development and `'deterministic'`
* for production.
*/
moduleIds?: 'named' | 'deterministic'
/**
* This is the repo root usually and only files above this
* directory can be resolved by turbopack.
*/
root?: string
}
export interface DeprecatedExperimentalTurboOptions extends TurbopackOptions {
/** /**
* (`next --turbopack` only) A list of webpack loaders to apply when running with Turbopack. * (`next --turbopack` only) A list of webpack loaders to apply when running with Turbopack.
* *
* @deprecated Use `rules` instead.
* @see [Turbopack Loaders](https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders) * @see [Turbopack Loaders](https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders)
*/ */
rules?: Record<string, TurboRuleConfigItemOrShortcut> loaders?: Record<string, TurbopackLoaderItem[]>
/** /**
* A target memory limit for turbo, in bytes. * A target memory limit for turbo, in bytes.
* @deprecated Use `experimental.turbopackMemoryLimit` instead.
*/ */
memoryLimit?: number memoryLimit?: number
/** /**
* Enable persistent caching for the turbopack dev server and build. * Enable minification. Defaults to true in build mode and false in dev mode.
* @deprecated Use `experimental.turbopackMinify` instead.
*/ */
unstablePersistentCaching?: boolean minify?: boolean
/** /**
* Enable tree shaking for the turbopack dev server and build. * Enable tree shaking for the turbopack dev server and build.
* @deprecated Use `experimental.turbopackTreeShaking` instead.
*/ */
treeShaking?: boolean treeShaking?: boolean
/**
* The module ID strategy to use for Turbopack.
* If not set, the default is `'named'` for development and `'deterministic'`
* for production.
*/
moduleIdStrategy?: 'named' | 'deterministic'
/**
* This is the repo root usually and only files above this
* directory can be resolved by turbopack.
*/
root?: string
/**
* Enable minification. Defaults to true in build mode and false in dev mode.
*/
minify?: boolean
/** /**
* Enable source maps. Defaults to true. * Enable source maps. Defaults to true.
* @deprecated Use `experimental.turbopackSourceMaps` instead.
*/ */
sourceMaps?: boolean sourceMaps?: boolean
} }
...@@ -377,7 +382,35 @@ export interface ExperimentalConfig { ...@@ -377,7 +382,35 @@ export interface ExperimentalConfig {
*/ */
optimizeServerReact?: boolean optimizeServerReact?: boolean
turbo?: ExperimentalTurboOptions /**
* @deprecated Use `config.turbopack` instead.
*/
turbo?: DeprecatedExperimentalTurboOptions
/**
* A target memory limit for turbo, in bytes.
*/
turbopackMemoryLimit?: number
/**
* Enable minification. Defaults to true in build mode and false in dev mode.
*/
turbopackMinify?: boolean
/**
* Enable persistent caching for the turbopack dev server and build.
*/
turbopackPersistentCaching?: boolean
/**
* Enable source maps. Defaults to true.
*/
turbopackSourceMaps?: boolean
/**
* Enable tree shaking for the turbopack dev server and build.
*/
turbopackTreeShaking?: boolean
/** /**
* For use with `@next/mdx`. Compile MDX files using the new Rust compiler. * For use with `@next/mdx`. Compile MDX files using the new Rust compiler.
...@@ -1017,6 +1050,11 @@ export interface NextConfig extends Record<string, any> { ...@@ -1017,6 +1050,11 @@ export interface NextConfig extends Record<string, any> {
*/ */
transpilePackages?: string[] transpilePackages?: string[]
/**
* Options for Turbopack. Temporarily also available as `experimental.turbo` for compatibility.
*/
turbopack?: TurbopackOptions
skipMiddlewareUrlNormalize?: boolean skipMiddlewareUrlNormalize?: boolean
skipTrailingSlashRedirect?: boolean skipTrailingSlashRedirect?: boolean
......
...@@ -127,14 +127,12 @@ describe('loadConfig', () => { ...@@ -127,14 +127,12 @@ describe('loadConfig', () => {
loadConfig('', __dirname, { loadConfig('', __dirname, {
customConfig: { customConfig: {
experimental: { experimental: {
turbo: { turbopackPersistentCaching: true,
unstablePersistentCaching: true,
},
}, },
}, },
}) })
).rejects.toThrow( ).rejects.toThrow(
/The experimental feature "experimental.turbo.unstablePersistentCaching" can only be enabled when using the latest canary version of Next.js./ /The experimental feature "experimental.turbopackPersistentCaching" can only be enabled when using the latest canary version of Next.js./
) )
}) })
}) })
......
...@@ -9,7 +9,7 @@ import type { ...@@ -9,7 +9,7 @@ import type {
ExperimentalConfig, ExperimentalConfig,
NextConfigComplete, NextConfigComplete,
NextConfig, NextConfig,
TurboLoaderItem, TurbopackLoaderItem,
} from './config-shared' } from './config-shared'
import { loadWebpackHook } from './config-utils' import { loadWebpackHook } from './config-utils'
...@@ -251,9 +251,9 @@ function assignDefaults( ...@@ -251,9 +251,9 @@ function assignDefaults(
throw new CanaryOnlyError({ feature: 'experimental.ppr' }) throw new CanaryOnlyError({ feature: 'experimental.ppr' })
} else if (result.experimental?.dynamicIO) { } else if (result.experimental?.dynamicIO) {
throw new CanaryOnlyError({ feature: 'experimental.dynamicIO' }) throw new CanaryOnlyError({ feature: 'experimental.dynamicIO' })
} else if (result.experimental?.turbo?.unstablePersistentCaching) { } else if (result.experimental?.turbopackPersistentCaching) {
throw new CanaryOnlyError({ throw new CanaryOnlyError({
feature: 'experimental.turbo.unstablePersistentCaching', feature: 'experimental.turbopackPersistentCaching',
}) })
} else if (result.experimental?.nodeMiddleware) { } else if (result.experimental?.nodeMiddleware) {
throw new CanaryOnlyError({ feature: 'experimental.nodeMiddleware' }) throw new CanaryOnlyError({ feature: 'experimental.nodeMiddleware' })
...@@ -655,14 +655,11 @@ function assignDefaults( ...@@ -655,14 +655,11 @@ function assignDefaults(
} }
} }
if ( if (result?.turbopack?.root && !isAbsolute(result.turbopack.root)) {
result?.experimental?.turbo?.root && result.turbopack.root = resolve(result.turbopack.root)
!isAbsolute(result.experimental.turbo.root)
) {
result.experimental.turbo.root = resolve(result.experimental.turbo.root)
if (!silent) { if (!silent) {
Log.warn( Log.warn(
`experimental.turbo.root should be absolute, using: ${result.experimental.turbo.root}` `turbopack.root should be absolute, using: ${result.turbopack.root}`
) )
} }
} }
...@@ -672,27 +669,21 @@ function assignDefaults( ...@@ -672,27 +669,21 @@ function assignDefaults(
result.deploymentId = process.env.NEXT_DEPLOYMENT_ID result.deploymentId = process.env.NEXT_DEPLOYMENT_ID
} }
if (result?.outputFileTracingRoot && !result?.experimental?.turbo?.root) { if (result?.outputFileTracingRoot && !result?.turbopack?.root) {
dset( dset(result, ['turbopack', 'root'], result.outputFileTracingRoot)
result,
['experimental', 'turbo', 'root'],
result.outputFileTracingRoot
)
} }
// use the closest lockfile as tracing root // use the closest lockfile as tracing root
if (!result?.outputFileTracingRoot || !result?.experimental?.turbo?.root) { if (!result?.outputFileTracingRoot || !result?.turbopack?.root) {
let rootDir = findRootDir(dir) let rootDir = findRootDir(dir)
if (rootDir) { if (rootDir) {
if (!result?.outputFileTracingRoot) { if (!result?.outputFileTracingRoot) {
result.outputFileTracingRoot = rootDir result.outputFileTracingRoot = rootDir
defaultConfig.outputFileTracingRoot = result.outputFileTracingRoot
} }
if (!result?.experimental?.turbo?.root) { if (!result?.turbopack?.root) {
dset(result, ['experimental', 'turbo', 'root'], rootDir) dset(result, ['turbopack', 'root'], rootDir)
dset(defaultConfig, ['experimental', 'turbo', 'root'], rootDir)
} }
} }
} }
...@@ -1273,16 +1264,36 @@ export default async function loadConfig( ...@@ -1273,16 +1264,36 @@ export default async function loadConfig(
'See more info here https://nextjs.org/docs/app/api-reference/next-config-js/turbo' 'See more info here https://nextjs.org/docs/app/api-reference/next-config-js/turbo'
) )
const rules: Record<string, TurboLoaderItem[]> = {} const rules: Record<string, TurbopackLoaderItem[]> = {}
for (const [ext, loaders] of Object.entries( for (const [ext, loaders] of Object.entries(
userConfig.experimental.turbo.loaders userConfig.experimental.turbo.loaders
)) { )) {
rules['*' + ext] = loaders as TurboLoaderItem[] rules['*' + ext] = loaders as TurbopackLoaderItem[]
} }
userConfig.experimental.turbo.rules = rules userConfig.experimental.turbo.rules = rules
} }
if (userConfig.experimental?.turbo) {
curLog.warn(
'The config property `experimental.turbo` is deprecated. Move this setting to `config.turbopack` as Turbopack is now stable.'
)
// Merge the two configs, preferring values in `config.turbopack`.
userConfig.turbopack = {
...userConfig.experimental.turbo,
...userConfig.turbopack,
}
userConfig.experimental.turbopackMemoryLimit ??=
userConfig.experimental.turbo.memoryLimit
userConfig.experimental.turbopackMinify ??=
userConfig.experimental.turbo.minify
userConfig.experimental.turbopackTreeShaking ??=
userConfig.experimental.turbo.treeShaking
userConfig.experimental.turbopackSourceMaps ??=
userConfig.experimental.turbo.sourceMaps
}
if (userConfig.experimental?.useLightningcss) { if (userConfig.experimental?.useLightningcss) {
const { loadBindings } = require('next/dist/build/swc') const { loadBindings } = require('next/dist/build/swc')
const isLightningSupported = (await loadBindings())?.css?.lightning const isLightningSupported = (await loadBindings())?.css?.lightning
......
...@@ -209,7 +209,7 @@ export async function createHotReloaderTurbopack( ...@@ -209,7 +209,7 @@ export async function createHotReloaderTurbopack(
{ {
projectPath: projectPath, projectPath: projectPath,
rootPath: rootPath:
opts.nextConfig.experimental.turbo?.root || opts.nextConfig.turbopack?.root ||
opts.nextConfig.outputFileTracingRoot || opts.nextConfig.outputFileTracingRoot ||
projectPath, projectPath,
distDir, distDir,
...@@ -240,7 +240,7 @@ export async function createHotReloaderTurbopack( ...@@ -240,7 +240,7 @@ export async function createHotReloaderTurbopack(
}, },
{ {
persistentCaching: isPersistentCachingEnabled(opts.nextConfig), persistentCaching: isPersistentCachingEnabled(opts.nextConfig),
memoryLimit: opts.nextConfig.experimental.turbo?.memoryLimit, memoryLimit: opts.nextConfig.experimental?.turbopackMemoryLimit,
} }
) )
setBundlerFindSourceMapImplementation( setBundlerFindSourceMapImplementation(
......
...@@ -262,5 +262,5 @@ export function renderStyledStringToErrorAnsi(string: StyledString): string { ...@@ -262,5 +262,5 @@ export function renderStyledStringToErrorAnsi(string: StyledString): string {
export function isPersistentCachingEnabled( export function isPersistentCachingEnabled(
config: NextConfigComplete config: NextConfigComplete
): boolean { ): boolean {
return config.experimental.turbo?.unstablePersistentCaching || false return config.experimental?.turbopackPersistentCaching || false
} }
...@@ -2,13 +2,11 @@ ...@@ -2,13 +2,11 @@
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
experimental: { turbopack: {
turbo: { rules: {
rules: { '*.svg': {
'*.svg': { loaders: ['@svgr/webpack'],
loaders: ['@svgr/webpack'], as: '*.js',
as: '*.js',
},
}, },
}, },
}, },
......
...@@ -4,10 +4,8 @@ const extensions = ['', '.png', '.tsx', '.ts', '.jsx', '.js', '.json'] ...@@ -4,10 +4,8 @@ const extensions = ['', '.png', '.tsx', '.ts', '.jsx', '.js', '.json']
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
experimental: { turbopack: {
turbo: { resolveExtensions: [...extensions],
resolveExtensions: [...extensions],
},
}, },
webpack(config) { webpack(config) {
config.resolve.extensions = [...extensions] config.resolve.extensions = [...extensions]
......
...@@ -3,12 +3,10 @@ ...@@ -3,12 +3,10 @@
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
const nextConfig = { const nextConfig = {
experimental: { turbopack: {
turbo: { resolveAlias: {
resolveAlias: { '/*': './*',
'/*': './*', '~*': '*',
'~*': '*',
},
}, },
}, },
} }
......
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
experimental: { turbopack: {
turbo: { rules: {
rules: { '*.mdx': {
'*.mdx': { loaders: ['test-loader.js'],
loaders: ['test-loader.js'], as: '*.js',
as: '*.js',
},
}, },
}, },
}, },
......
...@@ -2,38 +2,36 @@ ...@@ -2,38 +2,36 @@
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
experimental: { turbopack: {
turbo: { rules: {
rules: { '*.test-file.js': {
'*.test-file.js': { browser: {
browser: { foreign: {
foreign: { loaders: [
loaders: [ {
{ loader: require.resolve('./test-file-loader.js'),
loader: require.resolve('./test-file-loader.js'), options: { browser: true, foreign: true },
options: { browser: true, foreign: true }, },
}, ],
],
},
default: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true },
},
],
},
}, },
foreign: false,
default: { default: {
loaders: [ loaders: [
{ {
loader: require.resolve('./test-file-loader.js'), loader: require.resolve('./test-file-loader.js'),
options: { default: true }, options: { browser: true },
}, },
], ],
}, },
}, },
foreign: false,
default: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { default: true },
},
],
},
}, },
}, },
}, },
......
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
experimental: { turbopack: {
turbo: { rules: {
rules: { '*.test-file.ts': [require.resolve('./test-file-loader.js')],
'*.test-file.ts': [require.resolve('./test-file-loader.js')],
},
}, },
}, },
} }
......
module.exports = { module.exports = {
experimental: { turbopack: {
turbo: { resolveAlias: {
resolveAlias: { 'preact/compat': 'react',
'preact/compat': 'react',
},
}, },
}, },
serverExternalPackages: [ serverExternalPackages: [
......
...@@ -2,19 +2,19 @@ ...@@ -2,19 +2,19 @@
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
experimental: { turbopack: {
turbo: { rules: {
unstablePersistentCaching: true, 'app/page.tsx': {
rules: { loaders: ['./my-loader.js'],
'app/page.tsx': { },
loaders: ['./my-loader.js'], 'pages/pages.tsx': {
}, loaders: ['./my-loader.js'],
'pages/pages.tsx': {
loaders: ['./my-loader.js'],
},
}, },
}, },
}, },
experimental: {
turbopackPersistentCaching: true,
},
webpack(config) { webpack(config) {
config.module.rules.push({ config.module.rules.push({
test: /app\/page\.tsx|pages\/pages.tsx/, test: /app\/page\.tsx|pages\/pages.tsx/,
......
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import foo from 'foo'
export default function Page() {
return <div id="result">Hello {foo}</div>
}
/* eslint-disable jest/no-standalone-expect */
import { nextTestSetup } from 'e2e-utils'
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'turbopack-turbo-config-compatibility',
() => {
describe('including both turbopack and deprecated experimental turbo config', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('prefers turbopack config over deprecated experimental turbo config', async () => {
const $ = await next.render$('/')
expect($('#result').text()).toEqual('Hello turbopack')
})
})
describe('only including deprecated experimental turbo config', () => {
const { next } = nextTestSetup({
files: __dirname,
overrideFiles: {
'next.config.js': `module.exports = {
experimental: {
turbo: {
resolveAlias: {
foo: './turbo.js',
},
},
},
}`,
},
})
it('still uses the deprecated experimental turbo config', async () => {
const $ = await next.render$('/')
expect($('#result').text()).toEqual('Hello turbo')
})
})
}
)
module.exports = {
turbopack: {
resolveAlias: {
foo: './turbopack.js',
},
},
experimental: {
turbo: {
resolveAlias: {
foo: './turbo.js',
},
},
},
}
module.exports = { module.exports = {
bundlePagesRouterDependencies: true, bundlePagesRouterDependencies: true,
serverExternalPackages: ['opted-out-external-package'], serverExternalPackages: ['opted-out-external-package'],
experimental: {
turbo: {
moduleIdStrategy:
process.env.NODE_ENV === 'production' ? 'deterministic' : undefined,
},
},
} }
...@@ -12,6 +12,7 @@ function runTests() { ...@@ -12,6 +12,7 @@ function runTests() {
const jsFiles = browserFiles.filter( const jsFiles = browserFiles.filter(
(file) => file.endsWith('.js') && file.includes('/pages/') (file) => file.endsWith('.js') && file.includes('/pages/')
) )
expect(jsFiles).not.toBeEmpty()
jsFiles.forEach((file) => { jsFiles.forEach((file) => {
expect(browserFiles.includes(`${file}.map`)).toBe(true) expect(browserFiles.includes(`${file}.map`)).toBe(true)
......
module.exports = { module.exports = {
experimental: { experimental: {
turbo: { turbopackPersistentCaching: true
unstablePersistentCaching: true
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment