Skip to content

Commit

Permalink
Remove by_glob from ImportMap (#3069)
Browse files Browse the repository at this point in the history
I can't remember us ever using it. I think we should remove it
until we find a use for it again in the future (if ever).
  • Loading branch information
alexkirsz committed Dec 20, 2022
1 parent 31ba0c3 commit 66407e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
36 changes: 10 additions & 26 deletions crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,13 @@ impl AliasTemplate for ImportMappingVc {
#[turbo_tasks::value(shared)]
#[derive(Clone, Default)]
pub struct ImportMap {
direct: AliasMap<ImportMappingVc>,
by_glob: Vec<(Glob, ImportMappingVc)>,
map: AliasMap<ImportMappingVc>,
}

impl ImportMap {
/// Creates a new import map.
pub fn new(
direct: AliasMap<ImportMappingVc>,
by_glob: Vec<(Glob, ImportMappingVc)>,
) -> ImportMap {
Self { direct, by_glob }
pub fn new(map: AliasMap<ImportMappingVc>) -> ImportMap {
Self { map }
}

/// Creates a new empty import map.
Expand All @@ -176,14 +172,13 @@ impl ImportMap {

/// Extends the import map with another import map.
pub fn extend(&mut self, other: &ImportMap) {
let Self { direct, by_glob } = other.clone();
self.direct.extend(direct);
self.by_glob.extend(by_glob);
let Self { map } = other.clone();
self.map.extend(map);
}

/// Inserts an alias into the import map.
pub fn insert_alias(&mut self, alias: AliasPattern, mapping: ImportMappingVc) {
self.direct.insert(alias, mapping);
self.map.insert(alias, mapping);
}

/// Inserts an exact alias into the import map.
Expand All @@ -192,7 +187,7 @@ impl ImportMap {
pattern: impl Into<String> + 'a,
mapping: ImportMappingVc,
) {
self.direct.insert(AliasPattern::exact(pattern), mapping);
self.map.insert(AliasPattern::exact(pattern), mapping);
}

/// Inserts a wildcard alias into the import map.
Expand All @@ -201,8 +196,7 @@ impl ImportMap {
prefix: impl Into<String> + 'a,
mapping: ImportMappingVc,
) {
self.direct
.insert(AliasPattern::wildcard(prefix, ""), mapping);
self.map.insert(AliasPattern::wildcard(prefix, ""), mapping);
}

/// Inserts a wildcard alias with suffix into the import map.
Expand All @@ -212,7 +206,7 @@ impl ImportMap {
suffix: impl Into<String> + 's,
mapping: ImportMappingVc,
) {
self.direct
self.map
.insert(AliasPattern::wildcard(prefix, suffix), mapping);
}
}
Expand Down Expand Up @@ -333,24 +327,14 @@ impl ImportMapVc {
let this = self.await?;
// TODO lookup pattern
if let Some(request_string) = request.await?.request() {
if let Some(result) = this.direct.lookup(&request_string).next() {
if let Some(result) = this.map.lookup(&request_string).next() {
return Ok(import_mapping_to_result(
result.try_join_into_self().await?.into_owned(),
request,
)
.await?
.into());
}
let request_string_without_slash = if request_string.ends_with('/') {
&request_string[..request_string.len() - 1]
} else {
&request_string
};
for (glob, mapping) in this.by_glob.iter() {
if glob.execute(request_string_without_slash) {
return Ok(import_mapping_to_result(*mapping, request).await?.into());
}
}
}
Ok(ImportMapResult::NoEntry.into())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async fn base_resolve_options(
}
}

let mut import_map = ImportMap::new(direct_mappings, Default::default());
let mut import_map = ImportMap::new(direct_mappings);
if let Some(additional_import_map) = opt.import_map {
let additional_import_map = additional_import_map.await?;
import_map.extend(&additional_import_map);
Expand Down

0 comments on commit 66407e6

Please sign in to comment.