Skip to content

Commit

Permalink
fix: returning broken missing dependencies when alias and extensions …
Browse files Browse the repository at this point in the history
…are provided

Upstream: web-infra-dev/modern.js#5227
  • Loading branch information
Boshen committed Jan 17, 2024
1 parent 456899b commit 3fc870a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,12 +822,18 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
ctx: &mut Ctx,
) -> ResolveResult {
for (alias_key_raw, specifiers) in aliases {
let from = alias_key_raw.strip_suffix('$');
let alias_key = from.unwrap_or(alias_key_raw);
let exact_match = from.is_some() && specifier == alias_key;
if !(exact_match || Self::strip_package_name(specifier, alias_key).is_some()) {
continue;
}
let alias_key = if let Some(alias_key) = alias_key_raw.strip_suffix('$') {
if alias_key != specifier {
continue;
}
alias_key
} else {
let strip_package_name = Self::strip_package_name(specifier, alias_key_raw);
if strip_package_name.is_none() {
continue;
}
alias_key_raw
};
for r in specifiers {
match r {
AliasValue::Path(alias_value) => {
Expand Down
2 changes: 2 additions & 0 deletions src/tests/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn alias() {
("alias_query".into(), vec![AliasValue::Path("a?query_after".into())]),
("alias_fragment".into(), vec![AliasValue::Path("a#fragment_after".into())]),
("dash".into(), vec![AliasValue::Ignore]),
("@scope/package-name/file$".into(), vec![AliasValue::Path("/c/dir".into())]),
],
modules: vec!["/".into()],
..ResolveOptions::default()
Expand Down Expand Up @@ -97,6 +98,7 @@ fn alias() {
("should resolve query in alias value", "alias_query?query_before", "/a/index?query_after"),
("should resolve query in alias value", "alias_fragment#fragment_before", "/a/index#fragment_after"),
("should resolve dashed name", "dashed-name", "/dashed-name"),
("should resolve scoped package name with sub dir", "@scope/package-name/file", "/c/dir/index"),
];

for (comment, request, expected) in pass {
Expand Down
27 changes: 26 additions & 1 deletion src/tests/missing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! https://github.com/webpack/enhanced-resolve/blob/main/test/missing.test.js
use crate::{ResolveContext, Resolver};
use crate::{AliasValue, ResolveContext, ResolveOptions, Resolver};

#[test]
fn test() {
Expand Down Expand Up @@ -61,3 +61,28 @@ fn test() {
}
}
}

#[test]
fn alias_and_extensions() {
let f = super::fixture();

let resolver = Resolver::new(ResolveOptions {
alias: vec![(
"@scope-js/package-name/dir$".into(),
vec![AliasValue::Path(f.join("foo/index.js").to_string_lossy().to_string())],
)],
extensions: vec![".server.ts".into()],

..ResolveOptions::default()
});

let mut ctx = ResolveContext::default();
let _ = resolver.resolve_with_context(&f, "@scope-js/package-name/dir/router", &mut ctx);

for dep in ctx.missing_dependencies {
if let Some(path) = dep.parent() {
// Parent must not be a file
assert!(!path.is_file(), "{path:?}");
}
}
}

0 comments on commit 3fc870a

Please sign in to comment.