Skip to content

Commit 9e31b2d

Browse files
committed
review: ResolveOptions.parse_data_uris
1 parent af864e9 commit 9e31b2d

File tree

3 files changed

+46
-59
lines changed

3 files changed

+46
-59
lines changed

turbopack/crates/turbopack-core/src/resolve/mod.rs

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
output::{OutputAsset, OutputAssets},
4242
package_json::{read_package_json, PackageJsonIssue},
4343
raw_module::RawModule,
44-
reference_type::{ReferenceType, UrlReferenceSubType},
44+
reference_type::ReferenceType,
4545
resolve::{
4646
node::{node_cjs_resolve_options, node_esm_resolve_options},
4747
parse::stringify_data_uri,
@@ -1589,14 +1589,10 @@ pub async fn resolve_inline(
15891589
handle_before_resolve_plugins(lookup_path, reference_type.clone(), request, options)
15901590
.await?;
15911591

1592-
let retain_data_url = matches!(
1593-
*reference_type,
1594-
ReferenceType::Url(UrlReferenceSubType::CssUrl)
1595-
);
15961592
let raw_result = match before_plugins_result {
15971593
Some(result) => result,
15981594
None => {
1599-
resolve_internal(lookup_path, request, options, retain_data_url)
1595+
resolve_internal(lookup_path, request, options)
16001596
.resolve()
16011597
.await?
16021598
}
@@ -1761,16 +1757,14 @@ async fn resolve_internal(
17611757
lookup_path: ResolvedVc<FileSystemPath>,
17621758
request: ResolvedVc<Request>,
17631759
options: ResolvedVc<ResolveOptions>,
1764-
retain_data_url: bool,
17651760
) -> Result<Vc<ResolveResult>> {
1766-
resolve_internal_inline(*lookup_path, *request, *options, retain_data_url).await
1761+
resolve_internal_inline(*lookup_path, *request, *options).await
17671762
}
17681763

17691764
async fn resolve_internal_inline(
17701765
lookup_path: Vc<FileSystemPath>,
17711766
request: Vc<Request>,
17721767
options: Vc<ResolveOptions>,
1773-
retain_data_url: bool,
17741768
) -> Result<Vc<ResolveResult>> {
17751769
let span = {
17761770
let lookup_path = lookup_path.to_string().await?.to_string();
@@ -1825,9 +1819,7 @@ async fn resolve_internal_inline(
18251819
Request::Alternatives { requests } => {
18261820
let results = requests
18271821
.iter()
1828-
.map(|req| async {
1829-
resolve_internal_inline(lookup_path, **req, options, retain_data_url).await
1830-
})
1822+
.map(|req| async { resolve_internal_inline(lookup_path, **req, options).await })
18311823
.try_join()
18321824
.await?;
18331825

@@ -1960,7 +1952,6 @@ async fn resolve_internal_inline(
19601952
lookup_path.root(),
19611953
relative,
19621954
options,
1963-
retain_data_url,
19641955
))
19651956
.await?
19661957
}
@@ -2014,26 +2005,33 @@ async fn resolve_internal_inline(
20142005
encoding,
20152006
data,
20162007
} => {
2017-
if !retain_data_url {
2018-
*ResolveResult::primary(ResolveResultItem::Source(ResolvedVc::upcast(
2019-
DataUriSource::new(
2020-
media_type.clone(),
2021-
encoding.clone(),
2022-
**data,
2023-
lookup_path,
2024-
)
2025-
.to_resolved()
2026-
.await?,
2027-
)))
2008+
// Behave like Request::Uri
2009+
let uri: RcStr = stringify_data_uri(media_type, encoding, *data)
2010+
.await?
2011+
.into();
2012+
if options.await?.parse_data_uris {
2013+
*ResolveResult::primary_with_key(
2014+
RequestKey::new(uri.clone()),
2015+
ResolveResultItem::Source(ResolvedVc::upcast(
2016+
DataUriSource::new(
2017+
media_type.clone(),
2018+
encoding.clone(),
2019+
**data,
2020+
lookup_path,
2021+
)
2022+
.to_resolved()
2023+
.await?,
2024+
)),
2025+
)
20282026
} else {
2029-
let uri: RcStr = stringify_data_uri(media_type, encoding, *data)
2030-
.await?
2031-
.into();
2032-
*ResolveResult::primary(ResolveResultItem::External {
2033-
name: uri,
2034-
ty: ExternalType::Url,
2035-
traced: ExternalTraced::Untraced,
2036-
})
2027+
*ResolveResult::primary_with_key(
2028+
RequestKey::new(uri.clone()),
2029+
ResolveResultItem::External {
2030+
name: uri,
2031+
ty: ExternalType::Url,
2032+
traced: ExternalTraced::Untraced,
2033+
},
2034+
)
20372035
}
20382036
}
20392037
Request::Uri {
@@ -2127,10 +2125,9 @@ async fn resolve_into_folder(
21272125
} else {
21282126
options
21292127
};
2130-
let result =
2131-
&*resolve_internal_inline(*package_path, *request, options, false)
2132-
.await?
2133-
.await?;
2128+
let result = &*resolve_internal_inline(*package_path, *request, options)
2129+
.await?
2130+
.await?;
21342131
// we are not that strict when a main field fails to resolve
21352132
// we continue to try other alternatives
21362133
if !result.is_unresolvable_ref() {
@@ -2166,11 +2163,9 @@ async fn resolve_into_folder(
21662163

21672164
let request = Request::parse(Value::new(pattern));
21682165

2169-
Ok(
2170-
resolve_internal_inline(*package_path, request, options, false)
2171-
.await?
2172-
.with_request(".".into()),
2173-
)
2166+
Ok(resolve_internal_inline(*package_path, request, options)
2167+
.await?
2168+
.with_request(".".into()))
21742169
}
21752170

21762171
#[tracing::instrument(level = Level::TRACE, skip_all)]
@@ -2455,7 +2450,6 @@ async fn apply_in_package(
24552450
.with_query(query)
24562451
.with_fragment(fragment),
24572452
options,
2458-
false,
24592453
)
24602454
.with_replaced_request_key(value.into(), Value::new(request_key))
24612455
.with_affecting_sources(refs.into_iter().map(|src| *src).collect()),
@@ -2628,13 +2622,8 @@ async fn resolve_module_request(
26282622
let relative = Request::relative(Value::new(pattern), query, fragment, true)
26292623
.to_resolved()
26302624
.await?;
2631-
let relative_result = Box::pin(resolve_internal_inline(
2632-
lookup_path,
2633-
*relative,
2634-
options,
2635-
false,
2636-
))
2637-
.await?;
2625+
let relative_result =
2626+
Box::pin(resolve_internal_inline(lookup_path, *relative, options)).await?;
26382627
let relative_result = relative_result
26392628
.with_replaced_request_key(module_prefix, Value::new(RequestKey::new(module.into())));
26402629

@@ -2720,7 +2709,7 @@ async fn resolve_into_package(
27202709
let relative = Request::relative(Value::new(new_pat), query, fragment, true)
27212710
.to_resolved()
27222711
.await?;
2723-
results.push(resolve_internal_inline(*package_path, *relative, *options, false).await?);
2712+
results.push(resolve_internal_inline(*package_path, *relative, *options).await?);
27242713
}
27252714

27262715
Ok(merge_results(results))
@@ -2747,7 +2736,7 @@ async fn resolve_import_map_result(
27472736
if request == original_request && lookup_path == original_lookup_path {
27482737
None
27492738
} else {
2750-
let result = resolve_internal(lookup_path, request, options, false);
2739+
let result = resolve_internal(lookup_path, request, options);
27512740
Some(result.with_replaced_request_key_pattern(
27522741
request.request_pattern(),
27532742
original_request.request_pattern(),
@@ -2788,7 +2777,6 @@ async fn resolve_import_map_result(
27882777
node_esm_resolve_options(alias_lookup_path.root())
27892778
}
27902779
},
2791-
false,
27922780
)
27932781
.await?
27942782
.is_unresolvable_ref();
@@ -2934,13 +2922,8 @@ async fn handle_exports_imports_field(
29342922
.to_resolved()
29352923
.await?;
29362924

2937-
let resolve_result = Box::pin(resolve_internal_inline(
2938-
package_path,
2939-
*request,
2940-
options,
2941-
false,
2942-
))
2943-
.await?;
2925+
let resolve_result =
2926+
Box::pin(resolve_internal_inline(package_path, *request, options)).await?;
29442927
if conditions.is_empty() {
29452928
resolved_results.push(resolve_result.with_request(path.into()));
29462929
} else {

turbopack/crates/turbopack-core/src/resolve/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ pub struct ResolveOptions {
602602
pub enable_typescript_with_output_extension: bool,
603603
/// Warn instead of error for resolve errors
604604
pub loose_errors: bool,
605+
/// Whether to parse data URIs into modules (as opposed to keeping them as externals)
606+
pub parse_data_uris: bool,
605607

606608
pub placeholder_for_future_extensions: (),
607609
}

turbopack/crates/turbopack-resolve/src/ecmascript.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ async fn apply_esm_specific_options_internal(
7171
options.extensions.clear();
7272
}
7373

74+
options.parse_data_uris = true;
75+
7476
Ok(options.cell())
7577
}
7678

0 commit comments

Comments
 (0)