From c2bae12977c1d6d53e4ba4b9a29b67b0c641f135 Mon Sep 17 00:00:00 2001 From: xusd320 Date: Wed, 11 Dec 2024 16:18:19 +0800 Subject: [PATCH 1/2] feat: auto resolve entry file extension --- crates/mako/src/config.rs | 79 +++++++++++++--------- e2e/fixtures/config.entry/expect.js | 2 + e2e/fixtures/config.entry/mako.config.json | 4 +- e2e/fixtures/config.entry/src/soo.ts | 2 + e2e/fixtures/config.entry/src/yoo/index.ts | 1 + 5 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 e2e/fixtures/config.entry/src/soo.ts create mode 100644 e2e/fixtures/config.entry/src/yoo/index.ts diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 1cbb58574..1d6a4865a 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -74,6 +74,7 @@ pub use tree_shaking::{deserialize_tree_shaking, TreeShakingStrategy}; pub use umd::{deserialize_umd, Umd}; pub use watch::WatchConfig; +use crate::build::load::JS_EXTENSIONS; use crate::features::node::Node; #[derive(Debug, Diagnostic)] @@ -333,12 +334,14 @@ impl Config { // support default entries if config.entry.is_empty() { - let file_paths = vec!["src/index.tsx", "src/index.ts", "index.tsx", "index.ts"]; - for file_path in file_paths { - let file_path = root.join(file_path); - if file_path.exists() { - config.entry.insert("index".to_string(), file_path); - break; + let file_paths = ["src/index", "index"]; + 'outer: for file_path in file_paths { + for ext in JS_EXTENSIONS { + let file_path = root.join(file_path).with_extension(ext); + if file_path.exists() { + config.entry.insert("index".to_string(), file_path); + break 'outer; + } } } if config.entry.is_empty() { @@ -347,35 +350,47 @@ impl Config { } // normalize entry - let entry_tuples = config - .entry - .clone() - .into_iter() - .map(|(k, v)| { - if let Ok(entry_path) = root.join(v).canonicalize() { - Ok((k, entry_path)) - } else { - Err(anyhow!("entry:{} not found", k,)) + config.entry.iter_mut().try_for_each(|(k, v)| { + #[allow(clippy::needless_borrows_for_generic_args)] + if let Ok(entry_path) = root.join(&v).canonicalize() + && entry_path.is_file() + { + *v = entry_path; + } else { + for ext in JS_EXTENSIONS { + #[allow(clippy::needless_borrows_for_generic_args)] + if let Ok(entry_path) = root.join(&v).with_extension(ext).canonicalize() + && entry_path.is_file() + { + *v = entry_path; + return Ok(()); + } + + if let Ok(entry_path) = root + .join(&v) + .join("index") + .with_extension(ext) + .canonicalize() + && entry_path.is_file() + { + *v = entry_path; + return Ok(()); + } } - }) - .collect::>>()?; - config.entry = entry_tuples.into_iter().collect(); + return Err(anyhow!("entry:{} not found", k,)); + } + Ok(()) + })?; // support relative alias - config.resolve.alias = config - .resolve - .alias - .clone() - .into_iter() - .map(|(k, v)| { - let v = if v.starts_with('.') { - root.join(v).to_string_lossy().to_string() - } else { - v - }; - (k, v) - }) - .collect(); + config.resolve.alias.iter_mut().for_each(|(_, v)| { + *v = if v.starts_with('.') { + #[allow(clippy::needless_borrows_for_generic_args)] + root.join(&v).to_string_lossy().to_string() + } else { + v.clone() + }; + }); // dev 环境下不产生 hash, prod 环境下根据用户配置 if config.mode == Mode::Development { diff --git a/e2e/fixtures/config.entry/expect.js b/e2e/fixtures/config.entry/expect.js index b789d7038..6e9433684 100644 --- a/e2e/fixtures/config.entry/expect.js +++ b/e2e/fixtures/config.entry/expect.js @@ -9,3 +9,5 @@ assert(content, `should have foo.js`); assert(content.includes(`"src/bar.ts":`), `should have src/bar.ts module define`); assert(content.includes(`"src/foo.ts":`), `should have src/foo.ts module define`); assert(names.includes('hoo/hoo.js'), `should have hoo/hoo.js`); +assert(names.includes('soo.js'), `should have soo.js`); +assert(names.includes('yoo.js'), `should have yoo.js`); diff --git a/e2e/fixtures/config.entry/mako.config.json b/e2e/fixtures/config.entry/mako.config.json index a08dd5dcf..d81489223 100644 --- a/e2e/fixtures/config.entry/mako.config.json +++ b/e2e/fixtures/config.entry/mako.config.json @@ -2,6 +2,8 @@ "minify": false, "entry": { "foo": "src/foo.ts", - "hoo/hoo": "src/hoo.ts" + "hoo/hoo": "src/hoo.ts", + "soo": "src/soo", + "yoo": "src/yoo" } } diff --git a/e2e/fixtures/config.entry/src/soo.ts b/e2e/fixtures/config.entry/src/soo.ts new file mode 100644 index 000000000..d6269f515 --- /dev/null +++ b/e2e/fixtures/config.entry/src/soo.ts @@ -0,0 +1,2 @@ +console.log('bar'); + diff --git a/e2e/fixtures/config.entry/src/yoo/index.ts b/e2e/fixtures/config.entry/src/yoo/index.ts new file mode 100644 index 000000000..8cc7aa3e9 --- /dev/null +++ b/e2e/fixtures/config.entry/src/yoo/index.ts @@ -0,0 +1 @@ +console.log('bar'); From e491471bcf8d672125371ab829960132c94e56ba Mon Sep 17 00:00:00 2001 From: xusd320 Date: Wed, 11 Dec 2024 18:23:11 +0800 Subject: [PATCH 2/2] fix: remove useless clone --- crates/mako/src/config.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 1d6a4865a..d554110a3 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -384,12 +384,10 @@ impl Config { // support relative alias config.resolve.alias.iter_mut().for_each(|(_, v)| { - *v = if v.starts_with('.') { - #[allow(clippy::needless_borrows_for_generic_args)] - root.join(&v).to_string_lossy().to_string() - } else { - v.clone() - }; + #[allow(clippy::needless_borrows_for_generic_args)] + if v.starts_with('.') { + *v = root.join(&v).to_string_lossy().to_string() + } }); // dev 环境下不产生 hash, prod 环境下根据用户配置