Skip to content

Commit

Permalink
feat: auto resolve entry file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Dec 11, 2024
1 parent 6bee118 commit c2bae12
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
79 changes: 47 additions & 32 deletions crates/mako/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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() {
Expand All @@ -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::<Result<Vec<_>>>()?;
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 {
Expand Down
2 changes: 2 additions & 0 deletions e2e/fixtures/config.entry/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
4 changes: 3 additions & 1 deletion e2e/fixtures/config.entry/mako.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 2 additions & 0 deletions e2e/fixtures/config.entry/src/soo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('bar');

1 change: 1 addition & 0 deletions e2e/fixtures/config.entry/src/yoo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('bar');

0 comments on commit c2bae12

Please sign in to comment.