Unverified Commit 783d8455 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Implement next/font BeforeResolvePlugins as ImportMappingReplacement (#95808)

ImportMappingReplacement are more efficient: import maps are backed by a
single radix tree, as opposed to having to run every single
ResolvePlugin's matchers in sequence.

This basically undoes https://github.com/vercel/next.js/pull/65870 by
@wbinnssmith, but I don't entirely understand why this was done:
> This is used to replace ImportMapping replacers for @next/font/local
in an upcoming Next.js PR
parent c4430500
...@@ -48,7 +48,6 @@ use crate::{ ...@@ -48,7 +48,6 @@ use crate::{
transforms::get_next_client_transforms_rules, transforms::get_next_client_transforms_rules,
}, },
next_config::NextConfig, next_config::NextConfig,
next_font::local::NextFontLocalResolvePlugin,
next_import_map::{ next_import_map::{
get_next_client_fallback_import_map, get_next_client_import_map, get_next_client_fallback_import_map, get_next_client_import_map,
get_next_client_resolved_map, get_next_client_resolved_map,
...@@ -189,11 +188,6 @@ pub async fn get_client_resolve_options_context( ...@@ -189,11 +188,6 @@ pub async fn get_client_resolve_options_context(
resolved_map: Some(next_client_resolved_map), resolved_map: Some(next_client_resolved_map),
browser: true, browser: true,
module: true, module: true,
before_resolve_plugins: vec![ResolvedVc::upcast(
NextFontLocalResolvePlugin::new(project_path.clone())
.to_resolved()
.await?,
)],
after_resolve_plugins: vec![ResolvedVc::upcast( after_resolve_plugins: vec![ResolvedVc::upcast(
NextSharedRuntimeResolvePlugin::new(project_path.clone()) NextSharedRuntimeResolvePlugin::new(project_path.clone())
.to_resolved() .to_resolved()
......
...@@ -27,7 +27,6 @@ use crate::{ ...@@ -27,7 +27,6 @@ use crate::{
app_structure::CollectedRootParams, app_structure::CollectedRootParams,
mode::NextMode, mode::NextMode,
next_config::NextConfig, next_config::NextConfig,
next_font::local::NextFontLocalResolvePlugin,
next_import_map::{get_next_edge_and_server_fallback_import_map, get_next_edge_import_map}, next_import_map::{get_next_edge_and_server_fallback_import_map, get_next_edge_import_map},
next_server::context::ServerContextType, next_server::context::ServerContextType,
next_shared::resolve::NextSharedRuntimeResolvePlugin, next_shared::resolve::NextSharedRuntimeResolvePlugin,
...@@ -114,21 +113,6 @@ pub async fn get_edge_resolve_options_context( ...@@ -114,21 +113,6 @@ pub async fn get_edge_resolve_options_context(
.to_resolved() .to_resolved()
.await?; .await?;
let before_resolve_plugins = if matches!(
ty,
ServerContextType::Pages { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. }
) {
vec![ResolvedVc::upcast(
NextFontLocalResolvePlugin::new(project_path.clone())
.to_resolved()
.await?,
)]
} else {
vec![]
};
let after_resolve_plugins = vec![ResolvedVc::upcast( let after_resolve_plugins = vec![ResolvedVc::upcast(
NextSharedRuntimeResolvePlugin::new(project_path.clone()) NextSharedRuntimeResolvePlugin::new(project_path.clone())
.to_resolved() .to_resolved()
...@@ -156,7 +140,6 @@ pub async fn get_edge_resolve_options_context( ...@@ -156,7 +140,6 @@ pub async fn get_edge_resolve_options_context(
module: true, module: true,
browser: true, browser: true,
after_resolve_plugins, after_resolve_plugins,
before_resolve_plugins,
..Default::default() ..Default::default()
}; };
......
This diff is collapsed.
use std::{fmt::Display, str::FromStr}; use std::{fmt::Display, str::FromStr};
use anyhow::{Context, Result}; use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{Vc, trace::TraceRawVcs}; use turbo_tasks::{Vc, trace::TraceRawVcs};
use turbo_tasks_fs::json::parse_json_with_source_context;
use crate::next_font::local::request::{ use crate::next_font::local::request::{
AdjustFontFallback, NextFontLocalDeclaration, NextFontLocalRequest, AdjustFontFallback, NextFontLocalDeclaration, NextFontLocalRequest,
...@@ -48,6 +49,23 @@ impl NextFontLocalOptions { ...@@ -48,6 +49,23 @@ impl NextFontLocalOptions {
pub fn new(options: NextFontLocalOptions) -> Vc<NextFontLocalOptions> { pub fn new(options: NextFontLocalOptions) -> Vc<NextFontLocalOptions> {
Self::cell(options) Self::cell(options)
} }
#[turbo_tasks::function]
pub fn from_query_map(query: RcStr) -> Result<Vc<NextFontLocalOptions>> {
let query_map = qstring::QString::from(query.as_str());
if query_map.len() != 1 {
bail!("next/font/local queries have exactly one entry");
}
let Some((json, _)) = query_map.into_iter().next() else {
bail!("Expected one entry");
};
Ok(NextFontLocalOptions::new(options_from_request(
&parse_json_with_source_context(&json)?,
)?))
}
} }
/// Describes an individual font file's path, weight, style, etc. Derived from /// Describes an individual font file's path, weight, style, etc. Derived from
......
...@@ -37,12 +37,18 @@ use crate::{ ...@@ -37,12 +37,18 @@ use crate::{
next_client::context::ClientContextType, next_client::context::ClientContextType,
next_config::{NextConfig, OptionFileSystemPath}, next_config::{NextConfig, OptionFileSystemPath},
next_edge::unsupported::NextEdgeUnsupportedModuleReplacer, next_edge::unsupported::NextEdgeUnsupportedModuleReplacer,
next_font::google::{ next_font::{
GOOGLE_FONTS_INTERNAL_PREFIX, NextFontGoogleCssModuleReplacer, google::{
NextFontGoogleFontFileReplacer, NextFontGoogleReplacer, GOOGLE_FONTS_INTERNAL_PREFIX, NextFontGoogleCssModuleReplacer,
NextFontGoogleFontFileReplacer, NextFontGoogleReplacer,
},
local::{
NextFontLocalCssModuleReplacer, NextFontLocalFontFileReplacer, NextFontLocalReplacer,
},
}, },
next_root_params::insert_next_root_params_mapping, next_root_params::insert_next_root_params_mapping,
next_server::context::ServerContextType, next_server::context::ServerContextType,
next_shared::ContextType,
util::NextRuntime, util::NextRuntime,
}; };
...@@ -64,6 +70,7 @@ pub async fn get_next_client_import_map( ...@@ -64,6 +70,7 @@ pub async fn get_next_client_import_map(
execution_context, execution_context,
next_config, next_config,
next_mode, next_mode,
ContextType::Client(ty.clone()),
false, false,
) )
.await?; .await?;
...@@ -286,6 +293,7 @@ pub async fn get_next_server_import_map( ...@@ -286,6 +293,7 @@ pub async fn get_next_server_import_map(
execution_context, execution_context,
next_config, next_config,
next_mode, next_mode,
ContextType::Server(ty.clone()),
false, false,
) )
.await?; .await?;
...@@ -431,6 +439,7 @@ pub async fn get_next_edge_import_map( ...@@ -431,6 +439,7 @@ pub async fn get_next_edge_import_map(
execution_context, execution_context,
next_config, next_config,
next_mode, next_mode,
ContextType::Server(ty.clone()),
true, true,
) )
.await?; .await?;
...@@ -1061,6 +1070,7 @@ async fn insert_next_shared_aliases( ...@@ -1061,6 +1070,7 @@ async fn insert_next_shared_aliases(
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
next_mode: Vc<NextMode>, next_mode: Vc<NextMode>,
ty: ContextType,
is_runtime_edge: bool, is_runtime_edge: bool,
) -> Result<()> { ) -> Result<()> {
let package_root = next_js_fs().root().owned().await?; let package_root = next_js_fs().root().owned().await?;
...@@ -1082,10 +1092,43 @@ async fn insert_next_shared_aliases( ...@@ -1082,10 +1092,43 @@ async fn insert_next_shared_aliases(
package_root, package_root,
); );
// NOTE: `@next/font/local` has moved to a BeforeResolve Plugin, so it does not match ty {
// have ImportMapping replacers here. ContextType::Client(_)
// | ContextType::Server(
// TODO: Add BeforeResolve plugins for `@next/font/google` ServerContextType::Pages { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. },
) => {
import_map.insert_alias(
AliasPattern::exact(rcstr!("next/font/local/target.css")),
ImportMapping::Dynamic(ResolvedVc::upcast(
NextFontLocalReplacer::new(project_path.clone())
.to_resolved()
.await?,
))
.resolved_cell(),
);
import_map.insert_alias(
AliasPattern::exact(rcstr!(
"@vercel/turbopack-next/internal/font/local/cssmodule.module.css"
)),
ImportMapping::Dynamic(ResolvedVc::upcast(
NextFontLocalCssModuleReplacer::new().to_resolved().await?,
))
.resolved_cell(),
);
import_map.insert_alias(
AliasPattern::exact(rcstr!("@vercel/turbopack-next/internal/font/local/font")),
ImportMapping::Dynamic(ResolvedVc::upcast(
NextFontLocalFontFileReplacer::new().to_resolved().await?,
))
.resolved_cell(),
);
}
_ => {}
}
let next_font_google_replacer_mapping = ImportMapping::Dynamic(ResolvedVc::upcast( let next_font_google_replacer_mapping = ImportMapping::Dynamic(ResolvedVc::upcast(
NextFontGoogleReplacer::new(project_path.clone()) NextFontGoogleReplacer::new(project_path.clone())
......
...@@ -47,7 +47,6 @@ use crate::{ ...@@ -47,7 +47,6 @@ use crate::{
mode::NextMode, mode::NextMode,
next_build::get_postcss_package_mapping, next_build::get_postcss_package_mapping,
next_config::NextConfig, next_config::NextConfig,
next_font::local::NextFontLocalResolvePlugin,
next_import_map::{get_next_edge_and_server_fallback_import_map, get_next_server_import_map}, next_import_map::{get_next_edge_and_server_fallback_import_map, get_next_server_import_map},
next_server::{ next_server::{
resolve::{ExternalCjsModulesResolvePlugin, ExternalPredicate}, resolve::{ExternalCjsModulesResolvePlugin, ExternalPredicate},
...@@ -231,24 +230,6 @@ pub async fn get_server_resolve_options_context( ...@@ -231,24 +230,6 @@ pub async fn get_server_resolve_options_context(
.to_resolved() .to_resolved()
.await?; .await?;
let before_resolve_plugins = match &ty {
ServerContextType::Pages { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. } => {
vec![ResolvedVc::upcast(
NextFontLocalResolvePlugin::new(project_path.clone())
.to_resolved()
.await?,
)]
}
ServerContextType::PagesApi { .. }
| ServerContextType::AppRoute { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation { .. } => {
vec![]
}
};
let after_resolve_plugins = match ty { let after_resolve_plugins = match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesApi { .. } => { ServerContextType::Pages { .. } | ServerContextType::PagesApi { .. } => {
vec![ vec![
...@@ -283,7 +264,6 @@ pub async fn get_server_resolve_options_context( ...@@ -283,7 +264,6 @@ pub async fn get_server_resolve_options_context(
custom_conditions, custom_conditions,
import_map: Some(next_server_import_map), import_map: Some(next_server_import_map),
fallback_import_map: Some(next_server_fallback_import_map), fallback_import_map: Some(next_server_fallback_import_map),
before_resolve_plugins,
after_resolve_plugins, after_resolve_plugins,
..Default::default() ..Default::default()
}; };
......
use crate::{next_client::ClientContextType, next_server::ServerContextType};
pub(crate) mod resolve; pub(crate) mod resolve;
pub(crate) mod transforms; pub(crate) mod transforms;
pub(crate) mod webpack_rules; pub(crate) mod webpack_rules;
pub(crate) enum ContextType {
#[allow(dead_code)]
Client(ClientContextType),
Server(ServerContextType),
}
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