Skip to content

Commit

Permalink
feat(next-core): apply invalid import assertion on the remaining cont…
Browse files Browse the repository at this point in the history
…exts
  • Loading branch information
kwonoj committed Mar 11, 2024
1 parent d69b401 commit b201ecc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
13 changes: 9 additions & 4 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ use crate::{
next_server::resolve::ExternalPredicate,
next_shared::{
resolve::{
get_invalid_client_only_resolve_plugin, ModuleFeatureReportResolvePlugin,
NextExternalResolvePlugin, NextNodeSharedRuntimeResolvePlugin,
UnsupportedModulesResolvePlugin,
get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin,
ModuleFeatureReportResolvePlugin, NextExternalResolvePlugin,
NextNodeSharedRuntimeResolvePlugin, UnsupportedModulesResolvePlugin,
},
transforms::{
emotion::get_emotion_transform_rule, get_ecma_transform_rule,
Expand Down Expand Up @@ -114,6 +114,8 @@ pub async fn get_server_resolve_options_context(
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);

// Always load these predefined packages as external.
let mut external_packages: Vec<String> = load_next_js_templateon(
Expand Down Expand Up @@ -221,10 +223,13 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::AppRoute { .. }
| ServerContextType::Instrumentation => {
plugins.push(Vc::upcast(invalid_client_only_resolve_plugin));
plugins.push(Vc::upcast(invalid_styled_jsx_client_only_resolve_plugin));
}
ServerContextType::AppSSR { .. } => {
//[TODO] Build error in this context makes rsc-build-error.ts fail which expects runtime error code
// looks like webpack and turbopack have different order, webpack runs rsc transform first, turbopack triggers resolve plugin first.
//plugins.push(Vc::upcast(invalid_client_only_resolve_plugin));
//plugins.push(Vc::upcast(invalid_styled_jsx_client_only_resolve_plugin));
}
ServerContextType::Middleware => {
//noop
Expand Down Expand Up @@ -548,7 +553,7 @@ pub async fn get_server_module_options_context(
..
} => {
let mut custom_source_transform_rules: Vec<ModuleRule> =
vec![styled_components_transform_rule]
vec![styled_components_transform_rule, styled_jsx_transform_rule]
.into_iter()
.flatten()
.collect();
Expand Down
38 changes: 30 additions & 8 deletions packages/next-swc/crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl ResolvePlugin for UnsupportedModulesResolvePlugin {
pub struct InvalidImportModuleIssue {
pub file_path: Vc<FileSystemPath>,
pub messages: Vec<String>,
pub skip_context_message: bool,
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -141,19 +142,20 @@ impl Issue for InvalidImportModuleIssue {
let raw_context = &*self.file_path.await?;

let mut messages = self.messages.clone();
messages.push("\n".to_string());

//[TODO]: how do we get the import trace?
messages.push(format!(
"The error was caused by importing '{}'",
raw_context.path
));
if !self.skip_context_message {
//[TODO]: how do we get the import trace?
messages.push(format!(
"The error was caused by importing '{}'",
raw_context.path
));
}

Ok(Vc::cell(Some(
StyledString::Line(
messages
.iter()
.map(|v| StyledString::Text(v.into()))
.map(|v| StyledString::Text(format!("{}\n", v)))
.collect::<Vec<StyledString>>(),
)
.cell(),
Expand Down Expand Up @@ -205,6 +207,8 @@ impl ResolvePlugin for InvalidImportResolvePlugin {
InvalidImportModuleIssue {
file_path: context,
messages: self.message.clone(),
// styled-jsx specific resolve error have own message
skip_context_message: self.invalid_import == "styled-jsx",
}
.cell()
.emit();
Expand Down Expand Up @@ -235,7 +239,6 @@ pub(crate) fn get_invalid_client_only_resolve_plugin(
/// Returns a resolve plugin if context have imports to `server-only`.
/// Only the contexts that alises `server-only` to
/// `next/dist/compiled/server-only/index` should use this.
#[allow(unused)]
pub(crate) fn get_invalid_server_only_resolve_plugin(
root: Vc<FileSystemPath>,
) -> Vc<InvalidImportResolvePlugin> {
Expand All @@ -250,6 +253,25 @@ pub(crate) fn get_invalid_server_only_resolve_plugin(
)
}

/// Returns a resolve plugin if context have imports to `styled-jsx`.
pub(crate) fn get_invalid_styled_jsx_resolve_plugin(
root: Vc<FileSystemPath>,
) -> Vc<InvalidImportResolvePlugin> {
InvalidImportResolvePlugin::new(
root,
"styled-jsx".to_string(),
vec![
"'client-only' cannot be imported from a Server Component module. It should only be \
used from a Client Component."
.to_string(),
"The error was caused by using 'styled-jsx'. It only works in a Client Component but \
none of its parents are marked with \"use client\", so they're Server Components by \
default."
.to_string(),
],
)
}

#[turbo_tasks::value]
pub(crate) struct NextExternalResolvePlugin {
root: Vc<FileSystemPath>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use turbopack_binding::turbopack::{
use super::get_ecma_transform_rule;
use crate::next_config::NextConfig;

/// Returns a transform rule for the relay graphql transform.
/// Returns a transform rule for the styled jsx transform.
pub async fn get_styled_jsx_transform_rule(
next_config: Vc<NextConfig>,
target_browsers: Vc<RuntimeVersions>,
Expand Down

0 comments on commit b201ecc

Please sign in to comment.