Skip to content

Commit 82ef068

Browse files
authored
fix: abs path in main fields (#52)
1 parent 4cc4e44 commit 82ef068

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

fixtures/invalid/node_modules/a/dist/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/invalid/node_modules/a/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ mod tsconfig;
6262
#[cfg(test)]
6363
mod tests;
6464

65+
use dashmap::{mapref::one::Ref, DashMap};
66+
use rustc_hash::FxHashSet;
67+
use serde_json::Value as JSONValue;
6568
use std::{
6669
borrow::Cow,
6770
cmp::Ordering,
@@ -71,10 +74,6 @@ use std::{
7174
sync::Arc,
7275
};
7376

74-
use dashmap::{mapref::one::Ref, DashMap};
75-
use rustc_hash::FxHashSet;
76-
use serde_json::Value as JSONValue;
77-
7877
pub use crate::{
7978
builtins::NODEJS_BUILTINS,
8079
error::{JSONError, ResolveError, SpecifierError},
@@ -544,8 +543,16 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
544543
{
545544
// b. If "main" is a falsy value, GOTO 2.
546545
for main_field in package_json.main_fields(&self.options.main_fields) {
546+
// ref https://github.com/webpack/enhanced-resolve/blob/main/lib/MainFieldPlugin.js#L66-L67
547+
let main_field =
548+
if main_field.starts_with("./") || main_field.starts_with("../") {
549+
Cow::Borrowed(main_field)
550+
} else {
551+
Cow::Owned(format!("./{main_field}"))
552+
};
553+
547554
// c. let M = X + (json main field)
548-
let main_field_path = cached_path.path().normalize_with(main_field);
555+
let main_field_path = cached_path.path().normalize_with(main_field.as_ref());
549556
// d. LOAD_AS_FILE(M)
550557
let cached_path = self.cache.value(&main_field_path);
551558
if let Ok(Some(path)) = self.load_as_file(&cached_path, ctx) {

src/tests/simple.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ use std::env;
44

55
use crate::Resolver;
66

7+
#[test]
8+
fn resolve_abs_main() {
9+
let resolver = Resolver::default();
10+
let dirname = env::current_dir().unwrap().join("fixtures");
11+
let f = dirname.join("invalid/main.js");
12+
// a's main field id `/dist/index.js`
13+
let resolution = resolver.resolve(&f, "a").unwrap();
14+
15+
assert_eq!(resolution.path(), dirname.join("invalid/node_modules/a/dist/index.js"));
16+
}
17+
718
#[test]
819
fn simple() {
920
// mimic `enhanced-resolve/test/simple.test.js`

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy