Skip to content

feat: Disable char frequency analysis for mangler #77887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use turbopack_browser::{
};
use turbopack_core::{
chunk::{
module_id_strategies::ModuleIdStrategy, ChunkingConfig, ChunkingContext, MinifyType,
SourceMapsType,
module_id_strategies::ModuleIdStrategy, ChunkingConfig, ChunkingContext, MangleType,
MinifyType, SourceMapsType,
},
compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
Expand Down Expand Up @@ -397,7 +397,7 @@ pub async fn get_client_module_options_context(
css: CssOptionsContext {
minify_type: if *next_config.turbo_minify(mode).await? {
MinifyType::Minify {
mangle: !*no_mangling.await?,
mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
}
} else {
MinifyType::NoMinify
Expand Down Expand Up @@ -454,7 +454,7 @@ pub async fn get_client_chunking_context(
.chunk_suffix_path(chunk_suffix_path)
.minify_type(if *minify.await? {
MinifyType::Minify {
mangle: !*no_mangling.await?,
mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
}
} else {
MinifyType::NoMinify
Expand Down
9 changes: 5 additions & 4 deletions crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use turbopack::{css::chunk::CssChunkType, resolve_options_context::ResolveOption
use turbopack_browser::BrowserChunkingContext;
use turbopack_core::{
chunk::{
module_id_strategies::ModuleIdStrategy, ChunkingConfig, ChunkingContext, MinifyType,
SourceMapsType,
module_id_strategies::ModuleIdStrategy, ChunkingConfig, ChunkingContext, MangleType,
MinifyType, SourceMapsType,
},
compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
Expand Down Expand Up @@ -241,7 +241,8 @@ pub async fn get_edge_chunking_context_with_client_assets(
.asset_base_path(asset_prefix)
.minify_type(if *turbo_minify.await? {
MinifyType::Minify {
mangle: !*no_mangling.await?,
// React needs deterministic function names to work correctly.
mangle: (!*no_mangling.await?).then_some(MangleType::Deterministic),
}
} else {
MinifyType::NoMinify
Expand Down Expand Up @@ -304,7 +305,7 @@ pub async fn get_edge_chunking_context(
.asset_base_path(ResolvedVc::cell(Some("blob:server/edge/".into())))
.minify_type(if *turbo_minify.await? {
MinifyType::Minify {
mangle: !*no_mangling.await?,
mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
}
} else {
MinifyType::NoMinify
Expand Down
10 changes: 7 additions & 3 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use turbopack::{
transition::Transition,
};
use turbopack_core::{
chunk::{module_id_strategies::ModuleIdStrategy, ChunkingConfig, MinifyType, SourceMapsType},
chunk::{
module_id_strategies::ModuleIdStrategy, ChunkingConfig, MangleType, MinifyType,
SourceMapsType,
},
compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment,
FreeVarReferences,
Expand Down Expand Up @@ -1015,7 +1018,8 @@ pub async fn get_server_chunking_context_with_client_assets(
.asset_prefix(asset_prefix)
.minify_type(if *turbo_minify.await? {
MinifyType::Minify {
mangle: !*no_mangling.await?,
// React needs deterministic function names to work correctly.
mangle: (!*no_mangling.await?).then_some(MangleType::Deterministic),
}
} else {
MinifyType::NoMinify
Expand Down Expand Up @@ -1080,7 +1084,7 @@ pub async fn get_server_chunking_context(
)
.minify_type(if *turbo_minify.await? {
MinifyType::Minify {
mangle: !*no_mangling.await?,
mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
}
} else {
MinifyType::NoMinify
Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,10 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE': false,
},
},
mangle: !noMangling && { reserved: ['AbortSignal'] },
mangle: !noMangling && {
reserved: ['AbortSignal'],
disableCharFreq: !isClient,
},
},
}),
new (getRspackCore().LightningCssMinimizerRspackPlugin)({
Expand Down
10 changes: 7 additions & 3 deletions turbopack/crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use turbopack_core::{
asset::Asset,
chunk::{
availability_info::AvailabilityInfo, ChunkingConfig, ChunkingContext, EvaluatableAsset,
EvaluatableAssets, MinifyType, SourceMapsType,
EvaluatableAssets, MangleType, MinifyType, SourceMapsType,
},
environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment},
ident::AssetIdent,
Expand Down Expand Up @@ -93,7 +93,9 @@ impl TurbopackBuildBuilder {
show_all: false,
log_detail: false,
source_maps_type: SourceMapsType::Full,
minify_type: MinifyType::Minify { mangle: true },
minify_type: MinifyType::Minify {
mangle: Some(MangleType::OptimalSize),
},
target: Target::Node,
}
}
Expand Down Expand Up @@ -522,7 +524,9 @@ pub async fn build(args: &BuildArguments) -> Result<()> {
.minify_type(if args.no_minify {
MinifyType::NoMinify
} else {
MinifyType::Minify { mangle: true }
MinifyType::Minify {
mangle: Some(MangleType::OptimalSize),
}
})
.target(args.common.target.unwrap_or(Target::Node))
.show_all(args.common.show_all);
Expand Down
28 changes: 26 additions & 2 deletions turbopack/crates/turbopack-core/src/chunk/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ use crate::{
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(
Debug,
TaskInput,
Expand All @@ -32,13 +52,17 @@ use crate::{
NonLocalValue,
)]
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,
}

impl Default for MinifyType {
fn default() -> Self {
Self::Minify { mangle: true }
Self::Minify {
mangle: Some(MangleType::OptimalSize),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use self::{
},
chunking_context::{
ChunkGroupResult, ChunkGroupType, ChunkingConfig, ChunkingConfigs, ChunkingContext,
ChunkingContextExt, EntryChunkGroupResult, MinifyType, SourceMapsType,
ChunkingContextExt, EntryChunkGroupResult, MangleType, MinifyType, SourceMapsType,
},
data::{ChunkData, ChunkDataOption, ChunksData},
evaluate::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
Expand Down
31 changes: 20 additions & 11 deletions turbopack/crates/turbopack-ecmascript/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ use swc_core::{
},
};
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;

#[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
.then(|| code.generate_source_map_ref())
.transpose()?;
Expand Down Expand Up @@ -84,14 +87,20 @@ pub fn minify(code: &Code, source_maps: bool, mangle: bool) -> Result<Code> {
passes: 2,
..Default::default()
}),
mangle: if mangle {
Some(MangleOptions {
reserved: vec!["AbortSignal".into()],
..Default::default()
})
} else {
None
},
mangle: mangle.map(|mangle| {
let reserved = vec!["AbortSignal".into()];
match mangle {
MangleType::OptimalSize => MangleOptions {
reserved,
..Default::default()
},
MangleType::Deterministic => MangleOptions {
reserved,
disable_char_freq: true,
..Default::default()
},
}
}),
..Default::default()
},
&ExtraOptions {
Expand All @@ -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 {
top_level_mark,
..Default::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"runtime": "NodeJs",
"minifyType": {
"Minify": {
"mangle": true
"mangle": "optimal-size"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"minifyType": {
"Minify": {
"mangle": true
"mangle": "optimal-size"
}
}
}
Loading