Skip to content

Commit

Permalink
Remove UnsupportedModule plugin and move ModuleFeatureReportResolvePl…
Browse files Browse the repository at this point in the history
…ugin to BeforeResolvePlugin
  • Loading branch information
wbinnssmith committed Jun 11, 2024
1 parent 8011d69 commit b1dbbfd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 115 deletions.
11 changes: 5 additions & 6 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 6 additions & 7 deletions packages/next-swc/crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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 { .. }
Expand All @@ -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()];
Expand Down
14 changes: 4 additions & 10 deletions packages/next-swc/crates/next-core/src/next_font/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use turbopack_binding::{
resolve::{
parse::Request,
plugin::{BeforeResolvePlugin, BeforeResolvePluginCondition},
RequestKey, ResolveResult, ResolveResultItem, ResolveResultOption,
ResolveResult, ResolveResultItem, ResolveResultOption,
},
virtual_source::VirtualSource,
},
Expand Down Expand Up @@ -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(),
));
}
Expand Down
24 changes: 9 additions & 15 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -206,22 +205,25 @@ 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 {
ServerContextType::Pages { .. }
| 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),
Expand All @@ -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),
]
Expand Down
90 changes: 13 additions & 77 deletions packages/next-swc/crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;

use anyhow::Result;
use lazy_static::lazy_static;
Expand All @@ -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,
Expand All @@ -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([
(
Expand All @@ -49,69 +43,6 @@ lazy_static! {
]);
}

#[turbo_tasks::value]
pub(crate) struct UnsupportedModulesResolvePlugin {
root: Vc<FileSystemPath>,
}

#[turbo_tasks::value_impl]
impl UnsupportedModulesResolvePlugin {
#[turbo_tasks::function]
pub fn new(root: Vc<FileSystemPath>) -> Vc<Self> {
UnsupportedModulesResolvePlugin { root }.cell()
}
}

#[turbo_tasks::value_impl]
impl AfterResolvePlugin for UnsupportedModulesResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> {
AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into()))
}

#[turbo_tasks::function]
async fn after_resolve(
&self,
_fs_path: Vc<FileSystemPath>,
file_path: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
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<FileSystemPath>,
Expand Down Expand Up @@ -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> {
AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into()))
fn before_resolve_condition(&self) -> Vc<BeforeResolvePluginCondition> {
BeforeResolvePluginCondition::from_modules(
&FEATURE_MODULES
.keys()
.cloned()
.map(|k| k.into())
.collect::<Vec<RcStr>>(),
)
}

#[turbo_tasks::function]
async fn after_resolve(
async fn before_resolve(
&self,
_fs_path: Vc<FileSystemPath>,
_context: Vc<FileSystemPath>,
_lookup_path: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
Expand Down

0 comments on commit b1dbbfd

Please sign in to comment.