File size: 5,472 Bytes
1e92f2d |
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
use anyhow::Result;
use either::Either;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption},
file_source::FileSource,
raw_module::RawModule,
reference::ModuleReference,
resolve::{
ModuleResolveResult, RequestKey,
pattern::{Pattern, PatternMatch, read_matches},
},
source::Source,
};
#[turbo_tasks::value]
#[derive(Hash, Clone, Debug)]
pub struct PackageJsonReference {
pub package_json: FileSystemPath,
}
#[turbo_tasks::value_impl]
impl PackageJsonReference {
#[turbo_tasks::function]
pub fn new(package_json: FileSystemPath) -> Vc<Self> {
Self::cell(PackageJsonReference { package_json })
}
}
#[turbo_tasks::value_impl]
impl ModuleReference for PackageJsonReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
Ok(*ModuleResolveResult::module(ResolvedVc::upcast(
RawModule::new(Vc::upcast(FileSource::new(self.package_json.clone())))
.to_resolved()
.await?,
)))
}
}
#[turbo_tasks::value_impl]
impl ValueToString for PackageJsonReference {
#[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<RcStr>> {
Ok(Vc::cell(
format!(
"package.json {}",
self.package_json.value_to_string().await?
)
.into(),
))
}
}
#[turbo_tasks::value]
#[derive(Hash, Debug)]
pub struct DirAssetReference {
pub source: ResolvedVc<Box<dyn Source>>,
pub path: ResolvedVc<Pattern>,
}
#[turbo_tasks::value_impl]
impl DirAssetReference {
#[turbo_tasks::function]
pub fn new(source: ResolvedVc<Box<dyn Source>>, path: ResolvedVc<Pattern>) -> Vc<Self> {
Self::cell(DirAssetReference { source, path })
}
}
#[turbo_tasks::function]
async fn resolve_reference_from_dir(
parent_path: FileSystemPath,
path: Vc<Pattern>,
) -> Result<Vc<ModuleResolveResult>> {
let path_ref = path.await?;
let (abs_path, rel_path) = path_ref.split_could_match("/ROOT/");
let matches = match (abs_path, rel_path) {
(Some(abs_path), Some(rel_path)) => Either::Right(
read_matches(
parent_path.root().owned().await?,
rcstr!("/ROOT/"),
true,
Pattern::new(abs_path.or_any_nested_file()),
)
.await?
.into_iter()
.chain(
read_matches(
parent_path,
rcstr!(""),
true,
Pattern::new(rel_path.or_any_nested_file()),
)
.await?
.into_iter(),
),
),
(Some(abs_path), None) => Either::Left(
// absolute path only
read_matches(
parent_path.root().owned().await?,
rcstr!("/ROOT/"),
true,
Pattern::new(abs_path.or_any_nested_file()),
)
.await?
.into_iter(),
),
(None, Some(rel_path)) => Either::Left(
// relative path only
read_matches(
parent_path,
rcstr!(""),
true,
Pattern::new(rel_path.or_any_nested_file()),
)
.await?
.into_iter(),
),
(None, None) => return Ok(*ModuleResolveResult::unresolvable()),
};
let mut affecting_sources = Vec::new();
let mut results = Vec::new();
for pat_match in matches {
match pat_match {
PatternMatch::File(matched_path, file) => {
let realpath = file.realpath_with_links().await?;
for symlink in &realpath.symlinks {
affecting_sources.push(ResolvedVc::upcast(
FileSource::new(symlink.clone()).to_resolved().await?,
));
}
results.push((
RequestKey::new(matched_path.clone()),
ResolvedVc::upcast(
RawModule::new(Vc::upcast(FileSource::new(realpath.path.clone())))
.to_resolved()
.await?,
),
));
}
PatternMatch::Directory(..) => {}
}
}
Ok(*ModuleResolveResult::modules_with_affecting_sources(
results,
affecting_sources,
))
}
#[turbo_tasks::value_impl]
impl ModuleReference for DirAssetReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let parent_path = self.source.ident().path().await?.parent();
Ok(resolve_reference_from_dir(parent_path, *self.path))
}
}
#[turbo_tasks::value_impl]
impl ChunkableModuleReference for DirAssetReference {
#[turbo_tasks::function]
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
Vc::cell(Some(ChunkingType::Traced))
}
}
#[turbo_tasks::value_impl]
impl ValueToString for DirAssetReference {
#[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<RcStr>> {
Ok(Vc::cell(
format!("directory assets {}", self.path.to_string().await?,).into(),
))
}
}
|