1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! https://github.com/webpack/enhanced-resolve/blob/main/test/dependencies.test.js
#[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows.
mod test {
use std::path::PathBuf;
use super::super::memory_fs::MemoryFS;
use crate::{ResolveContext, ResolveOptions, ResolverGeneric};
fn file_system() -> MemoryFS {
MemoryFS::new(&[
("/a/b/node_modules/some-module/index.js", ""),
("/a/node_modules/module/package.json", r#"{"main":"entry.js"}"#),
("/a/node_modules/module/file.js", r#"{"main":"entry.js"}"#),
("/modules/other-module/file.js", ""),
])
}
#[test]
fn test() {
let data = [
(
"middle module request",
"/a/b/c",
"module/file",
"/a/node_modules/module/file.js",
// These dependencies are different from enhanced-resolve due to different code path to
// querying the file system
vec![
// found package.json
"/a/node_modules/module/package.json",
// symlink checks
"/a/node_modules/module/file.js",
// "/a/node_modules/module",
// "/a/node_modules",
// "/a",
// "/",
],
vec![
// missing package.jsons
// "/a/b/c/package.json",
"/a/b/package.json",
"/a/package.json",
"/package.json",
// missing modules directories
"/a/b/c",
// "/a/b/c/node_modules",
// missing single file modules
"/modules/module",
"/a/b/node_modules/module",
// missing files with alternative extensions
"/a/node_modules/module/file",
"/a/node_modules/module/file.json",
],
),
(
"fast found module",
"/a/b/c",
"other-module/file.js",
"/modules/other-module/file.js",
// These dependencies are different from enhanced-resolve due to different code path to
// querying the file system
vec![
// symlink checks
"/modules/other-module/file.js",
// "/modules/other-module",
// "/modules",
// "/",
],
vec![
// missing package.jsons
// "/a/b/c/package.json",
"/a/b/c",
"/a/b/package.json",
"/a/package.json",
"/package.json",
"/modules/other-module/package.json",
"/modules/package.json",
],
),
];
for (name, context, request, result, file_dependencies, missing_dependencies) in data {
let file_system = file_system();
let resolver = ResolverGeneric::new_with_file_system(
file_system,
ResolveOptions {
extensions: vec![".json".into(), ".js".into()],
modules: vec!["/modules".into(), "node_modules".into()],
..ResolveOptions::default()
},
);
let mut ctx = ResolveContext::default();
let path = PathBuf::from(context);
let resolved_path =
resolver.resolve_with_context(path, request, None, &mut ctx).map(|r| r.full_path());
assert_eq!(resolved_path, Ok(PathBuf::from(result)));
let file_dependencies = file_dependencies.iter().map(PathBuf::from).collect();
let missing_dependencies = missing_dependencies.iter().map(PathBuf::from).collect();
assert_eq!(ctx.file_dependencies, file_dependencies, "{name} file_dependencies");
assert_eq!(
ctx.missing_dependencies, missing_dependencies,
"{name} missing_dependencies"
);
}
}
}