From 8b793d0d2e2b9570a363b87f6b241b7f3be5f4d2 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 11:35:17 +0800 Subject: [PATCH 01/12] refactor: combine manifest config --- crates/mako/src/config.rs | 14 +++++++------- crates/mako/src/plugins/manifest.rs | 10 +++++++--- crates/node/src/lib.rs | 3 +-- docs/config.md | 13 +++---------- e2e/fixtures/config.manifest/mako.config.json | 2 +- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 642d817c8..580357806 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -14,8 +14,8 @@ use mako_core::twox_hash::XxHash64; use mako_core::{clap, config, thiserror}; use serde::Serialize; -use crate::optimize_chunk; use crate::plugins::node_polyfill::get_all_modules; +use crate::{optimize_chunk, plugins}; #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] @@ -33,9 +33,12 @@ pub struct OutputConfig { #[derive(Deserialize, Serialize, Debug)] pub struct ManifestConfig { - #[serde(rename(deserialize = "fileName"))] + #[serde( + rename(deserialize = "fileName"), + default = "plugins::manifest::default_manifest_file_name" + )] pub file_name: String, - #[serde(rename(deserialize = "basePath"))] + #[serde(rename(deserialize = "basePath"), default)] pub base_path: String, } @@ -261,8 +264,7 @@ pub struct Config { pub entry: HashMap, pub output: OutputConfig, pub resolve: ResolveConfig, - pub manifest: bool, - pub manifest_config: ManifestConfig, + pub manifest: Option, pub mode: Mode, pub minify: bool, pub devtool: DevtoolConfig, @@ -435,8 +437,6 @@ const DEFAULT_CONFIG: &str = r#" "targets": { "chrome": 80 }, "less": { "theme": {}, "lesscPath": "", javascriptEnabled: true }, "define": {}, - "manifest": false, - "manifestConfig": { "fileName": "asset-manifest.json", "basePath": "" }, "stats": false, "mdx": false, "platform": "browser", diff --git a/crates/mako/src/plugins/manifest.rs b/crates/mako/src/plugins/manifest.rs index 9f6e89ebe..788828e94 100644 --- a/crates/mako/src/plugins/manifest.rs +++ b/crates/mako/src/plugins/manifest.rs @@ -12,17 +12,21 @@ use crate::stats::StatsJsonMap; pub struct ManifestPlugin {} +pub(crate) fn default_manifest_file_name() -> String { + "asset-manifest.json".to_string() +} + impl Plugin for ManifestPlugin { fn name(&self) -> &str { "manifest" } fn build_success(&self, _stats: &StatsJsonMap, context: &Arc) -> Result> { - if context.config.manifest { + if let Some(manifest_config) = &context.config.manifest { let assets = &context.stats_info.lock().unwrap().assets; let mut manifest: BTreeMap = BTreeMap::new(); - let file_name = context.config.manifest_config.file_name.clone(); - let base_path = context.config.manifest_config.base_path.clone(); + let file_name = manifest_config.file_name.clone(); + let base_path = manifest_config.base_path.clone(); let path = normalize_path(base_path); diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index a815021ef..d82379e6e 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -58,8 +58,7 @@ pub struct BuildParams { alias?: Record; extensions?: string[]; }; - manifest?: boolean; - manifestConfig?: { + manifest?: { fileName: string; basePath: string; }; diff --git a/docs/config.md b/docs/config.md index 37437308b..976bf4b20 100644 --- a/docs/config.md +++ b/docs/config.md @@ -214,17 +214,10 @@ import("./a.js") ## manifest -- 类型:`boolean` -- 默认值:`false` - -是否生成 `manifest.json` 文件。 - -## manifestConfig - -- 类型:`{ fileName: string, basePath: string }` -- 默认值:`{ fileName: "asset-manifest.json", basePath: "" }` +- 类型:`null | { fileName?: string, basePath?: string }` +- 默认值:`null` -`manifest.json` 文件的配置。(注:此配置后续会合并到 `manifest` 配置里) +是否生成 `manifest.json` 文件,启用时 `fileName` 的默认值为 `asset-manifest.json`。 ## mdx diff --git a/e2e/fixtures/config.manifest/mako.config.json b/e2e/fixtures/config.manifest/mako.config.json index e177a86af..11d9dd0cc 100644 --- a/e2e/fixtures/config.manifest/mako.config.json +++ b/e2e/fixtures/config.manifest/mako.config.json @@ -1 +1 @@ -{ "manifest": true } +{ "manifest": {} } From 80524e72413cff3352a946197f946caf317fd60a Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 14:24:23 +0800 Subject: [PATCH 02/12] refactor: combine hmr config --- crates/mako/src/build.rs | 2 +- crates/mako/src/config.rs | 15 +++++++------ crates/mako/src/dev.rs | 8 +------ crates/mako/src/plugins/javascript.rs | 4 ++-- crates/mako/src/test_helper.rs | 2 +- .../mako/src/transformers/transform_react.rs | 2 +- crates/node/src/lib.rs | 4 +--- docs/config.md | 22 ++----------------- 8 files changed, 17 insertions(+), 42 deletions(-) diff --git a/crates/mako/src/build.rs b/crates/mako/src/build.rs index eccf4cab1..477882bfc 100644 --- a/crates/mako/src/build.rs +++ b/crates/mako/src/build.rs @@ -58,7 +58,7 @@ impl Compiler { .iter() .map(|entry| { let mut entry = entry.to_str().unwrap().to_string(); - if self.context.config.hmr + if self.context.config.hmr.is_some() && self.context.config.mode == Mode::Development && self.context.args.watch { diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 580357806..e8713817c 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -257,6 +257,12 @@ pub struct MinifishConfig { pub struct OptimizationConfig { pub skip_modules: Option, } +#[derive(Deserialize, Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct HmrConfig { + pub host: String, + pub port: u16, +} #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] @@ -280,10 +286,7 @@ pub struct Config { pub stats: bool, pub mdx: bool, pub less: LessConfig, - // temp solution - pub hmr: bool, - pub hmr_port: String, - pub hmr_host: String, + pub hmr: Option, pub code_splitting: CodeSplittingStrategy, pub px2rem: bool, #[serde(rename = "px2remConfig")] @@ -440,9 +443,7 @@ const DEFAULT_CONFIG: &str = r#" "stats": false, "mdx": false, "platform": "browser", - "hmr": true, - "hmrHost": "127.0.0.1", - "hmrPort": "3000", + "hmr": { "host": "127.0.0.1", "port": 3000 }, "moduleIdStrategy": "named", "codeSplitting": "none", "hash": false, diff --git a/crates/mako/src/dev.rs b/crates/mako/src/dev.rs index 47037e0a5..1106b8531 100644 --- a/crates/mako/src/dev.rs +++ b/crates/mako/src/dev.rs @@ -44,13 +44,7 @@ impl DevServer { }); // server - let port = self - .compiler - .context - .config - .hmr_port - .parse::() - .unwrap(); + let port = self.compiler.context.config.hmr.as_ref().unwrap().port; // TODO: host // let host = self.compiler.context.config.hmr_host.clone(); // TODO: find free port diff --git a/crates/mako/src/plugins/javascript.rs b/crates/mako/src/plugins/javascript.rs index 191acd03f..0df12deb4 100644 --- a/crates/mako/src/plugins/javascript.rs +++ b/crates/mako/src/plugins/javascript.rs @@ -33,8 +33,8 @@ impl Plugin for JavaScriptPlugin { .is_match(vec!["js", "jsx", "ts", "tsx", "cjs", "mjs"]) { if param.task.is_entry && param.task.request.has_query("hmr") { - let port = &context.config.hmr_port.to_string(); - let host = &context.config.hmr_host.to_string(); + let port = &context.config.hmr.as_ref().unwrap().port.to_string(); + let host = &context.config.hmr.as_ref().unwrap().host.to_string(); let host = if host == "0.0.0.0" { "127.0.0.1" } else { host }; let mut content = format!( "module.exports = require(\"{}\");", diff --git a/crates/mako/src/test_helper.rs b/crates/mako/src/test_helper.rs index 013b1e38c..30c61b109 100644 --- a/crates/mako/src/test_helper.rs +++ b/crates/mako/src/test_helper.rs @@ -96,7 +96,7 @@ pub fn setup_compiler(base: &str, cleanup: bool) -> Compiler { fs::create_dir_all(&root).unwrap(); } let mut config = Config::new(&root, None, None).unwrap(); - config.hmr = false; + config.hmr = None; config.minify = false; config.mode = Mode::Production; diff --git a/crates/mako/src/transformers/transform_react.rs b/crates/mako/src/transformers/transform_react.rs index fd7257aad..0bc5cf13d 100644 --- a/crates/mako/src/transformers/transform_react.rs +++ b/crates/mako/src/transformers/transform_react.rs @@ -30,7 +30,7 @@ pub fn mako_react( let is_browser = matches!(context.config.platform, crate::config::Platform::Browser); let use_refresh = is_dev && context.args.watch - && context.config.hmr + && context.config.hmr.is_some() && !task.path.contains("/node_modules/") && is_browser; diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index d82379e6e..484c1c0fe 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -87,9 +87,7 @@ pub struct BuildParams { inlineLimit?: number; targets?: Record; platform?: "node" | "browser"; - hmr?: boolean; - hmrPort?: string; - hmrHost?: string; + hmr?: { host?: string; port?: number }; px2rem?: boolean; px2remConfig?: { root: number; diff --git a/docs/config.md b/docs/config.md index 976bf4b20..3a5ab7562 100644 --- a/docs/config.md +++ b/docs/config.md @@ -167,29 +167,11 @@ import("./a.js") ## hmr -- 类型:`boolean` -- 默认值:`true` +- 类型:`null | { host?: string, port?: number }` +- 默认值:`{ host: '127.0.0.1', port: 3000 }` 是否开启热更新。 -## hmrHost - -- 类型:`string` -- 默认值:`"127.0.0.1"` - -热更新的 host。 - -注:后续会改成 `hmr` 的子配置。 - -## hmrPort - -- 类型:`number` -- 默认值:`3000` - -热更新的端口。 - -注:后续会改成 `hmr` 的子配置。 - ## ignoreCSSParserErrors > 注:已废弃,待移除。 From 2fa798d0afc13ff0a07050909c5cb28341f64f23 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 14:34:18 +0800 Subject: [PATCH 03/12] refactor: combine px2rem config --- crates/mako/src/config.rs | 17 ++++++------- crates/mako/src/transform.rs | 2 +- .../mako/src/transformers/transform_px2rem.rs | 25 +++++++++++++++---- crates/node/src/lib.rs | 13 +++++----- docs/config.md | 15 +++-------- e2e/fixtures/config.px2rem/mako.config.json | 2 +- 6 files changed, 38 insertions(+), 36 deletions(-) diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index e8713817c..86498a44c 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -15,7 +15,7 @@ use mako_core::{clap, config, thiserror}; use serde::Serialize; use crate::plugins::node_polyfill::get_all_modules; -use crate::{optimize_chunk, plugins}; +use crate::{optimize_chunk, plugins, transformers}; #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] @@ -136,14 +136,15 @@ pub enum TreeShakeStrategy { #[derive(Deserialize, Serialize, Clone, Debug)] pub struct Px2RemConfig { + #[serde(default = "transformers::transform_px2rem::default_root")] pub root: f64, - #[serde(rename = "propBlackList")] + #[serde(rename = "propBlackList", default)] pub prop_black_list: Vec, - #[serde(rename = "propWhiteList")] + #[serde(rename = "propWhiteList", default)] pub prop_white_list: Vec, - #[serde(rename = "selectorBlackList")] + #[serde(rename = "selectorBlackList", default)] pub selector_black_list: Vec, - #[serde(rename = "selectorWhiteList")] + #[serde(rename = "selectorWhiteList", default)] pub selector_white_list: Vec, } @@ -288,9 +289,7 @@ pub struct Config { pub less: LessConfig, pub hmr: Option, pub code_splitting: CodeSplittingStrategy, - pub px2rem: bool, - #[serde(rename = "px2remConfig")] - pub px2rem_config: Px2RemConfig, + pub px2rem: Option, pub hash: bool, pub tree_shake: TreeShakeStrategy, #[serde(rename = "autoCSSModules")] @@ -447,8 +446,6 @@ const DEFAULT_CONFIG: &str = r#" "moduleIdStrategy": "named", "codeSplitting": "none", "hash": false, - "px2rem": false, - "px2remConfig": { "root": 100, "propBlackList": [], "propWhiteList": [], "selectorBlackList": [], "selectorWhiteList": [] }, "treeShake": "basic", "autoCSSModules": false, "ignoreCSSParserErrors": false, diff --git a/crates/mako/src/transform.rs b/crates/mako/src/transform.rs index a2a97e8ef..c36b2db91 100644 --- a/crates/mako/src/transform.rs +++ b/crates/mako/src/transform.rs @@ -221,7 +221,7 @@ fn transform_css(ast: &mut Stylesheet, context: &Arc, task: &Task) -> R ast.visit_mut_with(&mut CSSFlexbugs {}); } - if context.config.px2rem { + if context.config.px2rem.is_some() { let mut px2rem = Px2Rem { path: &task.path, context, diff --git a/crates/mako/src/transformers/transform_px2rem.rs b/crates/mako/src/transformers/transform_px2rem.rs index d56f80289..1f2650f1f 100644 --- a/crates/mako/src/transformers/transform_px2rem.rs +++ b/crates/mako/src/transformers/transform_px2rem.rs @@ -5,6 +5,10 @@ use mako_core::swc_css_visit::{VisitMut, VisitMutWith}; use crate::compiler::Context; +pub(crate) fn default_root() -> f64 { + 100.0 +} + pub struct Px2Rem<'a> { pub context: &'a Arc, pub path: &'a str, @@ -16,17 +20,28 @@ pub struct Px2Rem<'a> { impl Px2Rem<'_> { fn should_transform(&self) -> bool { if let Some(current_decl) = &self.current_decl { - let is_in_whitelist = self.context.config.px2rem_config.prop_white_list.is_empty() + let is_in_whitelist = self + .context + .config + .px2rem + .as_ref() + .unwrap() + .prop_white_list + .is_empty() || self .context .config - .px2rem_config + .px2rem + .as_ref() + .unwrap() .prop_white_list .contains(current_decl); let is_in_blacklist = self .context .config - .px2rem_config + .px2rem + .as_ref() + .unwrap() .prop_black_list .contains(current_decl); return is_in_whitelist && !is_in_blacklist; @@ -48,7 +63,7 @@ impl VisitMut for Px2Rem<'_> { } fn visit_mut_length(&mut self, n: &mut Length) { if n.unit.value.to_string() == "px" && self.should_transform() { - n.value.value /= self.context.config.px2rem_config.root; + n.value.value /= self.context.config.px2rem.as_ref().unwrap().root; n.value.raw = None; n.unit.value = "rem".into(); } @@ -57,7 +72,7 @@ impl VisitMut for Px2Rem<'_> { fn visit_mut_token(&mut self, t: &mut Token) { if let Token::Dimension(dimension) = t { if dimension.unit.to_string() == "px" && self.should_transform() { - let rem_val = dimension.value / self.context.config.px2rem_config.root; + let rem_val = dimension.value / self.context.config.px2rem.as_ref().unwrap().root; dimension.raw_value = rem_val.to_string().into(); dimension.value = rem_val; dimension.raw_unit = "rem".into(); diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index 484c1c0fe..8c7667c78 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -88,13 +88,12 @@ pub struct BuildParams { targets?: Record; platform?: "node" | "browser"; hmr?: { host?: string; port?: number }; - px2rem?: boolean; - px2remConfig?: { - root: number; - propBlackList: string[]; - propWhiteList: string[]; - selectorBlackList: string[]; - selectorWhiteList: string[]; + px2rem?: { + root?: number; + propBlackList?: string[]; + propWhiteList?: string[]; + selectorBlackList?: string[]; + selectorWhiteList?: string[]; }; stats?: boolean; hash?: boolean; diff --git a/docs/config.md b/docs/config.md index 3a5ab7562..18d440cdf 100644 --- a/docs/config.md +++ b/docs/config.md @@ -308,19 +308,10 @@ publicPath 配置。注:有个特殊值 `"runtime"`,表示会切换到 runti ## px2rem -- 类型:`boolean` -- 默认值:`false` - -是否开启 px2rem 转换。 - -## px2remConfig - -- 类型:`{ root: number, propBlackList: string[], propWhiteList: string[], selectorBlackList: string[], selectorWhiteList: string[] }` -- 默认值:`{ root: 100, propBlackList: [], propWhiteList: [], selectorBlackList: [], selectorWhiteList: [] }` - -px2rem 的配置。 +- 类型:`null | { root?: number, propBlackList?: string[], propWhiteList?: string[], selectorBlackList?: string[], selectorWhiteList?: string[] }` +- 默认值:`null` -注:后续会合并到 px2rem 配置里,并将其改成 `Object` 类型。 +是否开启 px2rem 转换,启用时 `root` 的默认值为 `100`。 - `root`,根节点的字体大小 - `propBlackList`,属性黑名单 diff --git a/e2e/fixtures/config.px2rem/mako.config.json b/e2e/fixtures/config.px2rem/mako.config.json index a7cdb5850..0fa646e62 100644 --- a/e2e/fixtures/config.px2rem/mako.config.json +++ b/e2e/fixtures/config.px2rem/mako.config.json @@ -1 +1 @@ -{ "px2rem": true } +{ "px2rem": {} } From 9f4a989af5869b9161c05efbf2a7a14d9a745f02 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 14:41:08 +0800 Subject: [PATCH 04/12] refactor: remove less config --- crates/mako/src/config.rs | 10 ---------- docs/config.md | 6 ------ 2 files changed, 16 deletions(-) diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 86498a44c..ca9367dcb 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -98,15 +98,6 @@ pub enum DevtoolConfig { None, } -#[derive(Deserialize, Serialize, Debug)] -pub struct LessConfig { - pub theme: HashMap, - #[serde(rename(deserialize = "lesscPath"))] - pub lessc_path: String, - #[serde(rename(deserialize = "javascriptEnabled"))] - pub javascript_enabled: bool, -} - #[derive(Deserialize, Serialize, Clone, Copy, Debug)] pub enum ModuleIdStrategy { #[serde(rename = "hashed")] @@ -286,7 +277,6 @@ pub struct Config { pub define: HashMap, pub stats: bool, pub mdx: bool, - pub less: LessConfig, pub hmr: Option, pub code_splitting: CodeSplittingStrategy, pub px2rem: Option, diff --git a/docs/config.md b/docs/config.md index 18d440cdf..570455cc5 100644 --- a/docs/config.md +++ b/docs/config.md @@ -129,8 +129,6 @@ import("./a.js") } ``` -注:多 entry 的 code splitting 支持还在开发中。 - ## externals - 类型:`Record` @@ -190,10 +188,6 @@ import("./a.js") 小于 `inlineLimit` 大小的 assets 文件会被转换成 `base64` 格式。 -## less - -> 注:已废弃,待移除。 - ## manifest - 类型:`null | { fileName?: string, basePath?: string }` From b925e65a0e6ce0d46fc4111d2c6bfdbc5fc4a14c Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 14:48:48 +0800 Subject: [PATCH 05/12] refactor: use option for umd config --- crates/mako/src/chunk_pot/util.rs | 6 +----- crates/mako/src/config.rs | 11 ++++------- docs/config.md | 6 +++--- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/crates/mako/src/chunk_pot/util.rs b/crates/mako/src/chunk_pot/util.rs index 14a00099a..5ea1fc1c4 100644 --- a/crates/mako/src/chunk_pot/util.rs +++ b/crates/mako/src/chunk_pot/util.rs @@ -92,11 +92,7 @@ pub(crate) fn empty_module_fn_expr() -> FnExpr { create = "{ SizedCache::with_size(5) }" )] pub(crate) fn runtime_code(context: &Arc) -> Result { - let umd = if context.config.umd != "none" { - Some(context.config.umd.clone()) - } else { - None - }; + let umd = context.config.umd.clone(); let chunk_graph = context.chunk_graph.read().unwrap(); let has_dynamic_chunks = chunk_graph.get_all_chunks().len() > 1; let has_hmr = context.args.watch; diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index ca9367dcb..601e7a17b 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -287,7 +287,7 @@ pub struct Config { #[serde(rename = "ignoreCSSParserErrors")] pub ignore_css_parser_errors: bool, pub dynamic_import_to_require: bool, - pub umd: String, + pub umd: Option, pub write_to_disk: bool, pub transform_import: Vec, pub dev_eval: bool, @@ -440,7 +440,6 @@ const DEFAULT_CONFIG: &str = r#" "autoCSSModules": false, "ignoreCSSParserErrors": false, "dynamicImportToRequire": false, - "umd": "none", "writeToDisk": true, "transformImport": [], "devEval": false, @@ -631,10 +630,8 @@ impl Default for Config { } } -fn get_default_chunk_loading_global(umd: String, root: &Path) -> String { - let unique_name = if umd != "none" { - umd - } else { +fn get_default_chunk_loading_global(umd: Option, root: &Path) -> String { + let unique_name = umd.unwrap_or_else(|| { let pkg_json_path = root.join("package.json"); let mut pkg_name = "global".to_string(); @@ -648,7 +645,7 @@ fn get_default_chunk_loading_global(umd: String, root: &Path) -> String { } pkg_name - }; + }); format!("makoChunk_{}", unique_name) } diff --git a/docs/config.md b/docs/config.md index 570455cc5..72ddb5e0d 100644 --- a/docs/config.md +++ b/docs/config.md @@ -358,12 +358,12 @@ publicPath 配置。注:有个特殊值 `"runtime"`,表示会切换到 runti ## umd -- 类型:`"none" | string` -- 默认值:`"none"` +- 类型:`null | string` +- 默认值:`null` 是否输出 umd 格式的代码。 -注:1)后续会改成 `Object` 类型,支持更多子配置用于控制 umd 参数;2)`"none"` 会改成 `false` 类型。 +注:后续会改成 `Object` 类型,支持更多子配置用于控制 umd 参数。 ## writeToDisk From f9da1f1a1b6cc98a695aa265a08f9bc9f9265947 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 15:17:32 +0800 Subject: [PATCH 06/12] test: update config for all cases --- crates/mako/test/build/side-effects-flag/mako.config.json | 2 +- crates/mako/test/config/define/mako.config.json | 2 +- crates/mako/test/config/normal/mako.config.json | 2 +- e2e/fixtures/config.manifest.config/mako.config.json | 2 +- .../tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json | 2 +- .../tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json | 2 +- .../tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json | 2 +- e2e/fixtures/tree-shaking-export-all/mako.config.json | 2 +- e2e/fixtures/tree-shaking/mako.config.json | 2 +- e2e/fixtures/tree-shaking_class/mako.config.json | 2 +- .../tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json | 2 +- e2e/fixtures/tree-shaking_export_default/mako.config.json | 2 +- .../tree-shaking_export_namespace.failed/mako.config.json | 2 +- e2e/fixtures/tree-shaking_exported/mako.config.json | 2 +- e2e/fixtures/tree-shaking_fn/mako.config.json | 2 +- .../tree-shaking_import_multi.failed.todo/mako.config.json | 2 +- e2e/fixtures/tree-shaking_import_self/mako.config.json | 2 +- e2e/fixtures/tree-shaking_issues_271/mako.config.json | 2 +- e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json | 2 +- e2e/fixtures/tree-shaking_named_export/mako.config.json | 2 +- e2e/fixtures/tree-shaking_named_reexport/mako.config.json | 2 +- e2e/fixtures/tree-shaking_reexport/mako.config.json | 2 +- e2e/fixtures/tree-shaking_require_esm/mako.config.json | 2 +- e2e/fixtures/tree-shaking_require_self/mako.config.json | 2 +- e2e/fixtures/tree-shaking_side_effect/mako.config.json | 2 +- e2e/fixtures/tree-shaking_style.failed/mako.config.json | 2 +- examples/import-resources/mako.config.json | 2 +- examples/tree-shaking/mako.config.json | 2 +- 28 files changed, 28 insertions(+), 28 deletions(-) diff --git a/crates/mako/test/build/side-effects-flag/mako.config.json b/crates/mako/test/build/side-effects-flag/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/crates/mako/test/build/side-effects-flag/mako.config.json +++ b/crates/mako/test/build/side-effects-flag/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/crates/mako/test/config/define/mako.config.json b/crates/mako/test/config/define/mako.config.json index b3dc87a40..e43675799 100644 --- a/crates/mako/test/config/define/mako.config.json +++ b/crates/mako/test/config/define/mako.config.json @@ -14,5 +14,5 @@ "MEMBER_NAMES": [{ "name": "\"sorrycc\"" }, { "name": "\"xiaohuoni\"" }], "EXIT": true }, - "hmr": false + "hmr": null } diff --git a/crates/mako/test/config/normal/mako.config.json b/crates/mako/test/config/normal/mako.config.json index e684cdc09..5ef80828d 100644 --- a/crates/mako/test/config/normal/mako.config.json +++ b/crates/mako/test/config/normal/mako.config.json @@ -1,4 +1,4 @@ { "platform": "node", - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/config.manifest.config/mako.config.json b/e2e/fixtures/config.manifest.config/mako.config.json index c4d219457..fbcd18806 100644 --- a/e2e/fixtures/config.manifest.config/mako.config.json +++ b/e2e/fixtures/config.manifest.config/mako.config.json @@ -1,5 +1,5 @@ { - "manifest": true, + "manifest": {}, "manifestConfig": { "fileName": "manifest.json", "basePath": "aaa" diff --git a/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json index a553c1a78..19ed06124 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "treeShake": "basic" } diff --git a/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json index a553c1a78..19ed06124 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "treeShake": "basic" } diff --git a/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json index a553c1a78..19ed06124 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "treeShake": "basic" } diff --git a/e2e/fixtures/tree-shaking-export-all/mako.config.json b/e2e/fixtures/tree-shaking-export-all/mako.config.json index a553c1a78..19ed06124 100644 --- a/e2e/fixtures/tree-shaking-export-all/mako.config.json +++ b/e2e/fixtures/tree-shaking-export-all/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "treeShake": "basic" } diff --git a/e2e/fixtures/tree-shaking/mako.config.json b/e2e/fixtures/tree-shaking/mako.config.json index a553c1a78..19ed06124 100644 --- a/e2e/fixtures/tree-shaking/mako.config.json +++ b/e2e/fixtures/tree-shaking/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "treeShake": "basic" } diff --git a/e2e/fixtures/tree-shaking_class/mako.config.json b/e2e/fixtures/tree-shaking_class/mako.config.json index e4ce9b552..fda0c0e4a 100644 --- a/e2e/fixtures/tree-shaking_class/mako.config.json +++ b/e2e/fixtures/tree-shaking_class/mako.config.json @@ -1,4 +1,4 @@ { "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json b/e2e/fixtures/tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json +++ b/e2e/fixtures/tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_export_default/mako.config.json b/e2e/fixtures/tree-shaking_export_default/mako.config.json index c2b789458..c0d58a23f 100644 --- a/e2e/fixtures/tree-shaking_export_default/mako.config.json +++ b/e2e/fixtures/tree-shaking_export_default/mako.config.json @@ -1,7 +1,7 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "optimizePackageImports": false, "optimization": { "skipModules": false diff --git a/e2e/fixtures/tree-shaking_export_namespace.failed/mako.config.json b/e2e/fixtures/tree-shaking_export_namespace.failed/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_export_namespace.failed/mako.config.json +++ b/e2e/fixtures/tree-shaking_export_namespace.failed/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_exported/mako.config.json b/e2e/fixtures/tree-shaking_exported/mako.config.json index a553c1a78..19ed06124 100644 --- a/e2e/fixtures/tree-shaking_exported/mako.config.json +++ b/e2e/fixtures/tree-shaking_exported/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "treeShake": "basic" } diff --git a/e2e/fixtures/tree-shaking_fn/mako.config.json b/e2e/fixtures/tree-shaking_fn/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_fn/mako.config.json +++ b/e2e/fixtures/tree-shaking_fn/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json b/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json index a553c1a78..19ed06124 100644 --- a/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json +++ b/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "treeShake": "basic" } diff --git a/e2e/fixtures/tree-shaking_import_self/mako.config.json b/e2e/fixtures/tree-shaking_import_self/mako.config.json index c51556c5d..d4c4e1a31 100644 --- a/e2e/fixtures/tree-shaking_import_self/mako.config.json +++ b/e2e/fixtures/tree-shaking_import_self/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "codeSplitting": "none" } diff --git a/e2e/fixtures/tree-shaking_issues_271/mako.config.json b/e2e/fixtures/tree-shaking_issues_271/mako.config.json index e0d8a3d5a..6071166e8 100644 --- a/e2e/fixtures/tree-shaking_issues_271/mako.config.json +++ b/e2e/fixtures/tree-shaking_issues_271/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", - "hmr": false, + "hmr": null, "minify":false } diff --git a/e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json b/e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json index 5442c91d4..ffc8e3d30 100644 --- a/e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json +++ b/e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json @@ -1,4 +1,4 @@ { "mode": "production", - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_named_export/mako.config.json b/e2e/fixtures/tree-shaking_named_export/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_named_export/mako.config.json +++ b/e2e/fixtures/tree-shaking_named_export/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_named_reexport/mako.config.json b/e2e/fixtures/tree-shaking_named_reexport/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_named_reexport/mako.config.json +++ b/e2e/fixtures/tree-shaking_named_reexport/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_reexport/mako.config.json b/e2e/fixtures/tree-shaking_reexport/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_reexport/mako.config.json +++ b/e2e/fixtures/tree-shaking_reexport/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_require_esm/mako.config.json b/e2e/fixtures/tree-shaking_require_esm/mako.config.json index a553c1a78..19ed06124 100644 --- a/e2e/fixtures/tree-shaking_require_esm/mako.config.json +++ b/e2e/fixtures/tree-shaking_require_esm/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": false, + "hmr": null, "treeShake": "basic" } diff --git a/e2e/fixtures/tree-shaking_require_self/mako.config.json b/e2e/fixtures/tree-shaking_require_self/mako.config.json index e4ce9b552..fda0c0e4a 100644 --- a/e2e/fixtures/tree-shaking_require_self/mako.config.json +++ b/e2e/fixtures/tree-shaking_require_self/mako.config.json @@ -1,4 +1,4 @@ { "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_side_effect/mako.config.json b/e2e/fixtures/tree-shaking_side_effect/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_side_effect/mako.config.json +++ b/e2e/fixtures/tree-shaking_side_effect/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/e2e/fixtures/tree-shaking_style.failed/mako.config.json b/e2e/fixtures/tree-shaking_style.failed/mako.config.json index e5d67ed74..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_style.failed/mako.config.json +++ b/e2e/fixtures/tree-shaking_style.failed/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": false + "hmr": null } diff --git a/examples/import-resources/mako.config.json b/examples/import-resources/mako.config.json index 37d48581a..8c9bc381e 100644 --- a/examples/import-resources/mako.config.json +++ b/examples/import-resources/mako.config.json @@ -2,7 +2,7 @@ "inlineLimit": 0, "stats": true, "mdx": true, - "manifest": true, + "manifest": {}, "manifestConfig": { "fileName": "manifest.json", "basePath": "aaa" diff --git a/examples/tree-shaking/mako.config.json b/examples/tree-shaking/mako.config.json index b320d7305..ad75f89ed 100644 --- a/examples/tree-shaking/mako.config.json +++ b/examples/tree-shaking/mako.config.json @@ -5,7 +5,7 @@ "index": "./index.js" }, "minify": false, - "hmr": false, + "hmr": null, "codeSplitting": "none", "define": { "NODE_ENV": "'production'" From fdda5e36684733b67c9150171d1994f16d5fba08 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 15:38:57 +0800 Subject: [PATCH 07/12] refactor: use option for code splitting config --- crates/mako/src/config.rs | 5 +---- crates/mako/src/optimize_chunk.rs | 14 ++++---------- crates/node/src/lib.rs | 2 +- docs/config.md | 4 ++-- .../tree-shaking_import_self/mako.config.json | 3 +-- examples/interop-fallback/mako.config.json | 3 +-- examples/tree-shaking/mako.config.json | 1 - examples/with-dynamic-import/mako.config.json | 3 +-- 8 files changed, 11 insertions(+), 24 deletions(-) diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 601e7a17b..b55c4e05a 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -110,8 +110,6 @@ pub enum ModuleIdStrategy { pub enum CodeSplittingStrategy { #[serde(rename = "auto")] Auto, - #[serde(rename = "none")] - None, #[serde(untagged)] Advanced(OptimizeChunkOptions), } @@ -278,7 +276,7 @@ pub struct Config { pub stats: bool, pub mdx: bool, pub hmr: Option, - pub code_splitting: CodeSplittingStrategy, + pub code_splitting: Option, pub px2rem: Option, pub hash: bool, pub tree_shake: TreeShakeStrategy, @@ -434,7 +432,6 @@ const DEFAULT_CONFIG: &str = r#" "platform": "browser", "hmr": { "host": "127.0.0.1", "port": 3000 }, "moduleIdStrategy": "named", - "codeSplitting": "none", "hash": false, "treeShake": "basic", "autoCSSModules": false, diff --git a/crates/mako/src/optimize_chunk.rs b/crates/mako/src/optimize_chunk.rs index 766e0133d..330785a6d 100644 --- a/crates/mako/src/optimize_chunk.rs +++ b/crates/mako/src/optimize_chunk.rs @@ -8,9 +8,7 @@ use mako_core::tracing::debug; use crate::chunk::{Chunk, ChunkId, ChunkType}; use crate::compiler::Compiler; -use crate::config::{ - CodeSplittingStrategy, OptimizeAllowChunks, OptimizeChunkGroup, OptimizeChunkOptions, -}; +use crate::config::{OptimizeAllowChunks, OptimizeChunkGroup, OptimizeChunkOptions}; use crate::group_chunk::GroupUpdateResult; use crate::module::{Module, ModuleId, ModuleInfo}; use crate::resolve::{ResolvedResource, ResolverResource}; @@ -80,11 +78,7 @@ impl Compiler { debug!("optimize hot update chunk"); // skip if code splitting disabled or group result is invalid - if matches!( - &self.context.config.code_splitting, - CodeSplittingStrategy::None - ) || group_result.is_none() - { + if self.context.config.code_splitting.is_none() || group_result.is_none() { return; } @@ -501,7 +495,7 @@ impl Compiler { fn get_optimize_chunk_options(&self) -> Option { match &self.context.config.code_splitting { - crate::config::CodeSplittingStrategy::Auto => Some(OptimizeChunkOptions { + Some(crate::config::CodeSplittingStrategy::Auto) => Some(OptimizeChunkOptions { groups: vec![ OptimizeChunkGroup { name: "vendors".to_string(), @@ -520,7 +514,7 @@ impl Compiler { ], ..Default::default() }), - crate::config::CodeSplittingStrategy::Advanced(options) => Some(options.clone()), + Some(crate::config::CodeSplittingStrategy::Advanced(options)) => Some(options.clone()), _ => None, } } diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index 8c7667c78..fe181b0ac 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -81,7 +81,7 @@ pub struct BuildParams { } >; copy?: string[]; - codeSplitting?: "auto" | "none"; + codeSplitting?: "auto"; providers?: Record; publicPath?: string; inlineLimit?: number; diff --git a/docs/config.md b/docs/config.md index 72ddb5e0d..1f22e39d7 100644 --- a/docs/config.md +++ b/docs/config.md @@ -20,8 +20,8 @@ ## codeSplitting -- 类型:`"none" | "auto" | object` -- 默认值:`"none"` +- 类型:`null | "auto" | object` +- 默认值:`null` 拆包策略,SPA 通常配置为 `auto` 即可,该内置策略会根据项目情况提取 `vendors` chunk 和 `common` chunk;MPA 场景如果需要产出 shared chunk,可以配置为 `object`,配置项说明: diff --git a/e2e/fixtures/tree-shaking_import_self/mako.config.json b/e2e/fixtures/tree-shaking_import_self/mako.config.json index d4c4e1a31..a7c7fa1de 100644 --- a/e2e/fixtures/tree-shaking_import_self/mako.config.json +++ b/e2e/fixtures/tree-shaking_import_self/mako.config.json @@ -1,6 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null, - "codeSplitting": "none" + "hmr": null } diff --git a/examples/interop-fallback/mako.config.json b/examples/interop-fallback/mako.config.json index 845ed936a..4fbe1570b 100644 --- a/examples/interop-fallback/mako.config.json +++ b/examples/interop-fallback/mako.config.json @@ -1,4 +1,3 @@ { - "minify": false, - "codeSplitting": "none" + "minify": false } diff --git a/examples/tree-shaking/mako.config.json b/examples/tree-shaking/mako.config.json index ad75f89ed..47adeb1d5 100644 --- a/examples/tree-shaking/mako.config.json +++ b/examples/tree-shaking/mako.config.json @@ -6,7 +6,6 @@ }, "minify": false, "hmr": null, - "codeSplitting": "none", "define": { "NODE_ENV": "'production'" } diff --git a/examples/with-dynamic-import/mako.config.json b/examples/with-dynamic-import/mako.config.json index f87e64f01..ea1a2d8ca 100644 --- a/examples/with-dynamic-import/mako.config.json +++ b/examples/with-dynamic-import/mako.config.json @@ -1,4 +1,3 @@ { - "public_path": "/", - "codeSplitting": "none" + "public_path": "/" } From bf51148322c9b30318d465ee52490c5c0d1e21ea Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 15:46:06 +0800 Subject: [PATCH 08/12] refactor: use option for devtool config --- crates/mako/src/ast.rs | 15 +++++++-------- crates/mako/src/build.rs | 6 ++---- crates/mako/src/chunk_pot/ast_impl.rs | 4 ++-- crates/mako/src/chunk_pot/util.rs | 4 ++-- crates/mako/src/config.rs | 4 +--- crates/mako/src/generate.rs | 6 +++--- crates/mako/src/plugins/minifish/inject.rs | 5 ++--- crates/mako/src/plugins/minifish/unsimplify.rs | 1 - .../src/transformers/transform_css_flexbugs.rs | 4 ++-- crates/node/src/lib.rs | 2 +- docs/config.md | 4 +--- e2e/fixtures/config.devtool.none/mako.config.json | 2 +- e2e/fixtures/minifish.inject/mako.config.json | 2 +- .../mako.config.json | 2 +- .../minifish.un-simplify/mako.config.json | 2 +- 15 files changed, 27 insertions(+), 36 deletions(-) diff --git a/crates/mako/src/ast.rs b/crates/mako/src/ast.rs index 2cbf2413e..642600ac4 100644 --- a/crates/mako/src/ast.rs +++ b/crates/mako/src/ast.rs @@ -205,21 +205,21 @@ pub fn js_ast_to_code( } let sourcemap = match context.config.devtool { - DevtoolConfig::SourceMap | DevtoolConfig::InlineSourceMap => { + Some(DevtoolConfig::SourceMap | DevtoolConfig::InlineSourceMap) => { let src_buf = build_source_map(&source_map_buf, cm); String::from_utf8(src_buf).unwrap() } - DevtoolConfig::None => "".to_string(), + None => "".to_string(), }; - if matches!(context.config.devtool, DevtoolConfig::SourceMap) { + if matches!(context.config.devtool, Some(DevtoolConfig::SourceMap)) { // separate sourcemap file buf.append( &mut format!("\n//# sourceMappingURL={filename}.map") .as_bytes() .to_vec(), ); - } else if matches!(context.config.devtool, DevtoolConfig::InlineSourceMap) { + } else if matches!(context.config.devtool, Some(DevtoolConfig::InlineSourceMap)) { // inline sourcemap buf.append( &mut format!( @@ -257,10 +257,10 @@ pub fn css_ast_to_code( let src_buf = build_source_map(&source_map, &context.meta.css.cm); let sourcemap = String::from_utf8(src_buf).unwrap(); - if matches!(context.config.devtool, DevtoolConfig::SourceMap) { + if matches!(context.config.devtool, Some(DevtoolConfig::SourceMap)) { // separate sourcemap file css_code.push_str(format!("\n/*# sourceMappingURL={filename}.map*/").as_str()); - } else if matches!(context.config.devtool, DevtoolConfig::InlineSourceMap) { + } else if matches!(context.config.devtool, Some(DevtoolConfig::InlineSourceMap)) { // inline sourcemap css_code.push_str( format!( @@ -316,7 +316,6 @@ mod tests { use crate::assert_debug_snapshot; use crate::ast::js_ast_to_code; use crate::compiler::Context; - use crate::config::DevtoolConfig; use crate::test_helper::create_mock_module; #[tokio::test(flavor = "multi_thread")] @@ -331,7 +330,7 @@ export const bar = { "#, ); let mut context = Context::default(); - context.config.devtool = DevtoolConfig::None; + context.config.devtool = None; let (code, _) = js_ast_to_code( module.info.unwrap().ast.as_script_mut(), &Arc::new(context), diff --git a/crates/mako/src/build.rs b/crates/mako/src/build.rs index 477882bfc..a65e45c34 100644 --- a/crates/mako/src/build.rs +++ b/crates/mako/src/build.rs @@ -23,7 +23,7 @@ use crate::analyze_deps::analyze_deps; use crate::ast::{build_js_ast, generate_code_frame}; use crate::chunk_pot::util::{hash_hashmap, hash_vec}; use crate::compiler::{Compiler, Context}; -use crate::config::{DevtoolConfig, Mode}; +use crate::config::Mode; use crate::load::{ext_name, load, Content}; use crate::module::{ Dependency, ExportInfo, ExportSpecifierInfo, ImportInfo, ImportSpecifierInfo, Module, @@ -594,9 +594,7 @@ lazy_static! { } fn load_source_map(context: &Arc, content: &Content) -> Option> { - if matches!(context.config.devtool, DevtoolConfig::None) { - return None; - } + context.config.devtool.as_ref()?; // TODO support load js source map if !matches!(content, Content::Css(_)) { diff --git a/crates/mako/src/chunk_pot/ast_impl.rs b/crates/mako/src/chunk_pot/ast_impl.rs index 39567691a..39759e091 100644 --- a/crates/mako/src/chunk_pot/ast_impl.rs +++ b/crates/mako/src/chunk_pot/ast_impl.rs @@ -20,7 +20,7 @@ use crate::chunk::{Chunk, ChunkType}; use crate::chunk_pot::util::{pot_to_chunk_module, pot_to_module_object, runtime_code}; use crate::chunk_pot::{get_css_chunk_filename, util, ChunkPot}; use crate::compiler::Context; -use crate::config::{DevtoolConfig, Mode}; +use crate::config::Mode; use crate::generate_chunks::{ChunkFile, ChunkFileType}; use crate::load::file_content_hash; use crate::minify::{minify_css, minify_js}; @@ -79,7 +79,7 @@ pub(crate) fn render_css_chunk( let cm = &context.meta.css.cm; let source_map = match context.config.devtool { - DevtoolConfig::None => None, + None => None, _ => { mako_profile_scope!("build_source_map"); // source map chain diff --git a/crates/mako/src/chunk_pot/util.rs b/crates/mako/src/chunk_pot/util.rs index 5ea1fc1c4..d3a3427ba 100644 --- a/crates/mako/src/chunk_pot/util.rs +++ b/crates/mako/src/chunk_pot/util.rs @@ -18,7 +18,7 @@ use mako_core::twox_hash::XxHash64; use crate::chunk_pot::ChunkPot; use crate::compiler::Context; -use crate::config::{DevtoolConfig, Mode}; +use crate::config::Mode; use crate::load::file_content_hash; use crate::module::{Module, ModuleAst}; use crate::runtime::AppRuntimeTemplate; @@ -52,7 +52,7 @@ pub(crate) fn render_module_js( let source_map = { mako_core::mako_profile_scope!("build_source_map"); match context.config.devtool { - DevtoolConfig::None => None, + None => None, _ => Some(build_source_map(&source_map_buf, cm)), } }; diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index b55c4e05a..f6e82fbf6 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -94,8 +94,6 @@ pub enum DevtoolConfig { /// Generate inline sourcemap #[serde(rename = "inline-source-map")] InlineSourceMap, - #[serde(rename = "none")] - None, } #[derive(Deserialize, Serialize, Clone, Copy, Debug)] @@ -263,7 +261,7 @@ pub struct Config { pub manifest: Option, pub mode: Mode, pub minify: bool, - pub devtool: DevtoolConfig, + pub devtool: Option, pub externals: HashMap, pub providers: Providers, pub copy: Vec, diff --git a/crates/mako/src/generate.rs b/crates/mako/src/generate.rs index 146f7abc8..e94b9e7cd 100644 --- a/crates/mako/src/generate.rs +++ b/crates/mako/src/generate.rs @@ -435,7 +435,7 @@ fn emit_chunk_file(context: &Arc, chunk_file: &ChunkFile) { let to: PathBuf = context.config.output.path.join(chunk_file.disk_name()); match context.config.devtool { - DevtoolConfig::SourceMap => { + Some(DevtoolConfig::SourceMap) => { let mut code = Vec::new(); code.extend_from_slice(&chunk_file.content); @@ -485,7 +485,7 @@ fn emit_chunk_file(context: &Arc, chunk_file: &ChunkFile) { ); fs::write(to, &code).unwrap(); } - DevtoolConfig::InlineSourceMap => { + Some(DevtoolConfig::InlineSourceMap) => { let mut code = Vec::new(); code.extend_from_slice(&chunk_file.content); @@ -509,7 +509,7 @@ fn emit_chunk_file(context: &Arc, chunk_file: &ChunkFile) { ); fs::write(to, code).unwrap(); } - DevtoolConfig::None => { + None => { context.stats_info.lock().unwrap().add_assets( chunk_file.content.len() as u64, chunk_file.file_name.clone(), diff --git a/crates/mako/src/plugins/minifish/inject.rs b/crates/mako/src/plugins/minifish/inject.rs index 59065a40a..5df99cf63 100644 --- a/crates/mako/src/plugins/minifish/inject.rs +++ b/crates/mako/src/plugins/minifish/inject.rs @@ -219,14 +219,13 @@ mod tests { use crate::analyze_deps::analyze_deps; use crate::ast::{build_js_ast, js_ast_to_code}; use crate::compiler::Context; - use crate::config::DevtoolConfig; use crate::module::ModuleAst; use crate::plugin::PluginDriver; use crate::plugins::javascript::JavaScriptPlugin; fn apply_inject_to_code(injects: HashMap, code: &str) -> String { let mut context = Context::default(); - context.config.devtool = DevtoolConfig::None; + context.config.devtool = None; let context = Arc::new(context); let mut ast = build_js_ast("cut.js", code, &context).unwrap(); @@ -497,7 +496,7 @@ my.call("toast"); plugin_driver: PluginDriver::new(vec![Arc::new(JavaScriptPlugin {})]), ..Context::default() }; - context.config.devtool = DevtoolConfig::None; + context.config.devtool = None; let context = Arc::new(context); let mut ast = build_js_ast("cut.js", code, &context).unwrap(); diff --git a/crates/mako/src/plugins/minifish/unsimplify.rs b/crates/mako/src/plugins/minifish/unsimplify.rs index ca9e8a13c..a2b23f197 100644 --- a/crates/mako/src/plugins/minifish/unsimplify.rs +++ b/crates/mako/src/plugins/minifish/unsimplify.rs @@ -33,7 +33,6 @@ mod tests { use super::*; use crate::ast::{build_js_ast, js_ast_to_code}; use crate::compiler::Context; - use crate::config::DevtoolConfig::None; fn context() -> Arc { let mut c: Context = Default::default(); diff --git a/crates/mako/src/transformers/transform_css_flexbugs.rs b/crates/mako/src/transformers/transform_css_flexbugs.rs index 49f4b53b2..25061b65c 100644 --- a/crates/mako/src/transformers/transform_css_flexbugs.rs +++ b/crates/mako/src/transformers/transform_css_flexbugs.rs @@ -174,7 +174,7 @@ mod test { use crate::ast::{build_css_ast, css_ast_to_code}; use crate::compiler::Context; - use crate::config::{Config, DevtoolConfig, Mode}; + use crate::config::{Config, Mode}; // migrate from https://github.com/luisrudge/postcss-flexbugs-fixes/blob/683560e1f0a4e67009331b564d530ccfefb831ad/specs/bug4Spec.js #[test] @@ -312,7 +312,7 @@ mod test { let context: Arc = Arc::new(Context { config: Config { mode: Mode::Production, - devtool: DevtoolConfig::None, + devtool: None, minify: true, ..Default::default() }, diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index fe181b0ac..482f10586 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -64,7 +64,7 @@ pub struct BuildParams { }; mode?: "development" | "production"; define?: Record; - devtool?: "source-map" | "inline-source-map" | "none"; + devtool?: "source-map" | "inline-source-map"; externals?: Record< string, string | { diff --git a/docs/config.md b/docs/config.md index 1f22e39d7..18e0f6de0 100644 --- a/docs/config.md +++ b/docs/config.md @@ -85,13 +85,11 @@ ## devtool -- 类型:`"source-map" | "inline-source-map" | "none"` +- 类型:`"source-map" | "inline-source-map" | null` - 默认值:`"source-map"` Source Map 类型。 -注:`"none"` 类型后续会改成 `false`。 - ## dynamicImportToRequire - 类型:`boolean` diff --git a/e2e/fixtures/config.devtool.none/mako.config.json b/e2e/fixtures/config.devtool.none/mako.config.json index 6971b4219..f6b534dbe 100644 --- a/e2e/fixtures/config.devtool.none/mako.config.json +++ b/e2e/fixtures/config.devtool.none/mako.config.json @@ -1,3 +1,3 @@ { - "devtool": "none" + "devtool": null } diff --git a/e2e/fixtures/minifish.inject/mako.config.json b/e2e/fixtures/minifish.inject/mako.config.json index a867be3d3..68845811a 100644 --- a/e2e/fixtures/minifish.inject/mako.config.json +++ b/e2e/fixtures/minifish.inject/mako.config.json @@ -9,7 +9,7 @@ "preserveModulesRoot": "./src", "asciiOnly": false }, - "devtool": "none", + "devtool": null, "_minifish": { "mapping": {}, "inject": { diff --git a/e2e/fixtures/minifish.inject_prefer_require/mako.config.json b/e2e/fixtures/minifish.inject_prefer_require/mako.config.json index bc4ba28b5..e3ade82d3 100644 --- a/e2e/fixtures/minifish.inject_prefer_require/mako.config.json +++ b/e2e/fixtures/minifish.inject_prefer_require/mako.config.json @@ -9,7 +9,7 @@ "preserveModulesRoot": "./src", "asciiOnly": false }, - "devtool": "none", + "devtool": null, "_minifish": { "mapping": {}, "inject": { diff --git a/e2e/fixtures/minifish.un-simplify/mako.config.json b/e2e/fixtures/minifish.un-simplify/mako.config.json index e72105368..9a8c25439 100644 --- a/e2e/fixtures/minifish.un-simplify/mako.config.json +++ b/e2e/fixtures/minifish.un-simplify/mako.config.json @@ -9,7 +9,7 @@ "preserveModulesRoot": "./src", "asciiOnly": false }, - "devtool": "none", + "devtool": null, "_minifish": { "mapping": {} } From c54421715a8ab45c89de0b6bd691f55916a1f47a Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 16:09:12 +0800 Subject: [PATCH 09/12] refactor: use option and private for tree shaking config --- crates/mako/src/config.rs | 9 ++++----- crates/mako/src/generate.rs | 10 +++++----- docs/config.md | 9 --------- e2e/fixtures/optimize-package-imports/mako.config.json | 2 +- e2e/fixtures/resolve.ignored/mako.config.json | 2 +- .../mako.config.json | 2 +- .../mako.config.json | 2 +- .../mako.config.json | 2 +- e2e/fixtures/tree-shaking-export-all/mako.config.json | 2 +- e2e/fixtures/tree-shaking/mako.config.json | 2 +- e2e/fixtures/tree-shaking_exported/mako.config.json | 2 +- .../mako.config.json | 2 +- e2e/fixtures/tree-shaking_require_esm/mako.config.json | 2 +- examples/with-antd/mako.config.json | 2 +- 14 files changed, 20 insertions(+), 30 deletions(-) diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index f6e82fbf6..71af45309 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -112,13 +112,11 @@ pub enum CodeSplittingStrategy { Advanced(OptimizeChunkOptions), } #[derive(Deserialize, Serialize, Clone, Copy, Debug)] -pub enum TreeShakeStrategy { +pub enum TreeShakingStrategy { #[serde(rename = "basic")] Basic, #[serde(rename = "advanced")] Advanced, - #[serde(rename = "none")] - None, } #[derive(Deserialize, Serialize, Clone, Debug)] @@ -277,7 +275,8 @@ pub struct Config { pub code_splitting: Option, pub px2rem: Option, pub hash: bool, - pub tree_shake: TreeShakeStrategy, + #[serde(rename = "_treeShaking")] + pub _tree_shaking: Option, #[serde(rename = "autoCSSModules")] pub auto_css_modules: bool, #[serde(rename = "ignoreCSSParserErrors")] @@ -431,7 +430,7 @@ const DEFAULT_CONFIG: &str = r#" "hmr": { "host": "127.0.0.1", "port": 3000 }, "moduleIdStrategy": "named", "hash": false, - "treeShake": "basic", + "_treeShaking": "basic", "autoCSSModules": false, "ignoreCSSParserErrors": false, "dynamicImportToRequire": false, diff --git a/crates/mako/src/generate.rs b/crates/mako/src/generate.rs index e94b9e7cd..8f9bd8eba 100644 --- a/crates/mako/src/generate.rs +++ b/crates/mako/src/generate.rs @@ -13,7 +13,7 @@ use mako_core::tracing::debug; use crate::ast::base64_encode; use crate::compiler::{Compiler, Context}; -use crate::config::{DevtoolConfig, OutputMode, TreeShakeStrategy}; +use crate::config::{DevtoolConfig, OutputMode, TreeShakingStrategy}; use crate::generate_chunks::{ChunkFile, ChunkFileType}; use crate::module::ModuleId; use crate::stats::{create_stats_info, print_stats, write_stats}; @@ -48,8 +48,8 @@ impl Compiler { // Disable tree shaking in watch mode temporarily // ref: https://github.com/umijs/mako/issues/396 if !self.context.args.watch { - match self.context.config.tree_shake { - TreeShakeStrategy::Basic => { + match self.context.config._tree_shaking { + Some(TreeShakingStrategy::Basic) => { let mut module_graph = self.context.module_graph.write().unwrap(); mako_core::mako_profile_scope!("tree shake"); @@ -59,7 +59,7 @@ impl Compiler { let t_tree_shaking = t_tree_shaking.elapsed(); println!("basic optimize in {}ms.", t_tree_shaking.as_millis()); } - TreeShakeStrategy::Advanced => { + Some(TreeShakingStrategy::Advanced) => { mako_core::mako_profile_scope!("advanced tree shake"); let shaking_module_ids = self.tree_shaking(); let t_tree_shaking = t_tree_shaking.elapsed(); @@ -69,7 +69,7 @@ impl Compiler { t_tree_shaking.as_millis() ); } - TreeShakeStrategy::None => {} + None => {} } } let t_tree_shaking = t_tree_shaking.elapsed(); diff --git a/docs/config.md b/docs/config.md index 18e0f6de0..0fc6c58b0 100644 --- a/docs/config.md +++ b/docs/config.md @@ -345,15 +345,6 @@ publicPath 配置。注:有个特殊值 `"runtime"`,表示会切换到 runti > TODO: @辟殊。 -## treeShake - -- 类型:`"basic"` -- 默认值:`"basic"` - -注:1)配置名后续会改成 `treeShaking`;2)配置值也会做调整,目前只支持 `"basic"` 模式,应该也不会同时支持多种 treeShaking 模式。 - -注:目前只在 mode 为 "production" 时生效。 - ## umd - 类型:`null | string` diff --git a/e2e/fixtures/optimize-package-imports/mako.config.json b/e2e/fixtures/optimize-package-imports/mako.config.json index 1760697f3..c2c149806 100644 --- a/e2e/fixtures/optimize-package-imports/mako.config.json +++ b/e2e/fixtures/optimize-package-imports/mako.config.json @@ -1 +1 @@ -{"minify": false, "optimizePackageImports":true,"treeShake":"none"} +{"minify": false, "optimizePackageImports":true,"_treeShaking":null} diff --git a/e2e/fixtures/resolve.ignored/mako.config.json b/e2e/fixtures/resolve.ignored/mako.config.json index 923706bde..d9d0b276f 100644 --- a/e2e/fixtures/resolve.ignored/mako.config.json +++ b/e2e/fixtures/resolve.ignored/mako.config.json @@ -1,4 +1,4 @@ { "minify": false, - "treeShake": "none" + "_treeShaking": null } diff --git a/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json index 19ed06124..d343aa586 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json @@ -2,5 +2,5 @@ "mode": "production", "minify": false, "hmr": null, - "treeShake": "basic" + "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json index 19ed06124..d343aa586 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json @@ -2,5 +2,5 @@ "mode": "production", "minify": false, "hmr": null, - "treeShake": "basic" + "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json index 19ed06124..d343aa586 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json @@ -2,5 +2,5 @@ "mode": "production", "minify": false, "hmr": null, - "treeShake": "basic" + "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking-export-all/mako.config.json b/e2e/fixtures/tree-shaking-export-all/mako.config.json index 19ed06124..d343aa586 100644 --- a/e2e/fixtures/tree-shaking-export-all/mako.config.json +++ b/e2e/fixtures/tree-shaking-export-all/mako.config.json @@ -2,5 +2,5 @@ "mode": "production", "minify": false, "hmr": null, - "treeShake": "basic" + "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking/mako.config.json b/e2e/fixtures/tree-shaking/mako.config.json index 19ed06124..d343aa586 100644 --- a/e2e/fixtures/tree-shaking/mako.config.json +++ b/e2e/fixtures/tree-shaking/mako.config.json @@ -2,5 +2,5 @@ "mode": "production", "minify": false, "hmr": null, - "treeShake": "basic" + "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking_exported/mako.config.json b/e2e/fixtures/tree-shaking_exported/mako.config.json index 19ed06124..d343aa586 100644 --- a/e2e/fixtures/tree-shaking_exported/mako.config.json +++ b/e2e/fixtures/tree-shaking_exported/mako.config.json @@ -2,5 +2,5 @@ "mode": "production", "minify": false, "hmr": null, - "treeShake": "basic" + "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json b/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json index 19ed06124..d343aa586 100644 --- a/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json +++ b/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json @@ -2,5 +2,5 @@ "mode": "production", "minify": false, "hmr": null, - "treeShake": "basic" + "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking_require_esm/mako.config.json b/e2e/fixtures/tree-shaking_require_esm/mako.config.json index 19ed06124..d343aa586 100644 --- a/e2e/fixtures/tree-shaking_require_esm/mako.config.json +++ b/e2e/fixtures/tree-shaking_require_esm/mako.config.json @@ -2,5 +2,5 @@ "mode": "production", "minify": false, "hmr": null, - "treeShake": "basic" + "_treeShaking": "basic" } diff --git a/examples/with-antd/mako.config.json b/examples/with-antd/mako.config.json index b35280858..a9b577a57 100644 --- a/examples/with-antd/mako.config.json +++ b/examples/with-antd/mako.config.json @@ -1,6 +1,6 @@ { "externals": {}, - "treeShake": "basic", + "_treeShaking": "basic", "minify": true, "moduleIdStrategy": "named", "optimizePackageImports": true From 50d6cf02734df9e693529c4195b1d8f5b08e03c5 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 17:07:18 +0800 Subject: [PATCH 10/12] refactor: allow false for option config --- crates/mako/src/config.rs | 60 ++++++++++++++++++++++++++++++++++++--- crates/node/src/lib.rs | 16 +++++------ docs/config.md | 20 ++++++------- 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 71af45309..b3eb95097 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -6,7 +6,7 @@ use mako_core::anyhow::{anyhow, Result}; use mako_core::clap::ValueEnum; use mako_core::colored::Colorize; use mako_core::regex::Regex; -use mako_core::serde::Deserialize; +use mako_core::serde::{Deserialize, Deserializer}; use mako_core::serde_json::Value; use mako_core::swc_ecma_ast::EsVersion; use mako_core::thiserror::Error; @@ -17,6 +17,48 @@ use serde::Serialize; use crate::plugins::node_polyfill::get_all_modules; use crate::{optimize_chunk, plugins, transformers}; +/** + * a macro to create deserialize function that allow false value for optional struct + */ +macro_rules! create_deserialize_fn { + ($fn_name:ident, $struct_type:ty) => { + pub fn $fn_name<'de, D>(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let value: serde_json::Value = serde_json::Value::deserialize(deserializer)?; + + match value { + // allow false value for optional struct + serde_json::Value::Bool(false) => Ok(None), + // try deserialize + serde_json::Value::Object(obj) => Ok(Some( + serde_json::from_value::<$struct_type>(serde_json::Value::Object(obj)) + .map_err(serde::de::Error::custom)?, + )), + serde_json::Value::String(s) => Ok(Some( + serde_json::from_value::<$struct_type>(serde_json::Value::String(s.clone())) + .map_err(serde::de::Error::custom)?, + )), + _ => Err(serde::de::Error::custom(format!( + "invalid `{}` value: {}", + stringify!($fn_name).replace("deserialize_", ""), + value + ))), + } + } + }; +} +create_deserialize_fn!(deserialize_hmr, HmrConfig); +create_deserialize_fn!(deserialize_manifest, ManifestConfig); +create_deserialize_fn!(deserialize_code_splitting, CodeSplittingStrategy); +create_deserialize_fn!(deserialize_px2rem, Px2RemConfig); +create_deserialize_fn!(deserialize_umd, String); +create_deserialize_fn!(deserialize_devtool, DevtoolConfig); +create_deserialize_fn!(deserialize_tree_shaking, TreeShakingStrategy); +create_deserialize_fn!(deserialize_optimization, OptimizationConfig); +create_deserialize_fn!(deserialize_minifish, MinifishConfig); + #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] pub struct OutputConfig { @@ -256,9 +298,11 @@ pub struct Config { pub entry: HashMap, pub output: OutputConfig, pub resolve: ResolveConfig, + #[serde(deserialize_with = "deserialize_manifest", default)] pub manifest: Option, pub mode: Mode, pub minify: bool, + #[serde(deserialize_with = "deserialize_devtool")] pub devtool: Option, pub externals: HashMap, pub providers: Providers, @@ -271,17 +315,21 @@ pub struct Config { pub define: HashMap, pub stats: bool, pub mdx: bool, + #[serde(deserialize_with = "deserialize_hmr")] pub hmr: Option, + #[serde(deserialize_with = "deserialize_code_splitting", default)] pub code_splitting: Option, + #[serde(deserialize_with = "deserialize_px2rem", default)] pub px2rem: Option, pub hash: bool, - #[serde(rename = "_treeShaking")] + #[serde(rename = "_treeShaking", deserialize_with = "deserialize_tree_shaking")] pub _tree_shaking: Option, #[serde(rename = "autoCSSModules")] pub auto_css_modules: bool, #[serde(rename = "ignoreCSSParserErrors")] pub ignore_css_parser_errors: bool, pub dynamic_import_to_require: bool, + #[serde(deserialize_with = "deserialize_umd", default)] pub umd: Option, pub write_to_disk: bool, pub transform_import: Vec, @@ -289,12 +337,17 @@ pub struct Config { pub clean: bool, pub node_polyfill: bool, pub ignores: Vec, - #[serde(rename = "_minifish")] + #[serde( + rename = "_minifish", + deserialize_with = "deserialize_minifish", + default + )] pub _minifish: Option, #[serde(rename = "optimizePackageImports")] pub optimize_package_imports: bool, pub emotion: bool, pub flex_bugs: bool, + #[serde(deserialize_with = "deserialize_optimization")] pub optimization: Option, } @@ -440,7 +493,6 @@ const DEFAULT_CONFIG: &str = r#" "clean": true, "nodePolyfill": true, "ignores": [], - "_minifish": null, "optimizePackageImports": false, "emotion": false, "flexBugs": false, diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index 482f10586..07fa514d2 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -58,13 +58,13 @@ pub struct BuildParams { alias?: Record; extensions?: string[]; }; - manifest?: { + manifest?: false | { fileName: string; basePath: string; }; mode?: "development" | "production"; define?: Record; - devtool?: "source-map" | "inline-source-map"; + devtool?: false | "source-map" | "inline-source-map"; externals?: Record< string, string | { @@ -81,14 +81,14 @@ pub struct BuildParams { } >; copy?: string[]; - codeSplitting?: "auto"; + codeSplitting?: false | "auto"; providers?: Record; publicPath?: string; inlineLimit?: number; targets?: Record; platform?: "node" | "browser"; - hmr?: { host?: string; port?: number }; - px2rem?: { + hmr?: false | { host?: string; port?: number }; + px2rem?: false | { root?: number; propBlackList?: string[]; propWhiteList?: string[]; @@ -100,14 +100,14 @@ pub struct BuildParams { autoCSSModules?: boolean; ignoreCSSParserErrors?: boolean; dynamicImportToRequire?: boolean; - umd?: string; + umd?: false | string; transformImport?: { libraryName: string; libraryDirectory?: string; style?: boolean | string }[]; clean?: boolean; nodePolyfill?: boolean; ignores?: string[]; moduleIdStrategy?: "hashed" | "named"; minify?: boolean; - _minifish?: { + _minifish?: false | { mapping: Record; metaPath?: string; inject?: Record; }; - optimization?: { + optimization?: false | { skipModules?: boolean; }; }"#)] diff --git a/docs/config.md b/docs/config.md index 0fc6c58b0..b288547a5 100644 --- a/docs/config.md +++ b/docs/config.md @@ -20,8 +20,8 @@ ## codeSplitting -- 类型:`null | "auto" | object` -- 默认值:`null` +- 类型:`false | "auto" | object` +- 默认值:`false` 拆包策略,SPA 通常配置为 `auto` 即可,该内置策略会根据项目情况提取 `vendors` chunk 和 `common` chunk;MPA 场景如果需要产出 shared chunk,可以配置为 `object`,配置项说明: @@ -85,7 +85,7 @@ ## devtool -- 类型:`"source-map" | "inline-source-map" | null` +- 类型:`false | "source-map" | "inline-source-map"` - 默认值:`"source-map"` Source Map 类型。 @@ -163,7 +163,7 @@ import("./a.js") ## hmr -- 类型:`null | { host?: string, port?: number }` +- 类型:`false | { host?: string, port?: number }` - 默认值:`{ host: '127.0.0.1', port: 3000 }` 是否开启热更新。 @@ -188,8 +188,8 @@ import("./a.js") ## manifest -- 类型:`null | { fileName?: string, basePath?: string }` -- 默认值:`null` +- 类型:`false | { fileName?: string, basePath?: string }` +- 默认值:`false` 是否生成 `manifest.json` 文件,启用时 `fileName` 的默认值为 `asset-manifest.json`。 @@ -300,8 +300,8 @@ publicPath 配置。注:有个特殊值 `"runtime"`,表示会切换到 runti ## px2rem -- 类型:`null | { root?: number, propBlackList?: string[], propWhiteList?: string[], selectorBlackList?: string[], selectorWhiteList?: string[] }` -- 默认值:`null` +- 类型:`false | { root?: number, propBlackList?: string[], propWhiteList?: string[], selectorBlackList?: string[], selectorWhiteList?: string[] }` +- 默认值:`false` 是否开启 px2rem 转换,启用时 `root` 的默认值为 `100`。 @@ -347,8 +347,8 @@ publicPath 配置。注:有个特殊值 `"runtime"`,表示会切换到 runti ## umd -- 类型:`null | string` -- 默认值:`null` +- 类型:`false | string` +- 默认值:`false` 是否输出 umd 格式的代码。 From ea89b9878d8dd168f26cb8b9cdb6aba614c4f191 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 17:14:46 +0800 Subject: [PATCH 11/12] refactor: update config for all cases --- crates/mako/test/build/side-effects-flag/mako.config.json | 2 +- crates/mako/test/config/define/mako.config.json | 2 +- crates/mako/test/config/normal/mako.config.json | 2 +- e2e/fixtures/config.devtool.none/mako.config.json | 2 +- e2e/fixtures/config.manifest.config/mako.config.json | 3 +-- e2e/fixtures/javascript.target.ie11/expect.js | 1 + e2e/fixtures/javascript.target.ie11/mako.config.json | 2 +- e2e/fixtures/minifish.inject/mako.config.json | 2 +- e2e/fixtures/minifish.inject_prefer_require/mako.config.json | 2 +- e2e/fixtures/minifish.un-simplify/mako.config.json | 2 +- e2e/fixtures/optimize-package-imports/mako.config.json | 2 +- e2e/fixtures/resolve.ignored/mako.config.json | 2 +- .../mako.config.json | 2 +- .../tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json | 2 +- .../tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json | 2 +- e2e/fixtures/tree-shaking-export-all/mako.config.json | 2 +- e2e/fixtures/tree-shaking/mako.config.json | 2 +- e2e/fixtures/tree-shaking_class/mako.config.json | 2 +- .../tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json | 2 +- e2e/fixtures/tree-shaking_export_default/mako.config.json | 2 +- .../tree-shaking_export_namespace.failed/mako.config.json | 2 +- e2e/fixtures/tree-shaking_exported/mako.config.json | 2 +- e2e/fixtures/tree-shaking_fn/mako.config.json | 2 +- .../tree-shaking_import_multi.failed.todo/mako.config.json | 2 +- e2e/fixtures/tree-shaking_import_self/mako.config.json | 2 +- e2e/fixtures/tree-shaking_issues_271/mako.config.json | 2 +- e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json | 2 +- e2e/fixtures/tree-shaking_named_export/mako.config.json | 2 +- e2e/fixtures/tree-shaking_named_reexport/mako.config.json | 2 +- e2e/fixtures/tree-shaking_reexport/mako.config.json | 2 +- e2e/fixtures/tree-shaking_require_esm/mako.config.json | 2 +- e2e/fixtures/tree-shaking_require_self/mako.config.json | 2 +- e2e/fixtures/tree-shaking_side_effect/mako.config.json | 2 +- e2e/fixtures/tree-shaking_style.failed/mako.config.json | 2 +- examples/tree-shaking/mako.config.json | 2 +- 35 files changed, 35 insertions(+), 35 deletions(-) diff --git a/crates/mako/test/build/side-effects-flag/mako.config.json b/crates/mako/test/build/side-effects-flag/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/crates/mako/test/build/side-effects-flag/mako.config.json +++ b/crates/mako/test/build/side-effects-flag/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/crates/mako/test/config/define/mako.config.json b/crates/mako/test/config/define/mako.config.json index e43675799..b3dc87a40 100644 --- a/crates/mako/test/config/define/mako.config.json +++ b/crates/mako/test/config/define/mako.config.json @@ -14,5 +14,5 @@ "MEMBER_NAMES": [{ "name": "\"sorrycc\"" }, { "name": "\"xiaohuoni\"" }], "EXIT": true }, - "hmr": null + "hmr": false } diff --git a/crates/mako/test/config/normal/mako.config.json b/crates/mako/test/config/normal/mako.config.json index 5ef80828d..e684cdc09 100644 --- a/crates/mako/test/config/normal/mako.config.json +++ b/crates/mako/test/config/normal/mako.config.json @@ -1,4 +1,4 @@ { "platform": "node", - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/config.devtool.none/mako.config.json b/e2e/fixtures/config.devtool.none/mako.config.json index f6b534dbe..4b1efdec3 100644 --- a/e2e/fixtures/config.devtool.none/mako.config.json +++ b/e2e/fixtures/config.devtool.none/mako.config.json @@ -1,3 +1,3 @@ { - "devtool": null + "devtool": false } diff --git a/e2e/fixtures/config.manifest.config/mako.config.json b/e2e/fixtures/config.manifest.config/mako.config.json index fbcd18806..370204d59 100644 --- a/e2e/fixtures/config.manifest.config/mako.config.json +++ b/e2e/fixtures/config.manifest.config/mako.config.json @@ -1,6 +1,5 @@ { - "manifest": {}, - "manifestConfig": { + "manifest": { "fileName": "manifest.json", "basePath": "aaa" } diff --git a/e2e/fixtures/javascript.target.ie11/expect.js b/e2e/fixtures/javascript.target.ie11/expect.js index 066b9c30d..0ba0b6014 100644 --- a/e2e/fixtures/javascript.target.ie11/expect.js +++ b/e2e/fixtures/javascript.target.ie11/expect.js @@ -1,4 +1,5 @@ const assert = require("assert"); +const path = require("path"); const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils"); const { distDir } = parseBuildResult(__dirname); diff --git a/e2e/fixtures/javascript.target.ie11/mako.config.json b/e2e/fixtures/javascript.target.ie11/mako.config.json index c13b46cba..f47c4de02 100644 --- a/e2e/fixtures/javascript.target.ie11/mako.config.json +++ b/e2e/fixtures/javascript.target.ie11/mako.config.json @@ -1 +1 @@ -{ "targets": { "ie": 11 }, "umd": true } +{ "targets": { "ie": 11 }, "umd": "lib" } diff --git a/e2e/fixtures/minifish.inject/mako.config.json b/e2e/fixtures/minifish.inject/mako.config.json index 68845811a..6e419450a 100644 --- a/e2e/fixtures/minifish.inject/mako.config.json +++ b/e2e/fixtures/minifish.inject/mako.config.json @@ -9,7 +9,7 @@ "preserveModulesRoot": "./src", "asciiOnly": false }, - "devtool": null, + "devtool": false, "_minifish": { "mapping": {}, "inject": { diff --git a/e2e/fixtures/minifish.inject_prefer_require/mako.config.json b/e2e/fixtures/minifish.inject_prefer_require/mako.config.json index e3ade82d3..94208b9cd 100644 --- a/e2e/fixtures/minifish.inject_prefer_require/mako.config.json +++ b/e2e/fixtures/minifish.inject_prefer_require/mako.config.json @@ -9,7 +9,7 @@ "preserveModulesRoot": "./src", "asciiOnly": false }, - "devtool": null, + "devtool": false, "_minifish": { "mapping": {}, "inject": { diff --git a/e2e/fixtures/minifish.un-simplify/mako.config.json b/e2e/fixtures/minifish.un-simplify/mako.config.json index 9a8c25439..403f7a4c2 100644 --- a/e2e/fixtures/minifish.un-simplify/mako.config.json +++ b/e2e/fixtures/minifish.un-simplify/mako.config.json @@ -9,7 +9,7 @@ "preserveModulesRoot": "./src", "asciiOnly": false }, - "devtool": null, + "devtool": false, "_minifish": { "mapping": {} } diff --git a/e2e/fixtures/optimize-package-imports/mako.config.json b/e2e/fixtures/optimize-package-imports/mako.config.json index c2c149806..93309d137 100644 --- a/e2e/fixtures/optimize-package-imports/mako.config.json +++ b/e2e/fixtures/optimize-package-imports/mako.config.json @@ -1 +1 @@ -{"minify": false, "optimizePackageImports":true,"_treeShaking":null} +{"minify": false, "optimizePackageImports":true,"_treeShaking":false} diff --git a/e2e/fixtures/resolve.ignored/mako.config.json b/e2e/fixtures/resolve.ignored/mako.config.json index d9d0b276f..a6075f728 100644 --- a/e2e/fixtures/resolve.ignored/mako.config.json +++ b/e2e/fixtures/resolve.ignored/mako.config.json @@ -1,4 +1,4 @@ { "minify": false, - "_treeShaking": null + "_treeShaking": false } diff --git a/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json index d343aa586..48669c82c 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-cjs_COULD_BE_IMPROVED/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json index d343aa586..48669c82c 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-non-sideeffects-cjs/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json b/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json index d343aa586..48669c82c 100644 --- a/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json +++ b/e2e/fixtures/tree-shaking-esm-mix-sideeffects-cjs-2/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking-export-all/mako.config.json b/e2e/fixtures/tree-shaking-export-all/mako.config.json index d343aa586..48669c82c 100644 --- a/e2e/fixtures/tree-shaking-export-all/mako.config.json +++ b/e2e/fixtures/tree-shaking-export-all/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking/mako.config.json b/e2e/fixtures/tree-shaking/mako.config.json index d343aa586..48669c82c 100644 --- a/e2e/fixtures/tree-shaking/mako.config.json +++ b/e2e/fixtures/tree-shaking/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking_class/mako.config.json b/e2e/fixtures/tree-shaking_class/mako.config.json index fda0c0e4a..e4ce9b552 100644 --- a/e2e/fixtures/tree-shaking_class/mako.config.json +++ b/e2e/fixtures/tree-shaking_class/mako.config.json @@ -1,4 +1,4 @@ { "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json b/e2e/fixtures/tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json +++ b/e2e/fixtures/tree-shaking_dynamic-import_WHY_ADD_THIS/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_export_default/mako.config.json b/e2e/fixtures/tree-shaking_export_default/mako.config.json index c0d58a23f..c2b789458 100644 --- a/e2e/fixtures/tree-shaking_export_default/mako.config.json +++ b/e2e/fixtures/tree-shaking_export_default/mako.config.json @@ -1,7 +1,7 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "optimizePackageImports": false, "optimization": { "skipModules": false diff --git a/e2e/fixtures/tree-shaking_export_namespace.failed/mako.config.json b/e2e/fixtures/tree-shaking_export_namespace.failed/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_export_namespace.failed/mako.config.json +++ b/e2e/fixtures/tree-shaking_export_namespace.failed/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_exported/mako.config.json b/e2e/fixtures/tree-shaking_exported/mako.config.json index d343aa586..48669c82c 100644 --- a/e2e/fixtures/tree-shaking_exported/mako.config.json +++ b/e2e/fixtures/tree-shaking_exported/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking_fn/mako.config.json b/e2e/fixtures/tree-shaking_fn/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_fn/mako.config.json +++ b/e2e/fixtures/tree-shaking_fn/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json b/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json index d343aa586..48669c82c 100644 --- a/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json +++ b/e2e/fixtures/tree-shaking_import_multi.failed.todo/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking_import_self/mako.config.json b/e2e/fixtures/tree-shaking_import_self/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_import_self/mako.config.json +++ b/e2e/fixtures/tree-shaking_import_self/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_issues_271/mako.config.json b/e2e/fixtures/tree-shaking_issues_271/mako.config.json index 6071166e8..e0d8a3d5a 100644 --- a/e2e/fixtures/tree-shaking_issues_271/mako.config.json +++ b/e2e/fixtures/tree-shaking_issues_271/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", - "hmr": null, + "hmr": false, "minify":false } diff --git a/e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json b/e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json index ffc8e3d30..5442c91d4 100644 --- a/e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json +++ b/e2e/fixtures/tree-shaking_jsx_WHY_ADD_THIS/mako.config.json @@ -1,4 +1,4 @@ { "mode": "production", - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_named_export/mako.config.json b/e2e/fixtures/tree-shaking_named_export/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_named_export/mako.config.json +++ b/e2e/fixtures/tree-shaking_named_export/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_named_reexport/mako.config.json b/e2e/fixtures/tree-shaking_named_reexport/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_named_reexport/mako.config.json +++ b/e2e/fixtures/tree-shaking_named_reexport/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_reexport/mako.config.json b/e2e/fixtures/tree-shaking_reexport/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_reexport/mako.config.json +++ b/e2e/fixtures/tree-shaking_reexport/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_require_esm/mako.config.json b/e2e/fixtures/tree-shaking_require_esm/mako.config.json index d343aa586..48669c82c 100644 --- a/e2e/fixtures/tree-shaking_require_esm/mako.config.json +++ b/e2e/fixtures/tree-shaking_require_esm/mako.config.json @@ -1,6 +1,6 @@ { "mode": "production", "minify": false, - "hmr": null, + "hmr": false, "_treeShaking": "basic" } diff --git a/e2e/fixtures/tree-shaking_require_self/mako.config.json b/e2e/fixtures/tree-shaking_require_self/mako.config.json index fda0c0e4a..e4ce9b552 100644 --- a/e2e/fixtures/tree-shaking_require_self/mako.config.json +++ b/e2e/fixtures/tree-shaking_require_self/mako.config.json @@ -1,4 +1,4 @@ { "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_side_effect/mako.config.json b/e2e/fixtures/tree-shaking_side_effect/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_side_effect/mako.config.json +++ b/e2e/fixtures/tree-shaking_side_effect/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/e2e/fixtures/tree-shaking_style.failed/mako.config.json b/e2e/fixtures/tree-shaking_style.failed/mako.config.json index a7c7fa1de..e5d67ed74 100644 --- a/e2e/fixtures/tree-shaking_style.failed/mako.config.json +++ b/e2e/fixtures/tree-shaking_style.failed/mako.config.json @@ -1,5 +1,5 @@ { "mode": "production", "minify": false, - "hmr": null + "hmr": false } diff --git a/examples/tree-shaking/mako.config.json b/examples/tree-shaking/mako.config.json index 47adeb1d5..f5f224dc8 100644 --- a/examples/tree-shaking/mako.config.json +++ b/examples/tree-shaking/mako.config.json @@ -5,7 +5,7 @@ "index": "./index.js" }, "minify": false, - "hmr": null, + "hmr": false, "define": { "NODE_ENV": "'production'" } From 846ddf33ef35f0d2ed9a3d1123657972266b23c5 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 2 Jan 2024 18:39:49 +0800 Subject: [PATCH 12/12] refactor: update config logic for bundler okam --- packages/bundler-okam/index.js | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/packages/bundler-okam/index.js b/packages/bundler-okam/index.js index d048c253f..72deb169e 100644 --- a/packages/bundler-okam/index.js +++ b/packages/bundler-okam/index.js @@ -43,7 +43,7 @@ exports.build = async function (opts) { const okamConfig = await getOkamConfig(opts); const mode = process.argv.includes('--dev') ? 'development' : 'production'; okamConfig.mode = mode; - okamConfig.manifest = true; + okamConfig.manifest = {}; okamConfig.hash = !!opts.config.hash; if (okamConfig.hash) { okamConfig.moduleIdStrategy = 'hashed'; @@ -60,7 +60,7 @@ exports.build = async function (opts) { config: opts.config, // NOTICE: 有个缺点是 如果 alias 配置是 mako 插件修改的 less 这边就感知到不了 alias: okamConfig.resolve.alias, - modifyVars: okamConfig.less.theme, + modifyVars: opts.config.theme, sourceMap: getLessSourceMapConfig(okamConfig.devtool), }), }, @@ -79,7 +79,7 @@ exports.build = async function (opts) { path.join( cwd, 'dist', - okamConfig.manifestConfig?.fileName || 'asset-manifest.json', + okamConfig.manifest?.fileName || 'asset-manifest.json', ), ), ); @@ -180,9 +180,7 @@ exports.dev = async function (opts) { // okam dev const { build } = require('@okamjs/okam'); const okamConfig = await getOkamConfig(opts); - okamConfig.hmr = true; - okamConfig.hmrPort = String(hmrPort); - okamConfig.hmrHost = opts.host; + okamConfig.hmr = { port: hmrPort, host: opts.host }; const cwd = opts.cwd; try { await build({ @@ -193,7 +191,7 @@ exports.dev = async function (opts) { cwd, config: opts.config, alias: okamConfig.resolve.alias, - modifyVars: okamConfig.less.theme, + modifyVars: opts.config.theme, sourceMap: getLessSourceMapConfig(okamConfig.devtool), }), onBuildComplete: (args) => { @@ -407,7 +405,7 @@ async function getOkamConfig(opts) { opts.config.chainWebpack(webpackChainConfig, { env, webpack }); } const webpackConfig = webpackChainConfig.toConfig(); - let umd = 'none'; + let umd = false; if ( webpackConfig.output && webpackConfig.output.libraryTarget === 'umd' && @@ -429,8 +427,6 @@ async function getOkamConfig(opts) { runtimePublicPath, manifest, mdx, - theme, - lessLoader, codeSplitting, devtool, jsMinifier, @@ -544,19 +540,10 @@ async function getOkamConfig(opts) { targets: targets || { chrome: 80, }, - manifest: !!manifest, - manifestConfig: manifest || {}, + manifest: manifest, mdx: !!mdx, - codeSplitting: codeSplitting === false ? 'none' : 'auto', - devtool: devtool === false ? 'none' : 'source-map', - less: { - theme: { - // ignore function value - ...lodash.pickBy(theme, lodash.isString), - ...lessLoader?.modifyVars, - ...makoConfig.less?.theme, - }, - }, + codeSplitting: codeSplitting === false ? false : 'auto', + devtool: devtool === false ? false : 'source-map', minify, define, autoCSSModules: true, @@ -578,7 +565,7 @@ async function getOkamConfig(opts) { function getLessSourceMapConfig(devtool) { return ( - devtool !== 'none' && { + devtool && { sourceMapFileInline: true, outputSourceFiles: true, }