Unverified Commit b0086f0d authored by Alexander Lyon's avatar Alexander Lyon Committed by GitHub

refactor some of the font code (#69146)

Some basic refactors that I made while browsing the fonts code. No behavioral differences should be observed so existing tests suffice.
parent 90691cff
...@@ -63,6 +63,10 @@ pub const USER_AGENT_FOR_GOOGLE_FONTS: &str = "Mozilla/5.0 (Macintosh; Intel Mac ...@@ -63,6 +63,10 @@ pub const USER_AGENT_FOR_GOOGLE_FONTS: &str = "Mozilla/5.0 (Macintosh; Intel Mac
AppleWebKit/537.36 (KHTML, like Gecko) \ AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/104.0.0.0 Safari/537.36"; Chrome/104.0.0.0 Safari/537.36";
/// The google fonts plugin downloads fonts locally and transforms the url in the css into a
/// specific format that is then intercepted later. This is the prefix we use for the new url.
pub const GOOGLE_FONTS_INTERNAL_PREFIX: &str = "@vercel/turbopack-next/internal/font/google/font";
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
struct FontData(IndexMap<RcStr, FontDataEntry>); struct FontData(IndexMap<RcStr, FontDataEntry>);
...@@ -430,10 +434,7 @@ async fn update_google_stylesheet( ...@@ -430,10 +434,7 @@ async fn update_google_stylesheet(
stylesheet = stylesheet.replace( stylesheet = stylesheet.replace(
&font_url, &font_url,
&format!( &format!("{}?{}", GOOGLE_FONTS_INTERNAL_PREFIX, query_str),
"@vercel/turbopack-next/internal/font/google/font?{}",
query_str
),
) )
} }
......
...@@ -25,6 +25,7 @@ use crate::{ ...@@ -25,6 +25,7 @@ use crate::{
next_edge::unsupported::NextEdgeUnsupportedModuleReplacer, next_edge::unsupported::NextEdgeUnsupportedModuleReplacer,
next_font::google::{ next_font::google::{
NextFontGoogleCssModuleReplacer, NextFontGoogleFontFileReplacer, NextFontGoogleReplacer, NextFontGoogleCssModuleReplacer, NextFontGoogleFontFileReplacer, NextFontGoogleReplacer,
GOOGLE_FONTS_INTERNAL_PREFIX,
}, },
next_server::context::ServerContextType, next_server::context::ServerContextType,
util::NextRuntime, util::NextRuntime,
...@@ -865,7 +866,7 @@ async fn insert_next_shared_aliases( ...@@ -865,7 +866,7 @@ async fn insert_next_shared_aliases(
); );
import_map.insert_alias( import_map.insert_alias(
AliasPattern::exact("@vercel/turbopack-next/internal/font/google/font"), AliasPattern::exact(GOOGLE_FONTS_INTERNAL_PREFIX),
ImportMapping::Dynamic(Vc::upcast(NextFontGoogleFontFileReplacer::new( ImportMapping::Dynamic(Vc::upcast(NextFontGoogleFontFileReplacer::new(
project_path, project_path,
))) )))
......
...@@ -21,39 +21,19 @@ impl<'a> Visit for FontFunctionsCollector<'a> { ...@@ -21,39 +21,19 @@ impl<'a> Visit for FontFunctionsCollector<'a> {
.removeable_module_items .removeable_module_items
.insert(import_decl.span.lo); .insert(import_decl.span.lo);
for specifier in &import_decl.specifiers { for specifier in &import_decl.specifiers {
match specifier { let (local, function_name) = match specifier {
ImportSpecifier::Named(ImportNamedSpecifier { ImportSpecifier::Named(ImportNamedSpecifier {
local, imported, .. local, imported, ..
}) => { }) => {
self.state
.font_functions_in_allowed_scope
.insert(local.span.lo);
let function_name = if let Some(ModuleExportName::Ident(ident)) = imported { let function_name = if let Some(ModuleExportName::Ident(ident)) = imported {
ident.sym.clone() ident.sym.clone()
} else { } else {
local.sym.clone() local.sym.clone()
}; };
self.state.font_functions.insert(
local.to_id(), (local, Some(function_name))
super::FontFunction {
loader: import_decl.src.value.clone(),
function_name: Some(function_name),
},
);
}
ImportSpecifier::Default(ImportDefaultSpecifier { local, .. }) => {
self.state
.font_functions_in_allowed_scope
.insert(local.span.lo);
self.state.font_functions.insert(
local.to_id(),
super::FontFunction {
loader: import_decl.src.value.clone(),
function_name: None,
},
);
} }
ImportSpecifier::Default(ImportDefaultSpecifier { local, .. }) => (local, None),
ImportSpecifier::Namespace(_) => { ImportSpecifier::Namespace(_) => {
HANDLER.with(|handler| { HANDLER.with(|handler| {
handler handler
...@@ -63,8 +43,21 @@ impl<'a> Visit for FontFunctionsCollector<'a> { ...@@ -63,8 +43,21 @@ impl<'a> Visit for FontFunctionsCollector<'a> {
) )
.emit() .emit()
}); });
continue;
} }
} };
self.state
.font_functions_in_allowed_scope
.insert(local.span.lo);
self.state.font_functions.insert(
local.to_id(),
super::FontFunction {
loader: import_decl.src.value.clone(),
function_name,
},
);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment