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::{
};
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,
......@@ -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
......@@ -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
......
......@@ -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,
......@@ -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
......@@ -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
......
......@@ -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,
......@@ -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
......@@ -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
......
......@@ -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)({
......
......@@ -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,
......@@ -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,
}
}
......@@ -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);
......
......@@ -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,
......@@ -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),
}
}
}
......
......@@ -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},
......
......@@ -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()?;
......@@ -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 {
......@@ -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()
......
......@@ -2,7 +2,7 @@
"runtime": "NodeJs",
"minifyType": {
"Minify": {
"mangle": true
"mangle": "optimal-size"
}
}
}
{
"minifyType": {
"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