Skip to content

Commit

Permalink
refactor(next-core): emit unsupported package warning (#48837)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
kwonoj committed Apr 26, 2023
1 parent a4d6309 commit ea472e8
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/next-swc/crates/next-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion packages/next-swc/crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
};

Expand Down
11 changes: 10 additions & 1 deletion packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 { .. } => {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-core/src/next_shared/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod resolve;
pub(crate) mod transforms;
85 changes: 85 additions & 0 deletions packages/next-swc/crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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())
}
}

0 comments on commit ea472e8

Please sign in to comment.