Unverified Commit 10dc5fd5 authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Support `experimental.serverMinification` & expand…

[turbopack] Support `experimental.serverMinification` & expand `experimental.turbopackMinify` (#96578)

Closes https://github.com/vercel/next.js/issues/96574.

Implements:

> experimental.turbopackMinify accepts per-environment granularity, e.g.
{ server: false, client: true } (restoring serverMinification parity)

I also added the edge runtime in there, even though it isn't too widely
used.
parent de05823a
...@@ -1624,7 +1624,7 @@ impl Project { ...@@ -1624,7 +1624,7 @@ impl Project {
module_id_strategy: self.module_ids(), module_id_strategy: self.module_ids(),
export_usage: self.export_usage(), export_usage: self.export_usage(),
unused_references: self.unused_references(), unused_references: self.unused_references(),
minify: self.next_config().turbo_minify(self.next_mode()), minify: self.next_config().turbo_client_minify(self.next_mode()),
source_maps: self.next_config().client_source_maps(self.next_mode()), source_maps: self.next_config().client_source_maps(self.next_mode()),
no_mangling: self.no_mangling(), no_mangling: self.no_mangling(),
scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()), scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()),
...@@ -1663,7 +1663,7 @@ impl Project { ...@@ -1663,7 +1663,7 @@ impl Project {
output_root: self.node_root().owned().await?, output_root: self.node_root().owned().await?,
output_root_to_root_path: self.node_root_to_root_path().owned().await?, output_root_to_root_path: self.node_root_to_root_path().owned().await?,
environment: self.client_compile_time_info().environment(), environment: self.client_compile_time_info().environment(),
minify: self.next_config().turbo_minify(self.next_mode()), minify: self.next_config().turbo_client_minify(self.next_mode()),
source_maps: self.next_config().client_source_maps(self.next_mode()), source_maps: self.next_config().client_source_maps(self.next_mode()),
no_mangling: self.no_mangling(), no_mangling: self.no_mangling(),
hash_salt: self.next_config().output_hash_salt().to_resolved().await?, hash_salt: self.next_config().output_hash_salt().to_resolved().await?,
...@@ -1713,7 +1713,7 @@ impl Project { ...@@ -1713,7 +1713,7 @@ impl Project {
module_id_strategy: self.module_ids(), module_id_strategy: self.module_ids(),
export_usage: self.export_usage(), export_usage: self.export_usage(),
unused_references: self.unused_references(), unused_references: self.unused_references(),
minify: self.next_config().turbo_minify(self.next_mode()), minify: self.next_config().turbo_server_minify(self.next_mode()),
source_maps: self.next_config().server_source_maps(), source_maps: self.next_config().server_source_maps(),
no_mangling: self.no_mangling(), no_mangling: self.no_mangling(),
scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()), scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()),
...@@ -1755,7 +1755,7 @@ impl Project { ...@@ -1755,7 +1755,7 @@ impl Project {
module_id_strategy: self.module_ids(), module_id_strategy: self.module_ids(),
export_usage: self.export_usage(), export_usage: self.export_usage(),
unused_references: self.unused_references(), unused_references: self.unused_references(),
turbo_minify: self.next_config().turbo_minify(self.next_mode()), turbo_minify: self.next_config().turbo_edge_minify(self.next_mode()),
turbo_source_maps: self.next_config().server_source_maps(), turbo_source_maps: self.next_config().server_source_maps(),
no_mangling: self.no_mangling(), no_mangling: self.no_mangling(),
scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()), scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()),
......
...@@ -1385,7 +1385,7 @@ pub struct ExperimentalConfig { ...@@ -1385,7 +1385,7 @@ pub struct ExperimentalConfig {
webpack_build_worker: Option<bool>, webpack_build_worker: Option<bool>,
worker_threads: Option<bool>, worker_threads: Option<bool>,
turbopack_minify: Option<bool>, turbopack_minify: Option<TurbopackMinify>,
turbopack_module_ids: Option<ModuleIds>, turbopack_module_ids: Option<ModuleIds>,
turbopack_plugin_runtime_strategy: Option<TurbopackPluginRuntimeStrategy>, turbopack_plugin_runtime_strategy: Option<TurbopackPluginRuntimeStrategy>,
turbopack_source_maps: Option<bool>, turbopack_source_maps: Option<bool>,
...@@ -1640,6 +1640,47 @@ impl ReactRemoveProperties { ...@@ -1640,6 +1640,47 @@ impl ReactRemoveProperties {
} }
} }
/// `experimental.turbopackMinify`, either a single value for all output or a
/// per-environment configuration.
#[derive(
Clone, Debug, PartialEq, Deserialize, TraceRawVcs, NonLocalValue, OperationValue, Encode, Decode,
)]
#[serde(untagged)]
pub enum TurbopackMinify {
Boolean(bool),
Config {
server: Option<bool>,
client: Option<bool>,
edge: Option<bool>,
},
}
impl TurbopackMinify {
/// The configured value for browser output.
fn client(&self) -> Option<bool> {
match self {
Self::Boolean(enabled) => Some(*enabled),
Self::Config { client, .. } => *client,
}
}
/// The configured value for Node.js server output.
fn server(&self) -> Option<bool> {
match self {
Self::Boolean(enabled) => Some(*enabled),
Self::Config { server, .. } => *server,
}
}
/// The configured value for edge output.
fn edge(&self) -> Option<bool> {
match self {
Self::Boolean(enabled) => Some(*enabled),
Self::Config { edge, .. } => *edge,
}
}
}
#[derive( #[derive(
Clone, Debug, PartialEq, Deserialize, TraceRawVcs, NonLocalValue, OperationValue, Encode, Decode, Clone, Debug, PartialEq, Deserialize, TraceRawVcs, NonLocalValue, OperationValue, Encode, Decode,
)] )]
...@@ -2524,12 +2565,43 @@ impl NextConfig { ...@@ -2524,12 +2565,43 @@ impl NextConfig {
}) })
} }
/// Whether to minify browser output.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn turbo_minify(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> { pub async fn turbo_client_minify(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> {
let minify = self.experimental.turbopack_minify; let default = matches!(*mode.await?, NextMode::Build);
Ok(Vc::cell( let minify = self
minify.unwrap_or(matches!(*mode.await?, NextMode::Build)), .experimental
)) .turbopack_minify
.as_ref()
.and_then(TurbopackMinify::client);
Ok(Vc::cell(minify.unwrap_or(default)))
}
/// Whether to minify Node.js server output. `turbopackMinify` takes
/// precedence over `serverMinification`, which can only opt out.
#[turbo_tasks::function]
pub async fn turbo_server_minify(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> {
let default = matches!(*mode.await?, NextMode::Build)
&& self.experimental.server_minification.unwrap_or(true);
let minify = self
.experimental
.turbopack_minify
.as_ref()
.and_then(TurbopackMinify::server);
Ok(Vc::cell(minify.unwrap_or(default)))
}
/// Whether to minify edge output. `serverMinification` only covers the
/// Node.js server, matching webpack.
#[turbo_tasks::function]
pub async fn turbo_edge_minify(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> {
let default = matches!(*mode.await?, NextMode::Build);
let minify = self
.experimental
.turbopack_minify
.as_ref()
.and_then(TurbopackMinify::edge);
Ok(Vc::cell(minify.unwrap_or(default)))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -411,7 +411,7 @@ Additionally, the following experimental options are available under `experiment ...@@ -411,7 +411,7 @@ Additionally, the following experimental options are available under `experiment
| ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ----------------------------- | | ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ----------------------------- |
| [`turbopackFileSystemCacheForDev`](/docs/app/api-reference/config/next-config-js/turbopackFileSystemCache) | Enable filesystem cache for the dev server. | `true` | N/A | | [`turbopackFileSystemCacheForDev`](/docs/app/api-reference/config/next-config-js/turbopackFileSystemCache) | Enable filesystem cache for the dev server. | `true` | N/A |
| [`turbopackFileSystemCacheForBuild`](/docs/app/api-reference/config/next-config-js/turbopackFileSystemCache) | Enable filesystem cache for builds. | N/A | `true`<sup>1</sup> | | [`turbopackFileSystemCacheForBuild`](/docs/app/api-reference/config/next-config-js/turbopackFileSystemCache) | Enable filesystem cache for builds. | N/A | `true`<sup>1</sup> |
| `turbopackMinify` | Enable minification. | `false` | `true` | | `turbopackMinify` | Enable minification. Accepts a boolean, or `{ server, client, edge }` to configure each environment separately. | `false` | `true` |
| `turbopackSourceMaps` | Enable source maps. | `true` | `productionBrowserSourceMaps` | | `turbopackSourceMaps` | Enable source maps. | `true` | `productionBrowserSourceMaps` |
| `turbopackInputSourceMaps` | Enable extraction of source maps from input files. | `true` | `true` | | `turbopackInputSourceMaps` | Enable extraction of source maps from input files. | `true` | `true` |
| `turbopackModuleFragments` | Currently in active development. This splits modules into fragments and chunks only import the used fragments of the modules. | `false` | `false` | | `turbopackModuleFragments` | Currently in active development. This splits modules into fragments and chunks only import the used fragments of the modules. | `false` | `false` |
......
...@@ -374,7 +374,16 @@ export const experimentalSchema = { ...@@ -374,7 +374,16 @@ export const experimentalSchema = {
turbopackPluginRuntimeStrategy: z turbopackPluginRuntimeStrategy: z
.enum(['workerThreads', 'childProcesses']) .enum(['workerThreads', 'childProcesses'])
.optional(), .optional(),
turbopackMinify: z.boolean().optional(), turbopackMinify: z
.union([
z.boolean(),
z.strictObject({
server: z.boolean().optional(),
client: z.boolean().optional(),
edge: z.boolean().optional(),
}),
])
.optional(),
turbopackFileSystemCacheForDev: z.boolean().optional(), turbopackFileSystemCacheForDev: z.boolean().optional(),
turbopackFileSystemCacheForBuild: z.boolean().optional(), turbopackFileSystemCacheForBuild: z.boolean().optional(),
turbopackSeedCacheFromWorktree: z.boolean().optional(), turbopackSeedCacheFromWorktree: z.boolean().optional(),
......
...@@ -769,8 +769,18 @@ export interface ExperimentalConfig { ...@@ -769,8 +769,18 @@ export interface ExperimentalConfig {
/** /**
* Enable minification. Defaults to true in build mode and false in dev mode. * Enable minification. Defaults to true in build mode and false in dev mode.
*
* Pass an object to configure each environment separately, e.g.
* `{ server: false, client: true }`. The `server` option takes precedence
* over `experimental.serverMinification`.
*
* We don't recommend disabling minification in production. Disabling it
* increases server function size, slows down cold starts, and leads to
* degraded performance.
*/ */
turbopackMinify?: boolean turbopackMinify?:
| boolean
| { server?: boolean; client?: boolean; edge?: boolean }
/** /**
* Enable support for `with {type: "bytes"}` for ESM imports. * Enable support for `with {type: "bytes"}` for ESM imports.
...@@ -1153,6 +1163,9 @@ export interface ExperimentalConfig { ...@@ -1153,6 +1163,9 @@ export interface ExperimentalConfig {
/** /**
* enables the minification of server code. * enables the minification of server code.
*
* Under Turbopack this is overridden by `experimental.turbopackMinify` when
* that option specifies a `server` value.
*/ */
serverMinification?: boolean serverMinification?: boolean
......
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