use std::path::Path;
use crate::extract::ModuleInfo;
const GENERATED_IMPORT_EXTENSIONS: &[&str] = &[".js", ".ts"];
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VirtualModuleMatch {
Prefix(String),
Generated(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VirtualModuleProviderRule {
pub plugin: String,
pub scope: String,
pub matcher: VirtualModuleMatch,
pub package: String,
}
impl VirtualModuleProviderRule {
#[must_use]
pub fn matches(&self, root: &Path, path: &Path, specifier: &str) -> bool {
if !self.scope.is_empty() && !path.starts_with(root.join(&self.scope)) {
return false;
}
match &self.matcher {
VirtualModuleMatch::Prefix(prefix) => names_virtual_module(prefix, specifier),
VirtualModuleMatch::Generated(pattern) => names_generated_module(pattern, specifier),
}
}
}
fn names_generated_module(pattern: &str, specifier: &str) -> bool {
let bare = GENERATED_IMPORT_EXTENSIONS
.iter()
.find_map(|extension| specifier.strip_suffix(extension))
.unwrap_or(specifier);
let Some((dir, name)) = bare.rsplit_once('/') else {
return false;
};
name == pattern.trim_start_matches('/')
&& dir
.split('/')
.all(|segment| segment == "." || segment == "..")
}
fn names_virtual_module(prefix: &str, specifier: &str) -> bool {
if prefix.ends_with('/') {
return specifier.starts_with(prefix);
}
specifier
.strip_prefix(prefix)
.is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
}
pub fn module_specifiers(module: &ModuleInfo) -> impl Iterator<Item = &str> {
module
.imports
.iter()
.map(|import| import.source.as_str())
.chain(
module
.re_exports
.iter()
.map(|re_export| re_export.source.as_str()),
)
.chain(
module
.dynamic_imports
.iter()
.map(|import| import.source.as_str()),
)
.chain(module.require_calls.iter().map(|call| call.source.as_str()))
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::{VirtualModuleMatch, VirtualModuleProviderRule};
fn rule(matcher: VirtualModuleMatch) -> VirtualModuleProviderRule {
VirtualModuleProviderRule {
plugin: "sveltekit".to_string(),
scope: String::new(),
matcher,
package: "@sveltejs/kit".to_string(),
}
}
fn matches(matcher: VirtualModuleMatch, specifier: &str) -> bool {
let root = Path::new("/project");
rule(matcher).matches(root, &root.join("src/routes/+page.ts"), specifier)
}
#[test]
fn a_prefix_without_a_slash_names_one_module_and_its_subpaths() {
let prefix = || VirtualModuleMatch::Prefix("$service-worker".to_string());
assert!(matches(prefix(), "$service-worker"));
assert!(matches(prefix(), "$service-worker/build"));
assert!(!matches(prefix(), "$service-workerx"));
}
#[test]
fn a_prefix_with_a_slash_names_each_module_below_it() {
let prefix = || VirtualModuleMatch::Prefix("$app/".to_string());
assert!(matches(prefix(), "$app/state"));
assert!(!matches(prefix(), "$app"));
assert!(!matches(prefix(), "$application/state"));
}
#[test]
fn a_generated_import_must_be_relative() {
let generated = || VirtualModuleMatch::Generated("/$types".to_string());
assert!(matches(generated(), "./$types"));
assert!(matches(generated(), "../$types.js"));
assert!(matches(generated(), "../../$types.ts"));
assert!(!matches(generated(), "some-pkg/$types"));
assert!(!matches(generated(), "./$typesx"));
assert!(!matches(generated(), "./lib/$types"));
assert!(!matches(generated(), "../shared/$types.js"));
}
#[test]
fn a_scoped_rule_covers_only_its_package() {
let root = Path::new("/project");
let scoped = VirtualModuleProviderRule {
scope: "packages/app".to_string(),
..rule(VirtualModuleMatch::Prefix("$app/".to_string()))
};
assert!(scoped.matches(root, &root.join("packages/app/src/a.ts"), "$app/state"));
assert!(!scoped.matches(root, &root.join("packages/other/src/a.ts"), "$app/state"));
}
}