Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto resolve entry file extension #1718

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 45 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 umd::{deserialize_umd, Umd};
pub use watch::WatchConfig;

use crate::build::load::JS_EXTENSIONS;

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L77

Added line #L77 was not covered by tests
use crate::features::node::Node;

#[derive(Debug, Diagnostic)]
Expand Down Expand Up @@ -333,12 +334,14 @@

// 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,45 @@
}

// 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 {

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L359-L360

Added lines #L359 - L360 were not covered by tests
#[allow(clippy::needless_borrows_for_generic_args)]
if let Ok(entry_path) = root.join(&v).with_extension(ext).canonicalize()
&& entry_path.is_file()

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L362-L363

Added lines #L362 - L363 were not covered by tests
{
*v = entry_path;
return Ok(());
}

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L365-L367

Added lines #L365 - L367 were not covered by tests

if let Ok(entry_path) = root

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L369

Added line #L369 was not covered by tests
.join(&v)
.join("index")
.with_extension(ext)
.canonicalize()
&& entry_path.is_file()

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L373-L374

Added lines #L373 - L374 were not covered by tests
{
*v = entry_path;
return Ok(());
}

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L376-L378

Added lines #L376 - L378 were not covered by tests
}
})
.collect::<Result<Vec<_>>>()?;
config.entry = entry_tuples.into_iter().collect();
return Err(anyhow!("entry:{} not found", k,));

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L380

Added line #L380 was not covered by tests
}
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)| {
#[allow(clippy::needless_borrows_for_generic_args)]
if v.starts_with('.') {
*v = root.join(&v).to_string_lossy().to_string()
}
});

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L388-L391

Added lines #L388 - L391 were not covered by tests

// 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');
Loading