Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Jul 26, 2024
1 parent d064739 commit 2c96bfc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
4 changes: 2 additions & 2 deletions crates/turbopack-core/src/resolve/alias_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,10 @@ pub trait AliasTemplate {
Self: 'a;

/// Turn `self` into a `Self::Output`
fn convert<'a>(&'a self) -> Self::Output<'a>;
fn convert(&self) -> Self::Output<'_>;

/// Replaces `capture` within `self`.
fn replace<'a>(&'a self, capture: &'a Pattern) -> Self::Output<'a>;
fn replace<'a>(&'a self, capture: &Pattern) -> Self::Output<'a>;
}

#[cfg(test)]
Expand Down
38 changes: 31 additions & 7 deletions crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,35 @@ impl ImportMapping {
impl AliasTemplate for Vc<ImportMapping> {
type Output<'a> = Pin<Box<dyn Future<Output = Result<Vc<ReplacedImportMapping>>> + Send + 'a>>;

fn replace<'a>(&'a self, capture: &'a Pattern) -> Self::Output<'a> {
fn convert(&self) -> Self::Output<'_> {
Box::pin(async move {
let this = &*self.await?;
Ok(match this {
// _ => ImportMapping::Ignore
ImportMapping::External(name, ty) => {
ReplacedImportMapping::External(name.clone(), *ty)
}
ImportMapping::PrimaryAlternative(name, context) => {
ReplacedImportMapping::PrimaryAlternative((*name).clone().into(), *context)
}
ImportMapping::Direct(v) => ReplacedImportMapping::Direct(*v),
ImportMapping::Ignore => ReplacedImportMapping::Ignore,
ImportMapping::Empty => ReplacedImportMapping::Empty,
ImportMapping::Alternatives(alternatives) => ReplacedImportMapping::Alternatives(
alternatives
.iter()
.map(|mapping| mapping.convert())
.try_join()
.await?,
),
ImportMapping::Dynamic(replacement) => ReplacedImportMapping::Dynamic(*replacement),
}
.cell())
})
}

fn replace<'a>(&'a self, capture: &Pattern) -> Self::Output<'a> {
let capture = capture.clone();
Box::pin(async move {
let this = &*self.await?;
Ok(match this {
Expand All @@ -161,13 +189,13 @@ impl AliasTemplate for Vc<ImportMapping> {
*context,
)
}
ImportMapping::Direct(v) => ReplacedImportMapping::Direct(v.clone()),
ImportMapping::Direct(v) => ReplacedImportMapping::Direct(*v),
ImportMapping::Ignore => ReplacedImportMapping::Ignore,
ImportMapping::Empty => ReplacedImportMapping::Empty,
ImportMapping::Alternatives(alternatives) => ReplacedImportMapping::Alternatives(
alternatives
.iter()
.map(|mapping| mapping.replace(capture))
.map(|mapping| mapping.replace(&capture))
.try_join()
.await?,
),
Expand All @@ -178,10 +206,6 @@ impl AliasTemplate for Vc<ImportMapping> {
.cell())
})
}

fn convert<'a>(&'a self) -> Self::Output<'a> {
todo!()
}
}

#[turbo_tasks::value(shared)]
Expand Down
54 changes: 27 additions & 27 deletions crates/turbopack-core/src/resolve/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,33 +378,33 @@ impl<'a> Iterator for ResultsIterMut<'a> {
}
}

struct ResultsPatternIterMut<'a> {
stack: Vec<&'a mut SubpathValueResult>,
}

impl<'a> Iterator for ResultsPatternIterMut<'a> {
type Item = &'a mut Pattern;

fn next(&mut self) -> Option<Self::Item> {
while let Some(value) = self.stack.pop() {
match value {
SubpathValueResult::Alternatives(list) => {
for value in list {
self.stack.push(value);
}
}
SubpathValueResult::Conditional(list) => {
for (_, value) in list {
self.stack.push(value);
}
}
SubpathValueResult::Result(r) => return Some(r),
SubpathValueResult::Excluded => {}
}
}
None
}
}
// struct ResultsPatternIterMut<'a> {
// stack: Vec<&'a mut SubpathValueResult>,
// }

// impl<'a> Iterator for ResultsPatternIterMut<'a> {
// type Item = &'a mut Pattern;

// fn next(&mut self) -> Option<Self::Item> {
// while let Some(value) = self.stack.pop() {
// match value {
// SubpathValueResult::Alternatives(list) => {
// for value in list {
// self.stack.push(value);
// }
// }
// SubpathValueResult::Conditional(list) => {
// for (_, value) in list {
// self.stack.push(value);
// }
// }
// SubpathValueResult::Result(r) => return Some(r),
// SubpathValueResult::Excluded => {}
// }
// }
// None
// }
// }

/// Content of an "exports" field in a package.json
#[derive(PartialEq, Eq, Serialize, Deserialize)]
Expand Down

0 comments on commit 2c96bfc

Please sign in to comment.