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

feat(transforms): enable rsc transforms for the remaining contexts (#61231)

### What?

This PR wraps up supporting rsc transforms (mostly for the assertion) in
Turbopack. PR contains a few changes to support it, including:

- adjust / expand transform rules for the corresponding contexts
- fix file name / node_modules check
- extract visitors for the assertion / transforms

This change enables most of the rsc-build* tests and some more other
tests. The only failing tests in the rsc-build-errors is due to
Turbopack not triggering hmr with empty file.

Closes PACK-2303
parent 59bf406d
......@@ -57,6 +57,7 @@ use crate::{
},
transforms::{
emotion::get_emotion_transform_rule, get_ecma_transform_rule,
next_react_server_components::get_next_react_server_components_transform_rule,
relay::get_relay_transform_rule,
styled_components::get_styled_components_transform_rule,
styled_jsx::get_styled_jsx_transform_rule,
......@@ -382,12 +383,21 @@ pub async fn get_server_module_options_context(
ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {
let custom_source_transform_rules: Vec<ModuleRule> =
let mut custom_source_transform_rules: Vec<ModuleRule> =
vec![styled_components_transform_rule, styled_jsx_transform_rule]
.into_iter()
.flatten()
.collect();
let mut foreign_custom_transform_rules = custom_source_transform_rules.clone();
if let ServerContextType::Pages { .. } = ty.into_value() {
custom_source_transform_rules.push(
get_next_react_server_components_transform_rule(next_config, false, None)
.await?,
);
}
base_next_server_rules.extend(custom_source_transform_rules);
base_next_server_rules.extend(source_transform_rules);
......@@ -408,8 +418,9 @@ pub async fn get_server_module_options_context(
..Default::default()
};
foreign_custom_transform_rules.extend(internal_custom_rules);
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
custom_rules: foreign_custom_transform_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: enable_foreign_postcss_transform,
......@@ -419,7 +430,7 @@ pub async fn get_server_module_options_context(
let internal_module_options_context = ModuleOptionsContext {
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
enable_jsx: Some(JsxTransformOptions::default().cell()),
custom_rules: internal_custom_rules,
custom_rules: foreign_custom_transform_rules,
..module_options_context.clone()
};
......@@ -444,25 +455,33 @@ pub async fn get_server_module_options_context(
..module_options_context
}
}
ServerContextType::AppSSR { .. } => {
let custom_source_transform_rules: Vec<ModuleRule> =
ServerContextType::AppSSR { app_dir, .. } => {
let mut custom_source_transform_rules: Vec<ModuleRule> =
vec![styled_components_transform_rule, styled_jsx_transform_rule]
.into_iter()
.flatten()
.collect();
let mut foreign_custom_transform_rules = custom_source_transform_rules.clone();
custom_source_transform_rules.push(
get_next_react_server_components_transform_rule(next_config, false, Some(app_dir))
.await?,
);
base_next_server_rules.extend(custom_source_transform_rules.clone());
base_next_server_rules.extend(source_transform_rules);
let module_options_context = ModuleOptionsContext {
custom_rules: custom_source_transform_rules,
execution_context: Some(execution_context),
use_lightningcss,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
..Default::default()
};
foreign_custom_transform_rules.extend(internal_custom_rules);
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
custom_rules: foreign_custom_transform_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: enable_foreign_postcss_transform,
......@@ -470,7 +489,7 @@ pub async fn get_server_module_options_context(
};
let internal_module_options_context = ModuleOptionsContext {
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
custom_rules: internal_custom_rules,
custom_rules: foreign_custom_transform_rules,
..module_options_context.clone()
};
......@@ -496,6 +515,7 @@ pub async fn get_server_module_options_context(
}
}
ServerContextType::AppRSC {
app_dir,
ecmascript_client_reference_transition_name,
..
} => {
......@@ -517,6 +537,13 @@ pub async fn get_server_module_options_context(
));
}
let mut foreign_custom_transform_rules = custom_source_transform_rules.clone();
custom_source_transform_rules.push(
get_next_react_server_components_transform_rule(next_config, true, Some(app_dir))
.await?,
);
base_next_server_rules.extend(custom_source_transform_rules.clone());
base_next_server_rules.extend(source_transform_rules);
......@@ -527,9 +554,9 @@ pub async fn get_server_module_options_context(
..Default::default()
};
custom_source_transform_rules.extend(internal_custom_rules);
foreign_custom_transform_rules.extend(internal_custom_rules);
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: custom_source_transform_rules.clone(),
custom_rules: foreign_custom_transform_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: enable_foreign_postcss_transform,
......@@ -537,7 +564,7 @@ pub async fn get_server_module_options_context(
};
let internal_module_options_context = ModuleOptionsContext {
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
custom_rules: custom_source_transform_rules,
custom_rules: foreign_custom_transform_rules,
..module_options_context.clone()
};
ModuleOptionsContext {
......
......@@ -14,9 +14,7 @@ use crate::{
get_server_actions_transform_rule, next_amp_attributes::get_next_amp_attr_rule,
next_cjs_optimizer::get_next_cjs_optimizer_rule,
next_disallow_re_export_all_in_page::get_next_disallow_export_all_in_page_rule,
next_pure::get_next_pure_rule,
next_react_server_components::get_next_react_server_components_transform_rule,
server_actions::ActionsTransform,
next_pure::get_next_pure_rule, server_actions::ActionsTransform,
},
};
......@@ -74,8 +72,6 @@ pub async fn get_next_server_transforms_rules(
mdx_rs,
));
rules.push(get_next_react_server_components_transform_rule(next_config, true).await?);
if let Some(client_transition) = client_transition {
rules.push(get_next_css_client_reference_transforms_rule(
client_transition,
......
use std::path::PathBuf;
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::react_server_components::*;
use swc_core::{
common::{util::take::Take, FileName},
ecma::{
ast::{Module, Program},
visit::FoldWith,
},
common::FileName,
ecma::{ast::Program, visit::VisitWith},
};
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;
use turbopack_binding::turbopack::{
ecmascript::{CustomTransformer, TransformContext},
turbopack::module_options::ModuleRule,
......@@ -20,13 +16,31 @@ use super::get_ecma_transform_rule;
use crate::next_config::NextConfig;
/// Returns a rule which applies the Next.js react server components transform.
/// This transform owns responsibility to assert various import / usage
/// conditions against each code's context. Refer below table how we are
/// applying this rules against various contexts.
///
/// +-----------------+---------+--------------------+
/// | Context\Enabled | Enabled | isReactServerLayer |
/// +-----------------+---------+--------------------+
/// | SSR | true | false |
/// | Client | true | false |
/// | Middleware | false | false |
/// | Api | false | false |
/// | RSC | true | true |
/// | Pages | true | false |
/// +-----------------+---------+--------------------+
pub async fn get_next_react_server_components_transform_rule(
next_config: Vc<NextConfig>,
is_react_server_layer: bool,
app_dir: Option<Vc<FileSystemPath>>,
) -> Result<ModuleRule> {
let enable_mdx_rs = *next_config.mdx_rs().await?;
Ok(get_ecma_transform_rule(
Box::new(NextJsReactServerComponents::new(is_react_server_layer)),
Box::new(NextJsReactServerComponents::new(
is_react_server_layer,
app_dir,
)),
enable_mdx_rs,
true,
))
......@@ -35,12 +49,14 @@ pub async fn get_next_react_server_components_transform_rule(
#[derive(Debug)]
struct NextJsReactServerComponents {
is_react_server_layer: bool,
app_dir: Option<Vc<FileSystemPath>>,
}
impl NextJsReactServerComponents {
fn new(is_react_server_layer: bool) -> Self {
fn new(is_react_server_layer: bool, app_dir: Option<Vc<FileSystemPath>>) -> Self {
Self {
is_react_server_layer,
app_dir,
}
}
}
......@@ -48,19 +64,24 @@ impl NextJsReactServerComponents {
#[async_trait]
impl CustomTransformer for NextJsReactServerComponents {
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy()));
let file_name = if ctx.file_path_str.is_empty() {
FileName::Anon
} else {
FileName::Real(ctx.file_path_str.into())
};
let mut visitor = server_components(
FileName::Custom(ctx.file_path_str.to_string()),
let mut visitor = server_components_assert(
file_name,
Config::WithOptions(Options {
is_react_server_layer: self.is_react_server_layer,
}),
ctx.comments,
Some(PathBuf::from(ctx.file_path.parent().await?.path.clone())),
match self.app_dir {
None => None,
Some(path) => Some(path.await?.path.clone().into()),
},
);
*program = p.fold_with(&mut visitor);
program.visit_with(&mut visitor);
Ok(())
}
}
......@@ -356,26 +356,36 @@ describe('Error overlay - RSC build errors', () => {
() => session.getRedboxSource(),
/must be a Client \n| Component/
)
expect(
next.normalizeTestDirContent(await session.getRedboxSource())
).toMatchInlineSnapshot(
`
"./app/server-with-errors/error-file/error.js
Error:
x TEST_DIR/app/server-with-errors/error-file/error.js must be a Client Component. Add the "use client" directive the top
| of the file to resolve this issue.
| Learn more: https://nextjs.org/docs/getting-started/react-essentials#client-components
|
|
,-[TEST_DIR/app/server-with-errors/error-file/error.js:1:1]
1 | export default function Error() {}
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\`----
Import trace for requested module:
./app/server-with-errors/error-file/error.js"
`
)
if (process.env.TURBOPACK) {
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./app/server-with-errors/error-file/error.js:1:0
Parsing ecmascript source code failed
> 1 | export default function Error() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/server-with-errors/error-file/error.js must be a Client Component. Add the "use client" directive the top of the file to resolve this issue.
Learn more: https://nextjs.org/docs/getting-started/react-essentials#client-components"
`)
} else {
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./app/server-with-errors/error-file/error.js
Error:
x TEST_DIR/app/server-with-errors/error-file/error.js must be a Client Component. Add the "use client" directive the top
| of the file to resolve this issue.
| Learn more: https://nextjs.org/docs/getting-started/react-essentials#client-components
|
|
,-[TEST_DIR/app/server-with-errors/error-file/error.js:1:1]
1 | export default function Error() {}
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\`----
Import trace for requested module:
./app/server-with-errors/error-file/error.js"
`)
}
await cleanup()
})
......
......@@ -54,26 +54,41 @@ describe('Error Overlay for server components compiler errors in pages', () => {
/That only works in a Server Component/
)
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js
Error:
x You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/
| react-essentials#server-components
|
|
,-[TEST_DIR/components/Comp.js:1:1]
1 | import { cookies } from 'next/headers'
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 |
3 | export default function Page() {
4 | return <p>hello world</p>
\`----
Import trace for requested module:
./components/Comp.js
./pages/index.js"
`)
if (process.env.TURBOPACK) {
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js:1:0
Parsing ecmascript source code failed
> 1 | import { cookies } from 'next/headers'
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 |
3 | export default function Page() {
4 | return <p>hello world</p>
You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components"
`)
} else {
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js
Error:
x You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/
| react-essentials#server-components
|
|
,-[TEST_DIR/components/Comp.js:1:1]
1 | import { cookies } from 'next/headers'
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 |
3 | export default function Page() {
4 | return <p>hello world</p>
\`----
Import trace for requested module:
./components/Comp.js
./pages/index.js"
`)
}
await cleanup()
})
......@@ -98,27 +113,41 @@ describe('Error Overlay for server components compiler errors in pages', () => {
/That only works in a Server Component/
)
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js
Error:
x You're importing a component that needs server-only. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/
| react-essentials#server-components
|
|
,-[TEST_DIR/components/Comp.js:1:1]
1 | import 'server-only'
: ^^^^^^^^^^^^^^^^^^^^
2 |
3 | export default function Page() {
4 | return 'hello world'
\`----
Import trace for requested module:
./components/Comp.js
./pages/index.js"
`)
if (process.env.TURBOPACK) {
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js:1:0
Parsing ecmascript source code failed
> 1 | import 'server-only'
| ^^^^^^^^^^^^^^^^^^^^
2 |
3 | export default function Page() {
4 | return 'hello world'
You're importing a component that needs server-only. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components"
`)
} else {
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js
Error:
x You're importing a component that needs server-only. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/
| react-essentials#server-components
|
|
,-[TEST_DIR/components/Comp.js:1:1]
1 | import 'server-only'
: ^^^^^^^^^^^^^^^^^^^^
2 |
3 | export default function Page() {
4 | return 'hello world'
\`----
Import trace for requested module:
./components/Comp.js
./pages/index.js"
`)
}
await cleanup()
})
})
......@@ -1167,16 +1167,16 @@
"Error overlay - RSC build errors should error when useTransition from react is used in server component",
"Error overlay - RSC build errors should freeze parent resolved metadata to avoid mutating in generateMetadata",
"Error overlay - RSC build errors should throw an error when \"Component\" is imported in server components",
"Error overlay - RSC build errors should throw an error when \"use client\" is on the top level but after other expressions"
],
"failed": [
"Error overlay - RSC build errors should throw an error when \"use client\" is on the top level but after other expressions",
"Error overlay - RSC build errors should allow to use and handle rsc poisoning server-only",
"Error overlay - RSC build errors should throw an error when error file is a server component",
"Error overlay - RSC build errors should throw an error when error file is a server component with empty error file",
"Error overlay - RSC build errors should throw an error when getServerSideProps is used",
"Error overlay - RSC build errors should throw an error when metadata export is used in client components",
"Error overlay - RSC build errors should throw an error when metadata exports are used together in server components"
],
"failed": [
"Error overlay - RSC build errors should throw an error when error file is a server component with empty error file"
],
"pending": [
"Error overlay - RSC build errors should throw an error when getStaticProps is used"
],
......@@ -1413,11 +1413,11 @@
"runtimeError": false
},
"test/development/acceptance/server-component-compiler-errors-in-pages.test.ts": {
"passed": [],
"failed": [
"passed": [
"Error Overlay for server components compiler errors in pages importing 'next/headers' in pages",
"Error Overlay for server components compiler errors in pages importing 'server-only' in pages"
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
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