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 = {
ignoreBuildErrors: true,
},
experimental: {
turbo: {
unstablePersistentCaching: process.env.TURBO_CACHE === '1',
},
turbopackPersistentCaching: process.env.TURBO_CACHE === '1',
},
}
......
......@@ -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,
......@@ -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
......@@ -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(),
)
}
......@@ -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 {
......@@ -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(),
)
}
......@@ -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 {
......@@ -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(),
)
}
......@@ -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())),
......
......@@ -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),
......
......@@ -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.
......@@ -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,
......@@ -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(
......@@ -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,
......@@ -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>,
......@@ -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(
......@@ -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() {
......@@ -1234,9 +1230,7 @@ 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(),
))
}
......@@ -1244,8 +1238,7 @@ impl NextConfig {
#[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 {
......@@ -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 {
......@@ -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),
......@@ -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),
......@@ -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)))
}
......
......@@ -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,
......
......@@ -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. |
| `resolveAlias` | Map aliased imports to modules to load in their place. |
| `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. |
| `memoryLimit` | A target memory limit for turbo, in bytes. |
......@@ -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.
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"
module.exports = {
experimental: {
turbo: {
moduleIdStrategy: 'deterministic',
},
turbo: {
moduleIds: 'deterministic',
},
}
```
......
......@@ -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).
- **`resolveExtensions`**
Change or extend file extensions for module resolution.
- **`moduleIdStrategy`**
- **`moduleIds`**
Set how module IDs are generated (`'named'` vs `'deterministic'`).
- **`treeShaking`**
Enable or disable tree shaking in dev and future production builds.
......
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
turbo: {
resolveAlias: {
"react-native": "react-native-web",
},
resolveExtensions: [
".web.js",
".web.jsx",
".web.ts",
".web.tsx",
".mdx",
".tsx",
".ts",
".jsx",
".js",
".mjs",
".json",
],
turbopack: {
resolveAlias: {
"react-native": "react-native-web",
},
resolveExtensions: [
".web.js",
".web.jsx",
".web.ts",
".web.tsx",
".mdx",
".tsx",
".ts",
".jsx",
".js",
".mjs",
".json",
],
},
webpack: (config) => {
config.resolve.alias = {
......
module.exports = {
experimental: {
turbo: {
rules: {
"*.react.svg": {
loaders: ["@svgr/webpack"],
as: "*.js",
},
"*.styl": {
loaders: ["stylus-loader"],
as: "*.css",
},
turbopack: {
rules: {
"*.react.svg": {
loaders: ["@svgr/webpack"],
as: "*.js",
},
"*.styl": {
loaders: ["stylus-loader"],
as: "*.css",
},
},
},
......
......@@ -3756,9 +3756,14 @@ function warnAboutTurbopackBuilds(config?: NextConfigComplete) {
`We don't recommend deploying mission-critical applications to production.`
)
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 +=
'\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'
import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc'
import type {
NextConfigComplete,
TurboLoaderItem,
TurboRuleConfigItem,
TurboRuleConfigItemOptions,
TurboRuleConfigItemOrShortcut,
TurbopackLoaderItem,
TurbopackRuleConfigItem,
TurbopackRuleConfigItemOptions,
TurbopackRuleConfigItemOrShortcut,
} from '../../server/config-shared'
import { isDeepStrictEqual } from 'util'
import {
......@@ -786,24 +786,24 @@ function bindingToApi(
if (reactCompilerOptions) {
const ruleKeys = ['*.ts', '*.js', '*.jsx', '*.tsx']
if (
Object.keys(nextConfig?.experimental?.turbo?.rules ?? []).some((key) =>
Object.keys(nextConfig?.turbopack?.rules ?? []).some((key) =>
ruleKeys.includes(key)
)
) {
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 {
if (!nextConfig.experimental.turbo) {
nextConfig.experimental.turbo = {}
if (!nextConfig.turbopack) {
nextConfig.turbopack = {}
}
if (!nextConfig.experimental.turbo.rules) {
nextConfig.experimental.turbo.rules = {}
if (!nextConfig.turbopack.rules) {
nextConfig.turbopack.rules = {}
}
for (const key of ['*.ts', '*.js', '*.jsx', '*.tsx']) {
nextConfig.experimental.turbo.rules[key] = {
nextConfig.turbopack.rules[key] = {
browser: {
foreign: false,
loaders: [
......@@ -840,7 +840,7 @@ function bindingToApi(
if (nextConfigSerializable.experimental?.turbo?.rules) {
ensureLoadersHaveSerializableOptions(
nextConfigSerializable.experimental.turbo?.rules
nextConfigSerializable.turbopack?.rules
)
}
......@@ -878,7 +878,7 @@ function bindingToApi(
}
function ensureLoadersHaveSerializableOptions(
turbopackRules: Record<string, TurboRuleConfigItemOrShortcut>
turbopackRules: Record<string, TurbopackRuleConfigItemOrShortcut>
) {
for (const [glob, rule] of Object.entries(turbopackRules)) {
if (Array.isArray(rule)) {
......@@ -888,10 +888,10 @@ function bindingToApi(
}
}
function checkConfigItem(rule: TurboRuleConfigItem, glob: string) {
function checkConfigItem(rule: TurbopackRuleConfigItem, glob: string) {
if (!rule) return
if ('loaders' in rule) {
checkLoaderItems((rule as TurboRuleConfigItemOptions).loaders, glob)
checkLoaderItems((rule as TurbopackRuleConfigItemOptions).loaders, glob)
} else {
for (const key in rule) {
const inner = rule[key]
......@@ -902,7 +902,10 @@ function bindingToApi(
}
}
function checkLoaderItems(loaderItems: TurboLoaderItem[], glob: string) {
function checkLoaderItems(
loaderItems: TurbopackLoaderItem[],
glob: string
) {
for (const loaderItem of loaderItems) {
if (
typeof loaderItem !== 'string' &&
......
......@@ -54,8 +54,7 @@ export async function turbopackBuild(): Promise<{
const project = await bindings.turbo.createProject(
{
projectPath: dir,
rootPath:
config.experimental?.turbo?.root || config.outputFileTracingRoot || dir,
rootPath: config.turbopack?.root || config.outputFileTracingRoot || dir,
distDir,
nextConfig: config,
jsConfig: await getTurbopackJsConfig(dir, config),
......@@ -83,7 +82,7 @@ export async function turbopackBuild(): Promise<{
},
{
persistentCaching,
memoryLimit: config.experimental.turbo?.memoryLimit,
memoryLimit: config.experimental?.turbopackMemoryLimit,
dependencyTracking: persistentCaching,
}
)
......
......@@ -144,7 +144,7 @@ export async function validateTurboNextConfig({
if (key.startsWith('webpack') && rawNextConfig.webpack) {
hasWebpackConfig = true
}
if (key.startsWith('experimental.turbo')) {
if (key.startsWith('turbopack')) {
hasTurboConfig = true
}
......
......@@ -7,10 +7,12 @@ import type zod from 'next/dist/compiled/zod'
import type { SizeLimit } from '../types'
import type {
ExportPathMap,
TurboLoaderItem,
TurboRuleConfigItem,
TurboRuleConfigItemOptions,
TurboRuleConfigItemOrShortcut,
TurbopackLoaderItem,
DeprecatedExperimentalTurboOptions,
TurbopackOptions,
TurbopackRuleConfigItem,
TurbopackRuleConfigItemOptions,
TurbopackRuleConfigItemOrShortcut,
} from './config-shared'
import type {
Header,
......@@ -99,7 +101,7 @@ const zHeader: zod.ZodType<Header> = z.object({
internal: z.boolean().optional(),
})
const zTurboLoaderItem: zod.ZodType<TurboLoaderItem> = z.union([
const zTurboLoaderItem: zod.ZodType<TurbopackLoaderItem> = z.union([
z.string(),
z.object({
loader: z.string(),
......@@ -108,13 +110,13 @@ const zTurboLoaderItem: zod.ZodType<TurboLoaderItem> = z.union([
}),
])
const zTurboRuleConfigItemOptions: zod.ZodType<TurboRuleConfigItemOptions> =
const zTurboRuleConfigItemOptions: zod.ZodType<TurbopackRuleConfigItemOptions> =
z.object({
loaders: z.array(zTurboLoaderItem),
as: z.string().optional(),
})
const zTurboRuleConfigItem: zod.ZodType<TurboRuleConfigItem> = z.union([
const zTurboRuleConfigItem: zod.ZodType<TurbopackRuleConfigItem> = z.union([
z.literal(false),
z.record(
z.string(),
......@@ -122,10 +124,50 @@ const zTurboRuleConfigItem: zod.ZodType<TurboRuleConfigItem> = z.union([
),
zTurboRuleConfigItemOptions,
])
const zTurboRuleConfigItemOrShortcut: zod.ZodType<TurboRuleConfigItemOrShortcut> =
const zTurboRuleConfigItemOrShortcut: zod.ZodType<TurbopackRuleConfigItemOrShortcut> =
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(() =>
z.strictObject({
allowedDevOrigins: z.array(z.string()).optional(),
......@@ -392,36 +434,15 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
typedRoutes: z.boolean().optional(),
webpackBuildWorker: z.boolean().optional(),
webpackMemoryOptimizations: z.boolean().optional(),
turbo: z
.object({
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(),
moduleIdStrategy: z.enum(['named', 'deterministic']).optional(),
minify: z.boolean().optional(),
sourceMaps: z.boolean().optional(),
})
.optional(),
/**
* @deprecated Use `config.turbopack` instead.
*/
turbo: zDeprecatedExperimentalTurboConfig.optional(),
turbopackMemoryLimit: z.number().optional(),
turbopackMinify: z.boolean().optional(),
turbopackPersistentCaching: z.boolean().optional(),
turbopackSourceMaps: z.boolean().optional(),
turbopackTreeShaking: z.boolean().optional(),
optimizePackageImports: z.array(z.string()).optional(),
optimizeServerReact: z.boolean().optional(),
clientTraceMetadata: z.array(z.string()).optional(),
......@@ -658,6 +679,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
target: z.string().optional(),
trailingSlash: z.boolean().optional(),
transpilePackages: z.array(z.string()).optional(),
turbopack: zTurbopackConfig.optional(),
typescript: z
.strictObject({
ignoreBuildErrors: z.boolean().optional(),
......
......@@ -23,7 +23,7 @@ export type NextConfigComplete = Required<NextConfig> & {
// override NextConfigComplete.experimental.htmlLimitedBots to string
// because it's not defined in NextConfigComplete.experimental
htmlLimitedBots: string | undefined
experimental: ExperimentalConfig
experimental: Omit<ExperimentalConfig, 'turbo'>
}
export type I18NDomains = readonly DomainLocale[]
......@@ -94,7 +94,10 @@ type JSONValue =
| JSONValue[]
| { [k: string]: JSONValue }
export type TurboLoaderItem =
/**
* @deprecated Use `TurbopackRuleConfigItem` instead.
*/
export type TurbopackLoaderItem =
| string
| {
loader: string
......@@ -102,21 +105,21 @@ export type TurboLoaderItem =
options: Record<string, JSONValue>
}
export type TurboRuleConfigItemOrShortcut =
| TurboLoaderItem[]
| TurboRuleConfigItem
export type TurbopackRuleConfigItemOrShortcut =
| TurbopackLoaderItem[]
| TurbopackRuleConfigItem
export type TurboRuleConfigItemOptions = {
loaders: TurboLoaderItem[]
export type TurbopackRuleConfigItemOptions = {
loaders: TurbopackLoaderItem[]
as?: string
}
export type TurboRuleConfigItem =
| TurboRuleConfigItemOptions
| { [condition: string]: TurboRuleConfigItem }
export type TurbopackRuleConfigItem =
| TurbopackRuleConfigItemOptions
| { [condition: string]: TurbopackRuleConfigItem }
| false
export interface ExperimentalTurboOptions {
export interface TurbopackOptions {
/**
* (`next --turbopack` only) A mapping of aliased imports to modules to load in their place.
*
......@@ -139,50 +142,52 @@ export interface ExperimentalTurboOptions {
*
* @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.
*
* @deprecated Use `rules` instead.
* @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.
* @deprecated Use `experimental.turbopackMemoryLimit` instead.
*/
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.
* @deprecated Use `experimental.turbopackTreeShaking` instead.
*/
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.
* @deprecated Use `experimental.turbopackSourceMaps` instead.
*/
sourceMaps?: boolean
}
......@@ -377,7 +382,35 @@ export interface ExperimentalConfig {
*/
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.
......@@ -1017,6 +1050,11 @@ export interface NextConfig extends Record<string, any> {
*/
transpilePackages?: string[]
/**
* Options for Turbopack. Temporarily also available as `experimental.turbo` for compatibility.
*/
turbopack?: TurbopackOptions
skipMiddlewareUrlNormalize?: boolean
skipTrailingSlashRedirect?: boolean
......
......@@ -127,14 +127,12 @@ describe('loadConfig', () => {
loadConfig('', __dirname, {
customConfig: {
experimental: {
turbo: {
unstablePersistentCaching: true,
},
turbopackPersistentCaching: true,
},
},
})
).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 {
ExperimentalConfig,
NextConfigComplete,
NextConfig,
TurboLoaderItem,
TurbopackLoaderItem,
} from './config-shared'
import { loadWebpackHook } from './config-utils'
......@@ -251,9 +251,9 @@ function assignDefaults(
throw new CanaryOnlyError({ feature: 'experimental.ppr' })
} else if (result.experimental?.dynamicIO) {
throw new CanaryOnlyError({ feature: 'experimental.dynamicIO' })
} else if (result.experimental?.turbo?.unstablePersistentCaching) {
} else if (result.experimental?.turbopackPersistentCaching) {
throw new CanaryOnlyError({
feature: 'experimental.turbo.unstablePersistentCaching',
feature: 'experimental.turbopackPersistentCaching',
})
} else if (result.experimental?.nodeMiddleware) {
throw new CanaryOnlyError({ feature: 'experimental.nodeMiddleware' })
......@@ -655,14 +655,11 @@ function assignDefaults(
}
}
if (
result?.experimental?.turbo?.root &&
!isAbsolute(result.experimental.turbo.root)
) {
result.experimental.turbo.root = resolve(result.experimental.turbo.root)
if (result?.turbopack?.root && !isAbsolute(result.turbopack.root)) {
result.turbopack.root = resolve(result.turbopack.root)
if (!silent) {
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(
result.deploymentId = process.env.NEXT_DEPLOYMENT_ID
}
if (result?.outputFileTracingRoot && !result?.experimental?.turbo?.root) {
dset(
result,
['experimental', 'turbo', 'root'],
result.outputFileTracingRoot
)
if (result?.outputFileTracingRoot && !result?.turbopack?.root) {
dset(result, ['turbopack', 'root'], result.outputFileTracingRoot)
}
// use the closest lockfile as tracing root
if (!result?.outputFileTracingRoot || !result?.experimental?.turbo?.root) {
if (!result?.outputFileTracingRoot || !result?.turbopack?.root) {
let rootDir = findRootDir(dir)
if (rootDir) {
if (!result?.outputFileTracingRoot) {
result.outputFileTracingRoot = rootDir
defaultConfig.outputFileTracingRoot = result.outputFileTracingRoot
}
if (!result?.experimental?.turbo?.root) {
dset(result, ['experimental', 'turbo', 'root'], rootDir)
dset(defaultConfig, ['experimental', 'turbo', 'root'], rootDir)
if (!result?.turbopack?.root) {
dset(result, ['turbopack', 'root'], rootDir)
}
}
}
......@@ -1273,16 +1264,36 @@ export default async function loadConfig(
'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(
userConfig.experimental.turbo.loaders
)) {
rules['*' + ext] = loaders as TurboLoaderItem[]
rules['*' + ext] = loaders as TurbopackLoaderItem[]
}
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) {
const { loadBindings } = require('next/dist/build/swc')
const isLightningSupported = (await loadBindings())?.css?.lightning
......
......@@ -209,7 +209,7 @@ export async function createHotReloaderTurbopack(
{
projectPath: projectPath,
rootPath:
opts.nextConfig.experimental.turbo?.root ||
opts.nextConfig.turbopack?.root ||
opts.nextConfig.outputFileTracingRoot ||
projectPath,
distDir,
......@@ -240,7 +240,7 @@ export async function createHotReloaderTurbopack(
},
{
persistentCaching: isPersistentCachingEnabled(opts.nextConfig),
memoryLimit: opts.nextConfig.experimental.turbo?.memoryLimit,
memoryLimit: opts.nextConfig.experimental?.turbopackMemoryLimit,
}
)
setBundlerFindSourceMapImplementation(
......
......@@ -262,5 +262,5 @@ export function renderStyledStringToErrorAnsi(string: StyledString): string {
export function isPersistentCachingEnabled(
config: NextConfigComplete
): boolean {
return config.experimental.turbo?.unstablePersistentCaching || false
return config.experimental?.turbopackPersistentCaching || false
}
......@@ -2,13 +2,11 @@
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbo: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
turbopack: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
......
......@@ -4,10 +4,8 @@ const extensions = ['', '.png', '.tsx', '.ts', '.jsx', '.js', '.json']
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbo: {
resolveExtensions: [...extensions],
},
turbopack: {
resolveExtensions: [...extensions],
},
webpack(config) {
config.resolve.extensions = [...extensions]
......
......@@ -3,12 +3,10 @@
import { nextTestSetup } from 'e2e-utils'
const nextConfig = {
experimental: {
turbo: {
resolveAlias: {
'/*': './*',
'~*': '*',
},
turbopack: {
resolveAlias: {
'/*': './*',
'~*': '*',
},
},
}
......
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
turbo: {
rules: {
'*.mdx': {
loaders: ['test-loader.js'],
as: '*.js',
},
turbopack: {
rules: {
'*.mdx': {
loaders: ['test-loader.js'],
as: '*.js',
},
},
},
......
......@@ -2,38 +2,36 @@
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbo: {
rules: {
'*.test-file.js': {
browser: {
foreign: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true, foreign: true },
},
],
},
default: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true },
},
],
},
turbopack: {
rules: {
'*.test-file.js': {
browser: {
foreign: {
loaders: [
{
loader: require.resolve('./test-file-loader.js'),
options: { browser: true, foreign: true },
},
],
},
foreign: false,
default: {
loaders: [
{
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 @@
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbo: {
rules: {
'*.test-file.ts': [require.resolve('./test-file-loader.js')],
},
turbopack: {
rules: {
'*.test-file.ts': [require.resolve('./test-file-loader.js')],
},
},
}
......
module.exports = {
experimental: {
turbo: {
resolveAlias: {
'preact/compat': 'react',
},
turbopack: {
resolveAlias: {
'preact/compat': 'react',
},
},
serverExternalPackages: [
......
......@@ -2,19 +2,19 @@
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbo: {
unstablePersistentCaching: true,
rules: {
'app/page.tsx': {
loaders: ['./my-loader.js'],
},
'pages/pages.tsx': {
loaders: ['./my-loader.js'],
},
turbopack: {
rules: {
'app/page.tsx': {
loaders: ['./my-loader.js'],
},
'pages/pages.tsx': {
loaders: ['./my-loader.js'],
},
},
},
experimental: {
turbopackPersistentCaching: true,
},
webpack(config) {
config.module.rules.push({
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 = {
bundlePagesRouterDependencies: true,
serverExternalPackages: ['opted-out-external-package'],
experimental: {
turbo: {
moduleIdStrategy:
process.env.NODE_ENV === 'production' ? 'deterministic' : undefined,
},
},
}
......@@ -12,6 +12,7 @@ function runTests() {
const jsFiles = browserFiles.filter(
(file) => file.endsWith('.js') && file.includes('/pages/')
)
expect(jsFiles).not.toBeEmpty()
jsFiles.forEach((file) => {
expect(browserFiles.includes(`${file}.map`)).toBe(true)
......
module.exports = {
experimental: {
turbo: {
unstablePersistentCaching: true
}
turbopackPersistentCaching: 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