fallow_core/plugins/registry/
virtual_module.rs1use std::path::Path;
6
7use crate::extract::ModuleInfo;
8
9const GENERATED_IMPORT_EXTENSIONS: &[&str] = &[".js", ".ts"];
12
13#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum VirtualModuleMatch {
16 Prefix(String),
21 Generated(String),
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct VirtualModuleProviderRule {
35 pub plugin: String,
37 pub scope: String,
40 pub matcher: VirtualModuleMatch,
43 pub package: String,
45}
46
47impl VirtualModuleProviderRule {
48 #[must_use]
55 pub fn matches(&self, root: &Path, path: &Path, specifier: &str) -> bool {
56 if !self.scope.is_empty() && !path.starts_with(root.join(&self.scope)) {
57 return false;
58 }
59 match &self.matcher {
60 VirtualModuleMatch::Prefix(prefix) => names_virtual_module(prefix, specifier),
61 VirtualModuleMatch::Generated(pattern) => names_generated_module(pattern, specifier),
62 }
63 }
64}
65
66fn names_generated_module(pattern: &str, specifier: &str) -> bool {
67 let bare = GENERATED_IMPORT_EXTENSIONS
68 .iter()
69 .find_map(|extension| specifier.strip_suffix(extension))
70 .unwrap_or(specifier);
71 let Some((dir, name)) = bare.rsplit_once('/') else {
72 return false;
73 };
74 name == pattern.trim_start_matches('/')
75 && dir
76 .split('/')
77 .all(|segment| segment == "." || segment == "..")
78}
79
80fn names_virtual_module(prefix: &str, specifier: &str) -> bool {
81 if prefix.ends_with('/') {
82 return specifier.starts_with(prefix);
83 }
84 specifier
85 .strip_prefix(prefix)
86 .is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
87}
88
89pub fn module_specifiers(module: &ModuleInfo) -> impl Iterator<Item = &str> {
93 module
94 .imports
95 .iter()
96 .map(|import| import.source.as_str())
97 .chain(
98 module
99 .re_exports
100 .iter()
101 .map(|re_export| re_export.source.as_str()),
102 )
103 .chain(
104 module
105 .dynamic_imports
106 .iter()
107 .map(|import| import.source.as_str()),
108 )
109 .chain(module.require_calls.iter().map(|call| call.source.as_str()))
110}
111
112#[cfg(test)]
113mod tests {
114 use std::path::Path;
115
116 use super::{VirtualModuleMatch, VirtualModuleProviderRule};
117
118 fn rule(matcher: VirtualModuleMatch) -> VirtualModuleProviderRule {
119 VirtualModuleProviderRule {
120 plugin: "sveltekit".to_string(),
121 scope: String::new(),
122 matcher,
123 package: "@sveltejs/kit".to_string(),
124 }
125 }
126
127 fn matches(matcher: VirtualModuleMatch, specifier: &str) -> bool {
128 let root = Path::new("/project");
129 rule(matcher).matches(root, &root.join("src/routes/+page.ts"), specifier)
130 }
131
132 #[test]
133 fn a_prefix_without_a_slash_names_one_module_and_its_subpaths() {
134 let prefix = || VirtualModuleMatch::Prefix("$service-worker".to_string());
135 assert!(matches(prefix(), "$service-worker"));
136 assert!(matches(prefix(), "$service-worker/build"));
137 assert!(!matches(prefix(), "$service-workerx"));
138 }
139
140 #[test]
141 fn a_prefix_with_a_slash_names_each_module_below_it() {
142 let prefix = || VirtualModuleMatch::Prefix("$app/".to_string());
143 assert!(matches(prefix(), "$app/state"));
144 assert!(!matches(prefix(), "$app"));
145 assert!(!matches(prefix(), "$application/state"));
146 }
147
148 #[test]
149 fn a_generated_import_must_be_relative() {
150 let generated = || VirtualModuleMatch::Generated("/$types".to_string());
151 assert!(matches(generated(), "./$types"));
152 assert!(matches(generated(), "../$types.js"));
153 assert!(matches(generated(), "../../$types.ts"));
154 assert!(!matches(generated(), "some-pkg/$types"));
155 assert!(!matches(generated(), "./$typesx"));
156 assert!(!matches(generated(), "./lib/$types"));
157 assert!(!matches(generated(), "../shared/$types.js"));
158 }
159
160 #[test]
161 fn a_scoped_rule_covers_only_its_package() {
162 let root = Path::new("/project");
163 let scoped = VirtualModuleProviderRule {
164 scope: "packages/app".to_string(),
165 ..rule(VirtualModuleMatch::Prefix("$app/".to_string()))
166 };
167 assert!(scoped.matches(root, &root.join("packages/app/src/a.ts"), "$app/state"));
168 assert!(!scoped.matches(root, &root.join("packages/other/src/a.ts"), "$app/state"));
169 }
170}