Skip to content

Commit

Permalink
refactor some of the font code (vercel#69146)
Browse files Browse the repository at this point in the history
Some basic refactors that I made while browsing the fonts code. No behavioral differences should be observed so existing tests suffice.
  • Loading branch information
arlyon authored Sep 5, 2024
1 parent 90691cf commit b0086f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
9 changes: 5 additions & 4 deletions crates/next-core/src/next_font/google/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ pub const USER_AGENT_FOR_GOOGLE_FONTS: &str = "Mozilla/5.0 (Macintosh; Intel Mac
AppleWebKit/537.36 (KHTML, like Gecko) \
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)]
struct FontData(IndexMap<RcStr, FontDataEntry>);

Expand Down Expand Up @@ -430,10 +434,7 @@ async fn update_google_stylesheet(

stylesheet = stylesheet.replace(
&font_url,
&format!(
"@vercel/turbopack-next/internal/font/google/font?{}",
query_str
),
&format!("{}?{}", GOOGLE_FONTS_INTERNAL_PREFIX, query_str),
)
}

Expand Down
3 changes: 2 additions & 1 deletion crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
next_edge::unsupported::NextEdgeUnsupportedModuleReplacer,
next_font::google::{
NextFontGoogleCssModuleReplacer, NextFontGoogleFontFileReplacer, NextFontGoogleReplacer,
GOOGLE_FONTS_INTERNAL_PREFIX,
},
next_server::context::ServerContextType,
util::NextRuntime,
Expand Down Expand Up @@ -865,7 +866,7 @@ async fn insert_next_shared_aliases(
);

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(
project_path,
)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,19 @@ impl<'a> Visit for FontFunctionsCollector<'a> {
.removeable_module_items
.insert(import_decl.span.lo);
for specifier in &import_decl.specifiers {
match specifier {
let (local, function_name) = match specifier {
ImportSpecifier::Named(ImportNamedSpecifier {
local, imported, ..
}) => {
self.state
.font_functions_in_allowed_scope
.insert(local.span.lo);

let function_name = if let Some(ModuleExportName::Ident(ident)) = imported {
ident.sym.clone()
} else {
local.sym.clone()
};
self.state.font_functions.insert(
local.to_id(),
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,
},
);

(local, Some(function_name))
}
ImportSpecifier::Default(ImportDefaultSpecifier { local, .. }) => (local, None),
ImportSpecifier::Namespace(_) => {
HANDLER.with(|handler| {
handler
Expand All @@ -63,8 +43,21 @@ impl<'a> Visit for FontFunctionsCollector<'a> {
)
.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,
},
);
}
}
}
Expand Down

0 comments on commit b0086f0

Please sign in to comment.