Skip to content

Commit 142dfc3

Browse files
committed
feat(pack): support minify.extractComments
1 parent 72feee7 commit 142dfc3

File tree

8 files changed

+148
-12
lines changed

8 files changed

+148
-12
lines changed

crates/pack-core/src/client/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ use crate::{
4949
resolve::externals_plugin::ExternalsPlugin,
5050
transforms::{
5151
dynamic_import_to_require::get_dynamic_import_to_require_rule,
52-
emotion::get_emotion_transform_rule, remove_console::get_remove_console_transform_rule,
52+
emotion::get_emotion_transform_rule,
53+
remove_console::get_remove_console_transform_rule,
5354
styled_components::get_styled_components_transform_rule,
5455
styled_jsx::get_styled_jsx_transform_rule,
5556
swc_ecma_transform_plugins::get_swc_ecma_transform_plugin_rule,
@@ -461,8 +462,10 @@ pub async fn get_client_chunking_context(
461462
runtime_type,
462463
)
463464
.minify_type(if mode.is_production() && *minify.await? {
465+
let minify_config = config.minify_config().await?;
464466
MinifyType::Minify {
465467
mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
468+
extract_comments: minify_config.extract_comments(),
466469
}
467470
} else {
468471
MinifyType::NoMinify

crates/pack-core/src/config.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ pub struct OptimizationConfig {
280280
/// local names for variables, functions etc., which can be useful for
281281
/// debugging/profiling purposes.
282282
pub no_mangling: Option<bool>,
283-
pub minify: Option<bool>,
283+
pub minify: Option<MinifyConfig>,
284284
pub tree_shaking: Option<bool>,
285285
pub package_imports: Option<Vec<RcStr>>,
286286
pub modularize_imports: Option<FxIndexMap<String, ModularizeImportPackageConfig>>,
@@ -691,6 +691,43 @@ pub struct OptionServerActions(Option<ServerActions>);
691691
#[turbo_tasks::value(transparent)]
692692
pub struct ExternalsConfig(FxIndexMap<RcStr, ExternalConfig>);
693693

694+
#[turbo_tasks::value(transparent)]
695+
pub struct MinifyConfigValue(MinifyConfig);
696+
697+
#[derive(
698+
Clone, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, OperationValue,
699+
)]
700+
#[serde(untagged)]
701+
pub enum MinifyConfig {
702+
Boolean(bool),
703+
Config {
704+
#[serde(default)]
705+
extract_comments: bool,
706+
},
707+
}
708+
709+
impl Default for MinifyConfig {
710+
fn default() -> Self {
711+
MinifyConfig::Boolean(false)
712+
}
713+
}
714+
715+
impl MinifyConfig {
716+
pub fn is_enabled(&self) -> bool {
717+
match self {
718+
Self::Boolean(enabled) => *enabled,
719+
Self::Config { .. } => true,
720+
}
721+
}
722+
723+
pub fn extract_comments(&self) -> bool {
724+
match self {
725+
Self::Boolean(_) => false,
726+
Self::Config { extract_comments } => *extract_comments,
727+
}
728+
}
729+
}
730+
694731
#[turbo_tasks::value_impl]
695732
impl Config {
696733
#[turbo_tasks::function]
@@ -1135,13 +1172,26 @@ impl Config {
11351172
let minify = self
11361173
.optimization
11371174
.as_ref()
1138-
.map(|op| op.minify.is_none_or(|minify| minify));
1175+
.and_then(|op| op.minify.as_ref())
1176+
.map(|minify| minify.is_enabled());
11391177

11401178
Ok(Vc::cell(
11411179
minify.unwrap_or(matches!(*mode.await?, Mode::Production)),
11421180
))
11431181
}
11441182

1183+
#[turbo_tasks::function]
1184+
pub fn minify_config(&self) -> Vc<MinifyConfigValue> {
1185+
let minify_config = self
1186+
.optimization
1187+
.as_ref()
1188+
.and_then(|op| op.minify.as_ref())
1189+
.cloned()
1190+
.unwrap_or_default();
1191+
1192+
MinifyConfigValue(minify_config).cell()
1193+
}
1194+
11451195
#[turbo_tasks::function]
11461196
pub fn no_mangling(&self) -> Vc<bool> {
11471197
Vc::cell(

crates/pack-core/src/library/contexts.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ use turbopack_core::{
1010
environment::Environment,
1111
};
1212

13-
use crate::{config::Config, mode::Mode};
13+
use crate::{
14+
config::Config,
15+
mode::Mode,
16+
shared::transforms::extract_comments::get_extract_comments_transform_rule,
17+
};
1418

1519
use super::LibraryChunkingContext;
1620

@@ -52,8 +56,10 @@ pub async fn get_library_chunking_context(
5256
(*runtime_export.await?).clone(),
5357
)
5458
.minify_type(if mode.is_production() && *minify.await? {
59+
let minify_config = config.minify_config().await?;
5560
MinifyType::Minify {
5661
mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),
62+
extract_comments: minify_config.extract_comments(),
5763
}
5864
} else {
5965
MinifyType::NoMinify

crates/pack-core/src/library/ecmascript/chunk.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,21 @@ impl EcmascriptLibraryEvaluateChunk {
166166

167167
let mut code = code.build();
168168

169-
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
170-
code = minify(code, source_maps, mangle)?;
169+
if let MinifyType::Minify { mangle, extract_comments } = this.chunking_context.await?.minify_type() {
170+
let result = turbopack_ecmascript::minify::minify_with_options(
171+
code,
172+
source_maps,
173+
mangle,
174+
extract_comments
175+
)?;
176+
177+
code = result.code;
178+
179+
// TODO: Handle extracted comments by creating a license file
180+
// For now, we'll just ignore the extracted comments
181+
if let Some(_extracted_comments) = result.extracted_comments {
182+
// Future implementation: create a LicenseAsset and add it to output
183+
}
171184
}
172185

173186
Ok(code.cell())

crates/pack-core/src/shared/transforms/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use image::{StructuredImageModuleType, module::BlurPlaceholderMode};
99

1010
pub mod dynamic_import_to_require;
1111
pub mod emotion;
12+
pub mod extract_comments;
1213
pub mod image;
1314
pub mod modularize_imports;
1415
pub mod remove_console;

crates/pack-schema/src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,21 @@ pub enum SchemaOutputType {
193193
Export,
194194
}
195195

196+
/// Minify configuration that can be either boolean or advanced options
197+
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
198+
#[serde(untagged)]
199+
pub enum SchemaMinifyConfig {
200+
/// Simple boolean to enable/disable minification
201+
Boolean(bool),
202+
/// Advanced minification configuration
203+
Config {
204+
/// Comment extraction configuration
205+
#[serde(default)]
206+
#[schemars(description = "Whether to extract comments to separate files")]
207+
extract_comments: bool,
208+
},
209+
}
210+
196211
/// Optimization configuration
197212
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
198213
#[serde(rename_all = "camelCase")]
@@ -209,8 +224,8 @@ pub struct SchemaOptimizationConfig {
209224

210225
/// Whether to minify the output
211226
#[serde(skip_serializing_if = "Option::is_none")]
212-
#[schemars(description = "Whether to minify the output")]
213-
pub minify: Option<bool>,
227+
#[schemars(description = "Minify configuration - can be boolean or advanced options with extractComments")]
228+
pub minify: Option<SchemaMinifyConfig>,
214229

215230
/// Whether to enable tree shaking
216231
#[serde(skip_serializing_if = "Option::is_none")]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"$schema": "../../packages/pack/config_schema.json",
3+
"rootPath": "../../",
4+
"projectPath": "./",
5+
"config": {
6+
"entry": [
7+
{
8+
"import": "./src/index.js"
9+
}
10+
],
11+
"output": {
12+
"path": "./dist",
13+
"filename": "[name].[contenthash:6].js",
14+
"chunkFilename": "[name].[contenthash:8].js",
15+
"clean": true
16+
},
17+
"sourceMaps": false,
18+
"optimization": {
19+
"minify": {
20+
"extract_comments": true
21+
}
22+
}
23+
}
24+
}

packages/pack/config_schema.json

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,26 @@
493493
}
494494
}
495495
},
496+
"SchemaMinifyConfig": {
497+
"description": "Minify configuration that can be either boolean or advanced options",
498+
"anyOf": [
499+
{
500+
"description": "Simple boolean to enable/disable minification",
501+
"type": "boolean"
502+
},
503+
{
504+
"description": "Advanced minification configuration",
505+
"type": "object",
506+
"properties": {
507+
"extract_comments": {
508+
"description": "Whether to extract comments to separate files",
509+
"default": false,
510+
"type": "boolean"
511+
}
512+
}
513+
}
514+
]
515+
},
496516
"SchemaModuleConfig": {
497517
"description": "Module configuration",
498518
"type": "object",
@@ -527,10 +547,14 @@
527547
]
528548
},
529549
"minify": {
530-
"description": "Whether to minify the output",
531-
"type": [
532-
"boolean",
533-
"null"
550+
"description": "Minify configuration - can be boolean or advanced options with extractComments",
551+
"anyOf": [
552+
{
553+
"$ref": "#/definitions/SchemaMinifyConfig"
554+
},
555+
{
556+
"type": "null"
557+
}
534558
]
535559
},
536560
"modularizeImports": {

0 commit comments

Comments
 (0)