Unverified Commit 66b6fa15 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

feat: Disable char frequency analysis for mangler (#77887)

### What?

Disable character frequency analysis for server-side builds.

### Why?


https://vercel.slack.com/archives/C05KYT5S9FF/p1742935766157509?thread_ts=1742255598.708199&cid=C05KYT5S9FF

### How?

Closes NAR-118

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 76824e75
...@@ -19,8 +19,8 @@ use turbopack_browser::{ ...@@ -19,8 +19,8 @@ use turbopack_browser::{
}; };
use turbopack_core::{ use turbopack_core::{
chunk::{ chunk::{
module_id_strategies::ModuleIdStrategy, ChunkingConfig, ChunkingContext, MinifyType, module_id_strategies::ModuleIdStrategy, ChunkingConfig, ChunkingContext, MangleType,
SourceMapsType, MinifyType, SourceMapsType,
}, },
compile_time_info::{ compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
...@@ -397,7 +397,7 @@ pub async fn get_client_module_options_context( ...@@ -397,7 +397,7 @@ pub async fn get_client_module_options_context(
css: CssOptionsContext { css: CssOptionsContext {
minify_type: if *next_config.turbo_minify(mode).await? { minify_type: if *next_config.turbo_minify(mode).await? {
MinifyType::Minify { MinifyType::Minify {
mangle: !*no_mangling.await?, mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
} }
} else { } else {
MinifyType::NoMinify MinifyType::NoMinify
...@@ -454,7 +454,7 @@ pub async fn get_client_chunking_context( ...@@ -454,7 +454,7 @@ pub async fn get_client_chunking_context(
.chunk_suffix_path(chunk_suffix_path) .chunk_suffix_path(chunk_suffix_path)
.minify_type(if *minify.await? { .minify_type(if *minify.await? {
MinifyType::Minify { MinifyType::Minify {
mangle: !*no_mangling.await?, mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
} }
} else { } else {
MinifyType::NoMinify MinifyType::NoMinify
......
...@@ -7,8 +7,8 @@ use turbopack::{css::chunk::CssChunkType, resolve_options_context::ResolveOption ...@@ -7,8 +7,8 @@ use turbopack::{css::chunk::CssChunkType, resolve_options_context::ResolveOption
use turbopack_browser::BrowserChunkingContext; use turbopack_browser::BrowserChunkingContext;
use turbopack_core::{ use turbopack_core::{
chunk::{ chunk::{
module_id_strategies::ModuleIdStrategy, ChunkingConfig, ChunkingContext, MinifyType, module_id_strategies::ModuleIdStrategy, ChunkingConfig, ChunkingContext, MangleType,
SourceMapsType, MinifyType, SourceMapsType,
}, },
compile_time_info::{ compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
...@@ -241,7 +241,8 @@ pub async fn get_edge_chunking_context_with_client_assets( ...@@ -241,7 +241,8 @@ pub async fn get_edge_chunking_context_with_client_assets(
.asset_base_path(asset_prefix) .asset_base_path(asset_prefix)
.minify_type(if *turbo_minify.await? { .minify_type(if *turbo_minify.await? {
MinifyType::Minify { MinifyType::Minify {
mangle: !*no_mangling.await?, // React needs deterministic function names to work correctly.
mangle: (!*no_mangling.await?).then_some(MangleType::Deterministic),
} }
} else { } else {
MinifyType::NoMinify MinifyType::NoMinify
...@@ -304,7 +305,7 @@ pub async fn get_edge_chunking_context( ...@@ -304,7 +305,7 @@ pub async fn get_edge_chunking_context(
.asset_base_path(ResolvedVc::cell(Some("blob:server/edge/".into()))) .asset_base_path(ResolvedVc::cell(Some("blob:server/edge/".into())))
.minify_type(if *turbo_minify.await? { .minify_type(if *turbo_minify.await? {
MinifyType::Minify { MinifyType::Minify {
mangle: !*no_mangling.await?, mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
} }
} else { } else {
MinifyType::NoMinify MinifyType::NoMinify
......
...@@ -15,7 +15,10 @@ use turbopack::{ ...@@ -15,7 +15,10 @@ use turbopack::{
transition::Transition, transition::Transition,
}; };
use turbopack_core::{ use turbopack_core::{
chunk::{module_id_strategies::ModuleIdStrategy, ChunkingConfig, MinifyType, SourceMapsType}, chunk::{
module_id_strategies::ModuleIdStrategy, ChunkingConfig, MangleType, MinifyType,
SourceMapsType,
},
compile_time_info::{ compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
FreeVarReferences, FreeVarReferences,
...@@ -1015,7 +1018,8 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -1015,7 +1018,8 @@ pub async fn get_server_chunking_context_with_client_assets(
.asset_prefix(asset_prefix) .asset_prefix(asset_prefix)
.minify_type(if *turbo_minify.await? { .minify_type(if *turbo_minify.await? {
MinifyType::Minify { MinifyType::Minify {
mangle: !*no_mangling.await?, // React needs deterministic function names to work correctly.
mangle: (!*no_mangling.await?).then_some(MangleType::Deterministic),
} }
} else { } else {
MinifyType::NoMinify MinifyType::NoMinify
...@@ -1080,7 +1084,7 @@ pub async fn get_server_chunking_context( ...@@ -1080,7 +1084,7 @@ pub async fn get_server_chunking_context(
) )
.minify_type(if *turbo_minify.await? { .minify_type(if *turbo_minify.await? {
MinifyType::Minify { MinifyType::Minify {
mangle: !*no_mangling.await?, mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
} }
} else { } else {
MinifyType::NoMinify MinifyType::NoMinify
......
...@@ -1187,7 +1187,10 @@ export default async function getBaseWebpackConfig( ...@@ -1187,7 +1187,10 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE': false, 'process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE': false,
}, },
}, },
mangle: !noMangling && { reserved: ['AbortSignal'] }, mangle: !noMangling && {
reserved: ['AbortSignal'],
disableCharFreq: !isClient,
},
}, },
}), }),
new (getRspackCore().LightningCssMinimizerRspackPlugin)({ new (getRspackCore().LightningCssMinimizerRspackPlugin)({
......
...@@ -27,7 +27,7 @@ use turbopack_core::{ ...@@ -27,7 +27,7 @@ use turbopack_core::{
asset::Asset, asset::Asset,
chunk::{ chunk::{
availability_info::AvailabilityInfo, ChunkingConfig, ChunkingContext, EvaluatableAsset, availability_info::AvailabilityInfo, ChunkingConfig, ChunkingContext, EvaluatableAsset,
EvaluatableAssets, MinifyType, SourceMapsType, EvaluatableAssets, MangleType, MinifyType, SourceMapsType,
}, },
environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment}, environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment},
ident::AssetIdent, ident::AssetIdent,
...@@ -93,7 +93,9 @@ impl TurbopackBuildBuilder { ...@@ -93,7 +93,9 @@ impl TurbopackBuildBuilder {
show_all: false, show_all: false,
log_detail: false, log_detail: false,
source_maps_type: SourceMapsType::Full, source_maps_type: SourceMapsType::Full,
minify_type: MinifyType::Minify { mangle: true }, minify_type: MinifyType::Minify {
mangle: Some(MangleType::OptimalSize),
},
target: Target::Node, target: Target::Node,
} }
} }
...@@ -522,7 +524,9 @@ pub async fn build(args: &BuildArguments) -> Result<()> { ...@@ -522,7 +524,9 @@ pub async fn build(args: &BuildArguments) -> Result<()> {
.minify_type(if args.no_minify { .minify_type(if args.no_minify {
MinifyType::NoMinify MinifyType::NoMinify
} else { } else {
MinifyType::Minify { mangle: true } MinifyType::Minify {
mangle: Some(MangleType::OptimalSize),
}
}) })
.target(args.common.target.unwrap_or(Target::Node)) .target(args.common.target.unwrap_or(Target::Node))
.show_all(args.common.show_all); .show_all(args.common.show_all);
......
...@@ -17,6 +17,26 @@ use crate::{ ...@@ -17,6 +17,26 @@ use crate::{
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
}; };
#[derive(
Debug,
TaskInput,
Clone,
Copy,
PartialEq,
Eq,
Hash,
Serialize,
Deserialize,
TraceRawVcs,
DeterministicHash,
NonLocalValue,
)]
#[serde(rename_all = "kebab-case")]
pub enum MangleType {
OptimalSize,
Deterministic,
}
#[derive( #[derive(
Debug, Debug,
TaskInput, TaskInput,
...@@ -32,13 +52,17 @@ use crate::{ ...@@ -32,13 +52,17 @@ use crate::{
NonLocalValue, NonLocalValue,
)] )]
pub enum MinifyType { pub enum MinifyType {
Minify { mangle: bool }, // TODO instead of adding a new property here,
// refactor that to Minify(MinifyOptions) to allow defaults on MinifyOptions
Minify { mangle: Option<MangleType> },
NoMinify, NoMinify,
} }
impl Default for MinifyType { impl Default for MinifyType {
fn default() -> Self { fn default() -> Self {
Self::Minify { mangle: true } Self::Minify {
mangle: Some(MangleType::OptimalSize),
}
} }
} }
......
...@@ -29,7 +29,7 @@ pub use self::{ ...@@ -29,7 +29,7 @@ pub use self::{
}, },
chunking_context::{ chunking_context::{
ChunkGroupResult, ChunkGroupType, ChunkingConfig, ChunkingConfigs, ChunkingContext, ChunkGroupResult, ChunkGroupType, ChunkingConfig, ChunkingConfigs, ChunkingContext,
ChunkingContextExt, EntryChunkGroupResult, MinifyType, SourceMapsType, ChunkingContextExt, EntryChunkGroupResult, MangleType, MinifyType, SourceMapsType,
}, },
data::{ChunkData, ChunkDataOption, ChunksData}, data::{ChunkData, ChunkDataOption, ChunksData},
evaluate::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets}, evaluate::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
......
...@@ -23,12 +23,15 @@ use swc_core::{ ...@@ -23,12 +23,15 @@ use swc_core::{
}, },
}; };
use tracing::{instrument, Level}; use tracing::{instrument, Level};
use turbopack_core::code_builder::{Code, CodeBuilder}; use turbopack_core::{
chunk::MangleType,
code_builder::{Code, CodeBuilder},
};
use crate::parse::generate_js_source_map; use crate::parse::generate_js_source_map;
#[instrument(level = Level::INFO, skip_all)] #[instrument(level = Level::INFO, skip_all)]
pub fn minify(code: &Code, source_maps: bool, mangle: bool) -> Result<Code> { pub fn minify(code: &Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> {
let source_maps = source_maps let source_maps = source_maps
.then(|| code.generate_source_map_ref()) .then(|| code.generate_source_map_ref())
.transpose()?; .transpose()?;
...@@ -84,14 +87,20 @@ pub fn minify(code: &Code, source_maps: bool, mangle: bool) -> Result<Code> { ...@@ -84,14 +87,20 @@ pub fn minify(code: &Code, source_maps: bool, mangle: bool) -> Result<Code> {
passes: 2, passes: 2,
..Default::default() ..Default::default()
}), }),
mangle: if mangle { mangle: mangle.map(|mangle| {
Some(MangleOptions { let reserved = vec!["AbortSignal".into()];
reserved: vec!["AbortSignal".into()], match mangle {
..Default::default() MangleType::OptimalSize => MangleOptions {
}) reserved,
} else { ..Default::default()
None },
}, MangleType::Deterministic => MangleOptions {
reserved,
disable_char_freq: true,
..Default::default()
},
}
}),
..Default::default() ..Default::default()
}, },
&ExtraOptions { &ExtraOptions {
...@@ -101,7 +110,7 @@ pub fn minify(code: &Code, source_maps: bool, mangle: bool) -> Result<Code> { ...@@ -101,7 +110,7 @@ pub fn minify(code: &Code, source_maps: bool, mangle: bool) -> Result<Code> {
}, },
); );
if !mangle { if mangle.is_none() {
program.mutate(hygiene_with_config(hygiene::Config { program.mutate(hygiene_with_config(hygiene::Config {
top_level_mark, top_level_mark,
..Default::default() ..Default::default()
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"runtime": "NodeJs", "runtime": "NodeJs",
"minifyType": { "minifyType": {
"Minify": { "Minify": {
"mangle": true "mangle": "optimal-size"
} }
} }
} }
{ {
"minifyType": { "minifyType": {
"Minify": { "Minify": {
"mangle": true "mangle": "optimal-size"
} }
} }
} }
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