1mod dynamic_imports;
19pub(crate) mod fallbacks;
20mod path_info;
21mod re_exports;
22mod react_native;
23mod require_imports;
24mod specifier;
25mod static_imports;
26#[cfg(test)]
27mod tests;
28mod types;
29mod upgrades;
30
31pub use fallbacks::extract_package_name_from_node_modules_path;
32pub use path_info::{
33 extract_package_name, is_bare_specifier, is_path_alias, is_valid_package_name,
34};
35pub use types::{
36 ResolveResult, ResolvedImport, ResolvedModule, ResolvedReExport, ResolvedSourceEdge,
37};
38
39use std::path::{Path, PathBuf};
40use std::sync::Mutex;
41
42use rayon::prelude::*;
43use rustc_hash::{FxHashMap, FxHashSet};
44
45use fallow_config::{AutoImportKind, AutoImportRule};
46use fallow_types::discover::{DiscoveredFile, FileId};
47use fallow_types::extract::{ImportInfo, ImportedName, ModuleInfo};
48use oxc_span::Span;
49
50use dynamic_imports::{resolve_dynamic_imports, resolve_dynamic_patterns};
51use re_exports::resolve_re_exports;
52use react_native::{build_condition_names, build_extensions};
53use require_imports::resolve_require_imports;
54use specifier::create_resolver;
55use static_imports::resolve_static_imports;
56use types::{PackageManifestInfo, ResolveContext};
57use upgrades::apply_specifier_upgrades;
58
59#[must_use]
61#[expect(
62 clippy::too_many_arguments,
63 reason = "resolver inputs come from disjoint sources (config, plugins, workspace, filesystem); \
64 bundling them into a struct would be a cross-cutting refactor outside this task"
65)]
66pub fn resolve_all_imports(
67 modules: &[ModuleInfo],
68 files: &[DiscoveredFile],
69 workspaces: &[fallow_config::WorkspaceInfo],
70 active_plugins: &[String],
71 path_aliases: &[(String, String)],
72 auto_imports: &[AutoImportRule],
73 scss_include_paths: &[PathBuf],
74 static_dir_mappings: &[(PathBuf, String)],
75 root: &Path,
76 extra_conditions: &[String],
77) -> Vec<ResolvedModule> {
78 let canonical_ws_roots: Vec<PathBuf> = workspaces
79 .par_iter()
80 .map(|ws| dunce::canonicalize(&ws.root).unwrap_or_else(|_| ws.root.clone()))
81 .collect();
82 let workspace_roots: FxHashMap<&str, &Path> = workspaces
83 .iter()
84 .zip(canonical_ws_roots.iter())
85 .map(|(ws, canonical)| (ws.name.as_str(), canonical.as_path()))
86 .collect();
87 let root_canonical = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
88 let mut package_manifests = Vec::new();
89 if let Ok(package_json) = fallow_config::PackageJson::load(&root.join("package.json")) {
90 package_manifests.push(PackageManifestInfo {
91 root: root.to_path_buf(),
92 canonical_root: root_canonical,
93 name: package_json.name.clone(),
94 package_json,
95 });
96 }
97 for (ws, canonical_root) in workspaces.iter().zip(canonical_ws_roots.iter()) {
98 if let Ok(package_json) = fallow_config::PackageJson::load(&ws.root.join("package.json")) {
99 package_manifests.push(PackageManifestInfo {
100 root: ws.root.clone(),
101 canonical_root: canonical_root.clone(),
102 name: package_json.name.clone().or_else(|| Some(ws.name.clone())),
103 package_json,
104 });
105 }
106 }
107
108 let root_is_canonical = dunce::canonicalize(root).is_ok_and(|c| c == root);
109
110 let canonical_paths: Vec<PathBuf> = if root_is_canonical {
111 Vec::new()
112 } else {
113 files
114 .par_iter()
115 .map(|f| dunce::canonicalize(&f.path).unwrap_or_else(|_| f.path.clone()))
116 .collect()
117 };
118
119 let path_to_id: FxHashMap<&Path, FileId> = if root_is_canonical {
120 files.iter().map(|f| (f.path.as_path(), f.id)).collect()
121 } else {
122 canonical_paths
123 .iter()
124 .enumerate()
125 .map(|(idx, canonical)| (canonical.as_path(), files[idx].id))
126 .collect()
127 };
128
129 let raw_path_to_id: FxHashMap<&Path, FileId> =
130 files.iter().map(|f| (f.path.as_path(), f.id)).collect();
131
132 let file_paths: Vec<&Path> = files.iter().map(|f| f.path.as_path()).collect();
133
134 let extensions = build_extensions(active_plugins);
135 let condition_names = build_condition_names(active_plugins, extra_conditions);
136 let resolver = create_resolver(active_plugins, extra_conditions);
137 let mut style_conditions = extra_conditions.to_vec();
138 style_conditions.push("sass".to_string());
139 style_conditions.push("style".to_string());
140 let style_resolver = create_resolver(active_plugins, &style_conditions);
141
142 let canonical_fallback = if root_is_canonical {
143 Some(types::CanonicalFallback::new(files))
144 } else {
145 None
146 };
147
148 let tsconfig_warned: Mutex<FxHashSet<String>> = Mutex::new(FxHashSet::default());
149
150 let ctx = ResolveContext {
151 resolver: &resolver,
152 style_resolver: &style_resolver,
153 extensions: &extensions,
154 path_to_id: &path_to_id,
155 raw_path_to_id: &raw_path_to_id,
156 workspace_roots: &workspace_roots,
157 package_manifests: &package_manifests,
158 condition_names: &condition_names,
159 path_aliases,
160 scss_include_paths,
161 static_dir_mappings,
162 root,
163 canonical_fallback: canonical_fallback.as_ref(),
164 tsconfig_warned: &tsconfig_warned,
165 };
166
167 let mut resolved: Vec<ResolvedModule> = modules
168 .par_iter()
169 .filter_map(|module| {
170 resolve_module_imports(module, &ctx, &file_paths, &canonical_paths, files)
171 })
172 .collect();
173
174 apply_specifier_upgrades(&mut resolved);
175
176 synthesize_auto_import_edges(
177 &mut resolved,
178 modules,
179 auto_imports,
180 &path_to_id,
181 &raw_path_to_id,
182 );
183
184 resolved
185}
186
187fn resolve_module_imports(
188 module: &ModuleInfo,
189 ctx: &ResolveContext<'_>,
190 file_paths: &[&Path],
191 canonical_paths: &[PathBuf],
192 files: &[DiscoveredFile],
193) -> Option<ResolvedModule> {
194 let Some(file_path) = file_paths.get(module.file_id.0 as usize) else {
195 tracing::warn!(
196 file_id = module.file_id.0,
197 "Skipping module with unknown file_id during resolution"
198 );
199 return None;
200 };
201
202 let mut all_imports = resolve_static_imports(ctx, file_path, &module.imports);
203 all_imports.extend(resolve_require_imports(
204 ctx,
205 file_path,
206 &module.require_calls,
207 ));
208
209 let from_dir = if canonical_paths.is_empty() {
210 file_path.parent().unwrap_or(file_path)
211 } else {
212 canonical_paths
213 .get(module.file_id.0 as usize)
214 .and_then(|p| p.parent())
215 .unwrap_or(file_path)
216 };
217
218 Some(build_resolved_module(
219 module,
220 ctx,
221 file_path,
222 from_dir,
223 canonical_paths,
224 files,
225 all_imports,
226 ))
227}
228
229fn build_resolved_module(
230 module: &ModuleInfo,
231 ctx: &ResolveContext<'_>,
232 file_path: &Path,
233 from_dir: &Path,
234 canonical_paths: &[PathBuf],
235 files: &[DiscoveredFile],
236 all_imports: Vec<types::ResolvedImport>,
237) -> ResolvedModule {
238 ResolvedModule {
239 file_id: module.file_id,
240 path: file_path.to_path_buf(),
241 exports: module.exports.clone(),
242 re_exports: resolve_re_exports(ctx, file_path, &module.re_exports),
243 resolved_imports: all_imports,
244 resolved_dynamic_imports: resolve_dynamic_imports(ctx, file_path, &module.dynamic_imports),
245 resolved_dynamic_patterns: resolve_dynamic_patterns(
246 from_dir,
247 &module.dynamic_import_patterns,
248 canonical_paths,
249 files,
250 ),
251 member_accesses: module.member_accesses.clone(),
252 whole_object_uses: module.whole_object_uses.clone(),
253 has_cjs_exports: module.has_cjs_exports,
254 has_angular_component_template_url: module.has_angular_component_template_url,
255 unused_import_bindings: module.unused_import_bindings.iter().cloned().collect(),
256 type_referenced_import_bindings: module.type_referenced_import_bindings.clone(),
257 value_referenced_import_bindings: module.value_referenced_import_bindings.clone(),
258 namespace_object_aliases: module.namespace_object_aliases.clone(),
259 }
260}
261
262fn synthesize_auto_import_edges(
270 resolved: &mut [ResolvedModule],
271 modules: &[ModuleInfo],
272 auto_imports: &[AutoImportRule],
273 path_to_id: &FxHashMap<&Path, FileId>,
274 raw_path_to_id: &FxHashMap<&Path, FileId>,
275) {
276 if auto_imports.is_empty() {
277 return;
278 }
279
280 let mut table: FxHashMap<&str, Vec<(FileId, AutoImportKind)>> = FxHashMap::default();
281 for rule in auto_imports {
282 let source = rule.source.as_path();
283 let Some(file_id) = raw_path_to_id
284 .get(source)
285 .or_else(|| path_to_id.get(source))
286 .copied()
287 else {
288 continue;
289 };
290 table
291 .entry(rule.name.as_str())
292 .or_default()
293 .push((file_id, rule.kind));
294 }
295 if table.is_empty() {
296 return;
297 }
298
299 let candidates: FxHashMap<FileId, &[String]> = modules
300 .iter()
301 .filter(|module| !module.auto_import_candidates.is_empty())
302 .map(|module| (module.file_id, module.auto_import_candidates.as_slice()))
303 .collect();
304 if candidates.is_empty() {
305 return;
306 }
307
308 for module in resolved.iter_mut() {
309 let Some(names) = candidates.get(&module.file_id) else {
310 continue;
311 };
312 for name in *names {
313 if is_auto_import_builtin(name) {
314 continue;
315 }
316 let Some(targets) = table.get(name.as_str()) else {
317 continue;
318 };
319 for (target_id, kind) in targets {
320 if *target_id == module.file_id {
321 continue;
322 }
323 module.resolved_imports.push(ResolvedImport {
324 info: synthetic_auto_import_info(name, *kind),
325 target: ResolveResult::InternalModule(*target_id),
326 });
327 }
328 }
329 }
330}
331
332fn is_auto_import_builtin(name: &str) -> bool {
333 is_js_auto_import_builtin(name)
334 || is_vue_auto_import_builtin(name)
335 || is_nuxt_auto_import_builtin(name)
336}
337
338fn is_js_auto_import_builtin(name: &str) -> bool {
339 matches!(
340 name,
341 "AbortController"
342 | "AbortSignal"
343 | "Array"
344 | "ArrayBuffer"
345 | "BigInt"
346 | "Blob"
347 | "Boolean"
348 | "Buffer"
349 | "CSS"
350 | "DOMParser"
351 | "Date"
352 | "Document"
353 | "Error"
354 | "Event"
355 | "EventTarget"
356 | "File"
357 | "FormData"
358 | "Intl"
359 | "JSON"
360 | "Map"
361 | "Math"
362 | "Number"
363 | "Object"
364 | "Promise"
365 | "Reflect"
366 | "RegExp"
367 | "Response"
368 | "Set"
369 | "String"
370 | "Symbol"
371 | "URL"
372 | "URLSearchParams"
373 | "WeakMap"
374 | "WeakSet"
375 | "Window"
376 | "alert"
377 | "clearInterval"
378 | "clearTimeout"
379 | "console"
380 | "document"
381 | "fetch"
382 | "global"
383 | "globalThis"
384 | "localStorage"
385 | "navigator"
386 | "process"
387 | "requestAnimationFrame"
388 | "sessionStorage"
389 | "setInterval"
390 | "setTimeout"
391 | "window"
392 )
393}
394
395fn is_vue_auto_import_builtin(name: &str) -> bool {
396 matches!(name, |"computed"| "customRef"
397 | "defineAsyncComponent"
398 | "defineComponent"
399 | "effectScope"
400 | "getCurrentInstance"
401 | "h"
402 | "inject"
403 | "isProxy"
404 | "isReactive"
405 | "isReadonly"
406 | "isRef"
407 | "markRaw"
408 | "nextTick"
409 | "onActivated"
410 | "onBeforeMount"
411 | "onBeforeUnmount"
412 | "onBeforeUpdate"
413 | "onDeactivated"
414 | "onErrorCaptured"
415 | "onMounted"
416 | "onRenderTracked"
417 | "onRenderTriggered"
418 | "onScopeDispose"
419 | "onServerPrefetch"
420 | "onUnmounted"
421 | "onUpdated"
422 | "provide"
423 | "reactive"
424 | "readonly"
425 | "ref"
426 | "resolveComponent"
427 | "shallowReactive"
428 | "shallowReadonly"
429 | "shallowRef"
430 | "toRaw"
431 | "toRef"
432 | "toRefs"
433 | "triggerRef"
434 | "unref"
435 | "watch"
436 | "watchEffect"
437 | "watchPostEffect"
438 | "watchSyncEffect")
439}
440
441fn is_nuxt_auto_import_builtin(name: &str) -> bool {
442 matches!(name, |"useAsyncData"| "useCookie"
443 | "useError"
444 | "useFetch"
445 | "useHead"
446 | "useLazyAsyncData"
447 | "useLazyFetch"
448 | "useNuxtApp"
449 | "useRequestEvent"
450 | "useRequestHeaders"
451 | "useRoute"
452 | "useRouter"
453 | "useRuntimeConfig"
454 | "useSeoMeta"
455 | "useState")
456}
457
458fn synthetic_auto_import_info(name: &str, kind: AutoImportKind) -> ImportInfo {
461 let imported_name = match kind {
462 AutoImportKind::Named => ImportedName::Named(name.to_string()),
463 AutoImportKind::Default | AutoImportKind::DefaultComponent => ImportedName::Default,
464 };
465 ImportInfo {
466 source: format!("<auto-import:{name}>"),
467 imported_name,
468 local_name: name.to_string(),
469 is_type_only: false,
470 from_style: false,
471 span: Span::default(),
472 source_span: Span::default(),
473 }
474}