-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
Copy pathurl.rs
157 lines (137 loc) · 4.48 KB
/
url.rs
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
use std::convert::Infallible;
use anyhow::Result;
use lightningcss::{
values::url::Url,
visit_types,
visitor::{Visit, Visitor},
};
use rustc_hash::FxHashMap;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Value, ValueToString, Vc};
use turbopack_core::{
chunk::{ChunkableModuleReference, ChunkingContext},
ident::AssetIdent,
issue::IssueSource,
output::OutputAsset,
reference::ModuleReference,
reference_type::{ReferenceType, UrlReferenceSubType},
resolve::{origin::ResolveOrigin, parse::Request, url_resolve, ModuleResolveResult},
};
use crate::{embed::CssEmbed, StyleSheetLike};
#[turbo_tasks::value(into = "new")]
pub enum ReferencedAsset {
Some(ResolvedVc<Box<dyn OutputAsset>>),
None,
}
#[turbo_tasks::value]
#[derive(Hash, Debug)]
pub struct UrlAssetReference {
pub origin: ResolvedVc<Box<dyn ResolveOrigin>>,
pub request: ResolvedVc<Request>,
pub issue_source: IssueSource,
}
#[turbo_tasks::value_impl]
impl UrlAssetReference {
#[turbo_tasks::function]
pub fn new(
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
request: ResolvedVc<Request>,
issue_source: IssueSource,
) -> Vc<Self> {
Self::cell(UrlAssetReference {
origin,
request,
issue_source,
})
}
#[turbo_tasks::function]
pub async fn get_referenced_asset(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<ReferencedAsset>> {
if let Some(module) = *self.resolve_reference().first_module().await? {
if let Some(embeddable) = Vc::try_resolve_downcast::<Box<dyn CssEmbed>>(*module).await?
{
return Ok(ReferencedAsset::Some(
embeddable
.embedded_asset(chunking_context)
.to_resolved()
.await?,
)
.into());
}
}
Ok(ReferencedAsset::cell(ReferencedAsset::None))
}
}
#[turbo_tasks::value_impl]
impl ModuleReference for UrlAssetReference {
#[turbo_tasks::function]
fn resolve_reference(&self) -> Vc<ModuleResolveResult> {
url_resolve(
*self.origin,
*self.request,
Value::new(ReferenceType::Url(UrlReferenceSubType::CssUrl)),
Some(self.issue_source.clone()),
false,
)
}
}
#[turbo_tasks::value_impl]
impl ChunkableModuleReference for UrlAssetReference {}
#[turbo_tasks::value_impl]
impl ValueToString for UrlAssetReference {
#[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<RcStr>> {
Ok(Vc::cell(
format!("url {}", self.request.to_string().await?,).into(),
))
}
}
#[turbo_tasks::function]
pub async fn resolve_url_reference(
url: Vc<UrlAssetReference>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<Option<RcStr>>> {
let this = url.await?;
// TODO(WEB-662) This is not the correct way to get the current chunk path. It
// currently works as all chunks are in the same directory.
let chunk_path = chunking_context.chunk_path(
AssetIdent::from_path(this.origin.origin_path()),
".css".into(),
);
let context_path = chunk_path.parent().await?;
if let ReferencedAsset::Some(asset) = &*url.get_referenced_asset(chunking_context).await? {
// TODO(WEB-662) This is not the correct way to get the path of the asset.
// `asset` is on module-level, but we need the output-level asset instead.
let path = asset.path().await?;
let relative_path = context_path
.get_relative_path_to(&path)
.unwrap_or_else(|| format!("/{}", path.path).into());
return Ok(Vc::cell(Some(relative_path)));
}
Ok(Vc::cell(None))
}
pub fn replace_url_references(
ss: &mut StyleSheetLike<'static, 'static>,
urls: &FxHashMap<RcStr, RcStr>,
) {
let mut replacer = AssetReferenceReplacer { urls };
ss.0.visit(&mut replacer).unwrap();
}
struct AssetReferenceReplacer<'a> {
urls: &'a FxHashMap<RcStr, RcStr>,
}
impl Visitor<'_> for AssetReferenceReplacer<'_> {
type Error = Infallible;
fn visit_types(&self) -> lightningcss::visitor::VisitTypes {
visit_types!(URLS)
}
fn visit_url(&mut self, u: &mut Url) -> std::result::Result<(), Self::Error> {
u.visit_children(self)?;
if let Some(new) = self.urls.get(&*u.url) {
u.url = new.to_string().into();
}
Ok(())
}
}