LOAD_NODE_MODULES(X, START)
(
ctx: &Ctx<'_>,
x: &str,
start: String,
is_esm: bool,
)
| 426 | |
| 427 | // LOAD_NODE_MODULES(X, START) |
| 428 | fn load_node_modules<'a>( |
| 429 | ctx: &Ctx<'_>, |
| 430 | x: &str, |
| 431 | start: String, |
| 432 | is_esm: bool, |
| 433 | ) -> Option<Cow<'a, str>> { |
| 434 | trace!("| load_node_modules(x, start): ({}, {})", x, start); |
| 435 | |
| 436 | fn search_dir<'a>(ctx: &Ctx<'_>, dir: &str, x: &str, is_esm: bool) -> Option<Cow<'a, str>> { |
| 437 | // a. LOAD_PACKAGE_EXPORTS(X, DIR) |
| 438 | if let Ok(path) = load_package_exports(ctx, x, dir, is_esm) { |
| 439 | trace!("| load_node_modules(2.a): {}", path); |
| 440 | return Some(path); |
| 441 | } |
| 442 | let dir_slash_x = Rc::new([dir, "/", x].concat()); |
| 443 | // b. LOAD_AS_FILE(DIR/X) |
| 444 | if let Ok(Some(path)) = load_as_file(ctx, dir_slash_x.clone()) { |
| 445 | trace!("| load_node_modules(2.b): {}", path); |
| 446 | return Some(path); |
| 447 | } |
| 448 | // c. LOAD_AS_DIRECTORY(DIR/X) |
| 449 | if let Ok(Some(path)) = load_as_directory(ctx, dir_slash_x.clone()) { |
| 450 | trace!("| load_node_modules(2.c): {}", path); |
| 451 | return Some(path); |
| 452 | } |
| 453 | None |
| 454 | } |
| 455 | |
| 456 | // 1. let DIRS = NODE_MODULES_PATHS(START) |
| 457 | let mut cache = NODE_MODULES_PATHS_CACHE.lock().unwrap(); |
| 458 | |
| 459 | let start = start.into_boxed_str(); |
| 460 | |
| 461 | //fast path |
| 462 | if let Some(Some(dirs)) = cache.get(&start) { |
| 463 | for dir in dirs.0.borrow().iter() { |
| 464 | if let Some(path) = search_dir(ctx, dir, x, is_esm) { |
| 465 | return Some(path); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | let path = Path::new(start.as_ref()); |
| 471 | let results = NodePathList::new(); |
| 472 | let mut paths_to_cache = Vec::new(); |
| 473 | let mut current = Some(path); |
| 474 | |
| 475 | let mut i = 0; |
| 476 | let mut last_found_index = 0; |
| 477 | while let Some(dir) = current { |
| 478 | let str_dir = dir.to_string_lossy(); |
| 479 | if let Some(dirs) = cache.get(str_dir.as_ref()) { |
| 480 | if let Some(dirs) = dirs { |
| 481 | results |
| 482 | .0 |
| 483 | .borrow_mut() |
| 484 | .extend(dirs.0.borrow().clone().into_iter()); |
| 485 | } |
no test coverage detected