Skip to content
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

refactor: config codes organization #1618

Merged
merged 3 commits into from
Sep 27, 2024
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
491 changes: 488 additions & 3 deletions crates/mako/src/config.rs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions crates/mako/src/config/analyze.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Clone, Debug)]

Check warning on line 3 in crates/mako/src/config/analyze.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/analyze.rs#L3

Added line #L3 was not covered by tests
pub struct AnalyzeConfig {}
145 changes: 145 additions & 0 deletions crates/mako/src/config/code_splitting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use regex::Regex;
use serde::{Deserialize, Serialize};

use super::generic_usize::GenericUsizeDefault;
use crate::create_deserialize_fn;

#[derive(Deserialize, Serialize, Clone, Debug, Default)]
pub enum OptimizeAllowChunks {

Check warning on line 8 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L7-L8

Added lines #L7 - L8 were not covered by tests
#[serde(rename = "all")]
All,
#[serde(rename = "entry")]
Entry,
#[serde(rename = "async")]
#[default]
Async,

Check warning on line 15 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L15

Added line #L15 was not covered by tests
}

#[derive(Deserialize, Serialize, Clone, Debug)]

Check warning on line 18 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L18

Added line #L18 was not covered by tests
pub struct CodeSplitting {
pub strategy: CodeSplittingStrategy,
pub options: Option<CodeSplittingStrategyOptions>,

Check warning on line 21 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L20-L21

Added lines #L20 - L21 were not covered by tests
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub enum CodeSplittingStrategy {

Check warning on line 25 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L24-L25

Added lines #L24 - L25 were not covered by tests
#[serde(rename = "auto")]
Auto,
#[serde(rename = "granular")]
Granular,
#[serde(rename = "advanced")]
Advanced,
}

#[derive(Deserialize, Serialize, Clone, Debug)]

Check warning on line 34 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L34

Added line #L34 was not covered by tests
#[serde(untagged)]
pub enum CodeSplittingStrategyOptions {
Granular(CodeSplittingGranularOptions),
Advanced(CodeSplittingAdvancedOptions),

Check warning on line 38 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
}

#[derive(Deserialize, Serialize, Clone, Debug)]

Check warning on line 41 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L41

Added line #L41 was not covered by tests
#[serde(rename_all = "camelCase")]
pub struct CodeSplittingGranularOptions {
pub framework_packages: Vec<String>,
#[serde(default = "GenericUsizeDefault::<160000>::value")]
pub lib_min_size: usize,

Check warning on line 46 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L44-L46

Added lines #L44 - L46 were not covered by tests
}

#[derive(Deserialize, Serialize, Clone, Debug)]

Check warning on line 49 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L49

Added line #L49 was not covered by tests
#[serde(rename_all = "camelCase")]
pub struct CodeSplittingAdvancedOptions {
#[serde(default = "GenericUsizeDefault::<20000>::value")]
pub min_size: usize,
pub groups: Vec<OptimizeChunkGroup>,

Check warning on line 54 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L52-L54

Added lines #L52 - L54 were not covered by tests
}

impl Default for CodeSplittingAdvancedOptions {
fn default() -> Self {
CodeSplittingAdvancedOptions {
min_size: GenericUsizeDefault::<20000>::value(),
groups: vec![],

Check warning on line 61 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L58-L61

Added lines #L58 - L61 were not covered by tests
}
}

Check warning on line 63 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L63

Added line #L63 was not covered by tests
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub enum OptimizeChunkNameSuffixStrategy {

Check warning on line 67 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L66-L67

Added lines #L66 - L67 were not covered by tests
#[serde(rename = "packageName")]
PackageName,
#[serde(rename = "dependentsHash")]
DependentsHash,
}

#[derive(Deserialize, Serialize, Clone, Debug)]

Check warning on line 74 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L74

Added line #L74 was not covered by tests
#[serde(rename_all = "camelCase")]
pub struct OptimizeChunkGroup {
pub name: String,
#[serde(default)]
pub name_suffix: Option<OptimizeChunkNameSuffixStrategy>,
#[serde(default)]
pub allow_chunks: OptimizeAllowChunks,
#[serde(default = "GenericUsizeDefault::<1>::value")]
pub min_chunks: usize,
#[serde(default = "GenericUsizeDefault::<20000>::value")]
pub min_size: usize,
#[serde(default = "GenericUsizeDefault::<5000000>::value")]
pub max_size: usize,
#[serde(default)]
pub min_module_size: Option<usize>,
#[serde(default)]
pub priority: i8,
#[serde(default, with = "optimize_test_format")]
pub test: Option<Regex>,

Check warning on line 93 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L77-L93

Added lines #L77 - L93 were not covered by tests
}

impl Default for OptimizeChunkGroup {
fn default() -> Self {
Self {
allow_chunks: OptimizeAllowChunks::default(),
min_chunks: GenericUsizeDefault::<1>::value(),
min_size: GenericUsizeDefault::<20000>::value(),
max_size: GenericUsizeDefault::<5000000>::value(),
name: String::default(),
name_suffix: None,
min_module_size: None,
test: None,
priority: i8::default(),

Check warning on line 107 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L97-L107

Added lines #L97 - L107 were not covered by tests
}
}

Check warning on line 109 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L109

Added line #L109 was not covered by tests
}

/**
* custom formatter for convert string to regex
* @see https://serde.rs/custom-date-format.html
*/
mod optimize_test_format {
use regex::Regex;
use serde::{self, Deserialize, Deserializer, Serializer};

pub fn serialize<S>(v: &Option<Regex>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if let Some(v) = v {
serializer.serialize_str(&v.to_string())
} else {
serializer.serialize_none()
}
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Regex>, D::Error>

Check warning on line 131 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L131

Added line #L131 was not covered by tests
where
D: Deserializer<'de>,
{
let v = String::deserialize(deserializer)?;

Check warning on line 135 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L135

Added line #L135 was not covered by tests

if v.is_empty() {
Ok(None)

Check warning on line 138 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L137-L138

Added lines #L137 - L138 were not covered by tests
} else {
Ok(Regex::new(v.as_str()).ok())

Check warning on line 140 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L140

Added line #L140 was not covered by tests
}
}

Check warning on line 142 in crates/mako/src/config/code_splitting.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/code_splitting.rs#L142

Added line #L142 was not covered by tests
Comment on lines +135 to +142
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

避免在正则表达式解析失败时忽略错误

在反序列化正则表达式时,如果解析失败,当前实现会返回 Ok(None),这可能会导致潜在的错误被忽略。建议在解析失败时正确地传播错误,以便开发者及时发现并处理无效的正则表达式。

可以修改代码以在解析失败时返回错误:

  if v.is_empty() {
      Ok(None)
  } else {
-     Ok(Regex::new(v.as_str()).ok())
+     match Regex::new(v.as_str()) {
+         Ok(regex) => Ok(Some(regex)),
+         Err(err) => Err(serde::de::Error::custom(format!("Invalid regex pattern: {}", err))),
+     }
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let v = String::deserialize(deserializer)?;
if v.is_empty() {
Ok(None)
} else {
Ok(Regex::new(v.as_str()).ok())
}
}
let v = String::deserialize(deserializer)?;
if v.is_empty() {
Ok(None)
} else {
match Regex::new(v.as_str()) {
Ok(regex) => Ok(Some(regex)),
Err(err) => Err(serde::de::Error::custom(format!("Invalid regex pattern: {}", err))),
}
}
}

}

create_deserialize_fn!(deserialize_code_splitting, CodeSplitting);
Loading
Loading