From b1dbbfd436bba0067016c5b59415b71247394fc3 Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Tue, 11 Jun 2024 12:19:16 -0700 Subject: [PATCH] Remove UnsupportedModule plugin and move ModuleFeatureReportResolvePlugin to BeforeResolvePlugin --- .../next-core/src/next_client/context.rs | 11 ++- .../crates/next-core/src/next_edge/context.rs | 13 ++- .../next-core/src/next_font/local/mod.rs | 14 +-- .../next-core/src/next_server/context.rs | 24 ++--- .../next-core/src/next_shared/resolve.rs | 90 +++---------------- 5 files changed, 37 insertions(+), 115 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_client/context.rs b/packages/next-swc/crates/next-core/src/next_client/context.rs index 0aad56eac53fc..5b8e112c4b348 100644 --- a/packages/next-swc/crates/next-core/src/next_client/context.rs +++ b/packages/next-swc/crates/next-core/src/next_client/context.rs @@ -49,7 +49,7 @@ use crate::{ next_shared::{ resolve::{ get_invalid_server_only_resolve_plugin, ModuleFeatureReportResolvePlugin, - NextSharedRuntimeResolvePlugin, UnsupportedModulesResolvePlugin, + NextSharedRuntimeResolvePlugin, }, transforms::{ emotion::get_emotion_transform_rule, relay::get_relay_transform_rule, @@ -160,13 +160,12 @@ pub async fn get_client_resolve_options_context( module: true, before_resolve_plugins: vec![ Vc::upcast(get_invalid_server_only_resolve_plugin(project_path)), - Vc::upcast(NextFontLocalResolvePlugin::new(project_path)), - ], - after_resolve_plugins: vec![ Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)), - Vc::upcast(UnsupportedModulesResolvePlugin::new(project_path)), - Vc::upcast(NextSharedRuntimeResolvePlugin::new(project_path)), + Vc::upcast(NextFontLocalResolvePlugin::new(project_path)), ], + after_resolve_plugins: vec![Vc::upcast(NextSharedRuntimeResolvePlugin::new( + project_path, + ))], ..Default::default() }; Ok(ResolveOptionsContext { diff --git a/packages/next-swc/crates/next-core/src/next_edge/context.rs b/packages/next-swc/crates/next-core/src/next_edge/context.rs index 260d3f8046394..85547a8809637 100644 --- a/packages/next-swc/crates/next-core/src/next_edge/context.rs +++ b/packages/next-swc/crates/next-core/src/next_edge/context.rs @@ -28,7 +28,6 @@ use crate::{ next_shared::resolve::{ get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin, ModuleFeatureReportResolvePlugin, NextSharedRuntimeResolvePlugin, - UnsupportedModulesResolvePlugin, }, util::{foreign_code_context_condition, NextRuntime}, }; @@ -101,7 +100,9 @@ pub async fn get_edge_resolve_options_context( let ty = ty.into_value(); - let mut before_resolve_plugins = vec![]; + let mut before_resolve_plugins = vec![Vc::upcast(ModuleFeatureReportResolvePlugin::new( + project_path, + ))]; if matches!( ty, ServerContextType::Pages { .. } @@ -127,11 +128,9 @@ pub async fn get_edge_resolve_options_context( ))); } - let after_resolve_plugins = vec![ - Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)), - Vc::upcast(UnsupportedModulesResolvePlugin::new(project_path)), - Vc::upcast(NextSharedRuntimeResolvePlugin::new(project_path)), - ]; + let after_resolve_plugins = vec![Vc::upcast(NextSharedRuntimeResolvePlugin::new( + project_path, + ))]; // https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102 let mut custom_conditions = vec![mode.await?.condition().into()]; diff --git a/packages/next-swc/crates/next-core/src/next_font/local/mod.rs b/packages/next-swc/crates/next-core/src/next_font/local/mod.rs index 7cd5deacae260..653210411f5b8 100644 --- a/packages/next-swc/crates/next-core/src/next_font/local/mod.rs +++ b/packages/next-swc/crates/next-core/src/next_font/local/mod.rs @@ -15,7 +15,7 @@ use turbopack_binding::{ resolve::{ parse::Request, plugin::{BeforeResolvePlugin, BeforeResolvePluginCondition}, - RequestKey, ResolveResult, ResolveResultItem, ResolveResultOption, + ResolveResult, ResolveResultItem, ResolveResultOption, }, virtual_source::VirtualSource, }, @@ -125,16 +125,10 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin { .emit(); return Ok(ResolveResultOption::some( - ResolveResult::primary_with_key( - RequestKey::new(font_path.clone()), - ResolveResultItem::Error(Vc::cell( - format!( - "Font file not found: Can't resolve {}'", - font_path - ) + ResolveResult::primary(ResolveResultItem::Error(Vc::cell( + format!("Font file not found: Can't resolve {}'", font_path) .into(), - )), - ) + ))) .into(), )); } diff --git a/packages/next-swc/crates/next-core/src/next_server/context.rs b/packages/next-swc/crates/next-core/src/next_server/context.rs index d18790485bacc..13127c3ebfb5e 100644 --- a/packages/next-swc/crates/next-core/src/next_server/context.rs +++ b/packages/next-swc/crates/next-core/src/next_server/context.rs @@ -53,7 +53,7 @@ use crate::{ resolve::{ get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin, ModuleFeatureReportResolvePlugin, NextExternalResolvePlugin, - NextNodeSharedRuntimeResolvePlugin, UnsupportedModulesResolvePlugin, + NextNodeSharedRuntimeResolvePlugin, }, transforms::{ emotion::get_emotion_transform_rule, get_ecma_transform_rule, @@ -126,7 +126,6 @@ pub async fn get_server_resolve_options_context( foreign_code_context_condition(next_config, project_path).await?; let root_dir = project_path.root().resolve().await?; let module_feature_report_resolve_plugin = ModuleFeatureReportResolvePlugin::new(project_path); - let unsupported_modules_resolve_plugin = UnsupportedModulesResolvePlugin::new(project_path); let invalid_client_only_resolve_plugin = get_invalid_client_only_resolve_plugin(project_path); let invalid_styled_jsx_client_only_resolve_plugin = get_invalid_styled_jsx_resolve_plugin(project_path); @@ -206,13 +205,18 @@ pub async fn get_server_resolve_options_context( ServerContextType::Pages { .. } | ServerContextType::AppSSR { .. } | ServerContextType::AppRSC { .. } => { - vec![Vc::upcast(NextFontLocalResolvePlugin::new(project_path))] + vec![ + Vc::upcast(NextFontLocalResolvePlugin::new(project_path)), + Vc::upcast(module_feature_report_resolve_plugin), + ] } ServerContextType::PagesData { .. } | ServerContextType::PagesApi { .. } | ServerContextType::AppRoute { .. } | ServerContextType::Middleware { .. } - | ServerContextType::Instrumentation => vec![], + | ServerContextType::Instrumentation => { + vec![Vc::upcast(module_feature_report_resolve_plugin)] + } }; let after_resolve_plugins = match ty { @@ -220,8 +224,6 @@ pub async fn get_server_resolve_options_context( | ServerContextType::PagesApi { .. } | ServerContextType::PagesData { .. } => { vec![ - Vc::upcast(module_feature_report_resolve_plugin), - Vc::upcast(unsupported_modules_resolve_plugin), Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(external_cjs_modules_plugin), Vc::upcast(next_external_plugin), @@ -231,24 +233,16 @@ pub async fn get_server_resolve_options_context( | ServerContextType::AppRSC { .. } | ServerContextType::AppRoute { .. } => { vec![ - Vc::upcast(module_feature_report_resolve_plugin), - Vc::upcast(unsupported_modules_resolve_plugin), Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(server_external_packages_plugin), Vc::upcast(next_external_plugin), ] } ServerContextType::Middleware { .. } => { - vec![ - Vc::upcast(module_feature_report_resolve_plugin), - Vc::upcast(unsupported_modules_resolve_plugin), - Vc::upcast(next_node_shared_runtime_plugin), - ] + vec![Vc::upcast(next_node_shared_runtime_plugin)] } ServerContextType::Instrumentation { .. } => { vec![ - Vc::upcast(module_feature_report_resolve_plugin), - Vc::upcast(unsupported_modules_resolve_plugin), Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(next_external_plugin), ] diff --git a/packages/next-swc/crates/next-core/src/next_shared/resolve.rs b/packages/next-swc/crates/next-core/src/next_shared/resolve.rs index 9f106b3d462c6..79403e6ab6708 100644 --- a/packages/next-swc/crates/next-core/src/next_shared/resolve.rs +++ b/packages/next-swc/crates/next-core/src/next_shared/resolve.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use anyhow::Result; use lazy_static::lazy_static; @@ -9,14 +9,10 @@ use turbopack_binding::{ turbopack::core::{ diagnostics::DiagnosticExt, file_source::FileSource, - issue::{ - unsupported_module::UnsupportedModuleIssue, Issue, IssueExt, IssueSeverity, IssueStage, - OptionStyledString, StyledString, - }, + issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString}, reference_type::ReferenceType, resolve::{ parse::Request, - pattern::Pattern, plugin::{ AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin, BeforeResolvePluginCondition, @@ -29,8 +25,6 @@ use turbopack_binding::{ use crate::{next_server::ServerContextType, next_telemetry::ModuleFeatureTelemetry}; lazy_static! { - static ref UNSUPPORTED_PACKAGES: HashSet<&'static str> = [].into(); - static ref UNSUPPORTED_PACKAGE_PATHS: HashSet<(&'static str, &'static str)> = [].into(); // Set of the features we want to track, following existing references in webpack/plugins/telemetry-plugin. static ref FEATURE_MODULES: HashMap<&'static str, Vec<&'static str>> = HashMap::from([ ( @@ -49,69 +43,6 @@ lazy_static! { ]); } -#[turbo_tasks::value] -pub(crate) struct UnsupportedModulesResolvePlugin { - root: Vc, -} - -#[turbo_tasks::value_impl] -impl UnsupportedModulesResolvePlugin { - #[turbo_tasks::function] - pub fn new(root: Vc) -> Vc { - UnsupportedModulesResolvePlugin { root }.cell() - } -} - -#[turbo_tasks::value_impl] -impl AfterResolvePlugin for UnsupportedModulesResolvePlugin { - #[turbo_tasks::function] - fn after_resolve_condition(&self) -> Vc { - AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into())) - } - - #[turbo_tasks::function] - async fn after_resolve( - &self, - _fs_path: Vc, - file_path: Vc, - _reference_type: Value, - request: Vc, - ) -> Result> { - if let Request::Module { - module, - path, - query: _, - fragment: _, - } = &*request.await? - { - // Warn if the package is known not to be supported by Turbopack at the moment. - if UNSUPPORTED_PACKAGES.contains(module.as_str()) { - UnsupportedModuleIssue { - file_path, - package: module.clone(), - package_path: None, - } - .cell() - .emit(); - } - - if let Pattern::Constant(path) = path { - if UNSUPPORTED_PACKAGE_PATHS.contains(&(module, path)) { - UnsupportedModuleIssue { - file_path, - package: module.clone(), - package_path: Some(path.to_owned()), - } - .cell() - .emit(); - } - } - } - - Ok(ResolveResultOption::none()) - } -} - #[turbo_tasks::value(shared)] pub struct InvalidImportModuleIssue { pub file_path: Vc, @@ -405,17 +336,22 @@ impl ModuleFeatureReportResolvePlugin { } #[turbo_tasks::value_impl] -impl AfterResolvePlugin for ModuleFeatureReportResolvePlugin { +impl BeforeResolvePlugin for ModuleFeatureReportResolvePlugin { #[turbo_tasks::function] - fn after_resolve_condition(&self) -> Vc { - AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into())) + fn before_resolve_condition(&self) -> Vc { + BeforeResolvePluginCondition::from_modules( + &FEATURE_MODULES + .keys() + .cloned() + .map(|k| k.into()) + .collect::>(), + ) } #[turbo_tasks::function] - async fn after_resolve( + async fn before_resolve( &self, - _fs_path: Vc, - _context: Vc, + _lookup_path: Vc, _reference_type: Value, request: Vc, ) -> Result> {