Unverified Commit 8446b04a authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

feat(turbopack): Add an option to disable minification for builds (#72388)

### What?

Add an option to disable SWC minfiier for turbopack builds.

### Why?

Disabling minification was useful for debugging and @sokra suggested adding it as an option under `experimental.turbo`.

### How?

Closes PACK-3371
parent de17a9b1
...@@ -699,6 +699,7 @@ impl Project { ...@@ -699,6 +699,7 @@ impl Project {
self.client_compile_time_info().environment(), self.client_compile_time_info().environment(),
self.next_mode(), self.next_mode(),
self.module_id_strategy(), self.module_id_strategy(),
self.next_config().turbo_minify(self.next_mode()),
) )
} }
...@@ -716,6 +717,7 @@ impl Project { ...@@ -716,6 +717,7 @@ impl Project {
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_id_strategy(),
self.next_config().turbo_minify(self.next_mode()),
) )
} else { } else {
get_server_chunking_context( get_server_chunking_context(
...@@ -724,6 +726,7 @@ impl Project { ...@@ -724,6 +726,7 @@ impl Project {
self.node_root(), self.node_root(),
self.server_compile_time_info().environment(), self.server_compile_time_info().environment(),
self.module_id_strategy(), self.module_id_strategy(),
self.next_config().turbo_minify(self.next_mode()),
) )
} }
} }
...@@ -742,6 +745,7 @@ impl Project { ...@@ -742,6 +745,7 @@ impl Project {
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_id_strategy(),
self.next_config().turbo_minify(self.next_mode()),
) )
} else { } else {
get_edge_chunking_context( get_edge_chunking_context(
...@@ -750,6 +754,7 @@ impl Project { ...@@ -750,6 +754,7 @@ impl Project {
self.node_root(), self.node_root(),
self.edge_compile_time_info().environment(), self.edge_compile_time_info().environment(),
self.module_id_strategy(), self.module_id_strategy(),
self.next_config().turbo_minify(self.next_mode()),
) )
} }
} }
......
use turbo_tasks::TaskInput; use turbo_tasks::TaskInput;
use turbopack_core::chunk::MinifyType;
use turbopack_ecmascript_runtime::RuntimeType; use turbopack_ecmascript_runtime::RuntimeType;
/// The mode in which Next.js is running. /// The mode in which Next.js is running.
...@@ -51,13 +50,6 @@ impl NextMode { ...@@ -51,13 +50,6 @@ impl NextMode {
} }
} }
pub fn minify_type(&self) -> MinifyType {
match self {
NextMode::Development => MinifyType::NoMinify,
NextMode::Build => MinifyType::Minify,
}
}
pub fn runtime_type(&self) -> RuntimeType { pub fn runtime_type(&self) -> RuntimeType {
match self { match self {
NextMode::Development => RuntimeType::Development, NextMode::Development => RuntimeType::Development,
......
...@@ -13,7 +13,7 @@ use turbopack::{ ...@@ -13,7 +13,7 @@ use turbopack::{
}; };
use turbopack_browser::{react_refresh::assert_can_resolve_react_refresh, BrowserChunkingContext}; use turbopack_browser::{react_refresh::assert_can_resolve_react_refresh, BrowserChunkingContext};
use turbopack_core::{ use turbopack_core::{
chunk::{module_id_strategies::ModuleIdStrategy, ChunkingContext}, chunk::{module_id_strategies::ModuleIdStrategy, ChunkingContext, MinifyType},
compile_time_info::{ compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
FreeVarReference, FreeVarReferences, FreeVarReference, FreeVarReferences,
...@@ -224,7 +224,6 @@ pub async fn get_client_module_options_context( ...@@ -224,7 +224,6 @@ pub async fn get_client_module_options_context(
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
) -> Result<Vc<ModuleOptionsContext>> { ) -> Result<Vc<ModuleOptionsContext>> {
let next_mode = mode.await?; let next_mode = mode.await?;
let resolve_options_context = let resolve_options_context =
get_client_resolve_options_context(project_path, ty, mode, next_config, *execution_context); get_client_resolve_options_context(project_path, ty, mode, next_config, *execution_context);
...@@ -347,7 +346,11 @@ pub async fn get_client_module_options_context( ...@@ -347,7 +346,11 @@ pub async fn get_client_module_options_context(
enable_mdx_rs, enable_mdx_rs,
css: CssOptionsContext { css: CssOptionsContext {
use_swc_css, use_swc_css,
minify_type: next_mode.minify_type(), minify_type: if *next_config.turbo_minify(mode).await? {
MinifyType::Minify
} else {
MinifyType::NoMinify
},
..module_options_context.css ..module_options_context.css
}, },
rules: vec![ rules: vec![
...@@ -386,6 +389,7 @@ pub async fn get_client_chunking_context( ...@@ -386,6 +389,7 @@ pub async fn get_client_chunking_context(
environment: Vc<Environment>, environment: Vc<Environment>,
mode: Vc<NextMode>, mode: Vc<NextMode>,
module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>, module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
turbo_minify: Vc<bool>,
) -> Result<Vc<Box<dyn ChunkingContext>>> { ) -> Result<Vc<Box<dyn ChunkingContext>>> {
let next_mode = mode.await?; let next_mode = mode.await?;
let mut builder = BrowserChunkingContext::builder( let mut builder = BrowserChunkingContext::builder(
...@@ -398,7 +402,11 @@ pub async fn get_client_chunking_context( ...@@ -398,7 +402,11 @@ pub async fn get_client_chunking_context(
next_mode.runtime_type(), next_mode.runtime_type(),
) )
.chunk_base_path(asset_prefix) .chunk_base_path(asset_prefix)
.minify_type(next_mode.minify_type()) .minify_type(if *turbo_minify.await? {
MinifyType::Minify
} else {
MinifyType::NoMinify
})
.asset_base_path(asset_prefix) .asset_base_path(asset_prefix)
.module_id_strategy(module_id_strategy); .module_id_strategy(module_id_strategy);
......
...@@ -21,7 +21,8 @@ use turbopack_ecmascript_plugins::transform::{ ...@@ -21,7 +21,8 @@ use turbopack_ecmascript_plugins::transform::{
use turbopack_node::transforms::webpack::{WebpackLoaderItem, WebpackLoaderItems}; use turbopack_node::transforms::webpack::{WebpackLoaderItem, WebpackLoaderItems};
use crate::{ use crate::{
next_import_map::mdx_import_source_file, next_shared::transforms::ModularizeImportPackageConfig, mode::NextMode, next_import_map::mdx_import_source_file,
next_shared::transforms::ModularizeImportPackageConfig,
}; };
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -403,6 +404,7 @@ pub struct ExperimentalTurboConfig { ...@@ -403,6 +404,7 @@ pub struct ExperimentalTurboConfig {
pub use_swc_css: Option<bool>, pub use_swc_css: Option<bool>,
pub tree_shaking: Option<bool>, pub tree_shaking: Option<bool>,
pub module_id_strategy: Option<ModuleIdStrategy>, pub module_id_strategy: Option<ModuleIdStrategy>,
pub minify: Option<bool>,
} }
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs)]
...@@ -1234,6 +1236,15 @@ impl NextConfig { ...@@ -1234,6 +1236,15 @@ impl NextConfig {
}; };
Vc::cell(Some(module_id_strategy.clone())) Vc::cell(Some(module_id_strategy.clone()))
} }
#[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);
Ok(Vc::cell(
minify.unwrap_or(matches!(*mode.await?, NextMode::Build)),
))
}
} }
/// A subset of ts/jsconfig that next.js implicitly /// A subset of ts/jsconfig that next.js implicitly
......
...@@ -5,7 +5,7 @@ use turbo_tasks_fs::FileSystemPath; ...@@ -5,7 +5,7 @@ use turbo_tasks_fs::FileSystemPath;
use turbopack::resolve_options_context::ResolveOptionsContext; use turbopack::resolve_options_context::ResolveOptionsContext;
use turbopack_browser::BrowserChunkingContext; use turbopack_browser::BrowserChunkingContext;
use turbopack_core::{ use turbopack_core::{
chunk::{module_id_strategies::ModuleIdStrategy, ChunkingContext}, chunk::{module_id_strategies::ModuleIdStrategy, ChunkingContext, MinifyType},
compile_time_info::{ compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
FreeVarReference, FreeVarReferences, FreeVarReference, FreeVarReferences,
...@@ -196,6 +196,7 @@ pub async fn get_edge_chunking_context_with_client_assets( ...@@ -196,6 +196,7 @@ pub async fn get_edge_chunking_context_with_client_assets(
asset_prefix: Vc<Option<RcStr>>, asset_prefix: Vc<Option<RcStr>>,
environment: Vc<Environment>, environment: Vc<Environment>,
module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>, module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
turbo_minify: Vc<bool>,
) -> Result<Vc<Box<dyn ChunkingContext>>> { ) -> Result<Vc<Box<dyn ChunkingContext>>> {
let output_root = node_root.join("server/edge".into()); let output_root = node_root.join("server/edge".into());
let next_mode = mode.await?; let next_mode = mode.await?;
...@@ -210,7 +211,11 @@ pub async fn get_edge_chunking_context_with_client_assets( ...@@ -210,7 +211,11 @@ pub async fn get_edge_chunking_context_with_client_assets(
next_mode.runtime_type(), next_mode.runtime_type(),
) )
.asset_base_path(asset_prefix) .asset_base_path(asset_prefix)
.minify_type(next_mode.minify_type()) .minify_type(if *turbo_minify.await? {
MinifyType::Minify
} else {
MinifyType::NoMinify
})
.module_id_strategy(module_id_strategy) .module_id_strategy(module_id_strategy)
.build(), .build(),
)) ))
...@@ -223,6 +228,7 @@ pub async fn get_edge_chunking_context( ...@@ -223,6 +228,7 @@ pub async fn get_edge_chunking_context(
node_root: Vc<FileSystemPath>, node_root: Vc<FileSystemPath>,
environment: Vc<Environment>, environment: Vc<Environment>,
module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>, module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
turbo_minify: Vc<bool>,
) -> Result<Vc<Box<dyn ChunkingContext>>> { ) -> Result<Vc<Box<dyn ChunkingContext>>> {
let output_root = node_root.join("server/edge".into()); let output_root = node_root.join("server/edge".into());
let next_mode = mode.await?; let next_mode = mode.await?;
...@@ -241,7 +247,11 @@ pub async fn get_edge_chunking_context( ...@@ -241,7 +247,11 @@ pub async fn get_edge_chunking_context(
// implementation in the edge sandbox. It will respond with the // implementation in the edge sandbox. It will respond with the
// asset from the output directory. // asset from the output directory.
.asset_base_path(Vc::cell(Some("blob:server/edge/".into()))) .asset_base_path(Vc::cell(Some("blob:server/edge/".into())))
.minify_type(next_mode.minify_type()) .minify_type(if *turbo_minify.await? {
MinifyType::Minify
} else {
MinifyType::NoMinify
})
.module_id_strategy(module_id_strategy) .module_id_strategy(module_id_strategy)
.build(), .build(),
)) ))
......
...@@ -13,7 +13,7 @@ use turbopack::{ ...@@ -13,7 +13,7 @@ use turbopack::{
transition::Transition, transition::Transition,
}; };
use turbopack_core::{ use turbopack_core::{
chunk::module_id_strategies::ModuleIdStrategy, chunk::{module_id_strategies::ModuleIdStrategy, MinifyType},
compile_time_info::{ compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
FreeVarReferences, FreeVarReferences,
...@@ -944,6 +944,7 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -944,6 +944,7 @@ pub async fn get_server_chunking_context_with_client_assets(
asset_prefix: Vc<Option<RcStr>>, asset_prefix: Vc<Option<RcStr>>,
environment: Vc<Environment>, environment: Vc<Environment>,
module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>, module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
turbo_minify: Vc<bool>,
) -> Result<Vc<NodeJsChunkingContext>> { ) -> Result<Vc<NodeJsChunkingContext>> {
let next_mode = mode.await?; let next_mode = mode.await?;
// TODO(alexkirsz) This should return a trait that can be implemented by the // TODO(alexkirsz) This should return a trait that can be implemented by the
...@@ -959,7 +960,11 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -959,7 +960,11 @@ pub async fn get_server_chunking_context_with_client_assets(
next_mode.runtime_type(), next_mode.runtime_type(),
) )
.asset_prefix(asset_prefix) .asset_prefix(asset_prefix)
.minify_type(next_mode.minify_type()) .minify_type(if *turbo_minify.await? {
MinifyType::Minify
} else {
MinifyType::NoMinify
})
.module_id_strategy(module_id_strategy); .module_id_strategy(module_id_strategy);
if next_mode.is_development() { if next_mode.is_development() {
...@@ -975,6 +980,7 @@ pub async fn get_server_chunking_context( ...@@ -975,6 +980,7 @@ pub async fn get_server_chunking_context(
node_root: Vc<FileSystemPath>, node_root: Vc<FileSystemPath>,
environment: Vc<Environment>, environment: Vc<Environment>,
module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>, module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
turbo_minify: Vc<bool>,
) -> Result<Vc<NodeJsChunkingContext>> { ) -> Result<Vc<NodeJsChunkingContext>> {
let next_mode = mode.await?; let next_mode = mode.await?;
// TODO(alexkirsz) This should return a trait that can be implemented by the // TODO(alexkirsz) This should return a trait that can be implemented by the
...@@ -989,7 +995,11 @@ pub async fn get_server_chunking_context( ...@@ -989,7 +995,11 @@ pub async fn get_server_chunking_context(
environment, environment,
next_mode.runtime_type(), next_mode.runtime_type(),
) )
.minify_type(next_mode.minify_type()) .minify_type(if *turbo_minify.await? {
MinifyType::Minify
} else {
MinifyType::NoMinify
})
.module_id_strategy(module_id_strategy); .module_id_strategy(module_id_strategy);
if next_mode.is_development() { if next_mode.is_development() {
......
...@@ -411,6 +411,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -411,6 +411,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
.optional(), .optional(),
memoryLimit: z.number().optional(), memoryLimit: z.number().optional(),
moduleIdStrategy: z.enum(['named', 'deterministic']).optional(), moduleIdStrategy: z.enum(['named', 'deterministic']).optional(),
minify: z.boolean().optional(),
}) })
.optional(), .optional(),
optimizePackageImports: z.array(z.string()).optional(), optimizePackageImports: z.array(z.string()).optional(),
......
...@@ -179,6 +179,11 @@ export interface ExperimentalTurboOptions { ...@@ -179,6 +179,11 @@ export interface ExperimentalTurboOptions {
* directory can be resolved by turbopack. * directory can be resolved by turbopack.
*/ */
root?: string root?: string
/**
* Enable minification. Defaults to true in build mode and false in dev mode.
*/
minify?: boolean
} }
export interface WebpackConfigContext { export interface WebpackConfigContext {
......
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