Unverified Commit ea472e80 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

refactor(next-core): emit unsupported package warning (#48837)



### What?

WEB-953. 

This PR moves, bubbles up unsupported package warning from turbopack to next-core using resolve plugin. Featurewise it behaves same, however removes turbopack aware specific next.js package (`@vercel/og`) internally. 
parent a4d63092
......@@ -3239,6 +3239,7 @@ dependencies = [
"futures",
"indexmap",
"indoc",
"lazy_static",
"mime",
"next-transform-dynamic",
"next-transform-font",
......
......@@ -21,6 +21,7 @@ mime = { workspace = true }
indoc = { workspace = true }
allsorts = { workspace = true }
futures = { workspace = true }
lazy_static = { workspace = true }
turbo-binding = { workspace = true, features = [
"__swc_transform_modularize_imports",
"__feature_auto_hash_map",
......
......@@ -46,6 +46,7 @@ use crate::{
get_next_client_fallback_import_map, get_next_client_import_map,
get_next_client_resolved_map,
},
next_shared::resolve::UnsupportedModulesResolvePluginVc,
transform_options::{
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options,
get_styled_components_compiler_config, get_typescript_transform_options,
......@@ -135,6 +136,7 @@ pub async fn get_client_resolve_options_context(
resolved_map: Some(next_client_resolved_map),
browser: true,
module: true,
plugins: vec![UnsupportedModulesResolvePluginVc::new(project_path).into()],
..Default::default()
};
Ok(ResolveOptionsContext {
......
......@@ -22,7 +22,8 @@ use turbo_tasks::Value;
use crate::{
next_config::NextConfigVc, next_import_map::get_next_edge_import_map,
next_server::context::ServerContextType, util::foreign_code_context_condition,
next_server::context::ServerContextType,
next_shared::resolve::UnsupportedModulesResolvePluginVc, util::foreign_code_context_condition,
};
fn defines() -> CompileTimeDefines {
......@@ -97,6 +98,7 @@ pub async fn get_edge_resolve_options_context(
import_map: Some(next_edge_import_map),
module: true,
browser: true,
plugins: vec![UnsupportedModulesResolvePluginVc::new(project_path).into()],
..Default::default()
};
......
......@@ -35,6 +35,7 @@ use crate::{
next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping},
next_config::NextConfigVc,
next_import_map::get_next_server_import_map,
next_shared::resolve::UnsupportedModulesResolvePluginVc,
transform_options::{
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options,
get_styled_components_compiler_config, get_typescript_transform_options,
......@@ -64,6 +65,7 @@ pub async fn get_server_resolve_options_context(
get_next_server_import_map(project_path, ty, next_config, execution_context);
let foreign_code_context_condition = foreign_code_context_condition(next_config).await?;
let root_dir = project_path.root().resolve().await?;
let unsupported_modules_resolve_plugin = UnsupportedModulesResolvePluginVc::new(project_path);
Ok(match ty.into_value() {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {
......@@ -79,7 +81,10 @@ pub async fn get_server_resolve_options_context(
module: true,
custom_conditions: vec!["development".to_string()],
import_map: Some(next_server_import_map),
plugins: vec![external_cjs_modules_plugin.into()],
plugins: vec![
external_cjs_modules_plugin.into(),
unsupported_modules_resolve_plugin.into(),
],
..Default::default()
};
ResolveOptionsContext {
......@@ -100,6 +105,7 @@ pub async fn get_server_resolve_options_context(
module: true,
custom_conditions: vec!["development".to_string()],
import_map: Some(next_server_import_map),
plugins: vec![unsupported_modules_resolve_plugin.into()],
..Default::default()
};
ResolveOptionsContext {
......@@ -120,6 +126,7 @@ pub async fn get_server_resolve_options_context(
module: true,
custom_conditions: vec!["development".to_string(), "react-server".to_string()],
import_map: Some(next_server_import_map),
plugins: vec![unsupported_modules_resolve_plugin.into()],
..Default::default()
};
ResolveOptionsContext {
......@@ -138,6 +145,7 @@ pub async fn get_server_resolve_options_context(
module: true,
custom_conditions: vec!["development".to_string()],
import_map: Some(next_server_import_map),
plugins: vec![unsupported_modules_resolve_plugin.into()],
..Default::default()
};
ResolveOptionsContext {
......@@ -156,6 +164,7 @@ pub async fn get_server_resolve_options_context(
enable_node_externals: true,
module: true,
custom_conditions: vec!["development".to_string()],
plugins: vec![unsupported_modules_resolve_plugin.into()],
..Default::default()
};
ResolveOptionsContext {
......
use std::collections::HashSet;
use anyhow::Result;
use lazy_static::lazy_static;
use turbo_binding::{
turbo::tasks_fs::FileSystemPathVc,
turbopack::core::{
issue::unsupported_module::UnsupportedModuleIssue,
resolve::{
parse::{Request, RequestVc},
pattern::Pattern,
plugin::{ResolvePlugin, ResolvePluginConditionVc, ResolvePluginVc},
ResolveResultOptionVc,
},
},
};
use turbo_tasks_fs::glob::GlobVc;
lazy_static! {
static ref UNSUPPORTED_PACKAGES: HashSet<&'static str> = ["@vercel/og"].into();
static ref UNSUPPORTED_PACKAGE_PATHS: HashSet<(&'static str, &'static str)> = [].into();
}
#[turbo_tasks::value]
pub(crate) struct UnsupportedModulesResolvePlugin {
root: FileSystemPathVc,
}
#[turbo_tasks::value_impl]
impl UnsupportedModulesResolvePluginVc {
#[turbo_tasks::function]
pub fn new(root: FileSystemPathVc) -> Self {
UnsupportedModulesResolvePlugin { root }.cell()
}
}
#[turbo_tasks::value_impl]
impl ResolvePlugin for UnsupportedModulesResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> ResolvePluginConditionVc {
ResolvePluginConditionVc::new(self.root.root(), GlobVc::new("**"))
}
#[turbo_tasks::function]
async fn after_resolve(
&self,
_fs_path: FileSystemPathVc,
context: FileSystemPathVc,
request: RequestVc,
) -> Result<ResolveResultOptionVc> {
if let Request::Module {
module,
path,
query: _,
} = &*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 {
context,
package: module.into(),
package_path: None,
}
.cell()
.as_issue()
.emit();
}
if let Pattern::Constant(path) = path {
if UNSUPPORTED_PACKAGE_PATHS.contains(&(module, path)) {
UnsupportedModuleIssue {
context,
package: module.into(),
package_path: Some(path.to_owned()),
}
.cell()
.as_issue()
.emit();
}
}
}
Ok(ResolveResultOptionVc::none())
}
}
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